1   /**
2    * Copyright (c) 2000-2008 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.model.impl.LayoutImpl;
30  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
31  import com.liferay.portal.spring.hibernate.HibernateUtil;
32  import com.liferay.util.dao.hibernate.QueryPos;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import org.hibernate.Hibernate;
39  import org.hibernate.SQLQuery;
40  import org.hibernate.Session;
41  
42  /**
43   * <a href="LayoutFinderImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class LayoutFinderImpl 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 = HibernateUtil.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              HibernateUtil.closeSession(session);
75          }
76      }
77  
78      public List<LayoutReference> findByC_P_P(
79              long companyId, String portletId, String prefsKey,
80              String prefsValue)
81          throws SystemException {
82  
83          String prefs =
84              "%<preference><name>" + prefsKey + "</name><value>" + prefsValue +
85                  "</value>%";
86  
87          Session session = null;
88  
89          try {
90              session = HibernateUtil.openSession();
91  
92              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
93  
94              SQLQuery q = session.createSQLQuery(sql);
95  
96              q.addScalar("layoutPlid", Hibernate.LONG);
97              q.addScalar("prefsPortletId", Hibernate.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(prefs);
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 prefsPortletId = (String)array[1];
116 
117                 Layout layout = LayoutUtil.findByPrimaryKey(
118                     layoutPlid.longValue());
119 
120                 layoutReferences.add(
121                     new LayoutReference(
122                         LayoutSoap.toSoapModel(layout), prefsPortletId));
123             }
124 
125             return layoutReferences;
126         }
127         catch (Exception e) {
128             throw new SystemException(e);
129         }
130         finally {
131             HibernateUtil.closeSession(session);
132         }
133     }
134 
135 }