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