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             session.delete(userTracker);
130 
131             session.flush();
132 
133             return userTracker;
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCacheUtil.clearCache(UserTracker.class.getName());
142         }
143     }
144 
145     /**
146      * @deprecated Use <code>update(UserTracker userTracker, boolean merge)</code>.
147      */
148     public UserTracker update(UserTracker userTracker)
149         throws SystemException {
150         if (_log.isWarnEnabled()) {
151             _log.warn(
152                 "Using the deprecated update(UserTracker userTracker) method. Use update(UserTracker userTracker, boolean merge) instead.");
153         }
154 
155         return update(userTracker, false);
156     }
157 
158     /**
159      * Add, update, or merge, the entity. This method also calls the model
160      * listeners to trigger the proper events associated with adding, deleting,
161      * or updating an entity.
162      *
163      * @param        userTracker the entity to add, update, or merge
164      * @param        merge boolean value for whether to merge the entity. The
165      *                default value is false. Setting merge to true is more
166      *                expensive and should only be true when userTracker is
167      *                transient. See LEP-5473 for a detailed discussion of this
168      *                method.
169      * @return        true if the portlet can be displayed via Ajax
170      */
171     public UserTracker update(UserTracker userTracker, boolean merge)
172         throws SystemException {
173         boolean isNew = userTracker.isNew();
174 
175         if (_listeners.length > 0) {
176             for (ModelListener listener : _listeners) {
177                 if (isNew) {
178                     listener.onBeforeCreate(userTracker);
179                 }
180                 else {
181                     listener.onBeforeUpdate(userTracker);
182                 }
183             }
184         }
185 
186         userTracker = updateImpl(userTracker, merge);
187 
188         if (_listeners.length > 0) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onAfterCreate(userTracker);
192                 }
193                 else {
194                     listener.onAfterUpdate(userTracker);
195                 }
196             }
197         }
198 
199         return userTracker;
200     }
201 
202     public UserTracker updateImpl(
203         com.liferay.portal.model.UserTracker userTracker, boolean merge)
204         throws SystemException {
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             if (merge) {
211                 session.merge(userTracker);
212             }
213             else {
214                 if (userTracker.isNew()) {
215                     session.save(userTracker);
216                 }
217             }
218 
219             session.flush();
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 = (List<UserTracker>)QueryUtil.list(q,
1050                        getDialect(), start, end);
1051
1052                if (obc == null) {
1053                    Collections.sort(list);
1054                }
1055
1056                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1057                    finderClassName, finderMethodName, finderParams,
1058                    finderArgs, list);
1059
1060                return list;
1061            }
1062            catch (Exception e) {
1063                throw processException(e);
1064            }
1065            finally {
1066                closeSession(session);
1067            }
1068        }
1069        else {
1070            return (List<UserTracker>)result;
1071        }
1072    }
1073
1074    public void removeByCompanyId(long companyId) throws SystemException {
1075        for (UserTracker userTracker : findByCompanyId(companyId)) {
1076            remove(userTracker);
1077        }
1078    }
1079
1080    public void removeByUserId(long userId) throws SystemException {
1081        for (UserTracker userTracker : findByUserId(userId)) {
1082            remove(userTracker);
1083        }
1084    }
1085
1086    public void removeBySessionId(String sessionId) throws SystemException {
1087        for (UserTracker userTracker : findBySessionId(sessionId)) {
1088            remove(userTracker);
1089        }
1090    }
1091
1092    public void removeAll() throws SystemException {
1093        for (UserTracker userTracker : findAll()) {
1094            remove(userTracker);
1095        }
1096    }
1097
1098    public int countByCompanyId(long companyId) throws SystemException {
1099        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1100        String finderClassName = UserTracker.class.getName();
1101        String finderMethodName = "countByCompanyId";
1102        String[] finderParams = new String[] { Long.class.getName() };
1103        Object[] finderArgs = new Object[] { new Long(companyId) };
1104
1105        Object result = null;
1106
1107        if (finderClassNameCacheEnabled) {
1108            result = FinderCacheUtil.getResult(finderClassName,
1109                    finderMethodName, finderParams, finderArgs, this);
1110        }
1111
1112        if (result == null) {
1113            Session session = null;
1114
1115            try {
1116                session = openSession();
1117
1118                StringBuilder query = new StringBuilder();
1119
1120                query.append("SELECT COUNT(*) ");
1121                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1122
1123                query.append("companyId = ?");
1124
1125                query.append(" ");
1126
1127                Query q = session.createQuery(query.toString());
1128
1129                QueryPos qPos = QueryPos.getInstance(q);
1130
1131                qPos.add(companyId);
1132
1133                Long count = null;
1134
1135                Iterator<Long> itr = q.list().iterator();
1136
1137                if (itr.hasNext()) {
1138                    count = itr.next();
1139                }
1140
1141                if (count == null) {
1142                    count = new Long(0);
1143                }
1144
1145                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1146                    finderClassName, finderMethodName, finderParams,
1147                    finderArgs, count);
1148
1149                return count.intValue();
1150            }
1151            catch (Exception e) {
1152                throw processException(e);
1153            }
1154            finally {
1155                closeSession(session);
1156            }
1157        }
1158        else {
1159            return ((Long)result).intValue();
1160        }
1161    }
1162
1163    public int countByUserId(long userId) throws SystemException {
1164        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1165        String finderClassName = UserTracker.class.getName();
1166        String finderMethodName = "countByUserId";
1167        String[] finderParams = new String[] { Long.class.getName() };
1168        Object[] finderArgs = new Object[] { new Long(userId) };
1169
1170        Object result = null;
1171
1172        if (finderClassNameCacheEnabled) {
1173            result = FinderCacheUtil.getResult(finderClassName,
1174                    finderMethodName, finderParams, finderArgs, this);
1175        }
1176
1177        if (result == null) {
1178            Session session = null;
1179
1180            try {
1181                session = openSession();
1182
1183                StringBuilder query = new StringBuilder();
1184
1185                query.append("SELECT COUNT(*) ");
1186                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1187
1188                query.append("userId = ?");
1189
1190                query.append(" ");
1191
1192                Query q = session.createQuery(query.toString());
1193
1194                QueryPos qPos = QueryPos.getInstance(q);
1195
1196                qPos.add(userId);
1197
1198                Long count = null;
1199
1200                Iterator<Long> itr = q.list().iterator();
1201
1202                if (itr.hasNext()) {
1203                    count = itr.next();
1204                }
1205
1206                if (count == null) {
1207                    count = new Long(0);
1208                }
1209
1210                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1211                    finderClassName, finderMethodName, finderParams,
1212                    finderArgs, count);
1213
1214                return count.intValue();
1215            }
1216            catch (Exception e) {
1217                throw processException(e);
1218            }
1219            finally {
1220                closeSession(session);
1221            }
1222        }
1223        else {
1224            return ((Long)result).intValue();
1225        }
1226    }
1227
1228    public int countBySessionId(String sessionId) throws SystemException {
1229        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1230        String finderClassName = UserTracker.class.getName();
1231        String finderMethodName = "countBySessionId";
1232        String[] finderParams = new String[] { String.class.getName() };
1233        Object[] finderArgs = new Object[] { sessionId };
1234
1235        Object result = null;
1236
1237        if (finderClassNameCacheEnabled) {
1238            result = FinderCacheUtil.getResult(finderClassName,
1239                    finderMethodName, finderParams, finderArgs, this);
1240        }
1241
1242        if (result == null) {
1243            Session session = null;
1244
1245            try {
1246                session = openSession();
1247
1248                StringBuilder query = new StringBuilder();
1249
1250                query.append("SELECT COUNT(*) ");
1251                query.append("FROM com.liferay.portal.model.UserTracker WHERE ");
1252
1253                if (sessionId == null) {
1254                    query.append("sessionId IS NULL");
1255                }
1256                else {
1257                    query.append("sessionId = ?");
1258                }
1259
1260                query.append(" ");
1261
1262                Query q = session.createQuery(query.toString());
1263
1264                QueryPos qPos = QueryPos.getInstance(q);
1265
1266                if (sessionId != null) {
1267                    qPos.add(sessionId);
1268                }
1269
1270                Long count = null;
1271
1272                Iterator<Long> itr = q.list().iterator();
1273
1274                if (itr.hasNext()) {
1275                    count = itr.next();
1276                }
1277
1278                if (count == null) {
1279                    count = new Long(0);
1280                }
1281
1282                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1283                    finderClassName, finderMethodName, finderParams,
1284                    finderArgs, count);
1285
1286                return count.intValue();
1287            }
1288            catch (Exception e) {
1289                throw processException(e);
1290            }
1291            finally {
1292                closeSession(session);
1293            }
1294        }
1295        else {
1296            return ((Long)result).intValue();
1297        }
1298    }
1299
1300    public int countAll() throws SystemException {
1301        boolean finderClassNameCacheEnabled = UserTrackerModelImpl.CACHE_ENABLED;
1302        String finderClassName = UserTracker.class.getName();
1303        String finderMethodName = "countAll";
1304        String[] finderParams = new String[] {  };
1305        Object[] finderArgs = new Object[] {  };
1306
1307        Object result = null;
1308
1309        if (finderClassNameCacheEnabled) {
1310            result = FinderCacheUtil.getResult(finderClassName,
1311                    finderMethodName, finderParams, finderArgs, this);
1312        }
1313
1314        if (result == null) {
1315            Session session = null;
1316
1317            try {
1318                session = openSession();
1319
1320                Query q = session.createQuery(
1321                        "SELECT COUNT(*) FROM com.liferay.portal.model.UserTracker");
1322
1323                Long count = null;
1324
1325                Iterator<Long> itr = q.list().iterator();
1326
1327                if (itr.hasNext()) {
1328                    count = itr.next();
1329                }
1330
1331                if (count == null) {
1332                    count = new Long(0);
1333                }
1334
1335                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1336                    finderClassName, finderMethodName, finderParams,
1337                    finderArgs, count);
1338
1339                return count.intValue();
1340            }
1341            catch (Exception e) {
1342                throw processException(e);
1343            }
1344            finally {
1345                closeSession(session);
1346            }
1347        }
1348        else {
1349            return ((Long)result).intValue();
1350        }
1351    }
1352
1353    public void registerListener(ModelListener listener) {
1354        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1355
1356        listeners.add(listener);
1357
1358        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1359    }
1360
1361    public void unregisterListener(ModelListener listener) {
1362        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1363
1364        listeners.remove(listener);
1365
1366        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1367    }
1368
1369    public void afterPropertiesSet() {
1370        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1371                    com.liferay.portal.util.PropsUtil.get(
1372                        "value.object.listener.com.liferay.portal.model.UserTracker")));
1373
1374        if (listenerClassNames.length > 0) {
1375            try {
1376                List<ModelListener> listeners = new ArrayList<ModelListener>();
1377
1378                for (String listenerClassName : listenerClassNames) {
1379                    listeners.add((ModelListener)Class.forName(
1380                            listenerClassName).newInstance());
1381                }
1382
1383                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1384            }
1385            catch (Exception e) {
1386                _log.error(e);
1387            }
1388        }
1389    }
1390
1391    private static Log _log = LogFactory.getLog(UserTrackerPersistenceImpl.class);
1392    private ModelListener[] _listeners = new ModelListener[0];
1393}