1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.SQLQuery;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.Type;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.LayoutReference;
32  import com.liferay.portal.model.LayoutSoap;
33  import com.liferay.portal.model.impl.LayoutImpl;
34  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
35  import com.liferay.util.dao.orm.CustomSQLUtil;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="LayoutFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LayoutFinderImpl
48      extends BasePersistenceImpl implements LayoutFinder {
49  
50      public static String FIND_BY_NULL_FRIENDLY_URL =
51          LayoutFinder.class.getName() + ".findByNullFriendlyURL";
52  
53      public static String FIND_BY_C_P_P =
54          LayoutFinder.class.getName() + ".findByC_P_P";
55  
56      public List<Layout> findByNullFriendlyURL() throws SystemException {
57          Session session = null;
58  
59          try {
60              session = openSession();
61  
62              String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
63  
64              SQLQuery q = session.createSQLQuery(sql);
65  
66              q.addEntity("Layout", LayoutImpl.class);
67  
68              return q.list();
69          }
70          catch (Exception e) {
71              throw new SystemException(e);
72          }
73          finally {
74              closeSession(session);
75          }
76      }
77  
78      public List<LayoutReference> findByC_P_P(
79              long companyId, String portletId, String preferencesKey,
80              String preferencesValue)
81          throws SystemException {
82  
83          String preferences =
84              "%<preference><name>" + preferencesKey + "</name><value>" +
85                  preferencesValue + "</value>%";
86  
87          Session session = null;
88  
89          try {
90              session = openSession();
91  
92              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
93  
94              SQLQuery q = session.createSQLQuery(sql);
95  
96              q.addScalar("layoutPlid", Type.LONG);
97              q.addScalar("preferencesPortletId", Type.STRING);
98  
99              QueryPos qPos = QueryPos.getInstance(q);
100 
101             qPos.add(companyId);
102             qPos.add(portletId);
103             qPos.add(portletId + "_INSTANCE_%");
104             qPos.add(preferences);
105 
106             List<LayoutReference> layoutReferences =
107                 new ArrayList<LayoutReference>();
108 
109             Iterator<Object[]> itr = q.list().iterator();
110 
111             while (itr.hasNext()) {
112                 Object[] array = itr.next();
113 
114                 Long layoutPlid = (Long)array[0];
115                 String preferencesPortletId = (String)array[1];
116 
117                 Layout layout = LayoutUtil.findByPrimaryKey(
118                     layoutPlid.longValue());
119 
120                 layoutReferences.add(
121                     new LayoutReference(
122                         LayoutSoap.toSoapModel(layout), preferencesPortletId));
123             }
124 
125             return layoutReferences;
126         }
127         catch (Exception e) {
128             throw new SystemException(e);
129         }
130         finally {
131             closeSession(session);
132         }
133     }
134 
135 }