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