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