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.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.LayoutFinder;
028    import com.liferay.portal.service.persistence.LayoutUtil;
029    import com.liferay.util.dao.orm.CustomSQLUtil;
030    
031    import java.util.ArrayList;
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class LayoutFinderImpl
039            extends BasePersistenceImpl<Layout> implements LayoutFinder {
040    
041            public static final String FIND_BY_NO_PERMISSIONS =
042                    LayoutFinder.class.getName() + ".findByNoPermissions";
043    
044            public static final String FIND_BY_NULL_FRIENDLY_URL =
045                    LayoutFinder.class.getName() + ".findByNullFriendlyURL";
046    
047            public static final String FIND_BY_SCOPE_GROUP =
048                    LayoutFinder.class.getName() + ".findByScopeGroup";
049    
050            public static final String FIND_BY_C_P_P =
051                    LayoutFinder.class.getName() + ".findByC_P_P";
052    
053            @Override
054            public List<Layout> findByNoPermissions(long roleId) {
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.createSynchronizedSQLQuery(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            @Override
082            public List<Layout> findByNullFriendlyURL() {
083                    Session session = null;
084    
085                    try {
086                            session = openSession();
087    
088                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
089    
090                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
091    
092                            q.addEntity("Layout", LayoutImpl.class);
093    
094                            return q.list(true);
095                    }
096                    catch (Exception e) {
097                            throw new SystemException(e);
098                    }
099                    finally {
100                            closeSession(session);
101                    }
102            }
103    
104            @Override
105            public List<Layout> findByScopeGroup(long groupId, boolean privateLayout) {
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.createSynchronizedSQLQuery(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            @Override
133            public List<LayoutReference> findByC_P_P(
134                    long companyId, String portletId, String preferencesKey,
135                    String preferencesValue) {
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.createSynchronizedSQLQuery(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    }