All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.util.test.PrefsPropsTemporarySwapper Maven / Gradle / Ivy

There is a newer version: 7.4.3.112-ga112
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.util.test;

import com.liferay.portal.util.PrefsPropsUtil;

import java.util.HashMap;
import java.util.Map;

import javax.portlet.PortletPreferences;
import javax.portlet.ReadOnlyException;

/**
 * @author Adolfo Pérez
 */
public class PrefsPropsTemporarySwapper implements AutoCloseable {

	public PrefsPropsTemporarySwapper(
			String firstKey, Object firstValue, Object... keysAndValues)
		throws Exception {

		PortletPreferences portletPreferences = PrefsPropsUtil.getPreferences(
			0, false);

		_setTemporaryValue(
			portletPreferences, firstKey, String.valueOf(firstValue));

		for (int i = 0; i < keysAndValues.length; i += 2) {
			String key = String.valueOf(keysAndValues[i]);
			String value = String.valueOf(keysAndValues[i + 1]);

			_setTemporaryValue(portletPreferences, key, value);
		}

		portletPreferences.store();
	}

	@Override
	public void close() throws Exception {
		PortletPreferences portletPreferences = PrefsPropsUtil.getPreferences(
			0, false);

		for (Map.Entry entry : _oldValues.entrySet()) {
			portletPreferences.setValue(entry.getKey(), entry.getValue());
		}

		portletPreferences.store();
	}

	private void _setTemporaryValue(
			PortletPreferences portletPreferences, String key, String value)
		throws ReadOnlyException {

		_oldValues.put(key, PrefsPropsUtil.getString(key));

		portletPreferences.setValue(key, value);
	}

	private final Map _oldValues = new HashMap<>();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy