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             if (BatchSessionUtil.isEnabled()) {
127                 Object staleObject = session.get(OrgLaborImpl.class,
128                         orgLabor.getPrimaryKeyObj());
129 
130                 if (staleObject != null) {
131                     session.evict(staleObject);
132                 }
133             }
134 
135             session.delete(orgLabor);
136 
137             session.flush();
138 
139             return orgLabor;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(OrgLabor.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(OrgLabor orgLabor, boolean merge)</code>.
153      */
154     public OrgLabor update(OrgLabor orgLabor) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(OrgLabor orgLabor) method. Use update(OrgLabor orgLabor, boolean merge) instead.");
158         }
159 
160         return update(orgLabor, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        orgLabor the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when orgLabor is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public OrgLabor update(OrgLabor orgLabor, boolean merge)
177         throws SystemException {
178         boolean isNew = orgLabor.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(orgLabor);
184                 }
185                 else {
186                     listener.onBeforeUpdate(orgLabor);
187                 }
188             }
189         }
190 
191         orgLabor = updateImpl(orgLabor, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(orgLabor);
197                 }
198                 else {
199                     listener.onAfterUpdate(orgLabor);
200                 }
201             }
202         }
203 
204         return orgLabor;
205     }
206 
207     public OrgLabor updateImpl(com.liferay.portal.model.OrgLabor orgLabor,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             BatchSessionUtil.update(session, orgLabor, merge);
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 = null;
599 
600                 if (obc == null) {
601                     list = (List<OrgLabor>)QueryUtil.list(q, getDialect(),
602                             start, end, false);
603 
604                     Collections.sort(list);
605                 }
606                 else {
607                     list = (List<OrgLabor>)QueryUtil.list(q, getDialect(),
608                             start, end);
609                 }
610 
611                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
612                     finderClassName, finderMethodName, finderParams,
613                     finderArgs, list);
614 
615                 return list;
616             }
617             catch (Exception e) {
618                 throw processException(e);
619             }
620             finally {
621                 closeSession(session);
622             }
623         }
624         else {
625             return (List<OrgLabor>)result;
626         }
627     }
628 
629     public void removeByOrganizationId(long organizationId)
630         throws SystemException {
631         for (OrgLabor orgLabor : findByOrganizationId(organizationId)) {
632             remove(orgLabor);
633         }
634     }
635 
636     public void removeAll() throws SystemException {
637         for (OrgLabor orgLabor : findAll()) {
638             remove(orgLabor);
639         }
640     }
641 
642     public int countByOrganizationId(long organizationId)
643         throws SystemException {
644         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
645         String finderClassName = OrgLabor.class.getName();
646         String finderMethodName = "countByOrganizationId";
647         String[] finderParams = new String[] { Long.class.getName() };
648         Object[] finderArgs = new Object[] { new Long(organizationId) };
649 
650         Object result = null;
651 
652         if (finderClassNameCacheEnabled) {
653             result = FinderCacheUtil.getResult(finderClassName,
654                     finderMethodName, finderParams, finderArgs, this);
655         }
656 
657         if (result == null) {
658             Session session = null;
659 
660             try {
661                 session = openSession();
662 
663                 StringBuilder query = new StringBuilder();
664 
665                 query.append("SELECT COUNT(*) ");
666                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
667 
668                 query.append("organizationId = ?");
669 
670                 query.append(" ");
671 
672                 Query q = session.createQuery(query.toString());
673 
674                 QueryPos qPos = QueryPos.getInstance(q);
675 
676                 qPos.add(organizationId);
677 
678                 Long count = null;
679 
680                 Iterator<Long> itr = q.list().iterator();
681 
682                 if (itr.hasNext()) {
683                     count = itr.next();
684                 }
685 
686                 if (count == null) {
687                     count = new Long(0);
688                 }
689 
690                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
691                     finderClassName, finderMethodName, finderParams,
692                     finderArgs, count);
693 
694                 return count.intValue();
695             }
696             catch (Exception e) {
697                 throw processException(e);
698             }
699             finally {
700                 closeSession(session);
701             }
702         }
703         else {
704             return ((Long)result).intValue();
705         }
706     }
707 
708     public int countAll() throws SystemException {
709         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
710         String finderClassName = OrgLabor.class.getName();
711         String finderMethodName = "countAll";
712         String[] finderParams = new String[] {  };
713         Object[] finderArgs = new Object[] {  };
714 
715         Object result = null;
716 
717         if (finderClassNameCacheEnabled) {
718             result = FinderCacheUtil.getResult(finderClassName,
719                     finderMethodName, finderParams, finderArgs, this);
720         }
721 
722         if (result == null) {
723             Session session = null;
724 
725             try {
726                 session = openSession();
727 
728                 Query q = session.createQuery(
729                         "SELECT COUNT(*) FROM com.liferay.portal.model.OrgLabor");
730 
731                 Long count = null;
732 
733                 Iterator<Long> itr = q.list().iterator();
734 
735                 if (itr.hasNext()) {
736                     count = itr.next();
737                 }
738 
739                 if (count == null) {
740                     count = new Long(0);
741                 }
742 
743                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
744                     finderClassName, finderMethodName, finderParams,
745                     finderArgs, count);
746 
747                 return count.intValue();
748             }
749             catch (Exception e) {
750                 throw processException(e);
751             }
752             finally {
753                 closeSession(session);
754             }
755         }
756         else {
757             return ((Long)result).intValue();
758         }
759     }
760 
761     public void registerListener(ModelListener listener) {
762         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
763 
764         listeners.add(listener);
765 
766         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
767     }
768 
769     public void unregisterListener(ModelListener listener) {
770         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
771 
772         listeners.remove(listener);
773 
774         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
775     }
776 
777     public void afterPropertiesSet() {
778         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
779                     com.liferay.portal.util.PropsUtil.get(
780                         "value.object.listener.com.liferay.portal.model.OrgLabor")));
781 
782         if (listenerClassNames.length > 0) {
783             try {
784                 List<ModelListener> listeners = new ArrayList<ModelListener>();
785 
786                 for (String listenerClassName : listenerClassNames) {
787                     listeners.add((ModelListener)Class.forName(
788                             listenerClassName).newInstance());
789                 }
790 
791                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
792             }
793             catch (Exception e) {
794                 _log.error(e);
795             }
796         }
797     }
798 
799     private static Log _log = LogFactory.getLog(OrgLaborPersistenceImpl.class);
800     private ModelListener[] _listeners = new ModelListener[0];
801 }