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