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.BasePreferencesImpl;
030 import com.liferay.portlet.PortalPreferencesImpl;
031 import com.liferay.portlet.PortalPreferencesWrapper;
032 import com.liferay.portlet.PortletPreferencesFactoryUtil;
033 import com.liferay.portlet.PortletPreferencesThreadLocal;
034
035 import java.io.Serializable;
036
037 import java.util.Map;
038 import java.util.concurrent.locks.Lock;
039
040 import javax.portlet.PortletPreferences;
041
042
045 public class PortalPreferencesLocalServiceImpl
046 extends PortalPreferencesLocalServiceBaseImpl {
047
048 public PortalPreferences addPortalPreferences(
049 long companyId, long ownerId, int ownerType,
050 String defaultPreferences)
051 throws SystemException {
052
053 long portalPreferencesId = counterLocalService.increment();
054
055 PortalPreferences portalPreferences =
056 portalPreferencesPersistence.create(portalPreferencesId);
057
058 portalPreferences.setOwnerId(ownerId);
059 portalPreferences.setOwnerType(ownerType);
060
061 if (Validator.isNull(defaultPreferences)) {
062 defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
063 }
064
065 portalPreferences.setPreferences(defaultPreferences);
066
067 try {
068 portalPreferencesPersistence.update(portalPreferences, false);
069 }
070 catch (SystemException se) {
071 if (_log.isWarnEnabled()) {
072 _log.warn(
073 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
074 ownerType + "}");
075 }
076
077 portalPreferences = portalPreferencesPersistence.fetchByO_O(
078 ownerId, ownerType, false);
079
080 if (portalPreferences == null) {
081 throw se;
082 }
083 }
084
085 return portalPreferences;
086 }
087
088 public PortletPreferences getPreferences(
089 long companyId, long ownerId, int ownerType)
090 throws SystemException {
091
092 return getPreferences(companyId, ownerId, ownerType, null);
093 }
094
095 public PortletPreferences getPreferences(
096 long companyId, long ownerId, int ownerType,
097 String defaultPreferences)
098 throws SystemException {
099
100 DB db = DBFactoryUtil.getDB();
101
102 String dbType = db.getType();
103
104 if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
105 return doGetPreferences(
106 companyId, ownerId, ownerType, defaultPreferences);
107 }
108
109 StringBundler sb = new StringBundler(4);
110
111 sb.append(ownerId);
112 sb.append(StringPool.POUND);
113 sb.append(ownerType);
114 sb.append(StringPool.POUND);
115
116 String groupName = getClass().getName();
117 String key = sb.toString();
118
119 Lock lock = LockRegistry.allocateLock(groupName, key);
120
121 lock.lock();
122
123 try {
124 return doGetPreferences(
125 companyId, ownerId, ownerType, defaultPreferences);
126 }
127 finally {
128 lock.unlock();
129
130 LockRegistry.freeLock(groupName, key);
131 }
132 }
133
134 public PortalPreferences updatePreferences(
135 long ownerId, int ownerType,
136 com.liferay.portlet.PortalPreferences portalPreferences)
137 throws SystemException {
138
139 String xml = PortletPreferencesFactoryUtil.toXML(portalPreferences);
140
141 return updatePreferences(ownerId, ownerType, xml);
142 }
143
144 public PortalPreferences updatePreferences(
145 long ownerId, int ownerType, String xml)
146 throws SystemException {
147
148 PortalPreferences portalPreferences =
149 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
150
151 if (portalPreferences == null) {
152 long portalPreferencesId = counterLocalService.increment();
153
154 portalPreferences = portalPreferencesPersistence.create(
155 portalPreferencesId);
156
157 portalPreferences.setOwnerId(ownerId);
158 portalPreferences.setOwnerType(ownerType);
159 }
160
161 portalPreferences.setPreferences(xml);
162
163 portalPreferencesPersistence.update(portalPreferences, false);
164
165 PortletPreferencesLocalUtil.clearPreferencesPool(ownerId, ownerType);
166
167 return portalPreferences;
168 }
169
170 protected PortletPreferences doGetPreferences(
171 long companyId, long ownerId, int ownerType,
172 String defaultPreferences)
173 throws SystemException {
174
175 Map<Serializable, BasePreferencesImpl> preferencesPool =
176 PortletPreferencesLocalUtil.getPreferencesPool(ownerId, ownerType);
177
178 PortalPreferencesImpl portalPreferencesImpl =
179 (PortalPreferencesImpl)preferencesPool.get(companyId);
180
181 if (portalPreferencesImpl == null) {
182 PortalPreferences portalPreferences =
183 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
184
185 if (portalPreferences == null) {
186 if (PortletPreferencesThreadLocal.isStrict() &&
187 Validator.isNull(defaultPreferences)) {
188
189 return new PortalPreferencesWrapper(
190 new PortalPreferencesImpl());
191 }
192
193 portalPreferences =
194 portalPreferencesLocalService.addPortalPreferences(
195 companyId, ownerId, ownerType, defaultPreferences);
196 }
197
198 portalPreferencesImpl =
199 (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
200 companyId, ownerId, ownerType,
201 portalPreferences.getPreferences());
202
203 synchronized (preferencesPool) {
204 preferencesPool.put(companyId, portalPreferencesImpl);
205 }
206 }
207
208 return new PortalPreferencesWrapper(
209 (PortalPreferencesImpl)portalPreferencesImpl.clone());
210 }
211
212 private static Log _log = LogFactoryUtil.getLog(
213 PortalPreferencesLocalServiceImpl.class);
214
215 }