001    /**
002     * Copyright (c) 2000-2013 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;
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.impl.BasePersistenceImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Hugo Huijser
040     */
041    public class PortletPreferencesFinderImpl
042            extends BasePersistenceImpl<PortletPreferences>
043            implements PortletPreferencesFinder {
044    
045            public static final String COUNT_BY_O_O_P =
046            PortletPreferencesFinder.class.getName() + ".countByO_O_P";
047    
048            public static final String COUNT_BY_C_G_O_O_P_P_P =
049                    PortletPreferencesFinder.class.getName() + ".countByC_G_O_O_P_P_P";
050    
051            public static final String FIND_BY_PORTLET_ID =
052                    PortletPreferencesFinder.class.getName() + ".findByPortletId";
053    
054            public static final String FIND_BY_C_G_O_O_P_P =
055                    PortletPreferencesFinder.class.getName() + ".findByC_G_O_O_P_P";
056    
057            public static final FinderPath FINDER_PATH_FIND_BY_C_G_O_O_P_P =
058                    new FinderPath(
059                            PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
060                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
061                            PortletPreferencesImpl.class,
062                            PortletPreferencesPersistenceImpl.
063                                    FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
064                            "findByC_G_O_O_P_P",
065                            new String[] {
066                                    Long.class.getName(), Long.class.getName(),
067                                    Long.class.getName(), Integer.class.getName(),
068                                    String.class.getName(), Boolean.class.getName()
069                            }
070                    );
071    
072            @Override
073            public long countByO_O_P(
074                            long ownerId, int ownerType, String portletId,
075                            boolean excludeDefaultPreferences)
076                    throws SystemException {
077    
078                    Session session = null;
079    
080                    try {
081                            session = openSession();
082    
083                            String sql = CustomSQLUtil.get(COUNT_BY_O_O_P);
084    
085                            if (ownerId == -1) {
086                                    sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
087                            }
088    
089                            if (!excludeDefaultPreferences) {
090                                    sql = StringUtil.replace(
091                                            sql, _PREFERENCES_SQL, StringPool.BLANK);
092                            }
093    
094                            SQLQuery q = session.createSQLQuery(sql);
095    
096                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
097    
098                            QueryPos qPos = QueryPos.getInstance(q);
099    
100                            if (ownerId != -1) {
101                                    qPos.add(ownerId);
102                            }
103    
104                            qPos.add(ownerType);
105                            qPos.add(portletId);
106                            qPos.add(portletId.concat("%_INSTANCE_%"));
107    
108                            int count = 0;
109    
110                            Iterator<Long> itr = q.iterate();
111    
112                            while (itr.hasNext()) {
113                                    Long l = itr.next();
114    
115                                    if (l != null) {
116                                            count += l.intValue();
117                                    }
118                            }
119    
120                            return count;
121                    }
122                    catch (Exception e) {
123                            throw new SystemException(e);
124                    }
125                    finally {
126                            closeSession(session);
127                    }
128            }
129    
130            @Override
131            public long countByC_G_O_O_P_P_P(
132                            long companyId, long groupId, long ownerId, int ownerType,
133                            long plid, String portletId, boolean privateLayout,
134                            boolean excludeDefaultPreferences)
135                    throws SystemException {
136    
137                    Session session = null;
138    
139                    try {
140                            session = openSession();
141    
142                            String sql = CustomSQLUtil.get(COUNT_BY_C_G_O_O_P_P_P);
143    
144                            if (ownerId == -1) {
145                                    sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
146                            }
147    
148                            if (plid == -1) {
149                                    sql = StringUtil.replace(sql, _PLID_SQL, StringPool.BLANK);
150                            }
151                            else {
152                                    sql = StringUtil.replace(
153                                            sql, _PORTLET_ID_INSTANCE_SQL, StringPool.BLANK);
154    
155                                    sql = StringUtil.replace(
156                                            sql, _PRIVATE_LAYOUT_SQL, StringPool.BLANK);
157                            }
158    
159                            if (excludeDefaultPreferences) {
160                                    sql = StringUtil.replace(
161                                            sql, "[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]",
162                                            PortletConstants.DEFAULT_PREFERENCES);
163                            }
164                            else {
165                                    sql = StringUtil.replace(
166                                            sql, _PREFERENCES_SQL, StringPool.BLANK);
167                            }
168    
169                            SQLQuery q = session.createSQLQuery(sql);
170    
171                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
172    
173                            QueryPos qPos = QueryPos.getInstance(q);
174    
175                            qPos.add(companyId);
176                            qPos.add(groupId);
177    
178                            if (ownerId != -1) {
179                                    qPos.add(ownerId);
180                            }
181    
182                            qPos.add(ownerType);
183                            qPos.add(portletId);
184    
185                            if (plid != -1) {
186                                    qPos.add(plid);
187                            }
188                            else {
189                                    qPos.add(portletId.concat("%_INSTANCE_%"));
190                                    qPos.add(privateLayout);
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                    throws SystemException {
218    
219                    Session session = null;
220    
221                    try {
222                            session = openSession();
223    
224                            String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
225    
226                            SQLQuery q = session.createSQLQuery(sql);
227    
228                            q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
229    
230                            QueryPos qPos = QueryPos.getInstance(q);
231    
232                            qPos.add(portletId);
233    
234                            return q.list(true);
235                    }
236                    catch (Exception e) {
237                            throw new SystemException(e);
238                    }
239                    finally {
240                            closeSession(session);
241                    }
242            }
243    
244            @Override
245            public List<PortletPreferences> findByC_G_O_O_P_P(
246                            long companyId, long groupId, long ownerId, int ownerType,
247                            String portletId, boolean privateLayout)
248                    throws SystemException {
249    
250                    Object[] finderArgs = {
251                            companyId, groupId, ownerId, ownerType, portletId, privateLayout
252                    };
253    
254                    List<PortletPreferences> list =
255                            (List<PortletPreferences>)FinderCacheUtil.getResult(
256                                    FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
257    
258                    if ((list != null) && !list.isEmpty()) {
259                            for (PortletPreferences portletPreferences : list) {
260                                    if ((ownerId != portletPreferences.getOwnerId()) ||
261                                            (ownerType != portletPreferences.getOwnerType()) ||
262                                            !isInstanceOf(
263                                                    portletPreferences.getPortletId(), portletId)) {
264    
265                                            list = null;
266    
267                                            break;
268                                    }
269                            }
270                    }
271    
272                    if (list == null) {
273                            Session session = null;
274    
275                            try {
276                                    session = openSession();
277    
278                                    String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
279    
280                                    SQLQuery q = session.createSQLQuery(sql);
281    
282                                    q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
283    
284                                    QueryPos qPos = QueryPos.getInstance(q);
285    
286                                    qPos.add(companyId);
287                                    qPos.add(groupId);
288                                    qPos.add(ownerId);
289                                    qPos.add(ownerType);
290                                    qPos.add(portletId);
291                                    qPos.add(portletId.concat("_INSTANCE_%"));
292                                    qPos.add(privateLayout);
293    
294                                    list = q.list(true);
295    
296                                    PortletPreferencesUtil.cacheResult(list);
297    
298                                    FinderCacheUtil.putResult(
299                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
300                            }
301                            catch (Exception e) {
302                                    FinderCacheUtil.removeResult(
303                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
304    
305                                    throw new SystemException(e);
306                            }
307                            finally {
308                                    closeSession(session);
309                            }
310                    }
311    
312                    return list;
313            }
314    
315            protected boolean isInstanceOf(
316                    String portletPreferencesPortletId, String portletId) {
317    
318                    portletPreferencesPortletId = GetterUtil.getString(
319                            portletPreferencesPortletId);
320                    portletId = GetterUtil.getString(portletId);
321    
322                    if (portletPreferencesPortletId.equals(portletId)) {
323                            return true;
324                    }
325    
326                    if (portletPreferencesPortletId.startsWith(
327                                    portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
328    
329                            return true;
330                    }
331    
332                    return false;
333            }
334    
335            private static final String _OWNER_ID_SQL =
336                    "(PortletPreferences.ownerId = ?) AND";
337    
338            private static final String _PLID_SQL = "(Layout.plid = ?) AND";
339    
340            private static final String _PORTLET_ID_INSTANCE_SQL =
341                    "OR (PortletPreferences.portletId LIKE ?)";
342    
343            private static final String _PREFERENCES_SQL =
344                    "AND (PortletPreferences.preferences != " +
345                            "'[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]')";
346    
347            private static final String _PRIVATE_LAYOUT_SQL =
348                    "AND (Layout.privateLayout = ?)";
349    
350    }