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.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.SQLQuery;
019    import com.liferay.portal.kernel.dao.orm.Session;
020    import com.liferay.portal.kernel.dao.orm.Type;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutReference;
024    import com.liferay.portal.model.LayoutSoap;
025    import com.liferay.portal.model.ResourceConstants;
026    import com.liferay.portal.model.impl.LayoutImpl;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.ArrayList;
031    import java.util.Iterator;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class LayoutFinderImpl
038            extends BasePersistenceImpl<Layout> implements LayoutFinder {
039    
040            public static final String FIND_BY_NO_PERMISSIONS =
041                    LayoutFinder.class.getName() + ".findByNoPermissions";
042    
043            public static final String FIND_BY_NULL_FRIENDLY_URL =
044                    LayoutFinder.class.getName() + ".findByNullFriendlyURL";
045    
046            public static final String FIND_BY_SCOPE_GROUP =
047                    LayoutFinder.class.getName() + ".findByScopeGroup";
048    
049            public static final String FIND_BY_C_P_P =
050                    LayoutFinder.class.getName() + ".findByC_P_P";
051    
052            public List<Layout> findByNoPermissions(long roleId)
053                    throws SystemException {
054    
055                    Session session = null;
056    
057                    try {
058                            session = openSession();
059    
060                            String sql = CustomSQLUtil.get(FIND_BY_NO_PERMISSIONS);
061    
062                            SQLQuery q = session.createSQLQuery(sql);
063    
064                            q.addEntity("Layout", LayoutImpl.class);
065    
066                            QueryPos qPos = QueryPos.getInstance(q);
067    
068                            qPos.add(ResourceConstants.SCOPE_INDIVIDUAL);
069                            qPos.add(roleId);
070    
071                            return q.list(true);
072                    }
073                    catch (Exception e) {
074                            throw new SystemException(e);
075                    }
076                    finally {
077                            closeSession(session);
078                    }
079            }
080    
081            public List<Layout> findByNullFriendlyURL() throws SystemException {
082                    Session session = null;
083    
084                    try {
085                            session = openSession();
086    
087                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
088    
089                            SQLQuery q = session.createSQLQuery(sql);
090    
091                            q.addEntity("Layout", LayoutImpl.class);
092    
093                            return q.list(true);
094                    }
095                    catch (Exception e) {
096                            throw new SystemException(e);
097                    }
098                    finally {
099                            closeSession(session);
100                    }
101            }
102    
103            public List<Layout> findByScopeGroup(long groupId, boolean privateLayout)
104                    throws SystemException {
105    
106                    Session session = null;
107    
108                    try {
109                            session = openSession();
110    
111                            String sql = CustomSQLUtil.get(FIND_BY_SCOPE_GROUP);
112    
113                            SQLQuery q = session.createSQLQuery(sql);
114    
115                            q.addEntity("Layout", LayoutImpl.class);
116    
117                            QueryPos qPos = QueryPos.getInstance(q);
118    
119                            qPos.add(groupId);
120                            qPos.add(privateLayout);
121    
122                            return q.list(true);
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132            public List<LayoutReference> findByC_P_P(
133                            long companyId, String portletId, String preferencesKey,
134                            String preferencesValue)
135                    throws SystemException {
136    
137                    String preferences =
138                            "%<preference><name>" + preferencesKey + "</name><value>" +
139                                    preferencesValue + "</value>%";
140    
141                    Session session = null;
142    
143                    try {
144                            session = openSession();
145    
146                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
147    
148                            SQLQuery q = session.createSQLQuery(sql);
149    
150                            q.addScalar("layoutPlid", Type.LONG);
151                            q.addScalar("preferencesPortletId", Type.STRING);
152    
153                            QueryPos qPos = QueryPos.getInstance(q);
154    
155                            qPos.add(companyId);
156                            qPos.add(portletId);
157                            qPos.add(portletId.concat("_INSTANCE_%"));
158                            qPos.add(preferences);
159    
160                            List<LayoutReference> layoutReferences =
161                                    new ArrayList<LayoutReference>();
162    
163                            Iterator<Object[]> itr = q.iterate();
164    
165                            while (itr.hasNext()) {
166                                    Object[] array = itr.next();
167    
168                                    Long layoutPlid = (Long)array[0];
169                                    String preferencesPortletId = (String)array[1];
170    
171                                    Layout layout = LayoutUtil.findByPrimaryKey(
172                                            layoutPlid.longValue());
173    
174                                    layoutReferences.add(
175                                            new LayoutReference(
176                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
177                            }
178    
179                            return layoutReferences;
180                    }
181                    catch (Exception e) {
182                            throw new SystemException(e);
183                    }
184                    finally {
185                            closeSession(session);
186                    }
187            }
188    
189    }