1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
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 }