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.NoSuchUserTrackerPathException;
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.UserTrackerPath;
40  import com.liferay.portal.model.impl.UserTrackerPathImpl;
41  import com.liferay.portal.model.impl.UserTrackerPathModelImpl;
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="UserTrackerPathPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class UserTrackerPathPersistenceImpl extends BasePersistenceImpl
59      implements UserTrackerPathPersistence {
60      public UserTrackerPath create(long userTrackerPathId) {
61          UserTrackerPath userTrackerPath = new UserTrackerPathImpl();
62  
63          userTrackerPath.setNew(true);
64          userTrackerPath.setPrimaryKey(userTrackerPathId);
65  
66          return userTrackerPath;
67      }
68  
69      public UserTrackerPath remove(long userTrackerPathId)
70          throws NoSuchUserTrackerPathException, SystemException {
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              UserTrackerPath userTrackerPath = (UserTrackerPath)session.get(UserTrackerPathImpl.class,
77                      new Long(userTrackerPathId));
78  
79              if (userTrackerPath == null) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn("No UserTrackerPath exists with the primary key " +
82                          userTrackerPathId);
83                  }
84  
85                  throw new NoSuchUserTrackerPathException(
86                      "No UserTrackerPath exists with the primary key " +
87                      userTrackerPathId);
88              }
89  
90              return remove(userTrackerPath);
91          }
92          catch (NoSuchUserTrackerPathException nsee) {
93              throw nsee;
94          }
95          catch (Exception e) {
96              throw processException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public UserTrackerPath remove(UserTrackerPath userTrackerPath)
104         throws SystemException {
105         if (_listeners.length > 0) {
106             for (ModelListener listener : _listeners) {
107                 listener.onBeforeRemove(userTrackerPath);
108             }
109         }
110 
111         userTrackerPath = removeImpl(userTrackerPath);
112 
113         if (_listeners.length > 0) {
114             for (ModelListener listener : _listeners) {
115                 listener.onAfterRemove(userTrackerPath);
116             }
117         }
118 
119         return userTrackerPath;
120     }
121 
122     protected UserTrackerPath removeImpl(UserTrackerPath userTrackerPath)
123         throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             session.delete(userTrackerPath);
130 
131             session.flush();
132 
133             return userTrackerPath;
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCacheUtil.clearCache(UserTrackerPath.class.getName());
142         }
143     }
144 
145     /**
146      * @deprecated Use <code>update(UserTrackerPath userTrackerPath, boolean merge)</code>.
147      */
148     public UserTrackerPath update(UserTrackerPath userTrackerPath)
149         throws SystemException {
150         if (_log.isWarnEnabled()) {
151             _log.warn(
152                 "Using the deprecated update(UserTrackerPath userTrackerPath) method. Use update(UserTrackerPath userTrackerPath, boolean merge) instead.");
153         }
154 
155         return update(userTrackerPath, 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        userTrackerPath 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 userTrackerPath 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 UserTrackerPath update(UserTrackerPath userTrackerPath, boolean merge)
172         throws SystemException {
173         boolean isNew = userTrackerPath.isNew();
174 
175         if (_listeners.length > 0) {
176             for (ModelListener listener : _listeners) {
177                 if (isNew) {
178                     listener.onBeforeCreate(userTrackerPath);
179                 }
180                 else {
181                     listener.onBeforeUpdate(userTrackerPath);
182                 }
183             }
184         }
185 
186         userTrackerPath = updateImpl(userTrackerPath, merge);
187 
188         if (_listeners.length > 0) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onAfterCreate(userTrackerPath);
192                 }
193                 else {
194                     listener.onAfterUpdate(userTrackerPath);
195                 }
196             }
197         }
198 
199         return userTrackerPath;
200     }
201 
202     public UserTrackerPath updateImpl(
203         com.liferay.portal.model.UserTrackerPath userTrackerPath, boolean merge)
204         throws SystemException {
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             if (merge) {
211                 session.merge(userTrackerPath);
212             }
213             else {
214                 if (userTrackerPath.isNew()) {
215                     session.save(userTrackerPath);
216                 }
217             }
218 
219             session.flush();
220 
221             userTrackerPath.setNew(false);
222 
223             return userTrackerPath;
224         }
225         catch (Exception e) {
226             throw processException(e);
227         }
228         finally {
229             closeSession(session);
230 
231             FinderCacheUtil.clearCache(UserTrackerPath.class.getName());
232         }
233     }
234 
235     public UserTrackerPath findByPrimaryKey(long userTrackerPathId)
236         throws NoSuchUserTrackerPathException, SystemException {
237         UserTrackerPath userTrackerPath = fetchByPrimaryKey(userTrackerPathId);
238 
239         if (userTrackerPath == null) {
240             if (_log.isWarnEnabled()) {
241                 _log.warn("No UserTrackerPath exists with the primary key " +
242                     userTrackerPathId);
243             }
244 
245             throw new NoSuchUserTrackerPathException(
246                 "No UserTrackerPath exists with the primary key " +
247                 userTrackerPathId);
248         }
249 
250         return userTrackerPath;
251     }
252 
253     public UserTrackerPath fetchByPrimaryKey(long userTrackerPathId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (UserTrackerPath)session.get(UserTrackerPathImpl.class,
261                 new Long(userTrackerPathId));
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId)
272         throws SystemException {
273         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
274         String finderClassName = UserTrackerPath.class.getName();
275         String finderMethodName = "findByUserTrackerId";
276         String[] finderParams = new String[] { Long.class.getName() };
277         Object[] finderArgs = new Object[] { new Long(userTrackerId) };
278 
279         Object result = null;
280 
281         if (finderClassNameCacheEnabled) {
282             result = FinderCacheUtil.getResult(finderClassName,
283                     finderMethodName, finderParams, finderArgs, this);
284         }
285 
286         if (result == null) {
287             Session session = null;
288 
289             try {
290                 session = openSession();
291 
292                 StringBuilder query = new StringBuilder();
293 
294                 query.append(
295                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
296 
297                 query.append("userTrackerId = ?");
298 
299                 query.append(" ");
300 
301                 Query q = session.createQuery(query.toString());
302 
303                 QueryPos qPos = QueryPos.getInstance(q);
304 
305                 qPos.add(userTrackerId);
306 
307                 List<UserTrackerPath> list = q.list();
308 
309                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
310                     finderClassName, finderMethodName, finderParams,
311                     finderArgs, list);
312 
313                 return list;
314             }
315             catch (Exception e) {
316                 throw processException(e);
317             }
318             finally {
319                 closeSession(session);
320             }
321         }
322         else {
323             return (List<UserTrackerPath>)result;
324         }
325     }
326 
327     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId,
328         int start, int end) throws SystemException {
329         return findByUserTrackerId(userTrackerId, start, end, null);
330     }
331 
332     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId,
333         int start, int end, OrderByComparator obc) throws SystemException {
334         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
335         String finderClassName = UserTrackerPath.class.getName();
336         String finderMethodName = "findByUserTrackerId";
337         String[] finderParams = new String[] {
338                 Long.class.getName(),
339                 
340                 "java.lang.Integer", "java.lang.Integer",
341                 "com.liferay.portal.kernel.util.OrderByComparator"
342             };
343         Object[] finderArgs = new Object[] {
344                 new Long(userTrackerId),
345                 
346                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
347             };
348 
349         Object result = null;
350 
351         if (finderClassNameCacheEnabled) {
352             result = FinderCacheUtil.getResult(finderClassName,
353                     finderMethodName, finderParams, finderArgs, this);
354         }
355 
356         if (result == null) {
357             Session session = null;
358 
359             try {
360                 session = openSession();
361 
362                 StringBuilder query = new StringBuilder();
363 
364                 query.append(
365                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
366 
367                 query.append("userTrackerId = ?");
368 
369                 query.append(" ");
370 
371                 if (obc != null) {
372                     query.append("ORDER BY ");
373                     query.append(obc.getOrderBy());
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 QueryPos qPos = QueryPos.getInstance(q);
379 
380                 qPos.add(userTrackerId);
381 
382                 List<UserTrackerPath> list = (List<UserTrackerPath>)QueryUtil.list(q,
383                         getDialect(), start, end);
384 
385                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
386                     finderClassName, finderMethodName, finderParams,
387                     finderArgs, list);
388 
389                 return list;
390             }
391             catch (Exception e) {
392                 throw processException(e);
393             }
394             finally {
395                 closeSession(session);
396             }
397         }
398         else {
399             return (List<UserTrackerPath>)result;
400         }
401     }
402 
403     public UserTrackerPath findByUserTrackerId_First(long userTrackerId,
404         OrderByComparator obc)
405         throws NoSuchUserTrackerPathException, SystemException {
406         List<UserTrackerPath> list = findByUserTrackerId(userTrackerId, 0, 1,
407                 obc);
408 
409         if (list.size() == 0) {
410             StringBuilder msg = new StringBuilder();
411 
412             msg.append("No UserTrackerPath exists with the key {");
413 
414             msg.append("userTrackerId=" + userTrackerId);
415 
416             msg.append(StringPool.CLOSE_CURLY_BRACE);
417 
418             throw new NoSuchUserTrackerPathException(msg.toString());
419         }
420         else {
421             return list.get(0);
422         }
423     }
424 
425     public UserTrackerPath findByUserTrackerId_Last(long userTrackerId,
426         OrderByComparator obc)
427         throws NoSuchUserTrackerPathException, SystemException {
428         int count = countByUserTrackerId(userTrackerId);
429 
430         List<UserTrackerPath> list = findByUserTrackerId(userTrackerId,
431                 count - 1, count, obc);
432 
433         if (list.size() == 0) {
434             StringBuilder msg = new StringBuilder();
435 
436             msg.append("No UserTrackerPath exists with the key {");
437 
438             msg.append("userTrackerId=" + userTrackerId);
439 
440             msg.append(StringPool.CLOSE_CURLY_BRACE);
441 
442             throw new NoSuchUserTrackerPathException(msg.toString());
443         }
444         else {
445             return list.get(0);
446         }
447     }
448 
449     public UserTrackerPath[] findByUserTrackerId_PrevAndNext(
450         long userTrackerPathId, long userTrackerId, OrderByComparator obc)
451         throws NoSuchUserTrackerPathException, SystemException {
452         UserTrackerPath userTrackerPath = findByPrimaryKey(userTrackerPathId);
453 
454         int count = countByUserTrackerId(userTrackerId);
455 
456         Session session = null;
457 
458         try {
459             session = openSession();
460 
461             StringBuilder query = new StringBuilder();
462 
463             query.append("FROM com.liferay.portal.model.UserTrackerPath WHERE ");
464 
465             query.append("userTrackerId = ?");
466 
467             query.append(" ");
468 
469             if (obc != null) {
470                 query.append("ORDER BY ");
471                 query.append(obc.getOrderBy());
472             }
473 
474             Query q = session.createQuery(query.toString());
475 
476             QueryPos qPos = QueryPos.getInstance(q);
477 
478             qPos.add(userTrackerId);
479 
480             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
481                     userTrackerPath);
482 
483             UserTrackerPath[] array = new UserTrackerPathImpl[3];
484 
485             array[0] = (UserTrackerPath)objArray[0];
486             array[1] = (UserTrackerPath)objArray[1];
487             array[2] = (UserTrackerPath)objArray[2];
488 
489             return array;
490         }
491         catch (Exception e) {
492             throw processException(e);
493         }
494         finally {
495             closeSession(session);
496         }
497     }
498 
499     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
500         throws SystemException {
501         Session session = null;
502 
503         try {
504             session = openSession();
505 
506             dynamicQuery.compile(session);
507 
508             return dynamicQuery.list();
509         }
510         catch (Exception e) {
511             throw processException(e);
512         }
513         finally {
514             closeSession(session);
515         }
516     }
517 
518     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
519         int start, int end) throws SystemException {
520         Session session = null;
521 
522         try {
523             session = openSession();
524 
525             dynamicQuery.setLimit(start, end);
526 
527             dynamicQuery.compile(session);
528 
529             return dynamicQuery.list();
530         }
531         catch (Exception e) {
532             throw processException(e);
533         }
534         finally {
535             closeSession(session);
536         }
537     }
538 
539     public List<UserTrackerPath> findAll() throws SystemException {
540         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
541     }
542 
543     public List<UserTrackerPath> findAll(int start, int end)
544         throws SystemException {
545         return findAll(start, end, null);
546     }
547 
548     public List<UserTrackerPath> findAll(int start, int end,
549         OrderByComparator obc) throws SystemException {
550         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
551         String finderClassName = UserTrackerPath.class.getName();
552         String finderMethodName = "findAll";
553         String[] finderParams = new String[] {
554                 "java.lang.Integer", "java.lang.Integer",
555                 "com.liferay.portal.kernel.util.OrderByComparator"
556             };
557         Object[] finderArgs = new Object[] {
558                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
559             };
560 
561         Object result = null;
562 
563         if (finderClassNameCacheEnabled) {
564             result = FinderCacheUtil.getResult(finderClassName,
565                     finderMethodName, finderParams, finderArgs, this);
566         }
567 
568         if (result == null) {
569             Session session = null;
570 
571             try {
572                 session = openSession();
573 
574                 StringBuilder query = new StringBuilder();
575 
576                 query.append("FROM com.liferay.portal.model.UserTrackerPath ");
577 
578                 if (obc != null) {
579                     query.append("ORDER BY ");
580                     query.append(obc.getOrderBy());
581                 }
582 
583                 Query q = session.createQuery(query.toString());
584 
585                 List<UserTrackerPath> list = (List<UserTrackerPath>)QueryUtil.list(q,
586                         getDialect(), start, end);
587 
588                 if (obc == null) {
589                     Collections.sort(list);
590                 }
591 
592                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
593                     finderClassName, finderMethodName, finderParams,
594                     finderArgs, list);
595 
596                 return list;
597             }
598             catch (Exception e) {
599                 throw processException(e);
600             }
601             finally {
602                 closeSession(session);
603             }
604         }
605         else {
606             return (List<UserTrackerPath>)result;
607         }
608     }
609 
610     public void removeByUserTrackerId(long userTrackerId)
611         throws SystemException {
612         for (UserTrackerPath userTrackerPath : findByUserTrackerId(
613                 userTrackerId)) {
614             remove(userTrackerPath);
615         }
616     }
617 
618     public void removeAll() throws SystemException {
619         for (UserTrackerPath userTrackerPath : findAll()) {
620             remove(userTrackerPath);
621         }
622     }
623 
624     public int countByUserTrackerId(long userTrackerId)
625         throws SystemException {
626         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
627         String finderClassName = UserTrackerPath.class.getName();
628         String finderMethodName = "countByUserTrackerId";
629         String[] finderParams = new String[] { Long.class.getName() };
630         Object[] finderArgs = new Object[] { new Long(userTrackerId) };
631 
632         Object result = null;
633 
634         if (finderClassNameCacheEnabled) {
635             result = FinderCacheUtil.getResult(finderClassName,
636                     finderMethodName, finderParams, finderArgs, this);
637         }
638 
639         if (result == null) {
640             Session session = null;
641 
642             try {
643                 session = openSession();
644 
645                 StringBuilder query = new StringBuilder();
646 
647                 query.append("SELECT COUNT(*) ");
648                 query.append(
649                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
650 
651                 query.append("userTrackerId = ?");
652 
653                 query.append(" ");
654 
655                 Query q = session.createQuery(query.toString());
656 
657                 QueryPos qPos = QueryPos.getInstance(q);
658 
659                 qPos.add(userTrackerId);
660 
661                 Long count = null;
662 
663                 Iterator<Long> itr = q.list().iterator();
664 
665                 if (itr.hasNext()) {
666                     count = itr.next();
667                 }
668 
669                 if (count == null) {
670                     count = new Long(0);
671                 }
672 
673                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
674                     finderClassName, finderMethodName, finderParams,
675                     finderArgs, count);
676 
677                 return count.intValue();
678             }
679             catch (Exception e) {
680                 throw processException(e);
681             }
682             finally {
683                 closeSession(session);
684             }
685         }
686         else {
687             return ((Long)result).intValue();
688         }
689     }
690 
691     public int countAll() throws SystemException {
692         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
693         String finderClassName = UserTrackerPath.class.getName();
694         String finderMethodName = "countAll";
695         String[] finderParams = new String[] {  };
696         Object[] finderArgs = new Object[] {  };
697 
698         Object result = null;
699 
700         if (finderClassNameCacheEnabled) {
701             result = FinderCacheUtil.getResult(finderClassName,
702                     finderMethodName, finderParams, finderArgs, this);
703         }
704 
705         if (result == null) {
706             Session session = null;
707 
708             try {
709                 session = openSession();
710 
711                 Query q = session.createQuery(
712                         "SELECT COUNT(*) FROM com.liferay.portal.model.UserTrackerPath");
713 
714                 Long count = null;
715 
716                 Iterator<Long> itr = q.list().iterator();
717 
718                 if (itr.hasNext()) {
719                     count = itr.next();
720                 }
721 
722                 if (count == null) {
723                     count = new Long(0);
724                 }
725 
726                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
727                     finderClassName, finderMethodName, finderParams,
728                     finderArgs, count);
729 
730                 return count.intValue();
731             }
732             catch (Exception e) {
733                 throw processException(e);
734             }
735             finally {
736                 closeSession(session);
737             }
738         }
739         else {
740             return ((Long)result).intValue();
741         }
742     }
743 
744     public void registerListener(ModelListener listener) {
745         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
746 
747         listeners.add(listener);
748 
749         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
750     }
751 
752     public void unregisterListener(ModelListener listener) {
753         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
754 
755         listeners.remove(listener);
756 
757         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
758     }
759 
760     public void afterPropertiesSet() {
761         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
762                     com.liferay.portal.util.PropsUtil.get(
763                         "value.object.listener.com.liferay.portal.model.UserTrackerPath")));
764 
765         if (listenerClassNames.length > 0) {
766             try {
767                 List<ModelListener> listeners = new ArrayList<ModelListener>();
768 
769                 for (String listenerClassName : listenerClassNames) {
770                     listeners.add((ModelListener)Class.forName(
771                             listenerClassName).newInstance());
772                 }
773 
774                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
775             }
776             catch (Exception e) {
777                 _log.error(e);
778             }
779         }
780     }
781 
782     private static Log _log = LogFactory.getLog(UserTrackerPathPersistenceImpl.class);
783     private ModelListener[] _listeners = new ModelListener[0];
784 }