001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.spring.aop.Skip;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Portlet;
029    import com.liferay.portal.model.PortletConstants;
030    import com.liferay.portal.model.PortletPreferences;
031    import com.liferay.portal.model.PortletPreferencesIds;
032    import com.liferay.portal.service.base.PortletPreferencesLocalServiceBaseImpl;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    import com.liferay.portlet.PortletPreferencesImpl;
036    import com.liferay.portlet.PortletPreferencesThreadLocal;
037    
038    import java.util.List;
039    import java.util.concurrent.locks.Lock;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Shuyang Zhou
044     */
045    public class PortletPreferencesLocalServiceImpl
046            extends PortletPreferencesLocalServiceBaseImpl {
047    
048            public PortletPreferences addPortletPreferences(
049                            long companyId, long ownerId, int ownerType, long plid,
050                            String portletId, Portlet portlet, String defaultPreferences)
051                    throws SystemException {
052    
053                    long portletPreferencesId = counterLocalService.increment();
054    
055                    PortletPreferences portletPreferences =
056                            portletPreferencesPersistence.create(portletPreferencesId);
057    
058                    portletPreferences.setOwnerId(ownerId);
059                    portletPreferences.setOwnerType(ownerType);
060                    portletPreferences.setPlid(plid);
061                    portletPreferences.setPortletId(portletId);
062    
063                    if (Validator.isNull(defaultPreferences)) {
064                            if (portlet == null) {
065                                    defaultPreferences = PortletConstants.DEFAULT_PREFERENCES;
066                            }
067                            else {
068                                    defaultPreferences = portlet.getDefaultPreferences();
069                            }
070                    }
071    
072                    portletPreferences.setPreferences(defaultPreferences);
073    
074                    try {
075                            portletPreferencesPersistence.update(portletPreferences, false);
076                    }
077                    catch (SystemException se) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn(
080                                            "Add failed, fetch {ownerId=" + ownerId + ", ownerType=" +
081                                                    ownerType + ", plid=" + plid + ", portletId=" +
082                                                            portletId + "}");
083                            }
084    
085                            portletPreferences = portletPreferencesPersistence.fetchByO_O_P_P(
086                                    ownerId, ownerType, plid, portletId, false);
087    
088                            if (portletPreferences == null) {
089                                    throw se;
090                            }
091                    }
092    
093                    return portletPreferences;
094            }
095    
096            public void deletePortletPreferences(long ownerId, int ownerType, long plid)
097                    throws SystemException {
098    
099                    portletPreferencesPersistence.removeByO_O_P(ownerId, ownerType, plid);
100            }
101    
102            public void deletePortletPreferences(
103                            long ownerId, int ownerType, long plid, String portletId)
104                    throws PortalException, SystemException {
105    
106                    portletPreferencesPersistence.removeByO_O_P_P(
107                            ownerId, ownerType, plid, portletId);
108            }
109    
110            @Skip
111            public javax.portlet.PortletPreferences getDefaultPreferences(
112                            long companyId, String portletId)
113                    throws SystemException {
114    
115                    Portlet portlet = portletLocalService.getPortletById(
116                            companyId, portletId);
117    
118                    return PortletPreferencesFactoryUtil.fromDefaultXML(
119                            portlet.getDefaultPreferences());
120            }
121    
122            public List<PortletPreferences> getPortletPreferences()
123                    throws SystemException {
124    
125                    return portletPreferencesPersistence.findAll();
126            }
127    
128            public List<PortletPreferences> getPortletPreferences(
129                            int ownerType, long plid, String portletId)
130                    throws SystemException {
131    
132                    return portletPreferencesPersistence.findByO_P_P(
133                            ownerType, plid, portletId);
134            }
135    
136            public List<PortletPreferences> getPortletPreferences(
137                            long ownerId, int ownerType, long plid)
138                    throws SystemException {
139    
140                    return portletPreferencesPersistence.findByO_O_P(
141                            ownerId, ownerType, plid);
142            }
143    
144            public PortletPreferences getPortletPreferences(
145                            long ownerId, int ownerType, long plid, String portletId)
146                    throws PortalException, SystemException {
147    
148                    return portletPreferencesPersistence.findByO_O_P_P(
149                            ownerId, ownerType, plid, portletId);
150            }
151    
152            public List<PortletPreferences> getPortletPreferences(
153                            long companyId, long groupId, long ownerId, int ownerType,
154                            String portletId, boolean privateLayout)
155                    throws SystemException {
156    
157                    return portletPreferencesFinder.findByC_G_O_O_P_P(
158                            companyId, groupId, ownerId, ownerType, portletId, privateLayout);
159            }
160    
161            public List<PortletPreferences> getPortletPreferences(
162                            long plid, String portletId)
163                    throws SystemException {
164    
165                    return portletPreferencesPersistence.findByP_P(plid, portletId);
166            }
167    
168            public List<PortletPreferences> getPortletPreferencesByPlid(long plid)
169                    throws SystemException {
170    
171                    return portletPreferencesPersistence.findByPlid(plid);
172            }
173    
174            public javax.portlet.PortletPreferences getPreferences(
175                            long companyId, long ownerId, int ownerType, long plid,
176                            String portletId)
177                    throws SystemException {
178    
179                    return getPreferences(
180                            companyId, ownerId, ownerType, plid, portletId, null);
181            }
182    
183            public javax.portlet.PortletPreferences getPreferences(
184                            long companyId, long ownerId, int ownerType, long plid,
185                            String portletId, String defaultPreferences)
186                    throws SystemException {
187    
188                    DB db = DBFactoryUtil.getDB();
189    
190                    String dbType = db.getType();
191    
192                    if (!dbType.equals(DB.TYPE_HYPERSONIC)) {
193                            return doGetPreferences(
194                                    companyId, ownerId, ownerType, plid, portletId,
195                                    defaultPreferences);
196                    }
197    
198                    StringBundler sb = new StringBundler(7);
199    
200                    sb.append(ownerId);
201                    sb.append(StringPool.POUND);
202                    sb.append(ownerType);
203                    sb.append(StringPool.POUND);
204                    sb.append(plid);
205                    sb.append(StringPool.POUND);
206                    sb.append(portletId);
207    
208                    String groupName = getClass().getName();
209                    String key = sb.toString();
210    
211                    Lock lock = LockRegistry.allocateLock(groupName, key);
212    
213                    lock.lock();
214    
215                    try {
216                            return doGetPreferences(
217                                    companyId, ownerId, ownerType, plid, portletId,
218                                    defaultPreferences);
219                    }
220                    finally {
221                            lock.unlock();
222    
223                            LockRegistry.freeLock(groupName, key);
224                    }
225            }
226    
227            public javax.portlet.PortletPreferences getPreferences(
228                            PortletPreferencesIds portletPreferencesIds)
229                    throws SystemException {
230    
231                    return getPreferences(
232                            portletPreferencesIds.getCompanyId(),
233                            portletPreferencesIds.getOwnerId(),
234                            portletPreferencesIds.getOwnerType(),
235                            portletPreferencesIds.getPlid(),
236                            portletPreferencesIds.getPortletId());
237            }
238    
239            public javax.portlet.PortletPreferences getStrictPreferences(
240                            long companyId, long ownerId, int ownerType, long plid,
241                            String portletId)
242                    throws SystemException {
243    
244                    boolean strict = PortletPreferencesThreadLocal.isStrict();
245    
246                    PortletPreferencesThreadLocal.setStrict(!PropsValues.TCK_URL);
247    
248                    try {
249                            return getPreferences(
250                                    companyId, ownerId, ownerType, plid, portletId, null);
251                    }
252                    finally {
253                            PortletPreferencesThreadLocal.setStrict(strict);
254                    }
255            }
256    
257            public javax.portlet.PortletPreferences getStrictPreferences(
258                            PortletPreferencesIds portletPreferencesIds)
259                    throws SystemException {
260    
261                    return getStrictPreferences(
262                            portletPreferencesIds.getCompanyId(),
263                            portletPreferencesIds.getOwnerId(),
264                            portletPreferencesIds.getOwnerType(),
265                            portletPreferencesIds.getPlid(),
266                            portletPreferencesIds.getPortletId());
267            }
268    
269            public PortletPreferences updatePreferences(
270                            long ownerId, int ownerType, long plid, String portletId,
271                            javax.portlet.PortletPreferences portletPreferences)
272                    throws SystemException {
273    
274                    String xml = PortletPreferencesFactoryUtil.toXML(portletPreferences);
275    
276                    return updatePreferences(ownerId, ownerType, plid, portletId, xml);
277            }
278    
279            public PortletPreferences updatePreferences(
280                            long ownerId, int ownerType, long plid, String portletId,
281                            String xml)
282                    throws SystemException {
283    
284                    PortletPreferences portletPreferences =
285                            portletPreferencesPersistence.fetchByO_O_P_P(
286                                    ownerId, ownerType, plid, portletId);
287    
288                    if (portletPreferences == null) {
289                            long portletPreferencesId = counterLocalService.increment();
290    
291                            portletPreferences = portletPreferencesPersistence.create(
292                                    portletPreferencesId);
293    
294                            portletPreferences.setOwnerId(ownerId);
295                            portletPreferences.setOwnerType(ownerType);
296                            portletPreferences.setPlid(plid);
297                            portletPreferences.setPortletId(portletId);
298                    }
299    
300                    portletPreferences.setPreferences(xml);
301    
302                    portletPreferencesPersistence.update(portletPreferences, false);
303    
304                    return portletPreferences;
305            }
306    
307            protected javax.portlet.PortletPreferences doGetPreferences(
308                            long companyId, long ownerId, int ownerType, long plid,
309                            String portletId, String defaultPreferences)
310                    throws SystemException {
311    
312                    PortletPreferences portletPreferences =
313                            portletPreferencesPersistence.fetchByO_O_P_P(
314                                    ownerId, ownerType, plid, portletId);
315    
316                    if (portletPreferences == null) {
317                            Portlet portlet = portletLocalService.getPortletById(
318                                    companyId, portletId);
319    
320                            if (PortletPreferencesThreadLocal.isStrict() &&
321                                    (Validator.isNull(defaultPreferences) ||
322                                     ((portlet != null) && portlet.isUndeployedPortlet()))) {
323    
324                                    return new PortletPreferencesImpl();
325                            }
326    
327                            portletPreferences =
328                                    portletPreferencesLocalService.addPortletPreferences(
329                                            companyId, ownerId, ownerType, plid, portletId, portlet,
330                                            defaultPreferences);
331                    }
332    
333                    PortletPreferencesImpl portletPreferencesImpl =
334                            (PortletPreferencesImpl)PortletPreferencesFactoryUtil.fromXML(
335                                    companyId, ownerId, ownerType, plid, portletId,
336                                    portletPreferences.getPreferences());
337    
338                    return portletPreferencesImpl;
339            }
340    
341            private static Log _log = LogFactoryUtil.getLog(
342                    PortletPreferencesLocalServiceImpl.class);
343    
344    }