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, Collections.<String, Preference>emptyMap(), false);
044 }
045
046 public PortalPreferencesImpl(
047 long companyId, long ownerId, int ownerType,
048 Map<String, Preference> preferences, boolean signedIn) {
049
050 super(companyId, ownerId, ownerType, preferences);
051
052 _signedIn = signedIn;
053 }
054
055 @Override
056 public Object clone() {
057 return new PortalPreferencesImpl(
058 getCompanyId(), getOwnerId(), getOwnerType(),
059 getOriginalPreferences(), isSignedIn());
060 }
061
062 @Override
063 public boolean equals(Object obj) {
064 if (obj == null) {
065 return false;
066 }
067
068 PortalPreferencesImpl portalPreferences = (PortalPreferencesImpl)obj;
069
070 if (this == portalPreferences) {
071 return true;
072 }
073
074 if ((getCompanyId() == portalPreferences.getCompanyId()) &&
075 (getOwnerId() == portalPreferences.getOwnerId()) &&
076 (getOwnerType() == portalPreferences.getOwnerType()) &&
077 (getMap().equals(portalPreferences.getMap()))) {
078
079 return true;
080 }
081 else {
082 return false;
083 }
084 }
085
086 @Override
087 public String getValue(String namespace, String key) {
088 return getValue(namespace, key, null);
089 }
090
091 public String getValue(String namespace, String key, String defaultValue) {
092 key = _encodeKey(namespace, key);
093
094 return super.getValue(key, defaultValue);
095 }
096
097 public String[] getValues(String namespace, String key) {
098 return getValues(namespace, key, null);
099 }
100
101 public String[] getValues(
102 String namespace, String key, String[] defaultValue) {
103
104 key = _encodeKey(namespace, key);
105
106 return super.getValues(key, defaultValue);
107 }
108
109 @Override
110 public int hashCode() {
111 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
112
113 hashCode.append(getCompanyId());
114 hashCode.append(getOwnerId());
115 hashCode.append(getOwnerType());
116 hashCode.append(getPreferences());
117
118 return hashCode.toHashCode();
119 }
120
121 public boolean isSignedIn() {
122 return _signedIn;
123 }
124
125 @Override
126 public void reset(String key) throws ReadOnlyException {
127 if (isReadOnly(key)) {
128 throw new ReadOnlyException(key);
129 }
130
131 Map<String, Preference> modifiedPreferences = getModifiedPreferences();
132
133 modifiedPreferences.remove(key);
134 }
135
136 public void resetValues(String namespace) {
137 try {
138 Map<String, Preference> preferences = getPreferences();
139
140 for (Map.Entry<String, Preference> entry : preferences.entrySet()) {
141 String key = entry.getKey();
142
143 if (key.startsWith(namespace) && !isReadOnly(key)) {
144 reset(key);
145 }
146 }
147
148 store();
149 }
150 catch (Exception e) {
151 _log.error(e, e);
152 }
153 }
154
155 public void setSignedIn(boolean signedIn) {
156 _signedIn = signedIn;
157 }
158
159 public void setValue(String namespace, String key, String value) {
160 if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
161 return;
162 }
163
164 key = _encodeKey(namespace, key);
165
166 try {
167 if (value != null) {
168 super.setValue(key, value);
169 }
170 else {
171 reset(key);
172 }
173
174 if (_signedIn) {
175 store();
176 }
177 }
178 catch (Exception e) {
179 _log.error(e, e);
180 }
181 }
182
183 public void setValues(String namespace, String key, String[] values) {
184 if (Validator.isNull(key) || (key.equals(_RANDOM_KEY))) {
185 return;
186 }
187
188 key = _encodeKey(namespace, key);
189
190 try {
191 if (values != null) {
192 super.setValues(key, values);
193 }
194 else {
195 reset(key);
196 }
197
198 if (_signedIn) {
199 store();
200 }
201 }
202 catch (Exception e) {
203 _log.error(e, e);
204 }
205 }
206
207 @Override
208 public void store() throws IOException {
209 try {
210 PortalPreferencesLocalServiceUtil.updatePreferences(
211 getOwnerId(), getOwnerType(), this);
212 }
213 catch (SystemException se) {
214 throw new IOException(se.getMessage());
215 }
216 }
217
218 private String _encodeKey(String namespace, String key) {
219 if (Validator.isNull(namespace)) {
220 return key;
221 }
222 else {
223 return namespace + StringPool.POUND + key;
224 }
225 }
226
227 private static final String _RANDOM_KEY = "r";
228
229 private static Log _log = LogFactoryUtil.getLog(PortalPreferences.class);
230
231 private boolean _signedIn;
232
233 }