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.NoSuchUserTrackerException;
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.UserTracker;
36  import com.liferay.portal.model.impl.UserTrackerImpl;
37  import com.liferay.portal.model.impl.UserTrackerModelImpl;
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="UserTrackerPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class UserTrackerPersistenceImpl extends BasePersistence
62      implements UserTrackerPersistence {
63      public UserTracker create(long userTrackerId) {
64          UserTracker userTracker = new UserTrackerImpl();
65  
66          userTracker.setNew(true);
67          userTracker.setPrimaryKey(userTrackerId);
68  
69          return userTracker;
70      }
71  
72      public UserTracker remove(long userTrackerId)
73          throws NoSuchUserTrackerException, SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              UserTracker userTracker = (UserTracker)session.get(UserTrackerImpl.class,
80                      new Long(userTrackerId));
81  
82              if (userTracker == null) {
83                  if (_log.isWarnEnabled()) {
84                      _log.warn("No UserTracker exists with the primary key " +
85                          userTrackerId);
86                  }
87  
88                  throw new NoSuchUserTrackerException(
89                      "No UserTracker exists with the primary key " +
90                      userTrackerId);
91              }
92  
93              return remove(userTracker);
94          }
95          catch (NoSuchUserTrackerException 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 UserTracker remove(UserTracker userTracker)
107         throws SystemException {
108         if (_listeners != null) {
109             for (ModelListener listener : _listeners) {
110                 listener.onBeforeRemove(userTracker);
111             }
112         }
113 
114         userTracker = removeImpl(userTracker);
115 
116         if (_listeners != null) {
117             for (ModelListener listener : _listeners) {
118                 listener.onAfterRemove(userTracker);
119             }
120         }
121 
122         return userTracker;
123     }
124 
125     protected UserTracker removeImpl(UserTracker userTracker)
126         throws SystemException {
127         Session session = null;
128 
129         try {
130             session = openSession();
131 
132             session.delete(userTracker);
133 
134             session.flush();
135 
136             return userTracker;
137         }
138         catch (Exception e) {
139             throw HibernateUtil.processException(e);
140         }
141         finally {
142             closeSession(session);
143 
144             FinderCache.clearCache(UserTracker.class.getName());
145         }
146     }
147 
148     /**
149      * @deprecated Use <code>update(UserTracker userTracker, boolean merge)</code>.
150      */
151     public UserTracker update(UserTracker userTracker)
152         throws SystemException {
153         if (_log.isWarnEnabled()) {
154             _log.warn(
155                 "Using the deprecated update(UserTracker userTracker) method. Use update(UserTracker userTracker, boolean merge) instead.");
156         }
157 
158         return update(userTracker, 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        userTracker 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 userTracker 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 UserTracker update(UserTracker userTracker, boolean merge)
175         throws SystemException {
176         boolean isNew = userTracker.isNew();
177 
178         if (_listeners != null) {
179             for (ModelListener listener : _listeners) {
180                 if (isNew) {
181                     listener.onBeforeCreate(userTracker);
182                 }
183                 else {
184                     listener.onBeforeUpdate(userTracker);
185                 }
186             }
187         }
188 
189         userTracker = updateImpl(userTracker, merge);
190 
191         if (_listeners != null) {
192             for (ModelListener listener : _listeners) {
193                 if (isNew) {
194                     listener.onAfterCreate(userTracker);
195                 }
196                 else {
197                     listener.onAfterUpdate(userTracker);
198                 }
199             }
200         }
201 
202         return userTracker;
203     }
204 
205     public UserTracker updateImpl(
206         com.liferay.portal.model.UserTracker userTracker, boolean merge)
207         throws SystemException {
208         Session session = null;
209 
210         try {
211             session = openSession();
212 
213             if (merge) {
214                 session.merge(userTracker);
215             }
216             else {
217                 if (userTracker.isNew()) {
218                     session.save(userTracker);
219                 }
220             }
221 
222             session.flush();
223 
224             userTracker.setNew(false);
225 
226             return userTracker;
227         }
228         catch (Exception e) {
229             throw HibernateUtil.processException(e);
230         }
231         finally {
232             closeSession(session);
233 
234             FinderCache.clearCache(UserTracker.class.getName());
235         }
236     }
237 
238     public UserTracker findByPrimaryKey(long userTrackerId)
239         throws NoSuchUserTrackerException, SystemException {
240         UserTracker userTracker = fetchByPrimaryKey(userTrackerId);
241 
242         if (userTracker == null) {
243             if (_log.isWarnEnabled()) {
244                 _log.warn("No UserTracker exists with the primary key " +
245                     userTrackerId);
246             }
247 
248             throw new NoSuchUserTrackerException(
249                 "No UserTracker exists with the primary key " + userTrackerId);
250         }
251 
252         return userTracker;
253     }
254 
255     public UserTracker fetchByPrimaryKey(long userTrackerId)
256         throws SystemException {
257         Session session = null;
258 
259         try {
260             session = openSession();
261 
262             return (UserTracker)session.get(UserTrackerImpl.class,
263                 new Long(userTrackerId));
264         }
265         catch (Exception e) {
266             throw HibernateUtil.processException(e);
267         }
268         finally {
269             closeSession(session);
270         }
271     }
272 
273     public List<UserTracker> findByCompanyId(long companyId)
274         throws SystemException {
275         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
276         String finderClassName = UserTracker.class.getName();
277         String finderMethodName = "findByCompanyId";
278         String[] finderParams = new String[] { Long.class.getName() };
279         Object[] finderArgs = new Object[] { new Long(companyId) };
280 
281         Object result = null;
282 
283         if (finderClassNameCacheEnabled) {
284             result = FinderCache.getResult(finderClassName, finderMethodName,
285                     finderParams, finderArgs, getSessionFactory());
286         }
287 
288         if (result == null) {
289             Session session = null;
290 
291             try {
292                 session = openSession();
293 
294                 StringMaker query = new StringMaker();
295 
296                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
297 
298                 query.append("companyId = ?");
299 
300                 query.append(" ");
301 
302                 Query q = session.createQuery(query.toString());
303 
304                 int queryPos = 0;
305 
306                 q.setLong(queryPos++, companyId);
307 
308                 List<UserTracker> list = q.list();
309 
310                 FinderCache.putResult(finderClassNameCacheEnabled,
311                     finderClassName, finderMethodName, finderParams,
312                     finderArgs, list);
313 
314                 return list;
315             }
316             catch (Exception e) {
317                 throw HibernateUtil.processException(e);
318             }
319             finally {
320                 closeSession(session);
321             }
322         }
323         else {
324             return (List<UserTracker>)result;
325         }
326     }
327 
328     public List<UserTracker> findByCompanyId(long companyId, int begin, int end)
329         throws SystemException {
330         return findByCompanyId(companyId, begin, end, null);
331     }
332 
333     public List<UserTracker> findByCompanyId(long companyId, int begin,
334         int end, OrderByComparator obc) throws SystemException {
335         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
336         String finderClassName = UserTracker.class.getName();
337         String finderMethodName = "findByCompanyId";
338         String[] finderParams = new String[] {
339                 Long.class.getName(),
340                 
341                 "java.lang.Integer", "java.lang.Integer",
342                 "com.liferay.portal.kernel.util.OrderByComparator"
343             };
344         Object[] finderArgs = new Object[] {
345                 new Long(companyId),
346                 
347                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
348             };
349 
350         Object result = null;
351 
352         if (finderClassNameCacheEnabled) {
353             result = FinderCache.getResult(finderClassName, finderMethodName,
354                     finderParams, finderArgs, getSessionFactory());
355         }
356 
357         if (result == null) {
358             Session session = null;
359 
360             try {
361                 session = openSession();
362 
363                 StringMaker query = new StringMaker();
364 
365                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
366 
367                 query.append("companyId = ?");
368 
369                 query.append(" ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373                     query.append(obc.getOrderBy());
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 int queryPos = 0;
379 
380                 q.setLong(queryPos++, companyId);
381 
382                 List<UserTracker> list = (List<UserTracker>)QueryUtil.list(q,
383                         getDialect(), begin, end);
384 
385                 FinderCache.putResult(finderClassNameCacheEnabled,
386                     finderClassName, finderMethodName, finderParams,
387                     finderArgs, list);
388 
389                 return list;
390             }
391             catch (Exception e) {
392                 throw HibernateUtil.processException(e);
393             }
394             finally {
395                 closeSession(session);
396             }
397         }
398         else {
399             return (List<UserTracker>)result;
400         }
401     }
402 
403     public UserTracker findByCompanyId_First(long companyId,
404         OrderByComparator obc)
405         throws NoSuchUserTrackerException, SystemException {
406         List<UserTracker> list = findByCompanyId(companyId, 0, 1, obc);
407 
408         if (list.size() == 0) {
409             StringMaker msg = new StringMaker();
410 
411             msg.append("No UserTracker exists with the key {");
412 
413             msg.append("companyId=" + companyId);
414 
415             msg.append(StringPool.CLOSE_CURLY_BRACE);
416 
417             throw new NoSuchUserTrackerException(msg.toString());
418         }
419         else {
420             return list.get(0);
421         }
422     }
423 
424     public UserTracker findByCompanyId_Last(long companyId,
425         OrderByComparator obc)
426         throws NoSuchUserTrackerException, SystemException {
427         int count = countByCompanyId(companyId);
428 
429         List<UserTracker> list = findByCompanyId(companyId, count - 1, count,
430                 obc);
431 
432         if (list.size() == 0) {
433             StringMaker msg = new StringMaker();
434 
435             msg.append("No UserTracker exists with the key {");
436 
437             msg.append("companyId=" + companyId);
438 
439             msg.append(StringPool.CLOSE_CURLY_BRACE);
440 
441             throw new NoSuchUserTrackerException(msg.toString());
442         }
443         else {
444             return list.get(0);
445         }
446     }
447 
448     public UserTracker[] findByCompanyId_PrevAndNext(long userTrackerId,
449         long companyId, OrderByComparator obc)
450         throws NoSuchUserTrackerException, SystemException {
451         UserTracker userTracker = findByPrimaryKey(userTrackerId);
452 
453         int count = countByCompanyId(companyId);
454 
455         Session session = null;
456 
457         try {
458             session = openSession();
459 
460             StringMaker query = new StringMaker();
461 
462             query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
463 
464             query.append("companyId = ?");
465 
466             query.append(" ");
467 
468             if (obc != null) {
469                 query.append("ORDER BY ");
470                 query.append(obc.getOrderBy());
471             }
472 
473             Query q = session.createQuery(query.toString());
474 
475             int queryPos = 0;
476 
477             q.setLong(queryPos++, companyId);
478 
479             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
480                     userTracker);
481 
482             UserTracker[] array = new UserTrackerImpl[3];
483 
484             array[0] = (UserTracker)objArray[0];
485             array[1] = (UserTracker)objArray[1];
486             array[2] = (UserTracker)objArray[2];
487 
488             return array;
489         }
490         catch (Exception e) {
491             throw HibernateUtil.processException(e);
492         }
493         finally {
494             closeSession(session);
495         }
496     }
497 
498     public List<UserTracker> findByUserId(long userId)
499         throws SystemException {
500         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
501         String finderClassName = UserTracker.class.getName();
502         String finderMethodName = "findByUserId";
503         String[] finderParams = new String[] { Long.class.getName() };
504         Object[] finderArgs = new Object[] { new Long(userId) };
505 
506         Object result = null;
507 
508         if (finderClassNameCacheEnabled) {
509             result = FinderCache.getResult(finderClassName, finderMethodName,
510                     finderParams, finderArgs, getSessionFactory());
511         }
512 
513         if (result == null) {
514             Session session = null;
515 
516             try {
517                 session = openSession();
518 
519                 StringMaker query = new StringMaker();
520 
521                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
522 
523                 query.append("userId = ?");
524 
525                 query.append(" ");
526 
527                 Query q = session.createQuery(query.toString());
528 
529                 int queryPos = 0;
530 
531                 q.setLong(queryPos++, userId);
532 
533                 List<UserTracker> list = q.list();
534 
535                 FinderCache.putResult(finderClassNameCacheEnabled,
536                     finderClassName, finderMethodName, finderParams,
537                     finderArgs, list);
538 
539                 return list;
540             }
541             catch (Exception e) {
542                 throw HibernateUtil.processException(e);
543             }
544             finally {
545                 closeSession(session);
546             }
547         }
548         else {
549             return (List<UserTracker>)result;
550         }
551     }
552 
553     public List<UserTracker> findByUserId(long userId, int begin, int end)
554         throws SystemException {
555         return findByUserId(userId, begin, end, null);
556     }
557 
558     public List<UserTracker> findByUserId(long userId, int begin, int end,
559         OrderByComparator obc) throws SystemException {
560         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
561         String finderClassName = UserTracker.class.getName();
562         String finderMethodName = "findByUserId";
563         String[] finderParams = new String[] {
564                 Long.class.getName(),
565                 
566                 "java.lang.Integer", "java.lang.Integer",
567                 "com.liferay.portal.kernel.util.OrderByComparator"
568             };
569         Object[] finderArgs = new Object[] {
570                 new Long(userId),
571                 
572                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
573             };
574 
575         Object result = null;
576 
577         if (finderClassNameCacheEnabled) {
578             result = FinderCache.getResult(finderClassName, finderMethodName,
579                     finderParams, finderArgs, getSessionFactory());
580         }
581 
582         if (result == null) {
583             Session session = null;
584 
585             try {
586                 session = openSession();
587 
588                 StringMaker query = new StringMaker();
589 
590                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
591 
592                 query.append("userId = ?");
593 
594                 query.append(" ");
595 
596                 if (obc != null) {
597                     query.append("ORDER BY ");
598                     query.append(obc.getOrderBy());
599                 }
600 
601                 Query q = session.createQuery(query.toString());
602 
603                 int queryPos = 0;
604 
605                 q.setLong(queryPos++, userId);
606 
607                 List<UserTracker> list = (List<UserTracker>)QueryUtil.list(q,
608                         getDialect(), begin, end);
609 
610                 FinderCache.putResult(finderClassNameCacheEnabled,
611                     finderClassName, finderMethodName, finderParams,
612                     finderArgs, list);
613 
614                 return list;
615             }
616             catch (Exception e) {
617                 throw HibernateUtil.processException(e);
618             }
619             finally {
620                 closeSession(session);
621             }
622         }
623         else {
624             return (List<UserTracker>)result;
625         }
626     }
627 
628     public UserTracker findByUserId_First(long userId, OrderByComparator obc)
629         throws NoSuchUserTrackerException, SystemException {
630         List<UserTracker> list = findByUserId(userId, 0, 1, obc);
631 
632         if (list.size() == 0) {
633             StringMaker msg = new StringMaker();
634 
635             msg.append("No UserTracker exists with the key {");
636 
637             msg.append("userId=" + userId);
638 
639             msg.append(StringPool.CLOSE_CURLY_BRACE);
640 
641             throw new NoSuchUserTrackerException(msg.toString());
642         }
643         else {
644             return list.get(0);
645         }
646     }
647 
648     public UserTracker findByUserId_Last(long userId, OrderByComparator obc)
649         throws NoSuchUserTrackerException, SystemException {
650         int count = countByUserId(userId);
651 
652         List<UserTracker> list = findByUserId(userId, count - 1, count, obc);
653 
654         if (list.size() == 0) {
655             StringMaker msg = new StringMaker();
656 
657             msg.append("No UserTracker exists with the key {");
658 
659             msg.append("userId=" + userId);
660 
661             msg.append(StringPool.CLOSE_CURLY_BRACE);
662 
663             throw new NoSuchUserTrackerException(msg.toString());
664         }
665         else {
666             return list.get(0);
667         }
668     }
669 
670     public UserTracker[] findByUserId_PrevAndNext(long userTrackerId,
671         long userId, OrderByComparator obc)
672         throws NoSuchUserTrackerException, SystemException {
673         UserTracker userTracker = findByPrimaryKey(userTrackerId);
674 
675         int count = countByUserId(userId);
676 
677         Session session = null;
678 
679         try {
680             session = openSession();
681 
682             StringMaker query = new StringMaker();
683 
684             query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
685 
686             query.append("userId = ?");
687 
688             query.append(" ");
689 
690             if (obc != null) {
691                 query.append("ORDER BY ");
692                 query.append(obc.getOrderBy());
693             }
694 
695             Query q = session.createQuery(query.toString());
696 
697             int queryPos = 0;
698 
699             q.setLong(queryPos++, userId);
700 
701             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
702                     userTracker);
703 
704             UserTracker[] array = new UserTrackerImpl[3];
705 
706             array[0] = (UserTracker)objArray[0];
707             array[1] = (UserTracker)objArray[1];
708             array[2] = (UserTracker)objArray[2];
709 
710             return array;
711         }
712         catch (Exception e) {
713             throw HibernateUtil.processException(e);
714         }
715         finally {
716             closeSession(session);
717         }
718     }
719 
720     public List<UserTracker> findBySessionId(String sessionId)
721         throws SystemException {
722         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
723         String finderClassName = UserTracker.class.getName();
724         String finderMethodName = "findBySessionId";
725         String[] finderParams = new String[] { String.class.getName() };
726         Object[] finderArgs = new Object[] { sessionId };
727 
728         Object result = null;
729 
730         if (finderClassNameCacheEnabled) {
731             result = FinderCache.getResult(finderClassName, finderMethodName,
732                     finderParams, finderArgs, getSessionFactory());
733         }
734 
735         if (result == null) {
736             Session session = null;
737 
738             try {
739                 session = openSession();
740 
741                 StringMaker query = new StringMaker();
742 
743                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
744 
745                 if (sessionId == null) {
746                     query.append("sessionId IS NULL");
747                 }
748                 else {
749                     query.append("sessionId = ?");
750                 }
751 
752                 query.append(" ");
753 
754                 Query q = session.createQuery(query.toString());
755 
756                 int queryPos = 0;
757 
758                 if (sessionId != null) {
759                     q.setString(queryPos++, sessionId);
760                 }
761 
762                 List<UserTracker> list = q.list();
763 
764                 FinderCache.putResult(finderClassNameCacheEnabled,
765                     finderClassName, finderMethodName, finderParams,
766                     finderArgs, list);
767 
768                 return list;
769             }
770             catch (Exception e) {
771                 throw HibernateUtil.processException(e);
772             }
773             finally {
774                 closeSession(session);
775             }
776         }
777         else {
778             return (List<UserTracker>)result;
779         }
780     }
781 
782     public List<UserTracker> findBySessionId(String sessionId, int begin,
783         int end) throws SystemException {
784         return findBySessionId(sessionId, begin, end, null);
785     }
786 
787     public List<UserTracker> findBySessionId(String sessionId, int begin,
788         int end, OrderByComparator obc) throws SystemException {
789         boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
790         String finderClassName = UserTracker.class.getName();
791         String finderMethodName = "findBySessionId";
792         String[] finderParams = new String[] {
793                 String.class.getName(),
794                 
795                 "java.lang.Integer", "java.lang.Integer",
796                 "com.liferay.portal.kernel.util.OrderByComparator"
797             };
798         Object[] finderArgs = new Object[] {
799                 sessionId,
800                 
801                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
802             };
803 
804         Object result = null;
805 
806         if (finderClassNameCacheEnabled) {
807             result = FinderCache.getResult(finderClassName, finderMethodName,
808                     finderParams, finderArgs, getSessionFactory());
809         }
810 
811         if (result == null) {
812             Session session = null;
813 
814             try {
815                 session = openSession();
816 
817                 StringMaker query = new StringMaker();
818 
819                 query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
820 
821                 if (sessionId == null) {
822                     query.append("sessionId IS NULL");
823                 }
824                 else {
825                     query.append("sessionId = ?");
826                 }
827 
828                 query.append(" ");
829 
830                 if (obc != null) {
831                     query.append("ORDER BY ");
832                     query.append(obc.getOrderBy());
833                 }
834 
835                 Query q = session.createQuery(query.toString());
836 
837                 int queryPos = 0;
838 
839                 if (sessionId != null) {
840                     q.setString(queryPos++, sessionId);
841                 }
842 
843                 List<UserTracker> list = (List<UserTracker>)QueryUtil.list(q,
844                         getDialect(), begin, end);
845 
846                 FinderCache.putResult(finderClassNameCacheEnabled,
847                     finderClassName, finderMethodName, finderParams,
848                     finderArgs, list);
849 
850                 return list;
851             }
852             catch (Exception e) {
853                 throw HibernateUtil.processException(e);
854             }
855             finally {
856                 closeSession(session);
857             }
858         }
859         else {
860             return (List<UserTracker>)result;
861         }
862     }
863 
864     public UserTracker findBySessionId_First(String sessionId,
865         OrderByComparator obc)
866         throws NoSuchUserTrackerException, SystemException {
867         List<UserTracker> list = findBySessionId(sessionId, 0, 1, obc);
868 
869         if (list.size() == 0) {
870             StringMaker msg = new StringMaker();
871 
872             msg.append("No UserTracker exists with the key {");
873 
874             msg.append("sessionId=" + sessionId);
875 
876             msg.append(StringPool.CLOSE_CURLY_BRACE);
877 
878             throw new NoSuchUserTrackerException(msg.toString());
879         }
880         else {
881             return list.get(0);
882         }
883     }
884 
885     public UserTracker findBySessionId_Last(String sessionId,
886         OrderByComparator obc)
887         throws NoSuchUserTrackerException, SystemException {
888         int count = countBySessionId(sessionId);
889 
890         List<UserTracker> list = findBySessionId(sessionId, count - 1, count,
891                 obc);
892 
893         if (list.size() == 0) {
894             StringMaker msg = new StringMaker();
895 
896             msg.append("No UserTracker exists with the key {");
897 
898             msg.append("sessionId=" + sessionId);
899 
900             msg.append(StringPool.CLOSE_CURLY_BRACE);
901 
902             throw new NoSuchUserTrackerException(msg.toString());
903         }
904         else {
905             return list.get(0);
906         }
907     }
908 
909     public UserTracker[] findBySessionId_PrevAndNext(long userTrackerId,
910         String sessionId, OrderByComparator obc)
911         throws NoSuchUserTrackerException, SystemException {
912         UserTracker userTracker = findByPrimaryKey(userTrackerId);
913 
914         int count = countBySessionId(sessionId);
915 
916         Session session = null;
917 
918         try {
919             session = openSession();
920 
921             StringMaker query = new StringMaker();
922 
923             query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
924 
925             if (sessionId == null) {
926                 query.append("sessionId IS NULL");
927             }
928             else {
929                 query.append("sessionId = ?");
930             }
931 
932             query.append(" ");
933 
934             if (obc != null) {
935                 query.append("ORDER BY ");
936                 query.append(obc.getOrderBy());
937             }
938 
939             Query q = session.createQuery(query.toString());
940 
941             int queryPos = 0;
942 
943             if (sessionId != null) {
944                 q.setString(queryPos++, sessionId);
945             }
946 
947             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
948                     userTracker);
949 
950             UserTracker[] array = new UserTrackerImpl[3];
951 
952             array[0] = (UserTracker)objArray[0];
953             array[1] = (UserTracker)objArray[1];
954             array[2] = (UserTracker)objArray[2];
955 
956             return array;
957         }
958         catch (Exception e) {
959             throw HibernateUtil.processException(e);
960         }
961         finally {
962             closeSession(session);
963         }
964     }
965 
966     public List<UserTracker> findWithDynamicQuery(
967         DynamicQueryInitializer queryInitializer) throws SystemException {
968         Session session = null;
969 
970         try {
971             session = openSession();
972 
973             DynamicQuery query = queryInitializer.initialize(session);
974 
975             return query.list();
976         }
977         catch (Exception e) {
978             throw HibernateUtil.processException(e);
979         }
980         finally {
981             closeSession(session);
982         }
983     }
984 
985     public List<UserTracker> findWithDynamicQuery(
986         DynamicQueryInitializer queryInitializer, int begin, int end)
987         throws SystemException {
988         Session session = null;
989 
990         try {
991             session = openSession();
992 
993             DynamicQuery query = queryInitializer.initialize(session);
994 
995             query.setLimit(begin, end);
996 
997             return query.list();
998         }
999         catch (Exception e) {
1000            throw HibernateUtil.processException(e);
1001        }
1002        finally {
1003            closeSession(session);
1004        }
1005    }
1006
1007    public List<UserTracker> findAll() throws SystemException {
1008        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1009    }
1010
1011    public List<UserTracker> findAll(int begin, int end)
1012        throws SystemException {
1013        return findAll(begin, end, null);
1014    }
1015
1016    public List<UserTracker> findAll(int begin, int end, OrderByComparator obc)
1017        throws SystemException {
1018        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1019        String finderClassName = UserTracker.class.getName();
1020        String finderMethodName = "findAll";
1021        String[] finderParams = new String[] {
1022                "java.lang.Integer", "java.lang.Integer",
1023                "com.liferay.portal.kernel.util.OrderByComparator"
1024            };
1025        Object[] finderArgs = new Object[] {
1026                String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
1027            };
1028
1029        Object result = null;
1030
1031        if (finderClassNameCacheEnabled) {
1032            result = FinderCache.getResult(finderClassName, finderMethodName,
1033                    finderParams, finderArgs, getSessionFactory());
1034        }
1035
1036        if (result == null) {
1037            Session session = null;
1038
1039            try {
1040                session = openSession();
1041
1042                StringMaker query = new StringMaker();
1043
1044                query.append("FROM com.liferay.portal.model.UserTracker ");
1045
1046                if (obc != null) {
1047                    query.append("ORDER BY ");
1048                    query.append(obc.getOrderBy());
1049                }
1050
1051                Query q = session.createQuery(query.toString());
1052
1053                List<UserTracker> list = (List<UserTracker>)QueryUtil.list(q,
1054                        getDialect(), begin, end);
1055
1056                if (obc == null) {
1057                    Collections.sort(list);
1058                }
1059
1060                FinderCache.putResult(finderClassNameCacheEnabled,
1061                    finderClassName, finderMethodName, finderParams,
1062                    finderArgs, list);
1063
1064                return list;
1065            }
1066            catch (Exception e) {
1067                throw HibernateUtil.processException(e);
1068            }
1069            finally {
1070                closeSession(session);
1071            }
1072        }
1073        else {
1074            return (List<UserTracker>)result;
1075        }
1076    }
1077
1078    public void removeByCompanyId(long companyId) throws SystemException {
1079        for (UserTracker userTracker : findByCompanyId(companyId)) {
1080            remove(userTracker);
1081        }
1082    }
1083
1084    public void removeByUserId(long userId) throws SystemException {
1085        for (UserTracker userTracker : findByUserId(userId)) {
1086            remove(userTracker);
1087        }
1088    }
1089
1090    public void removeBySessionId(String sessionId) throws SystemException {
1091        for (UserTracker userTracker : findBySessionId(sessionId)) {
1092            remove(userTracker);
1093        }
1094    }
1095
1096    public void removeAll() throws SystemException {
1097        for (UserTracker userTracker : findAll()) {
1098            remove(userTracker);
1099        }
1100    }
1101
1102    public int countByCompanyId(long companyId) throws SystemException {
1103        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1104        String finderClassName = UserTracker.class.getName();
1105        String finderMethodName = "countByCompanyId";
1106        String[] finderParams = new String[] { Long.class.getName() };
1107        Object[] finderArgs = new Object[] { new Long(companyId) };
1108
1109        Object result = null;
1110
1111        if (finderClassNameCacheEnabled) {
1112            result = FinderCache.getResult(finderClassName, finderMethodName,
1113                    finderParams, finderArgs, getSessionFactory());
1114        }
1115
1116        if (result == null) {
1117            Session session = null;
1118
1119            try {
1120                session = openSession();
1121
1122                StringMaker query = new StringMaker();
1123
1124                query.append("SELECT COUNT(*) ");
1125                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1126
1127                query.append("companyId = ?");
1128
1129                query.append(" ");
1130
1131                Query q = session.createQuery(query.toString());
1132
1133                int queryPos = 0;
1134
1135                q.setLong(queryPos++, companyId);
1136
1137                Long count = null;
1138
1139                Iterator<Long> itr = q.list().iterator();
1140
1141                if (itr.hasNext()) {
1142                    count = itr.next();
1143                }
1144
1145                if (count == null) {
1146                    count = new Long(0);
1147                }
1148
1149                FinderCache.putResult(finderClassNameCacheEnabled,
1150                    finderClassName, finderMethodName, finderParams,
1151                    finderArgs, count);
1152
1153                return count.intValue();
1154            }
1155            catch (Exception e) {
1156                throw HibernateUtil.processException(e);
1157            }
1158            finally {
1159                closeSession(session);
1160            }
1161        }
1162        else {
1163            return ((Long)result).intValue();
1164        }
1165    }
1166
1167    public int countByUserId(long userId) throws SystemException {
1168        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1169        String finderClassName = UserTracker.class.getName();
1170        String finderMethodName = "countByUserId";
1171        String[] finderParams = new String[] { Long.class.getName() };
1172        Object[] finderArgs = new Object[] { new Long(userId) };
1173
1174        Object result = null;
1175
1176        if (finderClassNameCacheEnabled) {
1177            result = FinderCache.getResult(finderClassName, finderMethodName,
1178                    finderParams, finderArgs, getSessionFactory());
1179        }
1180
1181        if (result == null) {
1182            Session session = null;
1183
1184            try {
1185                session = openSession();
1186
1187                StringMaker query = new StringMaker();
1188
1189                query.append("SELECT COUNT(*) ");
1190                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1191
1192                query.append("userId = ?");
1193
1194                query.append(" ");
1195
1196                Query q = session.createQuery(query.toString());
1197
1198                int queryPos = 0;
1199
1200                q.setLong(queryPos++, userId);
1201
1202                Long count = null;
1203
1204                Iterator<Long> itr = q.list().iterator();
1205
1206                if (itr.hasNext()) {
1207                    count = itr.next();
1208                }
1209
1210                if (count == null) {
1211                    count = new Long(0);
1212                }
1213
1214                FinderCache.putResult(finderClassNameCacheEnabled,
1215                    finderClassName, finderMethodName, finderParams,
1216                    finderArgs, count);
1217
1218                return count.intValue();
1219            }
1220            catch (Exception e) {
1221                throw HibernateUtil.processException(e);
1222            }
1223            finally {
1224                closeSession(session);
1225            }
1226        }
1227        else {
1228            return ((Long)result).intValue();
1229        }
1230    }
1231
1232    public int countBySessionId(String sessionId) throws SystemException {
1233        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1234        String finderClassName = UserTracker.class.getName();
1235        String finderMethodName = "countBySessionId";
1236        String[] finderParams = new String[] { String.class.getName() };
1237        Object[] finderArgs = new Object[] { sessionId };
1238
1239        Object result = null;
1240
1241        if (finderClassNameCacheEnabled) {
1242            result = FinderCache.getResult(finderClassName, finderMethodName,
1243                    finderParams, finderArgs, getSessionFactory());
1244        }
1245
1246        if (result == null) {
1247            Session session = null;
1248
1249            try {
1250                session = openSession();
1251
1252                StringMaker query = new StringMaker();
1253
1254                query.append("SELECT COUNT(*) ");
1255                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1256
1257                if (sessionId == null) {
1258                    query.append("sessionId IS NULL");
1259                }
1260                else {
1261                    query.append("sessionId = ?");
1262                }
1263
1264                query.append(" ");
1265
1266                Query q = session.createQuery(query.toString());
1267
1268                int queryPos = 0;
1269
1270                if (sessionId != null) {
1271                    q.setString(queryPos++, sessionId);
1272                }
1273
1274                Long count = null;
1275
1276                Iterator<Long> itr = q.list().iterator();
1277
1278                if (itr.hasNext()) {
1279                    count = itr.next();
1280                }
1281
1282                if (count == null) {
1283                    count = new Long(0);
1284                }
1285
1286                FinderCache.putResult(finderClassNameCacheEnabled,
1287                    finderClassName, finderMethodName, finderParams,
1288                    finderArgs, count);
1289
1290                return count.intValue();
1291            }
1292            catch (Exception e) {
1293                throw HibernateUtil.processException(e);
1294            }
1295            finally {
1296                closeSession(session);
1297            }
1298        }
1299        else {
1300            return ((Long)result).intValue();
1301        }
1302    }
1303
1304    public int countAll() throws SystemException {
1305        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1306        String finderClassName = UserTracker.class.getName();
1307        String finderMethodName = "countAll";
1308        String[] finderParams = new String[] {  };
1309        Object[] finderArgs = new Object[] {  };
1310
1311        Object result = null;
1312
1313        if (finderClassNameCacheEnabled) {
1314            result = FinderCache.getResult(finderClassName, finderMethodName,
1315                    finderParams, finderArgs, getSessionFactory());
1316        }
1317
1318        if (result == null) {
1319            Session session = null;
1320
1321            try {
1322                session = openSession();
1323
1324                Query q = session.createQuery(
1325                        "SELECT COUNT(*) FROM com.liferay.portal.model.UserTracker");
1326
1327                Long count = null;
1328
1329                Iterator<Long> itr = q.list().iterator();
1330
1331                if (itr.hasNext()) {
1332                    count = itr.next();
1333                }
1334
1335                if (count == null) {
1336                    count = new Long(0);
1337                }
1338
1339                FinderCache.putResult(finderClassNameCacheEnabled,
1340                    finderClassName, finderMethodName, finderParams,
1341                    finderArgs, count);
1342
1343                return count.intValue();
1344            }
1345            catch (Exception e) {
1346                throw HibernateUtil.processException(e);
1347            }
1348            finally {
1349                closeSession(session);
1350            }
1351        }
1352        else {
1353            return ((Long)result).intValue();
1354        }
1355    }
1356
1357    protected void initDao() {
1358        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1359                    PropsUtil.get(
1360                        "value.object.listener.com.liferay.portal.model.UserTracker")));
1361
1362        if (listenerClassNames.length > 0) {
1363            try {
1364                List<ModelListener> listeners = new ArrayList<ModelListener>();
1365
1366                for (String listenerClassName : listenerClassNames) {
1367                    listeners.add((ModelListener)Class.forName(
1368                            listenerClassName).newInstance());
1369                }
1370
1371                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1372            }
1373            catch (Exception e) {
1374                _log.error(e);
1375            }
1376        }
1377    }
1378
1379    private static Log _log = LogFactory.getLog(UserTrackerPersistenceImpl.class);
1380    private ModelListener[] _listeners;
1381}