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.PortletPreferencesFactoryUtil;
032 import com.liferay.portlet.PortletPreferencesThreadLocal;
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 companyId, long ownerId, int ownerType,
047 String defaultPreferences)
048 throws SystemException {
049
050 long portalPreferencesId = counterLocalService.increment();
051
052 PortalPreferences portalPreferences =
053 portalPreferencesPersistence.create(portalPreferencesId);
054
055 portalPreferences.setOwnerId(ownerId);
056 portalPreferences.setOwnerType(ownerType);
057
058 if (Validator.isNull(defaultPreferences)) {
059 defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
060 }
061
062 portalPreferences.setPreferences(defaultPreferences);
063
064 try {
065 portalPreferencesPersistence.update(portalPreferences, false);
066 }
067 catch (SystemException se) {
068 if (_log.isWarnEnabled()) {
069 _log.warn(
070 "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
071 ownerType + "}");
072 }
073
074 portalPreferences = portalPreferencesPersistence.fetchByO_O(
075 ownerId, ownerType, false);
076
077 if (portalPreferences == null) {
078 throw se;
079 }
080 }
081
082 return portalPreferences;
083 }
084
085 @Override
086 public PortletPreferences getPreferences(
087 long companyId, long ownerId, int ownerType)
088 throws SystemException {
089
090 return getPreferences(companyId, ownerId, ownerType, null);
091 }
092
093 @Override
094 public PortletPreferences getPreferences(
095 long companyId, long ownerId, int ownerType,
096 String defaultPreferences)
097 throws SystemException {
098
099 DB db = DBFactoryUtil.getDB();
100
101 String dbType = db.getType();
102
103 if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
104 return doGetPreferences(
105 companyId, ownerId, ownerType, defaultPreferences);
106 }
107
108 StringBundler sb = new StringBundler(4);
109
110 sb.append(ownerId);
111 sb.append(StringPool.POUND);
112 sb.append(ownerType);
113 sb.append(StringPool.POUND);
114
115 String groupName = getClass().getName();
116 String key = sb.toString();
117
118 Lock lock = LockRegistry.allocateLock(groupName, key);
119
120 lock.lock();
121
122 try {
123 return doGetPreferences(
124 companyId, ownerId, ownerType, defaultPreferences);
125 }
126 finally {
127 lock.unlock();
128
129 LockRegistry.freeLock(groupName, key);
130 }
131 }
132
133 @Override
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 @Override
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 return portalPreferences;
167 }
168
169 protected PortletPreferences doGetPreferences(
170 long companyId, long ownerId, int ownerType,
171 String defaultPreferences)
172 throws SystemException {
173
174 PortalPreferences portalPreferences =
175 portalPreferencesPersistence.fetchByO_O(ownerId, ownerType);
176
177 if (portalPreferences == null) {
178 if (PortletPreferencesThreadLocal.isStrict() &&
179 Validator.isNull(defaultPreferences)) {
180
181 return new PortalPreferencesWrapper(
182 new PortalPreferencesImpl());
183 }
184
185 portalPreferences =
186 portalPreferencesLocalService.addPortalPreferences(
187 companyId, ownerId, ownerType, defaultPreferences);
188 }
189
190 PortalPreferencesImpl portalPreferencesImpl =
191 (PortalPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
192 companyId, ownerId, ownerType,
193 portalPreferences.getPreferences());
194
195 return new PortalPreferencesWrapper(portalPreferencesImpl);
196 }
197
198 private static Log _log = LogFactoryUtil.getLog(
199 PortalPreferencesLocalServiceImpl.class);
200
201 }