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             session.delete(passwordTracker);
130 
131             session.flush();
132 
133             return passwordTracker;
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCacheUtil.clearCache(PasswordTracker.class.getName());
142         }
143     }
144 
145     /**
146      * @deprecated Use <code>update(PasswordTracker passwordTracker, boolean merge)</code>.
147      */
148     public PasswordTracker update(PasswordTracker passwordTracker)
149         throws SystemException {
150         if (_log.isWarnEnabled()) {
151             _log.warn(
152                 "Using the deprecated update(PasswordTracker passwordTracker) method. Use update(PasswordTracker passwordTracker, boolean merge) instead.");
153         }
154 
155         return update(passwordTracker, false);
156     }
157 
158     /**
159      * Add, update, or merge, the entity. This method also calls the model
160      * listeners to trigger the proper events associated with adding, deleting,
161      * or updating an entity.
162      *
163      * @param        passwordTracker the entity to add, update, or merge
164      * @param        merge boolean value for whether to merge the entity. The
165      *                default value is false. Setting merge to true is more
166      *                expensive and should only be true when passwordTracker is
167      *                transient. See LEP-5473 for a detailed discussion of this
168      *                method.
169      * @return        true if the portlet can be displayed via Ajax
170      */
171     public PasswordTracker update(PasswordTracker passwordTracker, boolean merge)
172         throws SystemException {
173         boolean isNew = passwordTracker.isNew();
174 
175         if (_listeners.length > 0) {
176             for (ModelListener listener : _listeners) {
177                 if (isNew) {
178                     listener.onBeforeCreate(passwordTracker);
179                 }
180                 else {
181                     listener.onBeforeUpdate(passwordTracker);
182                 }
183             }
184         }
185 
186         passwordTracker = updateImpl(passwordTracker, merge);
187 
188         if (_listeners.length > 0) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onAfterCreate(passwordTracker);
192                 }
193                 else {
194                     listener.onAfterUpdate(passwordTracker);
195                 }
196             }
197         }
198 
199         return passwordTracker;
200     }
201 
202     public PasswordTracker updateImpl(
203         com.liferay.portal.model.PasswordTracker passwordTracker, boolean merge)
204         throws SystemException {
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             if (merge) {
211                 session.merge(passwordTracker);
212             }
213             else {
214                 if (passwordTracker.isNew()) {
215                     session.save(passwordTracker);
216                 }
217             }
218 
219             session.flush();
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 = (List<PasswordTracker>)QueryUtil.list(q,
608                         getDialect(), start, end);
609 
610                 if (obc == null) {
611                     Collections.sort(list);
612                 }
613 
614                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
615                     finderClassName, finderMethodName, finderParams,
616                     finderArgs, list);
617 
618                 return list;
619             }
620             catch (Exception e) {
621                 throw processException(e);
622             }
623             finally {
624                 closeSession(session);
625             }
626         }
627         else {
628             return (List<PasswordTracker>)result;
629         }
630     }
631 
632     public void removeByUserId(long userId) throws SystemException {
633         for (PasswordTracker passwordTracker : findByUserId(userId)) {
634             remove(passwordTracker);
635         }
636     }
637 
638     public void removeAll() throws SystemException {
639         for (PasswordTracker passwordTracker : findAll()) {
640             remove(passwordTracker);
641         }
642     }
643 
644     public int countByUserId(long userId) throws SystemException {
645         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
646         String finderClassName = PasswordTracker.class.getName();
647         String finderMethodName = "countByUserId";
648         String[] finderParams = new String[] { Long.class.getName() };
649         Object[] finderArgs = new Object[] { new Long(userId) };
650 
651         Object result = null;
652 
653         if (finderClassNameCacheEnabled) {
654             result = FinderCacheUtil.getResult(finderClassName,
655                     finderMethodName, finderParams, finderArgs, this);
656         }
657 
658         if (result == null) {
659             Session session = null;
660 
661             try {
662                 session = openSession();
663 
664                 StringBuilder query = new StringBuilder();
665 
666                 query.append("SELECT COUNT(*) ");
667                 query.append(
668                     "FROM com.liferay.portal.model.PasswordTracker WHERE ");
669 
670                 query.append("userId = ?");
671 
672                 query.append(" ");
673 
674                 Query q = session.createQuery(query.toString());
675 
676                 QueryPos qPos = QueryPos.getInstance(q);
677 
678                 qPos.add(userId);
679 
680                 Long count = null;
681 
682                 Iterator<Long> itr = q.list().iterator();
683 
684                 if (itr.hasNext()) {
685                     count = itr.next();
686                 }
687 
688                 if (count == null) {
689                     count = new Long(0);
690                 }
691 
692                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
693                     finderClassName, finderMethodName, finderParams,
694                     finderArgs, count);
695 
696                 return count.intValue();
697             }
698             catch (Exception e) {
699                 throw processException(e);
700             }
701             finally {
702                 closeSession(session);
703             }
704         }
705         else {
706             return ((Long)result).intValue();
707         }
708     }
709 
710     public int countAll() throws SystemException {
711         boolean finderClassNameCacheEnabled = PasswordTrackerModelImpl.CACHE_ENABLED;
712         String finderClassName = PasswordTracker.class.getName();
713         String finderMethodName = "countAll";
714         String[] finderParams = new String[] {  };
715         Object[] finderArgs = new Object[] {  };
716 
717         Object result = null;
718 
719         if (finderClassNameCacheEnabled) {
720             result = FinderCacheUtil.getResult(finderClassName,
721                     finderMethodName, finderParams, finderArgs, this);
722         }
723 
724         if (result == null) {
725             Session session = null;
726 
727             try {
728                 session = openSession();
729 
730                 Query q = session.createQuery(
731                         "SELECT COUNT(*) FROM com.liferay.portal.model.PasswordTracker");
732 
733                 Long count = null;
734 
735                 Iterator<Long> itr = q.list().iterator();
736 
737                 if (itr.hasNext()) {
738                     count = itr.next();
739                 }
740 
741                 if (count == null) {
742                     count = new Long(0);
743                 }
744 
745                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
746                     finderClassName, finderMethodName, finderParams,
747                     finderArgs, count);
748 
749                 return count.intValue();
750             }
751             catch (Exception e) {
752                 throw processException(e);
753             }
754             finally {
755                 closeSession(session);
756             }
757         }
758         else {
759             return ((Long)result).intValue();
760         }
761     }
762 
763     public void registerListener(ModelListener listener) {
764         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
765 
766         listeners.add(listener);
767 
768         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
769     }
770 
771     public void unregisterListener(ModelListener listener) {
772         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
773 
774         listeners.remove(listener);
775 
776         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
777     }
778 
779     public void afterPropertiesSet() {
780         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
781                     com.liferay.portal.util.PropsUtil.get(
782                         "value.object.listener.com.liferay.portal.model.PasswordTracker")));
783 
784         if (listenerClassNames.length > 0) {
785             try {
786                 List<ModelListener> listeners = new ArrayList<ModelListener>();
787 
788                 for (String listenerClassName : listenerClassNames) {
789                     listeners.add((ModelListener)Class.forName(
790                             listenerClassName).newInstance());
791                 }
792 
793                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
794             }
795             catch (Exception e) {
796                 _log.error(e);
797             }
798         }
799     }
800 
801     private static Log _log = LogFactory.getLog(PasswordTrackerPersistenceImpl.class);
802     private ModelListener[] _listeners = new ModelListener[0];
803 }