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.QueryUtil;
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.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.model.PasswordPolicy;
26  import com.liferay.portal.model.impl.PasswordPolicyImpl;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.util.dao.orm.CustomSQLUtil;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="PasswordPolicyFinderImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class PasswordPolicyFinderImpl
39      extends BasePersistenceImpl<PasswordPolicy>
40      implements PasswordPolicyFinder {
41  
42      public static String COUNT_BY_C_N =
43          PasswordPolicyFinder.class.getName() + ".countByC_N";
44  
45      public static String FIND_BY_C_N =
46          PasswordPolicyFinder.class.getName() + ".findByC_N";
47  
48      public int countByC_N(long companyId, String name) throws SystemException {
49          name = StringUtil.lowerCase(name);
50  
51          Session session = null;
52  
53          try {
54              session = openSession();
55  
56              String sql = CustomSQLUtil.get(COUNT_BY_C_N);
57  
58              SQLQuery q = session.createSQLQuery(sql);
59  
60              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
61  
62              QueryPos qPos = QueryPos.getInstance(q);
63  
64              qPos.add(companyId);
65              qPos.add(name);
66              qPos.add(name);
67  
68              Iterator<Long> itr = q.list().iterator();
69  
70              if (itr.hasNext()) {
71                  Long count = itr.next();
72  
73                  if (count != null) {
74                      return count.intValue();
75                  }
76              }
77  
78              return 0;
79          }
80          catch (Exception e) {
81              throw new SystemException(e);
82          }
83          finally {
84              closeSession(session);
85          }
86      }
87  
88      public List<PasswordPolicy> findByC_N(
89              long companyId, String name, int start, int end,
90              OrderByComparator obc)
91          throws SystemException {
92  
93          name = StringUtil.lowerCase(name);
94  
95          Session session = null;
96  
97          try {
98              session = openSession();
99  
100             String sql = CustomSQLUtil.get(FIND_BY_C_N);
101 
102             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
103 
104             SQLQuery q = session.createSQLQuery(sql);
105 
106             q.addEntity("PasswordPolicy", PasswordPolicyImpl.class);
107 
108             QueryPos qPos = QueryPos.getInstance(q);
109 
110             qPos.add(companyId);
111             qPos.add(name);
112             qPos.add(name);
113 
114             return (List<PasswordPolicy>)QueryUtil.list(
115                 q, getDialect(), start, end);
116         }
117         catch (Exception e) {
118             throw new SystemException(e);
119         }
120         finally {
121             closeSession(session);
122         }
123     }
124 
125 }