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