001
014
015 package com.liferay.portal.kernel.settings;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021
022 import java.io.IOException;
023
024 import java.util.Collection;
025 import java.util.Enumeration;
026 import java.util.HashSet;
027 import java.util.Set;
028
029 import javax.portlet.PortletPreferences;
030 import javax.portlet.ReadOnlyException;
031 import javax.portlet.ValidatorException;
032
033
037 public class PortletPreferencesSettings extends BaseModifiableSettings {
038
039 public PortletPreferencesSettings(PortletPreferences portletPreferences) {
040 this(portletPreferences, null);
041 }
042
043 public PortletPreferencesSettings(
044 PortletPreferences portletPreferences, Settings parentSettings) {
045
046 super(parentSettings);
047
048 _portletPreferences = portletPreferences;
049 }
050
051 @Override
052 public Collection<String> getModifiedKeys() {
053 Set<String> keys = new HashSet<>();
054
055 Enumeration<String> names = _portletPreferences.getNames();
056
057 while (names.hasMoreElements()) {
058 keys.add(names.nextElement());
059 }
060
061 return keys;
062 }
063
064 public PortletPreferences getPortletPreferences() {
065 return _portletPreferences;
066 }
067
068 @Override
069 public void reset(String key) {
070 try {
071 _portletPreferences.reset(key);
072 }
073 catch (ReadOnlyException roe) {
074 _log.error(
075 "Portlet preferences used to persist settings should never " +
076 "be read only",
077 roe);
078 }
079 }
080
081 @Override
082 public ModifiableSettings setValue(String key, String value) {
083 try {
084 _portletPreferences.setValue(key, value);
085 }
086 catch (ReadOnlyException roe) {
087 _log.error(
088 "Portlet preferences used to persist settings should never " +
089 "be read only",
090 roe);
091 }
092
093 return this;
094 }
095
096 @Override
097 public ModifiableSettings setValues(String key, String[] values) {
098 try {
099 _portletPreferences.setValues(key, values);
100 }
101 catch (ReadOnlyException roe) {
102 _log.error(
103 "Portlet preferences used to persist settings should never " +
104 "be read only",
105 roe);
106 }
107
108 return this;
109 }
110
111 @Override
112 public void store() throws IOException, ValidatorException {
113 _portletPreferences.store();
114 }
115
116 @Override
117 protected String doGetValue(String key) {
118 return normalizeValue(_portletPreferences.getValue(key, null));
119 }
120
121 @Override
122 protected String[] doGetValues(String key) {
123 return normalizeValues(_portletPreferences.getValues(key, null));
124 }
125
126 protected boolean isNull(String value) {
127 if ((value == null) || value.equals(_NULL_VALUE)) {
128 return true;
129 }
130
131 return false;
132 }
133
134 protected String normalizeValue(String value) {
135 if (isNull(value)) {
136 return null;
137 }
138
139 return StringUtil.replace(value, "[$NEW_LINE$]", StringPool.NEW_LINE);
140 }
141
142 protected String[] normalizeValues(String[] values) {
143 if (values == null) {
144 return null;
145 }
146
147 if (values.length == 1) {
148 String actualValue = normalizeValue(values[0]);
149
150 if (actualValue == null) {
151 return null;
152 }
153
154 return new String[] {actualValue};
155 }
156
157 String[] actualValues = new String[values.length];
158
159 for (int i = 0; i < actualValues.length; i++) {
160 actualValues[i] = normalizeValue(values[i]);
161 }
162
163 return actualValues;
164 }
165
166 private static final String _NULL_VALUE = "NULL_VALUE";
167
168 private static final Log _log = LogFactoryUtil.getLog(
169 PortletPreferencesSettings.class);
170
171 private final PortletPreferences _portletPreferences;
172
173 }