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

com.liferay.portal.template.TemplatePortletPreferences Maven / Gradle / Ivy

/**
 * 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.template;

import com.liferay.petra.lang.CentralizedThreadLocal;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.PortletConstants;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portlet.PortletPreferencesImpl;
import com.liferay.portlet.PreferencesValueUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import javax.portlet.ReadOnlyException;

/**
 * @author Brian Wing Shun Chan
 * @author László Csontos
 */
public class TemplatePortletPreferences {

	public String getPreferences(Map preferences)
		throws ReadOnlyException {

		StringBundler sb = new StringBundler();

		sb.append("");

		for (Map.Entry entry : preferences.entrySet()) {
			sb.append("");
			sb.append(entry.getKey());
			sb.append("");

			Object valueObject = entry.getValue();

			if (valueObject instanceof Collection) {
				for (Object value : (Collection)valueObject) {
					if (value instanceof String) {
						sb.append("");
						sb.append(
							PreferencesValueUtil.toCompactSafe((String)value));
						sb.append("");
					}
				}
			}
			else if (valueObject instanceof String) {
				sb.append("");
				sb.append(
					PreferencesValueUtil.toCompactSafe((String)valueObject));
				sb.append("");
			}
			else if (valueObject instanceof String[]) {
				for (String value : (String[])valueObject) {
					sb.append("");
					sb.append(PreferencesValueUtil.toCompactSafe(value));
					sb.append("");
				}
			}
			else {
				sb.setIndex(sb.index() - 3);

				continue;
			}

			sb.append("");
		}

		sb.append("");

		return sb.toString();
	}

	public String getPreferences(String key, String value)
		throws ReadOnlyException {

		return getPreferences(Collections.singletonMap(key, value));
	}

	@Override
	public String toString() {
		PortletPreferencesImpl portletPreferencesImpl =
			_portletPreferencesImplThreadLocal.get();

		try {
			return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
		}
		catch (Exception exception) {
			_log.error(exception);

			return PortletConstants.DEFAULT_PREFERENCES;
		}
	}

	private static final Log _log = LogFactoryUtil.getLog(
		TemplatePortletPreferences.class);

	private final ThreadLocal
		_portletPreferencesImplThreadLocal = new CentralizedThreadLocal<>(
			TemplatePortletPreferences.class.getName(),
			PortletPreferencesImpl::new);

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy