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 _portletPreferences.getValue(key, null);
119 }
120
121 @Override
122 protected String[] doGetValues(String key) {
123 return _portletPreferences.getValues(key, null);
124 }
125
126
129 @Deprecated
130 protected boolean isNull(String value) {
131 if ((value == null) || value.equals(_NULL_VALUE)) {
132 return true;
133 }
134
135 return false;
136 }
137
138
141 @Deprecated
142 protected String normalizeValue(String value) {
143 if (isNull(value)) {
144 return null;
145 }
146
147 return StringUtil.replace(value, "[$NEW_LINE$]", StringPool.NEW_LINE);
148 }
149
150
153 @Deprecated
154 protected String[] normalizeValues(String[] values) {
155 if (values == null) {
156 return null;
157 }
158
159 if (values.length == 1) {
160 String actualValue = normalizeValue(values[0]);
161
162 if (actualValue == null) {
163 return null;
164 }
165
166 return new String[] {actualValue};
167 }
168
169 String[] actualValues = new String[values.length];
170
171 for (int i = 0; i < actualValues.length; i++) {
172 actualValues[i] = normalizeValue(values[i]);
173 }
174
175 return actualValues;
176 }
177
178 private static final String _NULL_VALUE = "NULL_VALUE";
179
180 private static final Log _log = LogFactoryUtil.getLog(
181 PortletPreferencesSettings.class);
182
183 private final PortletPreferences _portletPreferences;
184
185 }