001    /**
002     * Copyright (c) 2000-2012 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.impl.LayoutImpl;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.util.dao.orm.CustomSQLUtil;
028    
029    import java.util.ArrayList;
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class LayoutFinderImpl
037            extends BasePersistenceImpl<Layout> implements LayoutFinder {
038    
039            public static final String FIND_BY_NULL_FRIENDLY_URL =
040                    LayoutFinder.class.getName() + ".findByNullFriendlyURL";
041    
042            public static final String FIND_BY_SCOPE_GROUP =
043                    LayoutFinder.class.getName() + ".findByScopeGroup";
044    
045            public static final String FIND_BY_C_P_P =
046                    LayoutFinder.class.getName() + ".findByC_P_P";
047    
048            public List<Layout> findByNullFriendlyURL() throws SystemException {
049                    Session session = null;
050    
051                    try {
052                            session = openSession();
053    
054                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
055    
056                            SQLQuery q = session.createSQLQuery(sql);
057    
058                            q.addEntity("Layout", LayoutImpl.class);
059    
060                            return q.list(true);
061                    }
062                    catch (Exception e) {
063                            throw new SystemException(e);
064                    }
065                    finally {
066                            closeSession(session);
067                    }
068            }
069    
070            public List<Layout> findByScopeGroup(long groupId, boolean privateLayout)
071                    throws SystemException {
072    
073                    Session session = null;
074    
075                    try {
076                            session = openSession();
077    
078                            String sql = CustomSQLUtil.get(FIND_BY_SCOPE_GROUP);
079    
080                            SQLQuery q = session.createSQLQuery(sql);
081    
082                            q.addEntity("Layout", LayoutImpl.class);
083    
084                            QueryPos qPos = QueryPos.getInstance(q);
085    
086                            qPos.add(groupId);
087                            qPos.add(privateLayout);
088    
089                            return q.list(true);
090                    }
091                    catch (Exception e) {
092                            throw new SystemException(e);
093                    }
094                    finally {
095                            closeSession(session);
096                    }
097            }
098    
099            public List<LayoutReference> findByC_P_P(
100                            long companyId, String portletId, String preferencesKey,
101                            String preferencesValue)
102                    throws SystemException {
103    
104                    String preferences =
105                            "%<preference><name>" + preferencesKey + "</name><value>" +
106                                    preferencesValue + "</value>%";
107    
108                    Session session = null;
109    
110                    try {
111                            session = openSession();
112    
113                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
114    
115                            SQLQuery q = session.createSQLQuery(sql);
116    
117                            q.addScalar("layoutPlid", Type.LONG);
118                            q.addScalar("preferencesPortletId", Type.STRING);
119    
120                            QueryPos qPos = QueryPos.getInstance(q);
121    
122                            qPos.add(companyId);
123                            qPos.add(portletId);
124                            qPos.add(portletId.concat("_INSTANCE_%"));
125                            qPos.add(preferences);
126    
127                            List<LayoutReference> layoutReferences =
128                                    new ArrayList<LayoutReference>();
129    
130                            Iterator<Object[]> itr = q.iterate();
131    
132                            while (itr.hasNext()) {
133                                    Object[] array = itr.next();
134    
135                                    Long layoutPlid = (Long)array[0];
136                                    String preferencesPortletId = (String)array[1];
137    
138                                    Layout layout = LayoutUtil.findByPrimaryKey(
139                                            layoutPlid.longValue());
140    
141                                    layoutReferences.add(
142                                            new LayoutReference(
143                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
144                            }
145    
146                            return layoutReferences;
147                    }
148                    catch (Exception e) {
149                            throw new SystemException(e);
150                    }
151                    finally {
152                            closeSession(session);
153                    }
154            }
155    
156    }