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