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