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 LayoutFinderBaseImpl 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_C_P_P =
048                    LayoutFinder.class.getName() + ".findByC_P_P";
049    
050            @Override
051            public List<Layout> findByNoPermissions(long roleId) {
052                    Session session = null;
053    
054                    try {
055                            session = openSession();
056    
057                            String sql = CustomSQLUtil.get(FIND_BY_NO_PERMISSIONS);
058    
059                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
060    
061                            q.addEntity("Layout", LayoutImpl.class);
062    
063                            QueryPos qPos = QueryPos.getInstance(q);
064    
065                            qPos.add(ResourceConstants.SCOPE_INDIVIDUAL);
066                            qPos.add(roleId);
067    
068                            return q.list(true);
069                    }
070                    catch (Exception e) {
071                            throw new SystemException(e);
072                    }
073                    finally {
074                            closeSession(session);
075                    }
076            }
077    
078            @Override
079            public List<Layout> findByNullFriendlyURL() {
080                    Session session = null;
081    
082                    try {
083                            session = openSession();
084    
085                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
086    
087                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
088    
089                            q.addEntity("Layout", LayoutImpl.class);
090    
091                            return q.list(true);
092                    }
093                    catch (Exception e) {
094                            throw new SystemException(e);
095                    }
096                    finally {
097                            closeSession(session);
098                    }
099            }
100    
101            @Override
102            public List<LayoutReference> findByC_P_P(
103                    long companyId, String portletId, String preferencesKey,
104                    String preferencesValue) {
105    
106                    String preferences =
107                            "%<preference><name>" + preferencesKey + "</name><value>" +
108                                    preferencesValue + "</value>%";
109    
110                    Session session = null;
111    
112                    try {
113                            session = openSession();
114    
115                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
116    
117                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
118    
119                            q.addScalar("layoutPlid", Type.LONG);
120                            q.addScalar("preferencesPortletId", Type.STRING);
121    
122                            QueryPos qPos = QueryPos.getInstance(q);
123    
124                            qPos.add(companyId);
125                            qPos.add(portletId);
126                            qPos.add(portletId.concat("_INSTANCE_%"));
127                            qPos.add(preferences);
128    
129                            List<LayoutReference> layoutReferences = new ArrayList<>();
130    
131                            Iterator<Object[]> itr = q.iterate();
132    
133                            while (itr.hasNext()) {
134                                    Object[] array = itr.next();
135    
136                                    Long layoutPlid = (Long)array[0];
137                                    String preferencesPortletId = (String)array[1];
138    
139                                    Layout layout = LayoutUtil.findByPrimaryKey(
140                                            layoutPlid.longValue());
141    
142                                    layoutReferences.add(
143                                            new LayoutReference(
144                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
145                            }
146    
147                            return layoutReferences;
148                    }
149                    catch (Exception e) {
150                            throw new SystemException(e);
151                    }
152                    finally {
153                            closeSession(session);
154                    }
155            }
156    
157    }