1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.SQLQuery;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.Type;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.model.LayoutReference;
32  import com.liferay.portal.model.LayoutSoap;
33  import com.liferay.portal.model.impl.LayoutImpl;
34  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
35  import com.liferay.util.dao.orm.CustomSQLUtil;
36  
37  import java.util.ArrayList;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="LayoutFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class LayoutFinderImpl
47      extends BasePersistenceImpl implements LayoutFinder {
48  
49      public static String FIND_BY_NULL_FRIENDLY_URL =
50          LayoutFinder.class.getName() + ".findByNullFriendlyURL";
51  
52      public static String FIND_BY_C_P_P =
53          LayoutFinder.class.getName() + ".findByC_P_P";
54  
55      public List<Layout> findByNullFriendlyURL() throws SystemException {
56          Session session = null;
57  
58          try {
59              session = openSession();
60  
61              String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
62  
63              SQLQuery q = session.createSQLQuery(sql);
64  
65              q.addEntity("Layout", LayoutImpl.class);
66  
67              return q.list();
68          }
69          catch (Exception e) {
70              throw new SystemException(e);
71          }
72          finally {
73              closeSession(session);
74          }
75      }
76  
77      public List<LayoutReference> findByC_P_P(
78              long companyId, String portletId, String prefsKey,
79              String prefsValue)
80          throws SystemException {
81  
82          String prefs =
83              "%<preference><name>" + prefsKey + "</name><value>" + prefsValue +
84                  "</value>%";
85  
86          Session session = null;
87  
88          try {
89              session = openSession();
90  
91              String sql = CustomSQLUtil.get(FIND_BY_C_P_P);
92  
93              SQLQuery q = session.createSQLQuery(sql);
94  
95              q.addScalar("layoutPlid", Type.LONG);
96              q.addScalar("prefsPortletId", Type.STRING);
97  
98              QueryPos qPos = QueryPos.getInstance(q);
99  
100             qPos.add(companyId);
101             qPos.add(portletId);
102             qPos.add(portletId + "_INSTANCE_%");
103             qPos.add(prefs);
104 
105             List<LayoutReference> layoutReferences =
106                 new ArrayList<LayoutReference>();
107 
108             Iterator<Object[]> itr = q.list().iterator();
109 
110             while (itr.hasNext()) {
111                 Object[] array = itr.next();
112 
113                 Long layoutPlid = (Long)array[0];
114                 String prefsPortletId = (String)array[1];
115 
116                 Layout layout = LayoutUtil.findByPrimaryKey(
117                     layoutPlid.longValue());
118 
119                 layoutReferences.add(
120                     new LayoutReference(
121                         LayoutSoap.toSoapModel(layout), prefsPortletId));
122             }
123 
124             return layoutReferences;
125         }
126         catch (Exception e) {
127             throw new SystemException(e);
128         }
129         finally {
130             closeSession(session);
131         }
132     }
133 
134 }