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