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