001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.kernel.concurrent.LockRegistry;
018 import com.liferay.portal.kernel.dao.db.DB;
019 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.PortalPreferences;
027 import com.liferay.portal.model.PortletConstants;
028 import com.liferay.portal.service.base.PortalPreferencesLocalServiceBaseImpl;
029 import com.liferay.portlet.PortalPreferencesImpl;
030 import com.liferay.portlet.PortalPreferencesWrapper;
031 import com.liferay.portlet.PortalPreferencesWrapperCacheUtil;
032 import com.liferay.portlet.PortletPreferencesFactoryUtil;
033
034 import java.util.concurrent.locks.Lock;
035
036 import javax.portlet.PortletPreferences;
037
038
041 public class PortalPreferencesLocalServiceImpl
042 extends PortalPreferencesLocalServiceBaseImpl {
043
044 @Override
045 public PortalPreferences addPortalPreferences(
046 long ownerId, int ownerType, String defaultPreferences)
047 throws SystemException {
048
049 PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
050
051 long portalPreferencesId = counterLocalService.increment();
052
053 PortalPreferences portalPreferences =
054 portalPreferencesPersistence.create(portalPreferencesId);
055
056 portalPreferences.setOwnerId(ownerId);
057 portalPreferences.setOwnerType(ownerType);
058
059 if (Validator.isNull(defaultPreferences)) {
060 defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
061 }
062
063 portalPreferences.setPreferences(defaultPreferences);
064
065 try {
066 portalPreferencesPersistence.update(portalPreferences);
067 }
068 catch (SystemException se) {
069 if (_log.isWarnEnabled()) {
070 _log.warn(
071 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
072 ownerType + "}");
073 }
074
075 portalPreferences = portalPreferencesPersistence.fetchByO_O(
076 ownerId, ownerType, false);
077
078 if (portalPreferences == null) {
079 throw se;
080 }
081 }
082
083 return portalPreferences;
084 }
085
086
090 @Override
091 public PortalPreferences addPortalPreferences(
092 long companyId, long ownerId, int ownerType,
093 String defaultPreferences)
094 throws SystemException {
095
096 return addPortalPreferences(ownerId, ownerType, defaultPreferences);
097 }
098
099 @Override
100 public PortletPreferences getPreferences(long ownerId, int ownerType)
101 throws SystemException {
102
103 return getPreferences(ownerId, ownerType, null);
104 }
105
106 @Override
107 public PortletPreferences getPreferences(
108 long ownerId, int ownerType, String defaultPreferences)
109 throws SystemException {
110
111 DB db = DBFactoryUtil.getDB();
112
113 String dbType = db.getType();
114
115 if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
116 return doGetPreferences(ownerId, ownerType, defaultPreferences);
117 }
118
119 StringBundler sb = new StringBundler(4);
120
121 sb.append(ownerId);
122 sb.append(StringPool.POUND);
123 sb.append(ownerType);
124 sb.append(StringPool.POUND);
125
126 String groupName = getClass().getName();
127 String key = sb.toString();
128
129 Lock lock = LockRegistry.allocateLock(groupName, key);
130
131 lock.lock();
132
133 try {
134 return doGetPreferences(ownerId, ownerType, defaultPreferences);
135 }
136 finally {
137 lock.unlock();
138
139 LockRegistry.freeLock(groupName, key);
140 }
141 }
142
143
146 @Override
147 public PortletPreferences getPreferences(
148 long companyId, long ownerId, int ownerType)
149 throws SystemException {
150
151 return getPreferences(ownerId, ownerType);
152 }
153
154
158 @Override
159 public PortletPreferences getPreferences(
160 long companyId, long ownerId, int ownerType,
161 String defaultPreferences)
162 throws SystemException {
163
164 return getPreferences(ownerId, ownerType, defaultPreferences);
165 }
166
167 @Override
168 public PortalPreferences updatePreferences(
169 long ownerId, int ownerType,
170 com.liferay.portlet.PortalPreferences portalPreferences)
171 throws SystemException {
172
173 String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
174
175 return updatePreferences(ownerId, ownerType, xml);
176 }
177
178 @Override
179 public PortalPreferences updatePreferences(
180 long ownerId, int ownerType, String xml)
181 throws SystemException {
182
183 PortalPreferencesWrapperCacheUtil.remove(ownerId, ownerType);
184
185 PortalPreferences portalPreferences =
186 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
187
188 if (portalPreferences == null) {
189 long portalPreferencesId = counterLocalService.increment();
190
191 portalPreferences = portalPreferencesPersistence.create(
192 portalPreferencesId);
193
194 portalPreferences.setOwnerId(ownerId);
195 portalPreferences.setOwnerType(ownerType);
196 }
197
198 portalPreferences.setPreferences(xml);
199
200 portalPreferencesPersistence.update(portalPreferences);
201
202 return portalPreferences;
203 }
204
205 protected PortletPreferences doGetPreferences(
206 long ownerId, int ownerType, String defaultPreferences)
207 throws SystemException {
208
209 PortalPreferencesWrapper portalPreferencesWrapper =
210 PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);
211
212 if (portalPreferencesWrapper != null) {
213 return portalPreferencesWrapper.clone();
214 }
215
216 PortalPreferences portalPreferences =
217 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
218
219 if (portalPreferences == null) {
220 portalPreferences =
221 portalPreferencesLocalService.addPortalPreferences(
222 ownerId, ownerType, defaultPreferences);
223 }
224
225 PortalPreferencesImpl portalPreferencesImpl =
226 (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
227 ownerId, ownerType, portalPreferences.getPreferences());
228
229 portalPreferencesWrapper = new PortalPreferencesWrapper(
230 portalPreferencesImpl);
231
232 PortalPreferencesWrapperCacheUtil.put(
233 ownerId, ownerType, portalPreferencesWrapper);
234
235 return portalPreferencesWrapper.clone();
236 }
237
238 private static Log _log = LogFactoryUtil.getLog(
239 PortalPreferencesLocalServiceImpl.class);
240
241 }