1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.NoSuchPasswordPolicyException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.PasswordPolicy;
33  import com.liferay.portal.model.impl.PasswordPolicyImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="PasswordPolicyPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class PasswordPolicyPersistenceImpl extends BasePersistence
57      implements PasswordPolicyPersistence {
58      public PasswordPolicy create(long passwordPolicyId) {
59          PasswordPolicy passwordPolicy = new PasswordPolicyImpl();
60          passwordPolicy.setNew(true);
61          passwordPolicy.setPrimaryKey(passwordPolicyId);
62  
63          return passwordPolicy;
64      }
65  
66      public PasswordPolicy remove(long passwordPolicyId)
67          throws NoSuchPasswordPolicyException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              PasswordPolicy passwordPolicy = (PasswordPolicy)session.get(PasswordPolicyImpl.class,
74                      new Long(passwordPolicyId));
75  
76              if (passwordPolicy == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No PasswordPolicy exists with the primary key " +
79                          passwordPolicyId);
80                  }
81  
82                  throw new NoSuchPasswordPolicyException(
83                      "No PasswordPolicy exists with the primary key " +
84                      passwordPolicyId);
85              }
86  
87              return remove(passwordPolicy);
88          }
89          catch (NoSuchPasswordPolicyException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public PasswordPolicy remove(PasswordPolicy passwordPolicy)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(passwordPolicy);
107             session.flush();
108 
109             return passwordPolicy;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(PasswordPolicy.class.getName());
117         }
118     }
119 
120     public PasswordPolicy update(
121         com.liferay.portal.model.PasswordPolicy passwordPolicy)
122         throws SystemException {
123         return update(passwordPolicy, false);
124     }
125 
126     public PasswordPolicy update(
127         com.liferay.portal.model.PasswordPolicy passwordPolicy, boolean merge)
128         throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(passwordPolicy);
136             }
137             else {
138                 if (passwordPolicy.isNew()) {
139                     session.save(passwordPolicy);
140                 }
141             }
142 
143             session.flush();
144             passwordPolicy.setNew(false);
145 
146             return passwordPolicy;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(PasswordPolicy.class.getName());
154         }
155     }
156 
157     public PasswordPolicy findByPrimaryKey(long passwordPolicyId)
158         throws NoSuchPasswordPolicyException, SystemException {
159         PasswordPolicy passwordPolicy = fetchByPrimaryKey(passwordPolicyId);
160 
161         if (passwordPolicy == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No PasswordPolicy exists with the primary key " +
164                     passwordPolicyId);
165             }
166 
167             throw new NoSuchPasswordPolicyException(
168                 "No PasswordPolicy exists with the primary key " +
169                 passwordPolicyId);
170         }
171 
172         return passwordPolicy;
173     }
174 
175     public PasswordPolicy fetchByPrimaryKey(long passwordPolicyId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (PasswordPolicy)session.get(PasswordPolicyImpl.class,
183                 new Long(passwordPolicyId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public PasswordPolicy findByC_DP(long companyId, boolean defaultPolicy)
194         throws NoSuchPasswordPolicyException, SystemException {
195         PasswordPolicy passwordPolicy = fetchByC_DP(companyId, defaultPolicy);
196 
197         if (passwordPolicy == null) {
198             StringMaker msg = new StringMaker();
199             msg.append("No PasswordPolicy exists with the key ");
200             msg.append(StringPool.OPEN_CURLY_BRACE);
201             msg.append("companyId=");
202             msg.append(companyId);
203             msg.append(", ");
204             msg.append("defaultPolicy=");
205             msg.append(defaultPolicy);
206             msg.append(StringPool.CLOSE_CURLY_BRACE);
207 
208             if (_log.isWarnEnabled()) {
209                 _log.warn(msg.toString());
210             }
211 
212             throw new NoSuchPasswordPolicyException(msg.toString());
213         }
214 
215         return passwordPolicy;
216     }
217 
218     public PasswordPolicy fetchByC_DP(long companyId, boolean defaultPolicy)
219         throws SystemException {
220         String finderClassName = PasswordPolicy.class.getName();
221         String finderMethodName = "fetchByC_DP";
222         String[] finderParams = new String[] {
223                 Long.class.getName(), Boolean.class.getName()
224             };
225         Object[] finderArgs = new Object[] {
226                 new Long(companyId), Boolean.valueOf(defaultPolicy)
227             };
228         Object result = FinderCache.getResult(finderClassName,
229                 finderMethodName, finderParams, finderArgs, getSessionFactory());
230 
231         if (result == null) {
232             Session session = null;
233 
234             try {
235                 session = openSession();
236 
237                 StringMaker query = new StringMaker();
238                 query.append(
239                     "FROM com.liferay.portal.model.PasswordPolicy WHERE ");
240                 query.append("companyId = ?");
241                 query.append(" AND ");
242                 query.append("defaultPolicy = ?");
243                 query.append(" ");
244 
245                 Query q = session.createQuery(query.toString());
246                 int queryPos = 0;
247                 q.setLong(queryPos++, companyId);
248                 q.setBoolean(queryPos++, defaultPolicy);
249 
250                 List list = q.list();
251                 FinderCache.putResult(finderClassName, finderMethodName,
252                     finderParams, finderArgs, list);
253 
254                 if (list.size() == 0) {
255                     return null;
256                 }
257                 else {
258                     return (PasswordPolicy)list.get(0);
259                 }
260             }
261             catch (Exception e) {
262                 throw HibernateUtil.processException(e);
263             }
264             finally {
265                 closeSession(session);
266             }
267         }
268         else {
269             List list = (List)result;
270 
271             if (list.size() == 0) {
272                 return null;
273             }
274             else {
275                 return (PasswordPolicy)list.get(0);
276             }
277         }
278     }
279 
280     public PasswordPolicy findByC_N(long companyId, String name)
281         throws NoSuchPasswordPolicyException, SystemException {
282         PasswordPolicy passwordPolicy = fetchByC_N(companyId, name);
283 
284         if (passwordPolicy == null) {
285             StringMaker msg = new StringMaker();
286             msg.append("No PasswordPolicy exists with the key ");
287             msg.append(StringPool.OPEN_CURLY_BRACE);
288             msg.append("companyId=");
289             msg.append(companyId);
290             msg.append(", ");
291             msg.append("name=");
292             msg.append(name);
293             msg.append(StringPool.CLOSE_CURLY_BRACE);
294 
295             if (_log.isWarnEnabled()) {
296                 _log.warn(msg.toString());
297             }
298 
299             throw new NoSuchPasswordPolicyException(msg.toString());
300         }
301 
302         return passwordPolicy;
303     }
304 
305     public PasswordPolicy fetchByC_N(long companyId, String name)
306         throws SystemException {
307         String finderClassName = PasswordPolicy.class.getName();
308         String finderMethodName = "fetchByC_N";
309         String[] finderParams = new String[] {
310                 Long.class.getName(), String.class.getName()
311             };
312         Object[] finderArgs = new Object[] { new Long(companyId), name };
313         Object result = FinderCache.getResult(finderClassName,
314                 finderMethodName, finderParams, finderArgs, getSessionFactory());
315 
316         if (result == null) {
317             Session session = null;
318 
319             try {
320                 session = openSession();
321 
322                 StringMaker query = new StringMaker();
323                 query.append(
324                     "FROM com.liferay.portal.model.PasswordPolicy WHERE ");
325                 query.append("companyId = ?");
326                 query.append(" AND ");
327 
328                 if (name == null) {
329                     query.append("name IS NULL");
330                 }
331                 else {
332                     query.append("name = ?");
333                 }
334 
335                 query.append(" ");
336 
337                 Query q = session.createQuery(query.toString());
338                 int queryPos = 0;
339                 q.setLong(queryPos++, companyId);
340 
341                 if (name != null) {
342                     q.setString(queryPos++, name);
343                 }
344 
345                 List list = q.list();
346                 FinderCache.putResult(finderClassName, finderMethodName,
347                     finderParams, finderArgs, list);
348 
349                 if (list.size() == 0) {
350                     return null;
351                 }
352                 else {
353                     return (PasswordPolicy)list.get(0);
354                 }
355             }
356             catch (Exception e) {
357                 throw HibernateUtil.processException(e);
358             }
359             finally {
360                 closeSession(session);
361             }
362         }
363         else {
364             List list = (List)result;
365 
366             if (list.size() == 0) {
367                 return null;
368             }
369             else {
370                 return (PasswordPolicy)list.get(0);
371             }
372         }
373     }
374 
375     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
376         throws SystemException {
377         Session session = null;
378 
379         try {
380             session = openSession();
381 
382             DynamicQuery query = queryInitializer.initialize(session);
383 
384             return query.list();
385         }
386         catch (Exception e) {
387             throw HibernateUtil.processException(e);
388         }
389         finally {
390             closeSession(session);
391         }
392     }
393 
394     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
395         int begin, int end) throws SystemException {
396         Session session = null;
397 
398         try {
399             session = openSession();
400 
401             DynamicQuery query = queryInitializer.initialize(session);
402             query.setLimit(begin, end);
403 
404             return query.list();
405         }
406         catch (Exception e) {
407             throw HibernateUtil.processException(e);
408         }
409         finally {
410             closeSession(session);
411         }
412     }
413 
414     public List findAll() throws SystemException {
415         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
416     }
417 
418     public List findAll(int begin, int end) throws SystemException {
419         return findAll(begin, end, null);
420     }
421 
422     public List findAll(int begin, int end, OrderByComparator obc)
423         throws SystemException {
424         String finderClassName = PasswordPolicy.class.getName();
425         String finderMethodName = "findAll";
426         String[] finderParams = new String[] {
427                 "java.lang.Integer", "java.lang.Integer",
428                 "com.liferay.portal.kernel.util.OrderByComparator"
429             };
430         Object[] finderArgs = new Object[] {
431                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
432             };
433         Object result = FinderCache.getResult(finderClassName,
434                 finderMethodName, finderParams, finderArgs, getSessionFactory());
435 
436         if (result == null) {
437             Session session = null;
438 
439             try {
440                 session = openSession();
441 
442                 StringMaker query = new StringMaker();
443                 query.append("FROM com.liferay.portal.model.PasswordPolicy ");
444 
445                 if (obc != null) {
446                     query.append("ORDER BY ");
447                     query.append(obc.getOrderBy());
448                 }
449 
450                 Query q = session.createQuery(query.toString());
451                 List list = QueryUtil.list(q, getDialect(), begin, end);
452 
453                 if (obc == null) {
454                     Collections.sort(list);
455                 }
456 
457                 FinderCache.putResult(finderClassName, finderMethodName,
458                     finderParams, finderArgs, list);
459 
460                 return list;
461             }
462             catch (Exception e) {
463                 throw HibernateUtil.processException(e);
464             }
465             finally {
466                 closeSession(session);
467             }
468         }
469         else {
470             return (List)result;
471         }
472     }
473 
474     public void removeByC_DP(long companyId, boolean defaultPolicy)
475         throws NoSuchPasswordPolicyException, SystemException {
476         PasswordPolicy passwordPolicy = findByC_DP(companyId, defaultPolicy);
477         remove(passwordPolicy);
478     }
479 
480     public void removeByC_N(long companyId, String name)
481         throws NoSuchPasswordPolicyException, SystemException {
482         PasswordPolicy passwordPolicy = findByC_N(companyId, name);
483         remove(passwordPolicy);
484     }
485 
486     public void removeAll() throws SystemException {
487         Iterator itr = findAll().iterator();
488 
489         while (itr.hasNext()) {
490             remove((PasswordPolicy)itr.next());
491         }
492     }
493 
494     public int countByC_DP(long companyId, boolean defaultPolicy)
495         throws SystemException {
496         String finderClassName = PasswordPolicy.class.getName();
497         String finderMethodName = "countByC_DP";
498         String[] finderParams = new String[] {
499                 Long.class.getName(), Boolean.class.getName()
500             };
501         Object[] finderArgs = new Object[] {
502                 new Long(companyId), Boolean.valueOf(defaultPolicy)
503             };
504         Object result = FinderCache.getResult(finderClassName,
505                 finderMethodName, finderParams, finderArgs, getSessionFactory());
506 
507         if (result == null) {
508             Session session = null;
509 
510             try {
511                 session = openSession();
512 
513                 StringMaker query = new StringMaker();
514                 query.append("SELECT COUNT(*) ");
515                 query.append(
516                     "FROM com.liferay.portal.model.PasswordPolicy WHERE ");
517                 query.append("companyId = ?");
518                 query.append(" AND ");
519                 query.append("defaultPolicy = ?");
520                 query.append(" ");
521 
522                 Query q = session.createQuery(query.toString());
523                 int queryPos = 0;
524                 q.setLong(queryPos++, companyId);
525                 q.setBoolean(queryPos++, defaultPolicy);
526 
527                 Long count = null;
528                 Iterator itr = q.list().iterator();
529 
530                 if (itr.hasNext()) {
531                     count = (Long)itr.next();
532                 }
533 
534                 if (count == null) {
535                     count = new Long(0);
536                 }
537 
538                 FinderCache.putResult(finderClassName, finderMethodName,
539                     finderParams, finderArgs, count);
540 
541                 return count.intValue();
542             }
543             catch (Exception e) {
544                 throw HibernateUtil.processException(e);
545             }
546             finally {
547                 closeSession(session);
548             }
549         }
550         else {
551             return ((Long)result).intValue();
552         }
553     }
554 
555     public int countByC_N(long companyId, String name)
556         throws SystemException {
557         String finderClassName = PasswordPolicy.class.getName();
558         String finderMethodName = "countByC_N";
559         String[] finderParams = new String[] {
560                 Long.class.getName(), String.class.getName()
561             };
562         Object[] finderArgs = new Object[] { new Long(companyId), name };
563         Object result = FinderCache.getResult(finderClassName,
564                 finderMethodName, finderParams, finderArgs, getSessionFactory());
565 
566         if (result == null) {
567             Session session = null;
568 
569             try {
570                 session = openSession();
571 
572                 StringMaker query = new StringMaker();
573                 query.append("SELECT COUNT(*) ");
574                 query.append(
575                     "FROM com.liferay.portal.model.PasswordPolicy WHERE ");
576                 query.append("companyId = ?");
577                 query.append(" AND ");
578 
579                 if (name == null) {
580                     query.append("name IS NULL");
581                 }
582                 else {
583                     query.append("name = ?");
584                 }
585 
586                 query.append(" ");
587 
588                 Query q = session.createQuery(query.toString());
589                 int queryPos = 0;
590                 q.setLong(queryPos++, companyId);
591 
592                 if (name != null) {
593                     q.setString(queryPos++, name);
594                 }
595 
596                 Long count = null;
597                 Iterator itr = q.list().iterator();
598 
599                 if (itr.hasNext()) {
600                     count = (Long)itr.next();
601                 }
602 
603                 if (count == null) {
604                     count = new Long(0);
605                 }
606 
607                 FinderCache.putResult(finderClassName, finderMethodName,
608                     finderParams, finderArgs, count);
609 
610                 return count.intValue();
611             }
612             catch (Exception e) {
613                 throw HibernateUtil.processException(e);
614             }
615             finally {
616                 closeSession(session);
617             }
618         }
619         else {
620             return ((Long)result).intValue();
621         }
622     }
623 
624     public int countAll() throws SystemException {
625         String finderClassName = PasswordPolicy.class.getName();
626         String finderMethodName = "countAll";
627         String[] finderParams = new String[] {  };
628         Object[] finderArgs = new Object[] {  };
629         Object result = FinderCache.getResult(finderClassName,
630                 finderMethodName, finderParams, finderArgs, getSessionFactory());
631 
632         if (result == null) {
633             Session session = null;
634 
635             try {
636                 session = openSession();
637 
638                 StringMaker query = new StringMaker();
639                 query.append("SELECT COUNT(*) ");
640                 query.append("FROM com.liferay.portal.model.PasswordPolicy");
641 
642                 Query q = session.createQuery(query.toString());
643                 Long count = null;
644                 Iterator itr = q.list().iterator();
645 
646                 if (itr.hasNext()) {
647                     count = (Long)itr.next();
648                 }
649 
650                 if (count == null) {
651                     count = new Long(0);
652                 }
653 
654                 FinderCache.putResult(finderClassName, finderMethodName,
655                     finderParams, finderArgs, count);
656 
657                 return count.intValue();
658             }
659             catch (Exception e) {
660                 throw HibernateUtil.processException(e);
661             }
662             finally {
663                 closeSession(session);
664             }
665         }
666         else {
667             return ((Long)result).intValue();
668         }
669     }
670 
671     protected void initDao() {
672     }
673 
674     private static Log _log = LogFactory.getLog(PasswordPolicyPersistenceImpl.class);
675 }