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