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