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