1   /**
2    * Copyright (c) 2000-2007 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.model.Layout;
27  import com.liferay.portal.model.LayoutReference;
28  import com.liferay.portal.model.LayoutSoap;
29  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
30  import com.liferay.portal.spring.hibernate.HibernateUtil;
31  import com.liferay.util.dao.hibernate.QueryPos;
32  
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.hibernate.Hibernate;
38  import org.hibernate.SQLQuery;
39  import org.hibernate.Session;
40  
41  /**
42   * <a href="LayoutFinder.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LayoutFinder {
48  
49      public static String FIND_BY_C_P_P =
50          LayoutFinder.class.getName() + ".findByC_P_P";
51  
52      public static List findByC_P_P(
53              long companyId, String portletId, String prefsKey,
54              String prefsValue)
55          throws SystemException {
56  
57          String prefs =
58              "%<preference><name>" + prefsKey + "</name><value>" + prefsValue +
59                  "</value>%";
60  
61          Session session = null;
62  
63          try {
64              session = HibernateUtil.openSession();
65  
66              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
67  
68              SQLQuery q = session.createSQLQuery(sql);
69  
70              q.addScalar("layoutPlid", Hibernate.LONG);
71              q.addScalar("prefsPortletId", Hibernate.STRING);
72  
73              QueryPos qPos = QueryPos.getInstance(q);
74  
75              qPos.add(companyId);
76              qPos.add(portletId);
77              qPos.add(portletId + "_INSTANCE_%");
78              qPos.add(prefs);
79  
80              List list = new ArrayList();
81  
82              Iterator itr = q.list().iterator();
83  
84              while (itr.hasNext()) {
85                  Object[] array = (Object[])itr.next();
86  
87                  Long layoutPlid = (Long)array[0];
88                  String prefsPortletId = (String)array[1];
89  
90                  Layout layout = LayoutUtil.findByPrimaryKey(
91                      layoutPlid.longValue());
92  
93                  list.add(
94                      new LayoutReference(
95                          LayoutSoap.toSoapModel(layout), prefsPortletId));
96              }
97  
98              return list;
99          }
100         catch (Exception e) {
101             throw new SystemException(e);
102         }
103         finally {
104             HibernateUtil.closeSession(session);
105         }
106     }
107 
108 }