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