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