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