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