001    /**
002     * Copyright (c) 2000-2010 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 String FIND_BY_NULL_FRIENDLY_URL =
040                    LayoutFinder.class.getName() + ".findByNullFriendlyURL";
041    
042            public static String FIND_BY_C_P_P =
043                    LayoutFinder.class.getName() + ".findByC_P_P";
044    
045            public List<Layout> findByNullFriendlyURL() throws SystemException {
046                    Session session = null;
047    
048                    try {
049                            session = openSession();
050    
051                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
052    
053                            SQLQuery q = session.createSQLQuery(sql);
054    
055                            q.addEntity("Layout", LayoutImpl.class);
056    
057                            return q.list();
058                    }
059                    catch (Exception e) {
060                            throw new SystemException(e);
061                    }
062                    finally {
063                            closeSession(session);
064                    }
065            }
066    
067            public List<LayoutReference> findByC_P_P(
068                            long companyId, String portletId, String preferencesKey,
069                            String preferencesValue)
070                    throws SystemException {
071    
072                    String preferences =
073                            "%<preference><name>" + preferencesKey + "</name><value>" +
074                                    preferencesValue + "</value>%";
075    
076                    Session session = null;
077    
078                    try {
079                            session = openSession();
080    
081                            String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
082    
083                            SQLQuery q = session.createSQLQuery(sql);
084    
085                            q.addScalar("layoutPlid", Type.LONG);
086                            q.addScalar("preferencesPortletId", Type.STRING);
087    
088                            QueryPos qPos = QueryPos.getInstance(q);
089    
090                            qPos.add(companyId);
091                            qPos.add(portletId);
092                            qPos.add(portletId + "_INSTANCE_%");
093                            qPos.add(preferences);
094    
095                            List<LayoutReference> layoutReferences =
096                                    new ArrayList<LayoutReference>();
097    
098                            Iterator<Object[]> itr = q.list().iterator();
099    
100                            while (itr.hasNext()) {
101                                    Object[] array = itr.next();
102    
103                                    Long layoutPlid = (Long)array[0];
104                                    String preferencesPortletId = (String)array[1];
105    
106                                    Layout layout = LayoutUtil.findByPrimaryKey(
107                                            layoutPlid.longValue());
108    
109                                    layoutReferences.add(
110                                            new LayoutReference(
111                                                    LayoutSoap.toSoapModel(layout), preferencesPortletId));
112                            }
113    
114                            return layoutReferences;
115                    }
116                    catch (Exception e) {
117                            throw new SystemException(e);
118                    }
119                    finally {
120                            closeSession(session);
121                    }
122            }
123    
124    }