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.exception.SystemException;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.model.PortletPreferences;
026    import com.liferay.portal.model.impl.PortletPreferencesImpl;
027    import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Hugo Huijser
036     */
037    public class PortletPreferencesFinderImpl
038            extends BasePersistenceImpl<PortletPreferences>
039            implements PortletPreferencesFinder {
040    
041            public static final String FIND_BY_PORTLET_ID =
042                    PortletPreferencesFinder.class.getName() + ".findByPortletId";
043    
044            public static final String FIND_BY_C_G_O_O_P_P =
045                    PortletPreferencesFinder.class.getName() + ".findByC_G_O_O_P_P";
046    
047            public static final FinderPath FINDER_PATH_FIND_BY_C_G_O_O_P_P =
048                    new FinderPath(
049                            PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
050                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
051                            PortletPreferencesImpl.class, PortletPreferencesPersistenceImpl
052                                    .FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
053                            "findByC_G_O_O_P_P",
054                            new String[] {
055                                    Long.class.getName(), Long.class.getName(),
056                                    Long.class.getName(), Integer.class.getName(),
057                                    String.class.getName(), Boolean.class.getName()
058                            }
059                    );
060    
061            public List<PortletPreferences> findByPortletId(String portletId)
062                    throws SystemException {
063    
064                    Session session = null;
065    
066                    try {
067                            session = openSession();
068    
069                            String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
070    
071                            SQLQuery q = session.createSQLQuery(sql);
072    
073                            q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
074    
075                            QueryPos qPos = QueryPos.getInstance(q);
076    
077                            qPos.add(portletId);
078    
079                            return q.list(true);
080                    }
081                    catch (Exception e) {
082                            throw new SystemException(e);
083                    }
084                    finally {
085                            closeSession(session);
086                    }
087            }
088    
089            public List<PortletPreferences> findByC_G_O_O_P_P(
090                            long companyId, long groupId, long ownerId, int ownerType,
091                            String portletId, boolean privateLayout)
092                    throws SystemException {
093    
094                    Object[] finderArgs = {
095                            companyId, groupId, ownerId, ownerType, portletId, privateLayout
096                    };
097    
098                    List<PortletPreferences> list =
099                            (List<PortletPreferences>)FinderCacheUtil.getResult(
100                                    FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
101    
102                    if ((list != null) && !list.isEmpty()) {
103                            for (PortletPreferences portletPreferences : list) {
104                                    if ((ownerId != portletPreferences.getOwnerId()) ||
105                                            (ownerType != portletPreferences.getOwnerType()) ||
106                                            !isInstanceOf(
107                                                    portletPreferences.getPortletId(), portletId)) {
108    
109                                            list = null;
110    
111                                            break;
112                                    }
113                            }
114                    }
115    
116                    if (list == null) {
117                            Session session = null;
118    
119                            try {
120                                    session = openSession();
121    
122                                    String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
123    
124                                    SQLQuery q = session.createSQLQuery(sql);
125    
126                                    q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
127    
128                                    QueryPos qPos = QueryPos.getInstance(q);
129    
130                                    qPos.add(companyId);
131                                    qPos.add(groupId);
132                                    qPos.add(ownerId);
133                                    qPos.add(ownerType);
134                                    qPos.add(portletId);
135                                    qPos.add(portletId.concat("_INSTANCE_%"));
136                                    qPos.add(privateLayout);
137    
138                                    list = q.list(true);
139    
140                                    PortletPreferencesUtil.cacheResult(list);
141    
142                                    FinderCacheUtil.putResult(
143                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
144                            }
145                            catch (Exception e) {
146                                    FinderCacheUtil.removeResult(
147                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
148    
149                                    throw new SystemException(e);
150                            }
151                            finally {
152                                    closeSession(session);
153                            }
154                    }
155    
156                    return list;
157            }
158    
159            protected boolean isInstanceOf(
160                    String portletPreferencesPortletId, String portletId) {
161    
162                    portletPreferencesPortletId = GetterUtil.getString(
163                            portletPreferencesPortletId);
164                    portletId = GetterUtil.getString(portletId);
165    
166                    if (portletPreferencesPortletId.equals(portletId)) {
167                            return true;
168                    }
169    
170                    if (portletPreferencesPortletId.startsWith(
171                                    portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
172    
173                            return true;
174                    }
175    
176                    return false;
177            }
178    
179    }