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