1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchUserTrackerPathException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
25  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
26  import com.liferay.portal.kernel.dao.orm.Query;
27  import com.liferay.portal.kernel.dao.orm.QueryPos;
28  import com.liferay.portal.kernel.dao.orm.QueryUtil;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.model.UserTrackerPath;
38  import com.liferay.portal.model.impl.UserTrackerPathImpl;
39  import com.liferay.portal.model.impl.UserTrackerPathModelImpl;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   * <a href="UserTrackerPathPersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class UserTrackerPathPersistenceImpl extends BasePersistenceImpl
54      implements UserTrackerPathPersistence {
55      public UserTrackerPath create(long userTrackerPathId) {
56          UserTrackerPath userTrackerPath = new UserTrackerPathImpl();
57  
58          userTrackerPath.setNew(true);
59          userTrackerPath.setPrimaryKey(userTrackerPathId);
60  
61          return userTrackerPath;
62      }
63  
64      public UserTrackerPath remove(long userTrackerPathId)
65          throws NoSuchUserTrackerPathException, SystemException {
66          Session session = null;
67  
68          try {
69              session = openSession();
70  
71              UserTrackerPath userTrackerPath = (UserTrackerPath)session.get(UserTrackerPathImpl.class,
72                      new Long(userTrackerPathId));
73  
74              if (userTrackerPath == null) {
75                  if (_log.isWarnEnabled()) {
76                      _log.warn("No UserTrackerPath exists with the primary key " +
77                          userTrackerPathId);
78                  }
79  
80                  throw new NoSuchUserTrackerPathException(
81                      "No UserTrackerPath exists with the primary key " +
82                      userTrackerPathId);
83              }
84  
85              return remove(userTrackerPath);
86          }
87          catch (NoSuchUserTrackerPathException nsee) {
88              throw nsee;
89          }
90          catch (Exception e) {
91              throw processException(e);
92          }
93          finally {
94              closeSession(session);
95          }
96      }
97  
98      public UserTrackerPath remove(UserTrackerPath userTrackerPath)
99          throws SystemException {
100         for (ModelListener listener : listeners) {
101             listener.onBeforeRemove(userTrackerPath);
102         }
103 
104         userTrackerPath = removeImpl(userTrackerPath);
105 
106         for (ModelListener listener : listeners) {
107             listener.onAfterRemove(userTrackerPath);
108         }
109 
110         return userTrackerPath;
111     }
112 
113     protected UserTrackerPath removeImpl(UserTrackerPath userTrackerPath)
114         throws SystemException {
115         Session session = null;
116 
117         try {
118             session = openSession();
119 
120             if (BatchSessionUtil.isEnabled()) {
121                 Object staleObject = session.get(UserTrackerPathImpl.class,
122                         userTrackerPath.getPrimaryKeyObj());
123 
124                 if (staleObject != null) {
125                     session.evict(staleObject);
126                 }
127             }
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         for (ModelListener listener : listeners) {
176             if (isNew) {
177                 listener.onBeforeCreate(userTrackerPath);
178             }
179             else {
180                 listener.onBeforeUpdate(userTrackerPath);
181             }
182         }
183 
184         userTrackerPath = updateImpl(userTrackerPath, merge);
185 
186         for (ModelListener listener : listeners) {
187             if (isNew) {
188                 listener.onAfterCreate(userTrackerPath);
189             }
190             else {
191                 listener.onAfterUpdate(userTrackerPath);
192             }
193         }
194 
195         return userTrackerPath;
196     }
197 
198     public UserTrackerPath updateImpl(
199         com.liferay.portal.model.UserTrackerPath userTrackerPath, boolean merge)
200         throws SystemException {
201         Session session = null;
202 
203         try {
204             session = openSession();
205 
206             BatchSessionUtil.update(session, userTrackerPath, merge);
207 
208             userTrackerPath.setNew(false);
209 
210             return userTrackerPath;
211         }
212         catch (Exception e) {
213             throw processException(e);
214         }
215         finally {
216             closeSession(session);
217 
218             FinderCacheUtil.clearCache(UserTrackerPath.class.getName());
219         }
220     }
221 
222     public UserTrackerPath findByPrimaryKey(long userTrackerPathId)
223         throws NoSuchUserTrackerPathException, SystemException {
224         UserTrackerPath userTrackerPath = fetchByPrimaryKey(userTrackerPathId);
225 
226         if (userTrackerPath == null) {
227             if (_log.isWarnEnabled()) {
228                 _log.warn("No UserTrackerPath exists with the primary key " +
229                     userTrackerPathId);
230             }
231 
232             throw new NoSuchUserTrackerPathException(
233                 "No UserTrackerPath exists with the primary key " +
234                 userTrackerPathId);
235         }
236 
237         return userTrackerPath;
238     }
239 
240     public UserTrackerPath fetchByPrimaryKey(long userTrackerPathId)
241         throws SystemException {
242         Session session = null;
243 
244         try {
245             session = openSession();
246 
247             return (UserTrackerPath)session.get(UserTrackerPathImpl.class,
248                 new Long(userTrackerPathId));
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             closeSession(session);
255         }
256     }
257 
258     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId)
259         throws SystemException {
260         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
261         String finderClassName = UserTrackerPath.class.getName();
262         String finderMethodName = "findByUserTrackerId";
263         String[] finderParams = new String[] { Long.class.getName() };
264         Object[] finderArgs = new Object[] { new Long(userTrackerId) };
265 
266         Object result = null;
267 
268         if (finderClassNameCacheEnabled) {
269             result = FinderCacheUtil.getResult(finderClassName,
270                     finderMethodName, finderParams, finderArgs, this);
271         }
272 
273         if (result == null) {
274             Session session = null;
275 
276             try {
277                 session = openSession();
278 
279                 StringBuilder query = new StringBuilder();
280 
281                 query.append(
282                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
283 
284                 query.append("userTrackerId = ?");
285 
286                 query.append(" ");
287 
288                 Query q = session.createQuery(query.toString());
289 
290                 QueryPos qPos = QueryPos.getInstance(q);
291 
292                 qPos.add(userTrackerId);
293 
294                 List<UserTrackerPath> list = q.list();
295 
296                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
297                     finderClassName, finderMethodName, finderParams,
298                     finderArgs, list);
299 
300                 return list;
301             }
302             catch (Exception e) {
303                 throw processException(e);
304             }
305             finally {
306                 closeSession(session);
307             }
308         }
309         else {
310             return (List<UserTrackerPath>)result;
311         }
312     }
313 
314     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId,
315         int start, int end) throws SystemException {
316         return findByUserTrackerId(userTrackerId, start, end, null);
317     }
318 
319     public List<UserTrackerPath> findByUserTrackerId(long userTrackerId,
320         int start, int end, OrderByComparator obc) throws SystemException {
321         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
322         String finderClassName = UserTrackerPath.class.getName();
323         String finderMethodName = "findByUserTrackerId";
324         String[] finderParams = new String[] {
325                 Long.class.getName(),
326                 
327                 "java.lang.Integer", "java.lang.Integer",
328                 "com.liferay.portal.kernel.util.OrderByComparator"
329             };
330         Object[] finderArgs = new Object[] {
331                 new Long(userTrackerId),
332                 
333                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
334             };
335 
336         Object result = null;
337 
338         if (finderClassNameCacheEnabled) {
339             result = FinderCacheUtil.getResult(finderClassName,
340                     finderMethodName, finderParams, finderArgs, this);
341         }
342 
343         if (result == null) {
344             Session session = null;
345 
346             try {
347                 session = openSession();
348 
349                 StringBuilder query = new StringBuilder();
350 
351                 query.append(
352                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
353 
354                 query.append("userTrackerId = ?");
355 
356                 query.append(" ");
357 
358                 if (obc != null) {
359                     query.append("ORDER BY ");
360                     query.append(obc.getOrderBy());
361                 }
362 
363                 Query q = session.createQuery(query.toString());
364 
365                 QueryPos qPos = QueryPos.getInstance(q);
366 
367                 qPos.add(userTrackerId);
368 
369                 List<UserTrackerPath> list = (List<UserTrackerPath>)QueryUtil.list(q,
370                         getDialect(), start, end);
371 
372                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
373                     finderClassName, finderMethodName, finderParams,
374                     finderArgs, list);
375 
376                 return list;
377             }
378             catch (Exception e) {
379                 throw processException(e);
380             }
381             finally {
382                 closeSession(session);
383             }
384         }
385         else {
386             return (List<UserTrackerPath>)result;
387         }
388     }
389 
390     public UserTrackerPath findByUserTrackerId_First(long userTrackerId,
391         OrderByComparator obc)
392         throws NoSuchUserTrackerPathException, SystemException {
393         List<UserTrackerPath> list = findByUserTrackerId(userTrackerId, 0, 1,
394                 obc);
395 
396         if (list.size() == 0) {
397             StringBuilder msg = new StringBuilder();
398 
399             msg.append("No UserTrackerPath exists with the key {");
400 
401             msg.append("userTrackerId=" + userTrackerId);
402 
403             msg.append(StringPool.CLOSE_CURLY_BRACE);
404 
405             throw new NoSuchUserTrackerPathException(msg.toString());
406         }
407         else {
408             return list.get(0);
409         }
410     }
411 
412     public UserTrackerPath findByUserTrackerId_Last(long userTrackerId,
413         OrderByComparator obc)
414         throws NoSuchUserTrackerPathException, SystemException {
415         int count = countByUserTrackerId(userTrackerId);
416 
417         List<UserTrackerPath> list = findByUserTrackerId(userTrackerId,
418                 count - 1, count, obc);
419 
420         if (list.size() == 0) {
421             StringBuilder msg = new StringBuilder();
422 
423             msg.append("No UserTrackerPath exists with the key {");
424 
425             msg.append("userTrackerId=" + userTrackerId);
426 
427             msg.append(StringPool.CLOSE_CURLY_BRACE);
428 
429             throw new NoSuchUserTrackerPathException(msg.toString());
430         }
431         else {
432             return list.get(0);
433         }
434     }
435 
436     public UserTrackerPath[] findByUserTrackerId_PrevAndNext(
437         long userTrackerPathId, long userTrackerId, OrderByComparator obc)
438         throws NoSuchUserTrackerPathException, SystemException {
439         UserTrackerPath userTrackerPath = findByPrimaryKey(userTrackerPathId);
440 
441         int count = countByUserTrackerId(userTrackerId);
442 
443         Session session = null;
444 
445         try {
446             session = openSession();
447 
448             StringBuilder query = new StringBuilder();
449 
450             query.append("FROM com.liferay.portal.model.UserTrackerPath WHERE ");
451 
452             query.append("userTrackerId = ?");
453 
454             query.append(" ");
455 
456             if (obc != null) {
457                 query.append("ORDER BY ");
458                 query.append(obc.getOrderBy());
459             }
460 
461             Query q = session.createQuery(query.toString());
462 
463             QueryPos qPos = QueryPos.getInstance(q);
464 
465             qPos.add(userTrackerId);
466 
467             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
468                     userTrackerPath);
469 
470             UserTrackerPath[] array = new UserTrackerPathImpl[3];
471 
472             array[0] = (UserTrackerPath)objArray[0];
473             array[1] = (UserTrackerPath)objArray[1];
474             array[2] = (UserTrackerPath)objArray[2];
475 
476             return array;
477         }
478         catch (Exception e) {
479             throw processException(e);
480         }
481         finally {
482             closeSession(session);
483         }
484     }
485 
486     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
487         throws SystemException {
488         Session session = null;
489 
490         try {
491             session = openSession();
492 
493             dynamicQuery.compile(session);
494 
495             return dynamicQuery.list();
496         }
497         catch (Exception e) {
498             throw processException(e);
499         }
500         finally {
501             closeSession(session);
502         }
503     }
504 
505     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
506         int start, int end) throws SystemException {
507         Session session = null;
508 
509         try {
510             session = openSession();
511 
512             dynamicQuery.setLimit(start, end);
513 
514             dynamicQuery.compile(session);
515 
516             return dynamicQuery.list();
517         }
518         catch (Exception e) {
519             throw processException(e);
520         }
521         finally {
522             closeSession(session);
523         }
524     }
525 
526     public List<UserTrackerPath> findAll() throws SystemException {
527         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
528     }
529 
530     public List<UserTrackerPath> findAll(int start, int end)
531         throws SystemException {
532         return findAll(start, end, null);
533     }
534 
535     public List<UserTrackerPath> findAll(int start, int end,
536         OrderByComparator obc) throws SystemException {
537         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
538         String finderClassName = UserTrackerPath.class.getName();
539         String finderMethodName = "findAll";
540         String[] finderParams = new String[] {
541                 "java.lang.Integer", "java.lang.Integer",
542                 "com.liferay.portal.kernel.util.OrderByComparator"
543             };
544         Object[] finderArgs = new Object[] {
545                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
546             };
547 
548         Object result = null;
549 
550         if (finderClassNameCacheEnabled) {
551             result = FinderCacheUtil.getResult(finderClassName,
552                     finderMethodName, finderParams, finderArgs, this);
553         }
554 
555         if (result == null) {
556             Session session = null;
557 
558             try {
559                 session = openSession();
560 
561                 StringBuilder query = new StringBuilder();
562 
563                 query.append("FROM com.liferay.portal.model.UserTrackerPath ");
564 
565                 if (obc != null) {
566                     query.append("ORDER BY ");
567                     query.append(obc.getOrderBy());
568                 }
569 
570                 Query q = session.createQuery(query.toString());
571 
572                 List<UserTrackerPath> list = null;
573 
574                 if (obc == null) {
575                     list = (List<UserTrackerPath>)QueryUtil.list(q,
576                             getDialect(), start, end, false);
577 
578                     Collections.sort(list);
579                 }
580                 else {
581                     list = (List<UserTrackerPath>)QueryUtil.list(q,
582                             getDialect(), start, end);
583                 }
584 
585                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
586                     finderClassName, finderMethodName, finderParams,
587                     finderArgs, list);
588 
589                 return list;
590             }
591             catch (Exception e) {
592                 throw processException(e);
593             }
594             finally {
595                 closeSession(session);
596             }
597         }
598         else {
599             return (List<UserTrackerPath>)result;
600         }
601     }
602 
603     public void removeByUserTrackerId(long userTrackerId)
604         throws SystemException {
605         for (UserTrackerPath userTrackerPath : findByUserTrackerId(
606                 userTrackerId)) {
607             remove(userTrackerPath);
608         }
609     }
610 
611     public void removeAll() throws SystemException {
612         for (UserTrackerPath userTrackerPath : findAll()) {
613             remove(userTrackerPath);
614         }
615     }
616 
617     public int countByUserTrackerId(long userTrackerId)
618         throws SystemException {
619         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
620         String finderClassName = UserTrackerPath.class.getName();
621         String finderMethodName = "countByUserTrackerId";
622         String[] finderParams = new String[] { Long.class.getName() };
623         Object[] finderArgs = new Object[] { new Long(userTrackerId) };
624 
625         Object result = null;
626 
627         if (finderClassNameCacheEnabled) {
628             result = FinderCacheUtil.getResult(finderClassName,
629                     finderMethodName, finderParams, finderArgs, this);
630         }
631 
632         if (result == null) {
633             Session session = null;
634 
635             try {
636                 session = openSession();
637 
638                 StringBuilder query = new StringBuilder();
639 
640                 query.append("SELECT COUNT(*) ");
641                 query.append(
642                     "FROM com.liferay.portal.model.UserTrackerPath WHERE ");
643 
644                 query.append("userTrackerId = ?");
645 
646                 query.append(" ");
647 
648                 Query q = session.createQuery(query.toString());
649 
650                 QueryPos qPos = QueryPos.getInstance(q);
651 
652                 qPos.add(userTrackerId);
653 
654                 Long count = null;
655 
656                 Iterator<Long> itr = q.list().iterator();
657 
658                 if (itr.hasNext()) {
659                     count = itr.next();
660                 }
661 
662                 if (count == null) {
663                     count = new Long(0);
664                 }
665 
666                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
667                     finderClassName, finderMethodName, finderParams,
668                     finderArgs, count);
669 
670                 return count.intValue();
671             }
672             catch (Exception e) {
673                 throw processException(e);
674             }
675             finally {
676                 closeSession(session);
677             }
678         }
679         else {
680             return ((Long)result).intValue();
681         }
682     }
683 
684     public int countAll() throws SystemException {
685         boolean finderClassNameCacheEnabled = UserTrackerPathModelImpl.CACHE_ENABLED;
686         String finderClassName = UserTrackerPath.class.getName();
687         String finderMethodName = "countAll";
688         String[] finderParams = new String[] {  };
689         Object[] finderArgs = new Object[] {  };
690 
691         Object result = null;
692 
693         if (finderClassNameCacheEnabled) {
694             result = FinderCacheUtil.getResult(finderClassName,
695                     finderMethodName, finderParams, finderArgs, this);
696         }
697 
698         if (result == null) {
699             Session session = null;
700 
701             try {
702                 session = openSession();
703 
704                 Query q = session.createQuery(
705                         "SELECT COUNT(*) FROM com.liferay.portal.model.UserTrackerPath");
706 
707                 Long count = null;
708 
709                 Iterator<Long> itr = q.list().iterator();
710 
711                 if (itr.hasNext()) {
712                     count = itr.next();
713                 }
714 
715                 if (count == null) {
716                     count = new Long(0);
717                 }
718 
719                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
720                     finderClassName, finderMethodName, finderParams,
721                     finderArgs, count);
722 
723                 return count.intValue();
724             }
725             catch (Exception e) {
726                 throw processException(e);
727             }
728             finally {
729                 closeSession(session);
730             }
731         }
732         else {
733             return ((Long)result).intValue();
734         }
735     }
736 
737     public void afterPropertiesSet() {
738         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
739                     com.liferay.portal.util.PropsUtil.get(
740                         "value.object.listener.com.liferay.portal.model.UserTrackerPath")));
741 
742         if (listenerClassNames.length > 0) {
743             try {
744                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
745 
746                 for (String listenerClassName : listenerClassNames) {
747                     listenersList.add((ModelListener)Class.forName(
748                             listenerClassName).newInstance());
749                 }
750 
751                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
752             }
753             catch (Exception e) {
754                 _log.error(e);
755             }
756         }
757     }
758 
759     private static Log _log = LogFactoryUtil.getLog(UserTrackerPathPersistenceImpl.class);
760 }