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