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            @Override
053            public List<Layout> findByNoPermissions(long roleId)
054                    throws SystemException {
055    
056                    Session session = null;
057    
058                    try {
059                            session = openSession();
060    
061                            String sql = CustomSQLUtil.get(FIND_BY_NO_PERMISSIONS);
062    
063                            SQLQuery q = session.createSQLQuery(sql);
064    
065                            q.addEntity("Layout", LayoutImpl.class);
066    
067                            QueryPos qPos = QueryPos.getInstance(q);
068    
069                            qPos.add(ResourceConstants.SCOPE_INDIVIDUAL);
070                            qPos.add(roleId);
071    
072                            return q.list(true);
073                    }
074                    catch (Exception e) {
075                            throw new SystemException(e);
076                    }
077                    finally {
078                            closeSession(session);
079                    }
080            }
081    
082            @Override
083            public List<Layout> findByNullFriendlyURL() throws SystemException {
084                    Session session = null;
085    
086                    try {
087                            session = openSession();
088    
089                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
090    
091                            SQLQuery q = session.createSQLQuery(sql);
092    
093                            q.addEntity("Layout", LayoutImpl.class);
094    
095                            return q.list(true);
096                    }
097                    catch (Exception e) {
098                            throw new SystemException(e);
099                    }
100                    finally {
101                            closeSession(session);
102                    }
103            }
104    
105            @Override
106            public List<Layout> findByScopeGroup(long groupId, boolean privateLayout)
107                    throws SystemException {
108    
109                    Session session = null;
110    
111                    try {
112                            session = openSession();
113    
114                            String sql = CustomSQLUtil.get(FIND_BY_SCOPE_GROUP);
115    
116                            SQLQuery q = session.createSQLQuery(sql);
117    
118                            q.addEntity("Layout", LayoutImpl.class);
119    
120                            QueryPos qPos = QueryPos.getInstance(q);
121    
122                            qPos.add(groupId);
123                            qPos.add(privateLayout);
124    
125                            return q.list(true);
126                    }
127                    catch (Exception e) {
128                            throw new SystemException(e);
129                    }
130                    finally {
131                            closeSession(session);
132                    }
133            }
134    
135            @Override
136            public List<LayoutReference> findByC_P_P(
137                            long companyId, String portletId, String preferencesKey,
138                            String preferencesValue)
139                    throws SystemException {
140    
141                    String preferences =
142                            "%<preference><name>" + preferencesKey + "</name><value>" +
143                                    preferencesValue + "</value>%";
144    
145                    Session session = null;
146    
147                    try {
148                            session = openSession();
149    
150                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
151    
152                            SQLQuery q = session.createSQLQuery(sql);
153    
154                            q.addScalar("layoutPlid", Type.LONG);
155                            q.addScalar("preferencesPortletId", Type.STRING);
156    
157                            QueryPos qPos = QueryPos.getInstance(q);
158    
159                            qPos.add(companyId);
160                            qPos.add(portletId);
161                            qPos.add(portletId.concat("_INSTANCE_%"));
162                            qPos.add(preferences);
163    
164                            List<LayoutReference> layoutReferences =
165                                    new ArrayList<LayoutReference>();
166    
167                            Iterator<Object[]> itr = q.iterate();
168    
169                            while (itr.hasNext()) {
170                                    Object[] array = itr.next();
171    
172                                    Long layoutPlid = (Long)array[0];
173                                    String preferencesPortletId = (String)array[1];
174    
175                                    Layout layout = LayoutUtil.findByPrimaryKey(
176                                            layoutPlid.longValue());
177    
178                                    layoutReferences.add(
179                                            new LayoutReference(
180                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
181                            }
182    
183                            return layoutReferences;
184                    }
185                    catch (Exception e) {
186                            throw new SystemException(e);
187                    }
188                    finally {
189                            closeSession(session);
190                    }
191            }
192    
193    }