001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(Layout.class.getName());
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() throws SystemException {
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.createSQLQuery(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, boolean privateLayout)
108                    throws SystemException {
109    
110                    Session session = null;
111    
112                    try {
113                            session = openSession();
114    
115                            String sql = CustomSQLUtil.get(FIND_BY_SCOPE_GROUP);
116    
117                            SQLQuery q = session.createSQLQuery(sql);
118    
119                            q.addEntity("Layout", LayoutImpl.class);
120    
121                            QueryPos qPos = QueryPos.getInstance(q);
122    
123                            qPos.add(groupId);
124                            qPos.add(privateLayout);
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<LayoutReference> findByC_P_P(
138                            long companyId, String portletId, String preferencesKey,
139                            String preferencesValue)
140                    throws SystemException {
141    
142                    String preferences =
143                            "%<preference><name>" + preferencesKey + "</name><value>" +
144                                    preferencesValue + "</value>%";
145    
146                    Session session = null;
147    
148                    try {
149                            session = openSession();
150    
151                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
152    
153                            SQLQuery q = session.createSQLQuery(sql);
154    
155                            q.addScalar("layoutPlid", Type.LONG);
156                            q.addScalar("preferencesPortletId", Type.STRING);
157    
158                            QueryPos qPos = QueryPos.getInstance(q);
159    
160                            qPos.add(companyId);
161                            qPos.add(portletId);
162                            qPos.add(portletId.concat("_INSTANCE_%"));
163                            qPos.add(preferences);
164    
165                            List<LayoutReference> layoutReferences =
166                                    new ArrayList<LayoutReference>();
167    
168                            Iterator<Object[]> itr = q.iterate();
169    
170                            while (itr.hasNext()) {
171                                    Object[] array = itr.next();
172    
173                                    Long layoutPlid = (Long)array[0];
174                                    String preferencesPortletId = (String)array[1];
175    
176                                    Layout layout = LayoutUtil.findByPrimaryKey(
177                                            layoutPlid.longValue());
178    
179                                    layoutReferences.add(
180                                            new LayoutReference(
181                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
182                            }
183    
184                            return layoutReferences;
185                    }
186                    catch (Exception e) {
187                            throw new SystemException(e);
188                    }
189                    finally {
190                            closeSession(session);
191                    }
192            }
193    
194    }