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,
052                            PortletPreferencesPersistenceImpl.
053                                    FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
054                            "findByC_G_O_O_P_P",
055                            new String[] {
056                                    Long.class.getName(), Long.class.getName(),
057                                    Long.class.getName(), Integer.class.getName(),
058                                    String.class.getName(), Boolean.class.getName()
059                            }
060                    );
061    
062            @Override
063            public List<PortletPreferences> findByPortletId(String portletId)
064                    throws SystemException {
065    
066                    Session session = null;
067    
068                    try {
069                            session = openSession();
070    
071                            String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
072    
073                            SQLQuery q = session.createSQLQuery(sql);
074    
075                            q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
076    
077                            QueryPos qPos = QueryPos.getInstance(q);
078    
079                            qPos.add(portletId);
080    
081                            return q.list(true);
082                    }
083                    catch (Exception e) {
084                            throw new SystemException(e);
085                    }
086                    finally {
087                            closeSession(session);
088                    }
089            }
090    
091            @Override
092            public List<PortletPreferences> findByC_G_O_O_P_P(
093                            long companyId, long groupId, long ownerId, int ownerType,
094                            String portletId, boolean privateLayout)
095                    throws SystemException {
096    
097                    Object[] finderArgs = {
098                            companyId, groupId, ownerId, ownerType, portletId, privateLayout
099                    };
100    
101                    List<PortletPreferences> list =
102                            (List<PortletPreferences>)FinderCacheUtil.getResult(
103                                    FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
104    
105                    if ((list != null) && !list.isEmpty()) {
106                            for (PortletPreferences portletPreferences : list) {
107                                    if ((ownerId != portletPreferences.getOwnerId()) ||
108                                            (ownerType != portletPreferences.getOwnerType()) ||
109                                            !isInstanceOf(
110                                                    portletPreferences.getPortletId(), portletId)) {
111    
112                                            list = null;
113    
114                                            break;
115                                    }
116                            }
117                    }
118    
119                    if (list == null) {
120                            Session session = null;
121    
122                            try {
123                                    session = openSession();
124    
125                                    String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
126    
127                                    SQLQuery q = session.createSQLQuery(sql);
128    
129                                    q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
130    
131                                    QueryPos qPos = QueryPos.getInstance(q);
132    
133                                    qPos.add(companyId);
134                                    qPos.add(groupId);
135                                    qPos.add(ownerId);
136                                    qPos.add(ownerType);
137                                    qPos.add(portletId);
138                                    qPos.add(portletId.concat("_INSTANCE_%"));
139                                    qPos.add(privateLayout);
140    
141                                    list = q.list(true);
142    
143                                    PortletPreferencesUtil.cacheResult(list);
144    
145                                    FinderCacheUtil.putResult(
146                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
147                            }
148                            catch (Exception e) {
149                                    FinderCacheUtil.removeResult(
150                                            FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
151    
152                                    throw new SystemException(e);
153                            }
154                            finally {
155                                    closeSession(session);
156                            }
157                    }
158    
159                    return list;
160            }
161    
162            protected boolean isInstanceOf(
163                    String portletPreferencesPortletId, String portletId) {
164    
165                    portletPreferencesPortletId = GetterUtil.getString(
166                            portletPreferencesPortletId);
167                    portletId = GetterUtil.getString(portletId);
168    
169                    if (portletPreferencesPortletId.equals(portletId)) {
170                            return true;
171                    }
172    
173                    if (portletPreferencesPortletId.startsWith(
174                                    portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
175    
176                            return true;
177                    }
178    
179                    return false;
180            }
181    
182    }