001
014
015 package com.liferay.portlet;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.HashCode;
021 import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.service.PortalPreferencesLocalServiceUtil;
025
026 import java.io.IOException;
027 import java.io.Serializable;
028
029 import java.util.Collections;
030 import java.util.Map;
031
032 import javax.portlet.ReadOnlyException;
033
034
038 public class PortalPreferencesImpl
039 extends BasePreferencesImpl
040 implements Cloneable, PortalPreferences, Serializable {
041
042 public PortalPreferencesImpl() {
043 this(0, 0, 0, null, Collections.<String, Preference>emptyMap(), false);
044 }
045
046 public PortalPreferencesImpl(
047 long companyId, long ownerId, int ownerType, String xml,
048 Map<String, Preference> preferences, boolean signedIn) {
049
050 super(companyId, ownerId, ownerType, xml, preferences);
051
052 _signedIn = signedIn;
053 }
054
055 @Override
056 public Object clone() {
057 return new PortalPreferencesImpl(
058 getCompanyId(), getOwnerId(), getOwnerType(), getOriginalXML(),
059 getOriginalPreferences(), isSignedIn());
060 }
061
062 @Override
063 public boolean equals(Object obj) {
064 if (this == obj) {
065 return true;
066 }
067
068 if (!(obj instanceof PortalPreferencesImpl)) {
069 return false;
070 }
071
072 PortalPreferencesImpl portalPreferences = (PortalPreferencesImpl)obj;
073
074 if ((getCompanyId() == portalPreferences.getCompanyId()) &&
075 (getOwnerId() == portalPreferences.getOwnerId()) &&
076 (getOwnerType() == portalPreferences.getOwnerType()) &&
077 getPreferences().equals(portalPreferences.getPreferences())) {
078
079 return true;
080 }
081 else {
082 return false;
083 }
084 }
085
086 @Override
087 public long getUserId() {
088 return _userId;
089 }
090
091 @Override
092 public String getValue(String namespace, String key) {
093 return getValue(namespace, key, null);
094 }
095
096 @Override
097 public String getValue(String namespace, String key, String defaultValue) {
098 key = _encodeKey(namespace, key);
099
100 return super.getValue(key, defaultValue);
101 }
102
103 @Override
104 public String[] getValues(String namespace, String key) {
105 return getValues(namespace, key, null);
106 }
107
108 @Override
109 public String[] getValues(
110 String namespace, String key, String[] defaultValue) {
111
112 key = _encodeKey(namespace, key);
113
114 return super.getValues(key, defaultValue);
115 }
116
117 @Override
118 public int hashCode() {
119 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
120
121 hashCode.append(getCompanyId());
122 hashCode.append(getOwnerId());
123 hashCode.append(getOwnerType());
124 hashCode.append(getPreferences());
125
126 return hashCode.toHashCode();
127 }
128
129 @Override
130 public boolean isSignedIn() {
131 return _signedIn;
132 }
133
134 @Override
135 public void reset(String key) throws ReadOnlyException {
136 if (isReadOnly(key)) {
137 throw new ReadOnlyException(key);
138 }
139
140 Map<String, Preference> modifiedPreferences = getModifiedPreferences();
141
142 modifiedPreferences.remove(key);
143 }
144
145 @Override
146 public void resetValues(String namespace) {
147 try {
148 Map<String, Preference> preferences = getPreferences();
149
150 for (Map.Entry<String, Preference> entry : preferences.entrySet()) {
151 String key = entry.getKey();
152
153 if (key.startsWith(namespace) && !isReadOnly(key)) {
154 reset(key);
155 }
156 }
157
158 store();
159 }
160 catch (Exception e) {
161 _log.error(e, e);
162 }
163 }
164
165 @Override
166 public void setSignedIn(boolean signedIn) {
167 _signedIn = signedIn;
168 }
169
170 @Override
171 public void setUserId(long userId) {
172 _userId = userId;
173 }
174
175 @Override
176 public void setValue(String namespace, String key, String value) {
177 if (Validator.isNull(key) || key.equals(_RANDOM_KEY)) {
178 return;
179 }
180
181 key = _encodeKey(namespace, key);
182
183 try {
184 if (value != null) {
185 super.setValue(key, value);
186 }
187 else {
188 reset(key);
189 }
190
191 if (_signedIn) {
192 store();
193 }
194 }
195 catch (Exception e) {
196 _log.error(e, e);
197 }
198 }
199
200 @Override
201 public void setValues(String namespace, String key, String[] values) {
202 if (Validator.isNull(key) || key.equals(_RANDOM_KEY)) {
203 return;
204 }
205
206 key = _encodeKey(namespace, key);
207
208 try {
209 if (values != null) {
210 super.setValues(key, values);
211 }
212 else {
213 reset(key);
214 }
215
216 if (_signedIn) {
217 store();
218 }
219 }
220 catch (Exception e) {
221 _log.error(e, e);
222 }
223 }
224
225 @Override
226 public void store() throws IOException {
227 try {
228 PortalPreferencesLocalServiceUtil.updatePreferences(
229 getOwnerId(), getOwnerType(), this);
230 }
231 catch (SystemException se) {
232 throw new IOException(se.getMessage());
233 }
234 }
235
236 private String _encodeKey(String namespace, String key) {
237 if (Validator.isNull(namespace)) {
238 return key;
239 }
240 else {
241 return namespace.concat(StringPool.POUND).concat(key);
242 }
243 }
244
245 private static final String _RANDOM_KEY = "r";
246
247 private static Log _log = LogFactoryUtil.getLog(
248 PortalPreferencesImpl.class);
249
250 private boolean _signedIn;
251 private long _userId;
252
253 }