1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.SQLQuery;
25  import com.liferay.portal.kernel.dao.orm.Session;
26  import com.liferay.portal.kernel.dao.orm.Type;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutReference;
29  import com.liferay.portal.model.LayoutSoap;
30  import com.liferay.portal.model.impl.LayoutImpl;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="LayoutFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class LayoutFinderImpl
45      extends BasePersistenceImpl implements LayoutFinder {
46  
47      public static String FIND_BY_NULL_FRIENDLY_URL =
48          LayoutFinder.class.getName() + ".findByNullFriendlyURL";
49  
50      public static String FIND_BY_C_P_P =
51          LayoutFinder.class.getName() + ".findByC_P_P";
52  
53      public List<Layout> findByNullFriendlyURL() throws SystemException {
54          Session session = null;
55  
56          try {
57              session = openSession();
58  
59              String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
60  
61              SQLQuery q = session.createSQLQuery(sql);
62  
63              q.addEntity("Layout", LayoutImpl.class);
64  
65              return q.list();
66          }
67          catch (Exception e) {
68              throw new SystemException(e);
69          }
70          finally {
71              closeSession(session);
72          }
73      }
74  
75      public List<LayoutReference> findByC_P_P(
76              long companyId, String portletId, String prefsKey,
77              String prefsValue)
78          throws SystemException {
79  
80          String prefs =
81              "%<preference><name>" + prefsKey + "</name><value>" + prefsValue +
82                  "</value>%";
83  
84          Session session = null;
85  
86          try {
87              session = openSession();
88  
89              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
90  
91              SQLQuery q = session.createSQLQuery(sql);
92  
93              q.addScalar("layoutPlid", Type.LONG);
94              q.addScalar("prefsPortletId", Type.STRING);
95  
96              QueryPos qPos = QueryPos.getInstance(q);
97  
98              qPos.add(companyId);
99              qPos.add(portletId);
100             qPos.add(portletId + "_INSTANCE_%");
101             qPos.add(prefs);
102 
103             List<LayoutReference> layoutReferences =
104                 new ArrayList<LayoutReference>();
105 
106             Iterator<Object[]> itr = q.list().iterator();
107 
108             while (itr.hasNext()) {
109                 Object[] array = itr.next();
110 
111                 Long layoutPlid = (Long)array[0];
112                 String prefsPortletId = (String)array[1];
113 
114                 Layout layout = LayoutUtil.findByPrimaryKey(
115                     layoutPlid.longValue());
116 
117                 layoutReferences.add(
118                     new LayoutReference(
119                         LayoutSoap.toSoapModel(layout), prefsPortletId));
120             }
121 
122             return layoutReferences;
123         }
124         catch (Exception e) {
125             throw new SystemException(e);
126         }
127         finally {
128             closeSession(session);
129         }
130     }
131 
132 }