1   /**
2    * Copyright (c) 2000-2008 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.NoSuchPasswordTrackerException;
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.GetterUtil;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.ModelListener;
35  import com.liferay.portal.model.PasswordTracker;
36  import com.liferay.portal.model.impl.PasswordTrackerImpl;
37  import com.liferay.portal.model.impl.PasswordTrackerModelImpl;
38  import com.liferay.portal.spring.hibernate.FinderCache;
39  import com.liferay.portal.spring.hibernate.HibernateUtil;
40  import com.liferay.portal.util.PropsUtil;
41  
42  import com.liferay.util.dao.hibernate.QueryUtil;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.hibernate.Query;
48  import org.hibernate.Session;
49  
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="PasswordTrackerPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class PasswordTrackerPersistenceImpl extends BasePersistence
61      implements PasswordTrackerPersistence {
62      public PasswordTracker create(long passwordTrackerId) {
63          PasswordTracker passwordTracker = new PasswordTrackerImpl();
64  
65          passwordTracker.setNew(true);
66          passwordTracker.setPrimaryKey(passwordTrackerId);
67  
68          return passwordTracker;
69      }
70  
71      public PasswordTracker remove(long passwordTrackerId)
72          throws NoSuchPasswordTrackerException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              PasswordTracker passwordTracker = (PasswordTracker)session.get(PasswordTrackerImpl.class,
79                      new Long(passwordTrackerId));
80  
81              if (passwordTracker == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No PasswordTracker exists with the primary key " +
84                          passwordTrackerId);
85                  }
86  
87                  throw new NoSuchPasswordTrackerException(
88                      "No PasswordTracker exists with the primary key " +
89                      passwordTrackerId);
90              }
91  
92              return remove(passwordTracker);
93          }
94          catch (NoSuchPasswordTrackerException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw HibernateUtil.processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public PasswordTracker remove(PasswordTracker passwordTracker)
106         throws SystemException {
107         ModelListener listener = _getListener();
108 
109         if (listener != null) {
110             listener.onBeforeRemove(passwordTracker);
111         }
112 
113         passwordTracker = removeImpl(passwordTracker);
114 
115         if (listener != null) {
116             listener.onAfterRemove(passwordTracker);
117         }
118 
119         return passwordTracker;
120     }
121 
122     protected PasswordTracker removeImpl(PasswordTracker passwordTracker)
123         throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             session.delete(passwordTracker);
130 
131             session.flush();
132 
133             return passwordTracker;
134         }
135         catch (Exception e) {
136             throw HibernateUtil.processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCache.clearCache(PasswordTracker.class.getName());
142         }
143     }
144 
145     public PasswordTracker update(PasswordTracker passwordTracker)
146         throws SystemException {
147         return update(passwordTracker, false);
148     }
149 
150     public PasswordTracker update(PasswordTracker passwordTracker, boolean merge)
151         throws SystemException {
152         ModelListener listener = _getListener();
153 
154         boolean isNew = passwordTracker.isNew();
155 
156         if (listener != null) {
157             if (isNew) {
158                 listener.onBeforeCreate(passwordTracker);
159             }
160             else {
161                 listener.onBeforeUpdate(passwordTracker);
162             }
163         }
164 
165         passwordTracker = updateImpl(passwordTracker, merge);
166 
167         if (listener != null) {
168             if (isNew) {
169                 listener.onAfterCreate(passwordTracker);
170             }
171             else {
172                 listener.onAfterUpdate(passwordTracker);
173             }
174         }
175 
176         return passwordTracker;
177     }
178 
179     public PasswordTracker updateImpl(
180         com.liferay.portal.model.PasswordTracker passwordTracker, boolean merge)
181         throws SystemException {
182         Session session = null;
183 
184         try {
185             session = openSession();
186 
187             if (merge) {
188                 session.merge(passwordTracker);
189             }
190             else {
191                 if (passwordTracker.isNew()) {
192                     session.save(passwordTracker);
193                 }
194             }
195 
196             session.flush();
197 
198             passwordTracker.setNew(false);
199 
200             return passwordTracker;
201         }
202         catch (Exception e) {
203             throw HibernateUtil.processException(e);
204         }
205         finally {
206             closeSession(session);
207 
208             FinderCache.clearCache(PasswordTracker.class.getName());
209         }
210     }
211 
212     public PasswordTracker findByPrimaryKey(long passwordTrackerId)
213         throws NoSuchPasswordTrackerException, SystemException {
214         PasswordTracker passwordTracker = fetchByPrimaryKey(passwordTrackerId);
215 
216         if (passwordTracker == null) {
217             if (_log.isWarnEnabled()) {
218                 _log.warn("No PasswordTracker exists with the primary key " +
219                     passwordTrackerId);
220             }
221 
222             throw new NoSuchPasswordTrackerException(
223                 "No PasswordTracker exists with the primary key " +
224                 passwordTrackerId);
225         }
226 
227         return passwordTracker;
228     }
229 
230     public PasswordTracker fetchByPrimaryKey(long passwordTrackerId)
231         throws SystemException {
232         Session session = null;
233 
234         try {
235             session = openSession();
236 
237             return (PasswordTracker)session.get(PasswordTrackerImpl.class,
238                 new Long(passwordTrackerId));
239         }
240         catch (Exception e) {
241             throw HibernateUtil.processException(e);
242         }
243         finally {
244             closeSession(session);
245         }
246     }
247 
248     public List findByUserId(long userId) throws SystemException {
249         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
250         String finderClassName = PasswordTracker.class.getName();
251         String finderMethodName = "findByUserId";
252         String[] finderParams = new String[] { Long.class.getName() };
253         Object[] finderArgs = new Object[] { new Long(userId) };
254 
255         Object result = null;
256 
257         if (finderClassNameCacheEnabled) {
258             result = FinderCache.getResult(finderClassName, finderMethodName,
259                     finderParams, finderArgs, getSessionFactory());
260         }
261 
262         if (result == null) {
263             Session session = null;
264 
265             try {
266                 session = openSession();
267 
268                 StringMaker query = new StringMaker();
269 
270                 query.append(
271                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
272 
273                 query.append("userId = ?");
274 
275                 query.append(" ");
276 
277                 query.append("ORDER BY ");
278 
279                 query.append("userId DESC, ");
280                 query.append("createDate DESC");
281 
282                 Query q = session.createQuery(query.toString());
283 
284                 int queryPos = 0;
285 
286                 q.setLong(queryPos++, userId);
287 
288                 List list = q.list();
289 
290                 FinderCache.putResult(finderClassNameCacheEnabled,
291                     finderClassName, finderMethodName, finderParams,
292                     finderArgs, list);
293 
294                 return list;
295             }
296             catch (Exception e) {
297                 throw HibernateUtil.processException(e);
298             }
299             finally {
300                 closeSession(session);
301             }
302         }
303         else {
304             return (List)result;
305         }
306     }
307 
308     public List findByUserId(long userId, int begin, int end)
309         throws SystemException {
310         return findByUserId(userId, begin, end, null);
311     }
312 
313     public List findByUserId(long userId, int begin, int end,
314         OrderByComparator obc) throws SystemException {
315         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
316         String finderClassName = PasswordTracker.class.getName();
317         String finderMethodName = "findByUserId";
318         String[] finderParams = new String[] {
319                 Long.class.getName(),
320                 
321                 "java.lang.Integer", "java.lang.Integer",
322                 "com.liferay.portal.kernel.util.OrderByComparator"
323             };
324         Object[] finderArgs = new Object[] {
325                 new Long(userId),
326                 
327                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
328             };
329 
330         Object result = null;
331 
332         if (finderClassNameCacheEnabled) {
333             result = FinderCache.getResult(finderClassName, finderMethodName,
334                     finderParams, finderArgs, getSessionFactory());
335         }
336 
337         if (result == null) {
338             Session session = null;
339 
340             try {
341                 session = openSession();
342 
343                 StringMaker query = new StringMaker();
344 
345                 query.append(
346                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
347 
348                 query.append("userId = ?");
349 
350                 query.append(" ");
351 
352                 if (obc != null) {
353                     query.append("ORDER BY ");
354                     query.append(obc.getOrderBy());
355                 }
356 
357                 else {
358                     query.append("ORDER BY ");
359 
360                     query.append("userId DESC, ");
361                     query.append("createDate DESC");
362                 }
363 
364                 Query q = session.createQuery(query.toString());
365 
366                 int queryPos = 0;
367 
368                 q.setLong(queryPos++, userId);
369 
370                 List list = QueryUtil.list(q, getDialect(), begin, end);
371 
372                 FinderCache.putResult(finderClassNameCacheEnabled,
373                     finderClassName, finderMethodName, finderParams,
374                     finderArgs, list);
375 
376                 return list;
377             }
378             catch (Exception e) {
379                 throw HibernateUtil.processException(e);
380             }
381             finally {
382                 closeSession(session);
383             }
384         }
385         else {
386             return (List)result;
387         }
388     }
389 
390     public PasswordTracker findByUserId_First(long userId, OrderByComparator obc)
391         throws NoSuchPasswordTrackerException, SystemException {
392         List list = findByUserId(userId, 0, 1, obc);
393 
394         if (list.size() == 0) {
395             StringMaker msg = new StringMaker();
396 
397             msg.append("No PasswordTracker exists with the key {");
398 
399             msg.append("userId=" + userId);
400 
401             msg.append(StringPool.CLOSE_CURLY_BRACE);
402 
403             throw new NoSuchPasswordTrackerException(msg.toString());
404         }
405         else {
406             return (PasswordTracker)list.get(0);
407         }
408     }
409 
410     public PasswordTracker findByUserId_Last(long userId, OrderByComparator obc)
411         throws NoSuchPasswordTrackerException, SystemException {
412         int count = countByUserId(userId);
413 
414         List list = findByUserId(userId, count - 1, count, obc);
415 
416         if (list.size() == 0) {
417             StringMaker msg = new StringMaker();
418 
419             msg.append("No PasswordTracker exists with the key {");
420 
421             msg.append("userId=" + userId);
422 
423             msg.append(StringPool.CLOSE_CURLY_BRACE);
424 
425             throw new NoSuchPasswordTrackerException(msg.toString());
426         }
427         else {
428             return (PasswordTracker)list.get(0);
429         }
430     }
431 
432     public PasswordTracker[] findByUserId_PrevAndNext(long passwordTrackerId,
433         long userId, OrderByComparator obc)
434         throws NoSuchPasswordTrackerException, SystemException {
435         PasswordTracker passwordTracker = findByPrimaryKey(passwordTrackerId);
436 
437         int count = countByUserId(userId);
438 
439         Session session = null;
440 
441         try {
442             session = openSession();
443 
444             StringMaker query = new StringMaker();
445 
446             query.append("FROM com.liferay.portal.model.PasswordTracker WHERE ");
447 
448             query.append("userId = ?");
449 
450             query.append(" ");
451 
452             if (obc != null) {
453                 query.append("ORDER BY ");
454                 query.append(obc.getOrderBy());
455             }
456 
457             else {
458                 query.append("ORDER BY ");
459 
460                 query.append("userId DESC, ");
461                 query.append("createDate DESC");
462             }
463 
464             Query q = session.createQuery(query.toString());
465 
466             int queryPos = 0;
467 
468             q.setLong(queryPos++, userId);
469 
470             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
471                     passwordTracker);
472 
473             PasswordTracker[] array = new PasswordTrackerImpl[3];
474 
475             array[0] = (PasswordTracker)objArray[0];
476             array[1] = (PasswordTracker)objArray[1];
477             array[2] = (PasswordTracker)objArray[2];
478 
479             return array;
480         }
481         catch (Exception e) {
482             throw HibernateUtil.processException(e);
483         }
484         finally {
485             closeSession(session);
486         }
487     }
488 
489     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
490         throws SystemException {
491         Session session = null;
492 
493         try {
494             session = openSession();
495 
496             DynamicQuery query = queryInitializer.initialize(session);
497 
498             return query.list();
499         }
500         catch (Exception e) {
501             throw HibernateUtil.processException(e);
502         }
503         finally {
504             closeSession(session);
505         }
506     }
507 
508     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
509         int begin, int end) throws SystemException {
510         Session session = null;
511 
512         try {
513             session = openSession();
514 
515             DynamicQuery query = queryInitializer.initialize(session);
516 
517             query.setLimit(begin, end);
518 
519             return query.list();
520         }
521         catch (Exception e) {
522             throw HibernateUtil.processException(e);
523         }
524         finally {
525             closeSession(session);
526         }
527     }
528 
529     public List findAll() throws SystemException {
530         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
531     }
532 
533     public List findAll(int begin, int end) throws SystemException {
534         return findAll(begin, end, null);
535     }
536 
537     public List findAll(int begin, int end, OrderByComparator obc)
538         throws SystemException {
539         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
540         String finderClassName = PasswordTracker.class.getName();
541         String finderMethodName = "findAll";
542         String[] finderParams = new String[] {
543                 "java.lang.Integer", "java.lang.Integer",
544                 "com.liferay.portal.kernel.util.OrderByComparator"
545             };
546         Object[] finderArgs = new Object[] {
547                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
548             };
549 
550         Object result = null;
551 
552         if (finderClassNameCacheEnabled) {
553             result = FinderCache.getResult(finderClassName, finderMethodName,
554                     finderParams, finderArgs, getSessionFactory());
555         }
556 
557         if (result == null) {
558             Session session = null;
559 
560             try {
561                 session = openSession();
562 
563                 StringMaker query = new StringMaker();
564 
565                 query.append("FROM com.liferay.portal.model.PasswordTracker ");
566 
567                 if (obc != null) {
568                     query.append("ORDER BY ");
569                     query.append(obc.getOrderBy());
570                 }
571 
572                 else {
573                     query.append("ORDER BY ");
574 
575                     query.append("userId DESC, ");
576                     query.append("createDate DESC");
577                 }
578 
579                 Query q = session.createQuery(query.toString());
580 
581                 List list = QueryUtil.list(q, getDialect(), begin, end);
582 
583                 if (obc == null) {
584                     Collections.sort(list);
585                 }
586 
587                 FinderCache.putResult(finderClassNameCacheEnabled,
588                     finderClassName, finderMethodName, finderParams,
589                     finderArgs, list);
590 
591                 return list;
592             }
593             catch (Exception e) {
594                 throw HibernateUtil.processException(e);
595             }
596             finally {
597                 closeSession(session);
598             }
599         }
600         else {
601             return (List)result;
602         }
603     }
604 
605     public void removeByUserId(long userId) throws SystemException {
606         Iterator itr = findByUserId(userId).iterator();
607 
608         while (itr.hasNext()) {
609             PasswordTracker passwordTracker = (PasswordTracker)itr.next();
610 
611             remove(passwordTracker);
612         }
613     }
614 
615     public void removeAll() throws SystemException {
616         Iterator itr = findAll().iterator();
617 
618         while (itr.hasNext()) {
619             remove((PasswordTracker)itr.next());
620         }
621     }
622 
623     public int countByUserId(long userId) throws SystemException {
624         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
625         String finderClassName = PasswordTracker.class.getName();
626         String finderMethodName = "countByUserId";
627         String[] finderParams = new String[] { Long.class.getName() };
628         Object[] finderArgs = new Object[] { new Long(userId) };
629 
630         Object result = null;
631 
632         if (finderClassNameCacheEnabled) {
633             result = FinderCache.getResult(finderClassName, finderMethodName,
634                     finderParams, finderArgs, getSessionFactory());
635         }
636 
637         if (result == null) {
638             Session session = null;
639 
640             try {
641                 session = openSession();
642 
643                 StringMaker query = new StringMaker();
644 
645                 query.append("SELECT COUNT(*) ");
646                 query.append(
647                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
648 
649                 query.append("userId = ?");
650 
651                 query.append(" ");
652 
653                 Query q = session.createQuery(query.toString());
654 
655                 int queryPos = 0;
656 
657                 q.setLong(queryPos++, userId);
658 
659                 Long count = null;
660 
661                 Iterator itr = q.list().iterator();
662 
663                 if (itr.hasNext()) {
664                     count = (Long)itr.next();
665                 }
666 
667                 if (count == null) {
668                     count = new Long(0);
669                 }
670 
671                 FinderCache.putResult(finderClassNameCacheEnabled,
672                     finderClassName, finderMethodName, finderParams,
673                     finderArgs, count);
674 
675                 return count.intValue();
676             }
677             catch (Exception e) {
678                 throw HibernateUtil.processException(e);
679             }
680             finally {
681                 closeSession(session);
682             }
683         }
684         else {
685             return ((Long)result).intValue();
686         }
687     }
688 
689     public int countAll() throws SystemException {
690         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
691         String finderClassName = PasswordTracker.class.getName();
692         String finderMethodName = "countAll";
693         String[] finderParams = new String[] {  };
694         Object[] finderArgs = new Object[] {  };
695 
696         Object result = null;
697 
698         if (finderClassNameCacheEnabled) {
699             result = FinderCache.getResult(finderClassName, finderMethodName,
700                     finderParams, finderArgs, getSessionFactory());
701         }
702 
703         if (result == null) {
704             Session session = null;
705 
706             try {
707                 session = openSession();
708 
709                 Query q = session.createQuery(
710                         "SELECT COUNT(*) FROM com.liferay.portal.model.PasswordTracker");
711 
712                 Long count = null;
713 
714                 Iterator itr = q.list().iterator();
715 
716                 if (itr.hasNext()) {
717                     count = (Long)itr.next();
718                 }
719 
720                 if (count == null) {
721                     count = new Long(0);
722                 }
723 
724                 FinderCache.putResult(finderClassNameCacheEnabled,
725                     finderClassName, finderMethodName, finderParams,
726                     finderArgs, count);
727 
728                 return count.intValue();
729             }
730             catch (Exception e) {
731                 throw HibernateUtil.processException(e);
732             }
733             finally {
734                 closeSession(session);
735             }
736         }
737         else {
738             return ((Long)result).intValue();
739         }
740     }
741 
742     protected void initDao() {
743     }
744 
745     private static ModelListener _getListener() {
746         if (Validator.isNotNull(_LISTENER)) {
747             try {
748                 return (ModelListener)Class.forName(_LISTENER).newInstance();
749             }
750             catch (Exception e) {
751                 _log.error(e);
752             }
753         }
754 
755         return null;
756     }
757 
758     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
759                 "value.object.listener.com.liferay.portal.model.PasswordTracker"));
760     private static Log _log = LogFactory.getLog(PasswordTrackerPersistenceImpl.class);
761 }