1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchOrgLaborException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
25  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
26  import com.liferay.portal.kernel.dao.orm.Query;
27  import com.liferay.portal.kernel.dao.orm.QueryPos;
28  import com.liferay.portal.kernel.dao.orm.QueryUtil;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.model.OrgLabor;
38  import com.liferay.portal.model.impl.OrgLaborImpl;
39  import com.liferay.portal.model.impl.OrgLaborModelImpl;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   * <a href="OrgLaborPersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class OrgLaborPersistenceImpl extends BasePersistenceImpl
54      implements OrgLaborPersistence {
55      public OrgLabor create(long orgLaborId) {
56          OrgLabor orgLabor = new OrgLaborImpl();
57  
58          orgLabor.setNew(true);
59          orgLabor.setPrimaryKey(orgLaborId);
60  
61          return orgLabor;
62      }
63  
64      public OrgLabor remove(long orgLaborId)
65          throws NoSuchOrgLaborException, SystemException {
66          Session session = null;
67  
68          try {
69              session = openSession();
70  
71              OrgLabor orgLabor = (OrgLabor)session.get(OrgLaborImpl.class,
72                      new Long(orgLaborId));
73  
74              if (orgLabor == null) {
75                  if (_log.isWarnEnabled()) {
76                      _log.warn("No OrgLabor exists with the primary key " +
77                          orgLaborId);
78                  }
79  
80                  throw new NoSuchOrgLaborException(
81                      "No OrgLabor exists with the primary key " + orgLaborId);
82              }
83  
84              return remove(orgLabor);
85          }
86          catch (NoSuchOrgLaborException nsee) {
87              throw nsee;
88          }
89          catch (Exception e) {
90              throw processException(e);
91          }
92          finally {
93              closeSession(session);
94          }
95      }
96  
97      public OrgLabor remove(OrgLabor orgLabor) throws SystemException {
98          for (ModelListener listener : listeners) {
99              listener.onBeforeRemove(orgLabor);
100         }
101 
102         orgLabor = removeImpl(orgLabor);
103 
104         for (ModelListener listener : listeners) {
105             listener.onAfterRemove(orgLabor);
106         }
107 
108         return orgLabor;
109     }
110 
111     protected OrgLabor removeImpl(OrgLabor orgLabor) throws SystemException {
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             if (BatchSessionUtil.isEnabled()) {
118                 Object staleObject = session.get(OrgLaborImpl.class,
119                         orgLabor.getPrimaryKeyObj());
120 
121                 if (staleObject != null) {
122                     session.evict(staleObject);
123                 }
124             }
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         for (ModelListener listener : listeners) {
172             if (isNew) {
173                 listener.onBeforeCreate(orgLabor);
174             }
175             else {
176                 listener.onBeforeUpdate(orgLabor);
177             }
178         }
179 
180         orgLabor = updateImpl(orgLabor, merge);
181 
182         for (ModelListener listener : listeners) {
183             if (isNew) {
184                 listener.onAfterCreate(orgLabor);
185             }
186             else {
187                 listener.onAfterUpdate(orgLabor);
188             }
189         }
190 
191         return orgLabor;
192     }
193 
194     public OrgLabor updateImpl(com.liferay.portal.model.OrgLabor orgLabor,
195         boolean merge) throws SystemException {
196         Session session = null;
197 
198         try {
199             session = openSession();
200 
201             BatchSessionUtil.update(session, orgLabor, merge);
202 
203             orgLabor.setNew(false);
204 
205             return orgLabor;
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212 
213             FinderCacheUtil.clearCache(OrgLabor.class.getName());
214         }
215     }
216 
217     public OrgLabor findByPrimaryKey(long orgLaborId)
218         throws NoSuchOrgLaborException, SystemException {
219         OrgLabor orgLabor = fetchByPrimaryKey(orgLaborId);
220 
221         if (orgLabor == null) {
222             if (_log.isWarnEnabled()) {
223                 _log.warn("No OrgLabor exists with the primary key " +
224                     orgLaborId);
225             }
226 
227             throw new NoSuchOrgLaborException(
228                 "No OrgLabor exists with the primary key " + orgLaborId);
229         }
230 
231         return orgLabor;
232     }
233 
234     public OrgLabor fetchByPrimaryKey(long orgLaborId)
235         throws SystemException {
236         Session session = null;
237 
238         try {
239             session = openSession();
240 
241             return (OrgLabor)session.get(OrgLaborImpl.class,
242                 new Long(orgLaborId));
243         }
244         catch (Exception e) {
245             throw processException(e);
246         }
247         finally {
248             closeSession(session);
249         }
250     }
251 
252     public List<OrgLabor> findByOrganizationId(long organizationId)
253         throws SystemException {
254         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
255         String finderClassName = OrgLabor.class.getName();
256         String finderMethodName = "findByOrganizationId";
257         String[] finderParams = new String[] { Long.class.getName() };
258         Object[] finderArgs = new Object[] { new Long(organizationId) };
259 
260         Object result = null;
261 
262         if (finderClassNameCacheEnabled) {
263             result = FinderCacheUtil.getResult(finderClassName,
264                     finderMethodName, finderParams, finderArgs, this);
265         }
266 
267         if (result == null) {
268             Session session = null;
269 
270             try {
271                 session = openSession();
272 
273                 StringBuilder query = new StringBuilder();
274 
275                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
276 
277                 query.append("organizationId = ?");
278 
279                 query.append(" ");
280 
281                 query.append("ORDER BY ");
282 
283                 query.append("organizationId ASC, ");
284                 query.append("typeId ASC");
285 
286                 Query q = session.createQuery(query.toString());
287 
288                 QueryPos qPos = QueryPos.getInstance(q);
289 
290                 qPos.add(organizationId);
291 
292                 List<OrgLabor> list = q.list();
293 
294                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
295                     finderClassName, finderMethodName, finderParams,
296                     finderArgs, list);
297 
298                 return list;
299             }
300             catch (Exception e) {
301                 throw processException(e);
302             }
303             finally {
304                 closeSession(session);
305             }
306         }
307         else {
308             return (List<OrgLabor>)result;
309         }
310     }
311 
312     public List<OrgLabor> findByOrganizationId(long organizationId, int start,
313         int end) throws SystemException {
314         return findByOrganizationId(organizationId, start, end, null);
315     }
316 
317     public List<OrgLabor> findByOrganizationId(long organizationId, int start,
318         int end, OrderByComparator obc) throws SystemException {
319         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
320         String finderClassName = OrgLabor.class.getName();
321         String finderMethodName = "findByOrganizationId";
322         String[] finderParams = new String[] {
323                 Long.class.getName(),
324                 
325                 "java.lang.Integer", "java.lang.Integer",
326                 "com.liferay.portal.kernel.util.OrderByComparator"
327             };
328         Object[] finderArgs = new Object[] {
329                 new Long(organizationId),
330                 
331                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
332             };
333 
334         Object result = null;
335 
336         if (finderClassNameCacheEnabled) {
337             result = FinderCacheUtil.getResult(finderClassName,
338                     finderMethodName, finderParams, finderArgs, this);
339         }
340 
341         if (result == null) {
342             Session session = null;
343 
344             try {
345                 session = openSession();
346 
347                 StringBuilder query = new StringBuilder();
348 
349                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
350 
351                 query.append("organizationId = ?");
352 
353                 query.append(" ");
354 
355                 if (obc != null) {
356                     query.append("ORDER BY ");
357                     query.append(obc.getOrderBy());
358                 }
359 
360                 else {
361                     query.append("ORDER BY ");
362 
363                     query.append("organizationId ASC, ");
364                     query.append("typeId ASC");
365                 }
366 
367                 Query q = session.createQuery(query.toString());
368 
369                 QueryPos qPos = QueryPos.getInstance(q);
370 
371                 qPos.add(organizationId);
372 
373                 List<OrgLabor> list = (List<OrgLabor>)QueryUtil.list(q,
374                         getDialect(), start, end);
375 
376                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
377                     finderClassName, finderMethodName, finderParams,
378                     finderArgs, list);
379 
380                 return list;
381             }
382             catch (Exception e) {
383                 throw processException(e);
384             }
385             finally {
386                 closeSession(session);
387             }
388         }
389         else {
390             return (List<OrgLabor>)result;
391         }
392     }
393 
394     public OrgLabor findByOrganizationId_First(long organizationId,
395         OrderByComparator obc) throws NoSuchOrgLaborException, SystemException {
396         List<OrgLabor> list = findByOrganizationId(organizationId, 0, 1, obc);
397 
398         if (list.size() == 0) {
399             StringBuilder msg = new StringBuilder();
400 
401             msg.append("No OrgLabor exists with the key {");
402 
403             msg.append("organizationId=" + organizationId);
404 
405             msg.append(StringPool.CLOSE_CURLY_BRACE);
406 
407             throw new NoSuchOrgLaborException(msg.toString());
408         }
409         else {
410             return list.get(0);
411         }
412     }
413 
414     public OrgLabor findByOrganizationId_Last(long organizationId,
415         OrderByComparator obc) throws NoSuchOrgLaborException, SystemException {
416         int count = countByOrganizationId(organizationId);
417 
418         List<OrgLabor> list = findByOrganizationId(organizationId, count - 1,
419                 count, obc);
420 
421         if (list.size() == 0) {
422             StringBuilder msg = new StringBuilder();
423 
424             msg.append("No OrgLabor exists with the key {");
425 
426             msg.append("organizationId=" + organizationId);
427 
428             msg.append(StringPool.CLOSE_CURLY_BRACE);
429 
430             throw new NoSuchOrgLaborException(msg.toString());
431         }
432         else {
433             return list.get(0);
434         }
435     }
436 
437     public OrgLabor[] findByOrganizationId_PrevAndNext(long orgLaborId,
438         long organizationId, OrderByComparator obc)
439         throws NoSuchOrgLaborException, SystemException {
440         OrgLabor orgLabor = findByPrimaryKey(orgLaborId);
441 
442         int count = countByOrganizationId(organizationId);
443 
444         Session session = null;
445 
446         try {
447             session = openSession();
448 
449             StringBuilder query = new StringBuilder();
450 
451             query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
452 
453             query.append("organizationId = ?");
454 
455             query.append(" ");
456 
457             if (obc != null) {
458                 query.append("ORDER BY ");
459                 query.append(obc.getOrderBy());
460             }
461 
462             else {
463                 query.append("ORDER BY ");
464 
465                 query.append("organizationId ASC, ");
466                 query.append("typeId ASC");
467             }
468 
469             Query q = session.createQuery(query.toString());
470 
471             QueryPos qPos = QueryPos.getInstance(q);
472 
473             qPos.add(organizationId);
474 
475             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, orgLabor);
476 
477             OrgLabor[] array = new OrgLaborImpl[3];
478 
479             array[0] = (OrgLabor)objArray[0];
480             array[1] = (OrgLabor)objArray[1];
481             array[2] = (OrgLabor)objArray[2];
482 
483             return array;
484         }
485         catch (Exception e) {
486             throw processException(e);
487         }
488         finally {
489             closeSession(session);
490         }
491     }
492 
493     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
494         throws SystemException {
495         Session session = null;
496 
497         try {
498             session = openSession();
499 
500             dynamicQuery.compile(session);
501 
502             return dynamicQuery.list();
503         }
504         catch (Exception e) {
505             throw processException(e);
506         }
507         finally {
508             closeSession(session);
509         }
510     }
511 
512     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
513         int start, int end) throws SystemException {
514         Session session = null;
515 
516         try {
517             session = openSession();
518 
519             dynamicQuery.setLimit(start, end);
520 
521             dynamicQuery.compile(session);
522 
523             return dynamicQuery.list();
524         }
525         catch (Exception e) {
526             throw processException(e);
527         }
528         finally {
529             closeSession(session);
530         }
531     }
532 
533     public List<OrgLabor> findAll() throws SystemException {
534         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
535     }
536 
537     public List<OrgLabor> findAll(int start, int end) throws SystemException {
538         return findAll(start, end, null);
539     }
540 
541     public List<OrgLabor> findAll(int start, int end, OrderByComparator obc)
542         throws SystemException {
543         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
544         String finderClassName = OrgLabor.class.getName();
545         String finderMethodName = "findAll";
546         String[] finderParams = new String[] {
547                 "java.lang.Integer", "java.lang.Integer",
548                 "com.liferay.portal.kernel.util.OrderByComparator"
549             };
550         Object[] finderArgs = new Object[] {
551                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
552             };
553 
554         Object result = null;
555 
556         if (finderClassNameCacheEnabled) {
557             result = FinderCacheUtil.getResult(finderClassName,
558                     finderMethodName, finderParams, finderArgs, this);
559         }
560 
561         if (result == null) {
562             Session session = null;
563 
564             try {
565                 session = openSession();
566 
567                 StringBuilder query = new StringBuilder();
568 
569                 query.append("FROM com.liferay.portal.model.OrgLabor ");
570 
571                 if (obc != null) {
572                     query.append("ORDER BY ");
573                     query.append(obc.getOrderBy());
574                 }
575 
576                 else {
577                     query.append("ORDER BY ");
578 
579                     query.append("organizationId ASC, ");
580                     query.append("typeId ASC");
581                 }
582 
583                 Query q = session.createQuery(query.toString());
584 
585                 List<OrgLabor> list = null;
586 
587                 if (obc == null) {
588                     list = (List<OrgLabor>)QueryUtil.list(q, getDialect(),
589                             start, end, false);
590 
591                     Collections.sort(list);
592                 }
593                 else {
594                     list = (List<OrgLabor>)QueryUtil.list(q, getDialect(),
595                             start, end);
596                 }
597 
598                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
599                     finderClassName, finderMethodName, finderParams,
600                     finderArgs, list);
601 
602                 return list;
603             }
604             catch (Exception e) {
605                 throw processException(e);
606             }
607             finally {
608                 closeSession(session);
609             }
610         }
611         else {
612             return (List<OrgLabor>)result;
613         }
614     }
615 
616     public void removeByOrganizationId(long organizationId)
617         throws SystemException {
618         for (OrgLabor orgLabor : findByOrganizationId(organizationId)) {
619             remove(orgLabor);
620         }
621     }
622 
623     public void removeAll() throws SystemException {
624         for (OrgLabor orgLabor : findAll()) {
625             remove(orgLabor);
626         }
627     }
628 
629     public int countByOrganizationId(long organizationId)
630         throws SystemException {
631         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
632         String finderClassName = OrgLabor.class.getName();
633         String finderMethodName = "countByOrganizationId";
634         String[] finderParams = new String[] { Long.class.getName() };
635         Object[] finderArgs = new Object[] { new Long(organizationId) };
636 
637         Object result = null;
638 
639         if (finderClassNameCacheEnabled) {
640             result = FinderCacheUtil.getResult(finderClassName,
641                     finderMethodName, finderParams, finderArgs, this);
642         }
643 
644         if (result == null) {
645             Session session = null;
646 
647             try {
648                 session = openSession();
649 
650                 StringBuilder query = new StringBuilder();
651 
652                 query.append("SELECT COUNT(*) ");
653                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
654 
655                 query.append("organizationId = ?");
656 
657                 query.append(" ");
658 
659                 Query q = session.createQuery(query.toString());
660 
661                 QueryPos qPos = QueryPos.getInstance(q);
662 
663                 qPos.add(organizationId);
664 
665                 Long count = null;
666 
667                 Iterator<Long> itr = q.list().iterator();
668 
669                 if (itr.hasNext()) {
670                     count = itr.next();
671                 }
672 
673                 if (count == null) {
674                     count = new Long(0);
675                 }
676 
677                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
678                     finderClassName, finderMethodName, finderParams,
679                     finderArgs, count);
680 
681                 return count.intValue();
682             }
683             catch (Exception e) {
684                 throw processException(e);
685             }
686             finally {
687                 closeSession(session);
688             }
689         }
690         else {
691             return ((Long)result).intValue();
692         }
693     }
694 
695     public int countAll() throws SystemException {
696         boolean finderClassNameCacheEnabled = OrgLaborModelImpl.CACHE_ENABLED;
697         String finderClassName = OrgLabor.class.getName();
698         String finderMethodName = "countAll";
699         String[] finderParams = new String[] {  };
700         Object[] finderArgs = new Object[] {  };
701 
702         Object result = null;
703 
704         if (finderClassNameCacheEnabled) {
705             result = FinderCacheUtil.getResult(finderClassName,
706                     finderMethodName, finderParams, finderArgs, this);
707         }
708 
709         if (result == null) {
710             Session session = null;
711 
712             try {
713                 session = openSession();
714 
715                 Query q = session.createQuery(
716                         "SELECT COUNT(*) FROM com.liferay.portal.model.OrgLabor");
717 
718                 Long count = null;
719 
720                 Iterator<Long> itr = q.list().iterator();
721 
722                 if (itr.hasNext()) {
723                     count = itr.next();
724                 }
725 
726                 if (count == null) {
727                     count = new Long(0);
728                 }
729 
730                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
731                     finderClassName, finderMethodName, finderParams,
732                     finderArgs, count);
733 
734                 return count.intValue();
735             }
736             catch (Exception e) {
737                 throw processException(e);
738             }
739             finally {
740                 closeSession(session);
741             }
742         }
743         else {
744             return ((Long)result).intValue();
745         }
746     }
747 
748     public void afterPropertiesSet() {
749         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
750                     com.liferay.portal.util.PropsUtil.get(
751                         "value.object.listener.com.liferay.portal.model.OrgLabor")));
752 
753         if (listenerClassNames.length > 0) {
754             try {
755                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
756 
757                 for (String listenerClassName : listenerClassNames) {
758                     listenersList.add((ModelListener)Class.forName(
759                             listenerClassName).newInstance());
760                 }
761 
762                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
763             }
764             catch (Exception e) {
765                 _log.error(e);
766             }
767         }
768     }
769 
770     private static Log _log = LogFactoryUtil.getLog(OrgLaborPersistenceImpl.class);
771 }