001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018    import com.liferay.portal.kernel.dao.orm.FinderPath;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.model.PortletConstants;
028    import com.liferay.portal.model.PortletPreferences;
029    import com.liferay.portal.model.impl.PortletPreferencesImpl;
030    import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
031    import com.liferay.portal.service.persistence.PortletPreferencesFinder;
032    import com.liferay.portal.service.persistence.PortletPreferencesUtil;
033    import com.liferay.util.dao.orm.CustomSQLUtil;
034    
035    import java.io.Serializable;
036    
037    import java.util.Iterator;
038    import java.util.List;
039    import java.util.Map;
040    import java.util.Set;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Hugo Huijser
045     */
046    public class PortletPreferencesFinderImpl
047            extends BasePersistenceImpl<PortletPreferences>
048            implements PortletPreferencesFinder {
049    
050            public static final String COUNT_BY_O_O_P =
051                    PortletPreferencesFinder.class.getName() + ".countByO_O_P";
052    
053            public static final String COUNT_BY_O_O_P_P_P =
054                    PortletPreferencesFinder.class.getName() + ".countByO_O_P_P_P";
055    
056            public static final String FIND_BY_PORTLET_ID =
057                    PortletPreferencesFinder.class.getName() + ".findByPortletId";
058    
059            public static final String FIND_BY_C_G_O_O_P_P =
060                    PortletPreferencesFinder.class.getName() + ".findByC_G_O_O_P_P";
061    
062            public static final FinderPath FINDER_PATH_FIND_BY_C_G_O_O_P_P =
063                    new FinderPath(
064                            PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
065                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
066                            PortletPreferencesImpl.class,
067                            PortletPreferencesPersistenceImpl.
068                                    FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
069                            "findByC_G_O_O_P_P",
070                            new String[] {
071                                    Long.class.getName(), Long.class.getName(),
072                                    Long.class.getName(), Integer.class.getName(),
073                                    String.class.getName(), Boolean.class.getName()
074                            }
075                    );
076    
077            @Override
078            public long countByO_O_P(
079                    long ownerId, int ownerType, String portletId,
080                    boolean excludeDefaultPreferences) {
081    
082                    Session session = null;
083    
084                    try {
085                            session = openSession();
086    
087                            String sql = CustomSQLUtil.get(COUNT_BY_O_O_P);
088    
089                            if (ownerId == -1) {
090                                    sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
091                            }
092    
093                            if (excludeDefaultPreferences) {
094                                    sql = StringUtil.replace(
095                                            sql, "[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]",
096                                            PortletConstants.DEFAULT_PREFERENCES);
097                            }
098                            else {
099                                    sql = StringUtil.replace(
100                                            sql, _PREFERENCES_SQL, StringPool.BLANK);
101                            }
102    
103                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
104    
105                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
106    
107                            QueryPos qPos = QueryPos.getInstance(q);
108    
109                            if (ownerId != -1) {
110                                    qPos.add(ownerId);
111                            }
112    
113                            qPos.add(ownerType);
114                            qPos.add(portletId);
115                            qPos.add(portletId.concat("%_INSTANCE_%"));
116    
117                            int count = 0;
118    
119                            Iterator<Long> itr = q.iterate();
120    
121                            while (itr.hasNext()) {
122                                    Long l = itr.next();
123    
124                                    if (l != null) {
125                                            count += l.intValue();
126                                    }
127                            }
128    
129                            return count;
130                    }
131                    catch (Exception e) {
132                            throw new SystemException(e);
133                    }
134                    finally {
135                            closeSession(session);
136                    }
137            }
138    
139            @Override
140            public long countByO_O_P_P_P(
141                    long ownerId, int ownerType, long plid, String portletId,
142                    boolean excludeDefaultPreferences) {
143    
144                    Session session = null;
145    
146                    try {
147                            session = openSession();
148    
149                            String sql = CustomSQLUtil.get(COUNT_BY_O_O_P_P_P);
150    
151                            if (ownerId == -1) {
152                                    sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
153                            }
154    
155                            if (plid == -1) {
156                                    sql = StringUtil.replace(sql, _PLID_SQL, StringPool.BLANK);
157                            }
158                            else {
159                                    sql = StringUtil.replace(
160                                            sql, _PORTLET_ID_INSTANCE_SQL, StringPool.BLANK);
161                            }
162    
163                            if (excludeDefaultPreferences) {
164                                    sql = StringUtil.replace(
165                                            sql, "[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]",
166                                            PortletConstants.DEFAULT_PREFERENCES);
167                            }
168                            else {
169                                    sql = StringUtil.replace(
170                                            sql, _PREFERENCES_SQL, StringPool.BLANK);
171                            }
172    
173                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
174    
175                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
176    
177                            QueryPos qPos = QueryPos.getInstance(q);
178    
179                            if (ownerId != -1) {
180                                    qPos.add(ownerId);
181                            }
182    
183                            qPos.add(ownerType);
184                            qPos.add(portletId);
185    
186                            if (plid != -1) {
187                                    qPos.add(plid);
188                            }
189                            else {
190                                    qPos.add(portletId.concat("%_INSTANCE_%"));
191                            }
192    
193                            int count = 0;
194    
195                            Iterator<Long> itr = q.iterate();
196    
197                            while (itr.hasNext()) {
198                                    Long l = itr.next();
199    
200                                    if (l != null) {
201                                            count += l.intValue();
202                                    }
203                            }
204    
205                            return count;
206                    }
207                    catch (Exception e) {
208                            throw new SystemException(e);
209                    }
210                    finally {
211                            closeSession(session);
212                    }
213            }
214    
215            @Override
216            public List<PortletPreferences> findByPortletId(String portletId) {
217                    Session session = null;
218    
219                    try {
220                            session = openSession();
221    
222                            String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
223    
224                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
225    
226                            q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
227    
228                            QueryPos qPos = QueryPos.getInstance(q);
229    
230                            qPos.add(portletId);
231    
232                            return q.list(true);
233                    }
234                    catch (Exception e) {
235                            throw new SystemException(e);
236                    }
237                    finally {
238                            closeSession(session);
239                    }
240            }
241    
242            @Override
243            public Map<Serializable, PortletPreferences> fetchByPrimaryKeys(
244                    Set<Serializable> primaryKeys) {
245    
246                    return PortletPreferencesUtil.fetchByPrimaryKeys(primaryKeys);
247            }
248    
249            @Override
250            public List<PortletPreferences> findByC_G_O_O_P_P(
251                    long companyId, long groupId, long ownerId, int ownerType,
252                    String portletId, boolean privateLayout) {
253    
254                    Object[] finderArgs = {
255                            companyId, groupId, ownerId, ownerType, portletId, privateLayout
256                    };
257    
258                    List<PortletPreferences> list =
259                            (List<PortletPreferences>)FinderCacheUtil.getResult(
260                                    FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
261    
262                    if ((list != null) && !list.isEmpty()) {
263                            for (PortletPreferences portletPreferences : list) {
264                                    if ((ownerId != portletPreferences.getOwnerId()) ||
265                                            (ownerType != portletPreferences.getOwnerType()) ||
266                                            !isInstanceOf(
267                                                    portletPreferences.getPortletId(), portletId)) {
268    
269                                            list = null;
270    
271                                            break;
272                                    }
273                            }
274                    }
275    
276                    if (list == null) {
277                            Session session = null;
278    
279                            try {
280                                    session = openSession();
281    
282                                    String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
283    
284                                    SQLQuery q = session.createSynchronizedSQLQuery(sql);
285    
286                                    q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
287    
288                                    QueryPos qPos = QueryPos.getInstance(q);
289    
290                                    qPos.add(companyId);
291                                    qPos.add(groupId);
292                                    qPos.add(ownerId);
293                                    qPos.add(ownerType);
294                                    qPos.add(portletId);
295                                    qPos.add(portletId.concat("_INSTANCE_%"));
296                                    qPos.add(privateLayout);
297    
298                                    list = q.list(true);
299    
300                                    PortletPreferencesUtil.cacheResult(list);
301    
302                                    FinderCacheUtil.putResult(
303                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
304                            }
305                            catch (Exception e) {
306                                    FinderCacheUtil.removeResult(
307                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
308    
309                                    throw new SystemException(e);
310                            }
311                            finally {
312                                    closeSession(session);
313                            }
314                    }
315    
316                    return list;
317            }
318    
319            protected boolean isInstanceOf(
320                    String portletPreferencesPortletId, String portletId) {
321    
322                    portletPreferencesPortletId = GetterUtil.getString(
323                            portletPreferencesPortletId);
324                    portletId = GetterUtil.getString(portletId);
325    
326                    if (portletPreferencesPortletId.equals(portletId)) {
327                            return true;
328                    }
329    
330                    if (portletPreferencesPortletId.startsWith(
331                                    portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
332    
333                            return true;
334                    }
335    
336                    return false;
337            }
338    
339            private static final String _OWNER_ID_SQL =
340                    "(PortletPreferences.ownerId = ?) AND";
341    
342            private static final String _PLID_SQL = "AND (PortletPreferences.plid = ?)";
343    
344            private static final String _PORTLET_ID_INSTANCE_SQL =
345                    "OR (PortletPreferences.portletId LIKE ?)";
346    
347            private static final String _PREFERENCES_SQL =
348                    "AND (PortletPreferences.preferences != " +
349                            "'[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]')";
350    
351    }