1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.SQLQuery;
19  import com.liferay.portal.kernel.dao.orm.Session;
20  import com.liferay.portal.kernel.dao.orm.Type;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.model.LayoutReference;
24  import com.liferay.portal.model.LayoutSoap;
25  import com.liferay.portal.model.impl.LayoutImpl;
26  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.util.dao.orm.CustomSQLUtil;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="LayoutFinderImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class LayoutFinderImpl
39      extends BasePersistenceImpl<Layout> implements LayoutFinder {
40  
41      public static String FIND_BY_NULL_FRIENDLY_URL =
42          LayoutFinder.class.getName() + ".findByNullFriendlyURL";
43  
44      public static String FIND_BY_C_P_P =
45          LayoutFinder.class.getName() + ".findByC_P_P";
46  
47      public List<Layout> findByNullFriendlyURL() throws SystemException {
48          Session session = null;
49  
50          try {
51              session = openSession();
52  
53              String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
54  
55              SQLQuery q = session.createSQLQuery(sql);
56  
57              q.addEntity("Layout", LayoutImpl.class);
58  
59              return q.list();
60          }
61          catch (Exception e) {
62              throw new SystemException(e);
63          }
64          finally {
65              closeSession(session);
66          }
67      }
68  
69      public List<LayoutReference> findByC_P_P(
70              long companyId, String portletId, String preferencesKey,
71              String preferencesValue)
72          throws SystemException {
73  
74          String preferences =
75              "%<preference><name>" + preferencesKey + "</name><value>" +
76                  preferencesValue + "</value>%";
77  
78          Session session = null;
79  
80          try {
81              session = openSession();
82  
83              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
84  
85              SQLQuery q = session.createSQLQuery(sql);
86  
87              q.addScalar("layoutPlid", Type.LONG);
88              q.addScalar("preferencesPortletId", Type.STRING);
89  
90              QueryPos qPos = QueryPos.getInstance(q);
91  
92              qPos.add(companyId);
93              qPos.add(portletId);
94              qPos.add(portletId + "_INSTANCE_%");
95              qPos.add(preferences);
96  
97              List<LayoutReference> layoutReferences =
98                  new ArrayList<LayoutReference>();
99  
100             Iterator<Object[]> itr = q.list().iterator();
101 
102             while (itr.hasNext()) {
103                 Object[] array = itr.next();
104 
105                 Long layoutPlid = (Long)array[0];
106                 String preferencesPortletId = (String)array[1];
107 
108                 Layout layout = LayoutUtil.findByPrimaryKey(
109                     layoutPlid.longValue());
110 
111                 layoutReferences.add(
112                     new LayoutReference(
113                         LayoutSoap.toSoapModel(layout), preferencesPortletId));
114             }
115 
116             return layoutReferences;
117         }
118         catch (Exception e) {
119             throw new SystemException(e);
120         }
121         finally {
122             closeSession(session);
123         }
124     }
125 
126 }