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