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.model.Portlet;
023 import com.liferay.portal.service.PortletLocalServiceUtil;
024 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
025 import com.liferay.portal.util.PortalUtil;
026
027 import java.io.IOException;
028 import java.io.Serializable;
029
030 import java.util.Collections;
031 import java.util.Map;
032
033 import javax.portlet.PortletPreferences;
034 import javax.portlet.PreferencesValidator;
035 import javax.portlet.ReadOnlyException;
036 import javax.portlet.ValidatorException;
037
038
042 public class PortletPreferencesImpl
043 extends BasePreferencesImpl
044 implements Cloneable, PortletPreferences, Serializable {
045
046 public PortletPreferencesImpl() {
047 this(0, 0, 0, 0, null, Collections.<String, Preference>emptyMap());
048 }
049
050 public PortletPreferencesImpl(
051 long companyId, long ownerId, int ownerType, long plid,
052 String portletId, Map<String, Preference> preferences) {
053
054 super(companyId, ownerId, ownerType, preferences);
055
056 _plid = plid;
057 _portletId = portletId;
058 }
059
060 @Override
061 public Object clone() {
062 return new PortletPreferencesImpl(
063 getCompanyId(), getOwnerId(), getOwnerType(), _plid, _portletId,
064 getOriginalPreferences());
065 }
066
067 @Override
068 public boolean equals(Object obj) {
069 PortletPreferencesImpl portletPreferences = (PortletPreferencesImpl)obj;
070
071 if (this == portletPreferences) {
072 return true;
073 }
074
075 if ((getCompanyId() == portletPreferences.getCompanyId()) &&
076 (getOwnerId() == portletPreferences.getOwnerId()) &&
077 (getOwnerType() == portletPreferences.getOwnerType()) &&
078 (getPlid() == portletPreferences.getPlid()) &&
079 (getPortletId().equals(portletPreferences.getPortletId())) &&
080 (getMap().equals(portletPreferences.getMap()))) {
081
082 return true;
083 }
084 else {
085 return false;
086 }
087 }
088
089 @Override
090 public int hashCode() {
091 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
092
093 hashCode.append(getCompanyId());
094 hashCode.append(getOwnerId());
095 hashCode.append(getOwnerType());
096 hashCode.append(_plid);
097 hashCode.append(_portletId);
098 hashCode.append(getPreferences());
099
100 return hashCode.toHashCode();
101 }
102
103 @Override
104 public void reset(String key) throws ReadOnlyException {
105 if (isReadOnly(key)) {
106 throw new ReadOnlyException(key);
107 }
108
109 if (_defaultPreferences == null) {
110 try {
111 if (_portletId != null) {
112 _defaultPreferences = PortletPreferencesLocalServiceUtil.
113 getDefaultPreferences(getCompanyId(), _portletId);
114 }
115 }
116 catch (Exception e) {
117 if (_log.isWarnEnabled()) {
118 _log.warn(e, e);
119 }
120 }
121 }
122
123 String[] defaultValues = null;
124
125 if (_defaultPreferences != null) {
126 defaultValues = _defaultPreferences.getValues(key, defaultValues);
127 }
128
129 if (defaultValues != null) {
130 setValues(key, defaultValues);
131 }
132 else {
133 Map<String, Preference> modifiedPreferences =
134 getModifiedPreferences();
135
136 modifiedPreferences.remove(key);
137 }
138 }
139
140 @Override
141 public void store() throws IOException, ValidatorException {
142 if (_portletId == null) {
143 throw new UnsupportedOperationException();
144 }
145
146 try {
147 Portlet portlet = PortletLocalServiceUtil.getPortletById(
148 getCompanyId(), _portletId);
149
150 PreferencesValidator preferencesValidator =
151 PortalUtil.getPreferencesValidator(portlet);
152
153 if (preferencesValidator != null) {
154 preferencesValidator.validate(this);
155 }
156
157 PortletPreferencesLocalServiceUtil.updatePreferences(
158 getOwnerId(), getOwnerType(), _plid, _portletId, this);
159 }
160 catch (SystemException se) {
161 throw new IOException(se.getMessage());
162 }
163 }
164
165 protected long getPlid() {
166 return _plid;
167 }
168
169 protected String getPortletId() {
170 return _portletId;
171 }
172
173 private static Log _log = LogFactoryUtil.getLog(
174 PortletPreferencesImpl.class);
175
176 private PortletPreferences _defaultPreferences;
177 private long _plid;
178 private String _portletId;
179
180 }