1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchOrgLaborException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.OrgLabor;
33  import com.liferay.portal.model.impl.OrgLaborImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="OrgLaborPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class OrgLaborPersistenceImpl extends BasePersistence
57      implements OrgLaborPersistence {
58      public OrgLabor create(long orgLaborId) {
59          OrgLabor orgLabor = new OrgLaborImpl();
60          orgLabor.setNew(true);
61          orgLabor.setPrimaryKey(orgLaborId);
62  
63          return orgLabor;
64      }
65  
66      public OrgLabor remove(long orgLaborId)
67          throws NoSuchOrgLaborException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              OrgLabor orgLabor = (OrgLabor)session.get(OrgLaborImpl.class,
74                      new Long(orgLaborId));
75  
76              if (orgLabor == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No OrgLabor exists with the primary key " +
79                          orgLaborId);
80                  }
81  
82                  throw new NoSuchOrgLaborException(
83                      "No OrgLabor exists with the primary key " + orgLaborId);
84              }
85  
86              return remove(orgLabor);
87          }
88          catch (NoSuchOrgLaborException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw HibernateUtil.processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public OrgLabor remove(OrgLabor orgLabor) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(orgLabor);
105             session.flush();
106 
107             return orgLabor;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(OrgLabor.class.getName());
115         }
116     }
117 
118     public OrgLabor update(com.liferay.portal.model.OrgLabor orgLabor)
119         throws SystemException {
120         return update(orgLabor, false);
121     }
122 
123     public OrgLabor update(com.liferay.portal.model.OrgLabor orgLabor,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(orgLabor);
132             }
133             else {
134                 if (orgLabor.isNew()) {
135                     session.save(orgLabor);
136                 }
137             }
138 
139             session.flush();
140             orgLabor.setNew(false);
141 
142             return orgLabor;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(OrgLabor.class.getName());
150         }
151     }
152 
153     public OrgLabor findByPrimaryKey(long orgLaborId)
154         throws NoSuchOrgLaborException, SystemException {
155         OrgLabor orgLabor = fetchByPrimaryKey(orgLaborId);
156 
157         if (orgLabor == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No OrgLabor exists with the primary key " +
160                     orgLaborId);
161             }
162 
163             throw new NoSuchOrgLaborException(
164                 "No OrgLabor exists with the primary key " + orgLaborId);
165         }
166 
167         return orgLabor;
168     }
169 
170     public OrgLabor fetchByPrimaryKey(long orgLaborId)
171         throws SystemException {
172         Session session = null;
173 
174         try {
175             session = openSession();
176 
177             return (OrgLabor)session.get(OrgLaborImpl.class,
178                 new Long(orgLaborId));
179         }
180         catch (Exception e) {
181             throw HibernateUtil.processException(e);
182         }
183         finally {
184             closeSession(session);
185         }
186     }
187 
188     public List findByOrganizationId(long organizationId)
189         throws SystemException {
190         String finderClassName = OrgLabor.class.getName();
191         String finderMethodName = "findByOrganizationId";
192         String[] finderParams = new String[] { Long.class.getName() };
193         Object[] finderArgs = new Object[] { new Long(organizationId) };
194         Object result = FinderCache.getResult(finderClassName,
195                 finderMethodName, finderParams, finderArgs, getSessionFactory());
196 
197         if (result == null) {
198             Session session = null;
199 
200             try {
201                 session = openSession();
202 
203                 StringMaker query = new StringMaker();
204                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
205                 query.append("organizationId = ?");
206                 query.append(" ");
207                 query.append("ORDER BY ");
208                 query.append("organizationId ASC").append(", ");
209                 query.append("typeId ASC");
210 
211                 Query q = session.createQuery(query.toString());
212                 int queryPos = 0;
213                 q.setLong(queryPos++, organizationId);
214 
215                 List list = q.list();
216                 FinderCache.putResult(finderClassName, finderMethodName,
217                     finderParams, finderArgs, list);
218 
219                 return list;
220             }
221             catch (Exception e) {
222                 throw HibernateUtil.processException(e);
223             }
224             finally {
225                 closeSession(session);
226             }
227         }
228         else {
229             return (List)result;
230         }
231     }
232 
233     public List findByOrganizationId(long organizationId, int begin, int end)
234         throws SystemException {
235         return findByOrganizationId(organizationId, begin, end, null);
236     }
237 
238     public List findByOrganizationId(long organizationId, int begin, int end,
239         OrderByComparator obc) throws SystemException {
240         String finderClassName = OrgLabor.class.getName();
241         String finderMethodName = "findByOrganizationId";
242         String[] finderParams = new String[] {
243                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
244                 "com.liferay.portal.kernel.util.OrderByComparator"
245             };
246         Object[] finderArgs = new Object[] {
247                 new Long(organizationId), String.valueOf(begin),
248                 String.valueOf(end), String.valueOf(obc)
249             };
250         Object result = FinderCache.getResult(finderClassName,
251                 finderMethodName, finderParams, finderArgs, getSessionFactory());
252 
253         if (result == null) {
254             Session session = null;
255 
256             try {
257                 session = openSession();
258 
259                 StringMaker query = new StringMaker();
260                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
261                 query.append("organizationId = ?");
262                 query.append(" ");
263 
264                 if (obc != null) {
265                     query.append("ORDER BY ");
266                     query.append(obc.getOrderBy());
267                 }
268                 else {
269                     query.append("ORDER BY ");
270                     query.append("organizationId ASC").append(", ");
271                     query.append("typeId ASC");
272                 }
273 
274                 Query q = session.createQuery(query.toString());
275                 int queryPos = 0;
276                 q.setLong(queryPos++, organizationId);
277 
278                 List list = QueryUtil.list(q, getDialect(), begin, end);
279                 FinderCache.putResult(finderClassName, finderMethodName,
280                     finderParams, finderArgs, list);
281 
282                 return list;
283             }
284             catch (Exception e) {
285                 throw HibernateUtil.processException(e);
286             }
287             finally {
288                 closeSession(session);
289             }
290         }
291         else {
292             return (List)result;
293         }
294     }
295 
296     public OrgLabor findByOrganizationId_First(long organizationId,
297         OrderByComparator obc) throws NoSuchOrgLaborException, SystemException {
298         List list = findByOrganizationId(organizationId, 0, 1, obc);
299 
300         if (list.size() == 0) {
301             StringMaker msg = new StringMaker();
302             msg.append("No OrgLabor exists with the key ");
303             msg.append(StringPool.OPEN_CURLY_BRACE);
304             msg.append("organizationId=");
305             msg.append(organizationId);
306             msg.append(StringPool.CLOSE_CURLY_BRACE);
307             throw new NoSuchOrgLaborException(msg.toString());
308         }
309         else {
310             return (OrgLabor)list.get(0);
311         }
312     }
313 
314     public OrgLabor findByOrganizationId_Last(long organizationId,
315         OrderByComparator obc) throws NoSuchOrgLaborException, SystemException {
316         int count = countByOrganizationId(organizationId);
317         List list = findByOrganizationId(organizationId, count - 1, count, obc);
318 
319         if (list.size() == 0) {
320             StringMaker msg = new StringMaker();
321             msg.append("No OrgLabor exists with the key ");
322             msg.append(StringPool.OPEN_CURLY_BRACE);
323             msg.append("organizationId=");
324             msg.append(organizationId);
325             msg.append(StringPool.CLOSE_CURLY_BRACE);
326             throw new NoSuchOrgLaborException(msg.toString());
327         }
328         else {
329             return (OrgLabor)list.get(0);
330         }
331     }
332 
333     public OrgLabor[] findByOrganizationId_PrevAndNext(long orgLaborId,
334         long organizationId, OrderByComparator obc)
335         throws NoSuchOrgLaborException, SystemException {
336         OrgLabor orgLabor = findByPrimaryKey(orgLaborId);
337         int count = countByOrganizationId(organizationId);
338         Session session = null;
339 
340         try {
341             session = openSession();
342 
343             StringMaker query = new StringMaker();
344             query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
345             query.append("organizationId = ?");
346             query.append(" ");
347 
348             if (obc != null) {
349                 query.append("ORDER BY ");
350                 query.append(obc.getOrderBy());
351             }
352             else {
353                 query.append("ORDER BY ");
354                 query.append("organizationId ASC").append(", ");
355                 query.append("typeId ASC");
356             }
357 
358             Query q = session.createQuery(query.toString());
359             int queryPos = 0;
360             q.setLong(queryPos++, organizationId);
361 
362             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, orgLabor);
363             OrgLabor[] array = new OrgLaborImpl[3];
364             array[0] = (OrgLabor)objArray[0];
365             array[1] = (OrgLabor)objArray[1];
366             array[2] = (OrgLabor)objArray[2];
367 
368             return array;
369         }
370         catch (Exception e) {
371             throw HibernateUtil.processException(e);
372         }
373         finally {
374             closeSession(session);
375         }
376     }
377 
378     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
379         throws SystemException {
380         Session session = null;
381 
382         try {
383             session = openSession();
384 
385             DynamicQuery query = queryInitializer.initialize(session);
386 
387             return query.list();
388         }
389         catch (Exception e) {
390             throw HibernateUtil.processException(e);
391         }
392         finally {
393             closeSession(session);
394         }
395     }
396 
397     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
398         int begin, int end) throws SystemException {
399         Session session = null;
400 
401         try {
402             session = openSession();
403 
404             DynamicQuery query = queryInitializer.initialize(session);
405             query.setLimit(begin, end);
406 
407             return query.list();
408         }
409         catch (Exception e) {
410             throw HibernateUtil.processException(e);
411         }
412         finally {
413             closeSession(session);
414         }
415     }
416 
417     public List findAll() throws SystemException {
418         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
419     }
420 
421     public List findAll(int begin, int end) throws SystemException {
422         return findAll(begin, end, null);
423     }
424 
425     public List findAll(int begin, int end, OrderByComparator obc)
426         throws SystemException {
427         String finderClassName = OrgLabor.class.getName();
428         String finderMethodName = "findAll";
429         String[] finderParams = new String[] {
430                 "java.lang.Integer", "java.lang.Integer",
431                 "com.liferay.portal.kernel.util.OrderByComparator"
432             };
433         Object[] finderArgs = new Object[] {
434                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
435             };
436         Object result = FinderCache.getResult(finderClassName,
437                 finderMethodName, finderParams, finderArgs, getSessionFactory());
438 
439         if (result == null) {
440             Session session = null;
441 
442             try {
443                 session = openSession();
444 
445                 StringMaker query = new StringMaker();
446                 query.append("FROM com.liferay.portal.model.OrgLabor ");
447 
448                 if (obc != null) {
449                     query.append("ORDER BY ");
450                     query.append(obc.getOrderBy());
451                 }
452                 else {
453                     query.append("ORDER BY ");
454                     query.append("organizationId ASC").append(", ");
455                     query.append("typeId ASC");
456                 }
457 
458                 Query q = session.createQuery(query.toString());
459                 List list = QueryUtil.list(q, getDialect(), begin, end);
460 
461                 if (obc == null) {
462                     Collections.sort(list);
463                 }
464 
465                 FinderCache.putResult(finderClassName, finderMethodName,
466                     finderParams, finderArgs, list);
467 
468                 return list;
469             }
470             catch (Exception e) {
471                 throw HibernateUtil.processException(e);
472             }
473             finally {
474                 closeSession(session);
475             }
476         }
477         else {
478             return (List)result;
479         }
480     }
481 
482     public void removeByOrganizationId(long organizationId)
483         throws SystemException {
484         Iterator itr = findByOrganizationId(organizationId).iterator();
485 
486         while (itr.hasNext()) {
487             OrgLabor orgLabor = (OrgLabor)itr.next();
488             remove(orgLabor);
489         }
490     }
491 
492     public void removeAll() throws SystemException {
493         Iterator itr = findAll().iterator();
494 
495         while (itr.hasNext()) {
496             remove((OrgLabor)itr.next());
497         }
498     }
499 
500     public int countByOrganizationId(long organizationId)
501         throws SystemException {
502         String finderClassName = OrgLabor.class.getName();
503         String finderMethodName = "countByOrganizationId";
504         String[] finderParams = new String[] { Long.class.getName() };
505         Object[] finderArgs = new Object[] { new Long(organizationId) };
506         Object result = FinderCache.getResult(finderClassName,
507                 finderMethodName, finderParams, finderArgs, getSessionFactory());
508 
509         if (result == null) {
510             Session session = null;
511 
512             try {
513                 session = openSession();
514 
515                 StringMaker query = new StringMaker();
516                 query.append("SELECT COUNT(*) ");
517                 query.append("FROM com.liferay.portal.model.OrgLabor WHERE ");
518                 query.append("organizationId = ?");
519                 query.append(" ");
520 
521                 Query q = session.createQuery(query.toString());
522                 int queryPos = 0;
523                 q.setLong(queryPos++, organizationId);
524 
525                 Long count = null;
526                 Iterator itr = q.list().iterator();
527 
528                 if (itr.hasNext()) {
529                     count = (Long)itr.next();
530                 }
531 
532                 if (count == null) {
533                     count = new Long(0);
534                 }
535 
536                 FinderCache.putResult(finderClassName, finderMethodName,
537                     finderParams, finderArgs, count);
538 
539                 return count.intValue();
540             }
541             catch (Exception e) {
542                 throw HibernateUtil.processException(e);
543             }
544             finally {
545                 closeSession(session);
546             }
547         }
548         else {
549             return ((Long)result).intValue();
550         }
551     }
552 
553     public int countAll() throws SystemException {
554         String finderClassName = OrgLabor.class.getName();
555         String finderMethodName = "countAll";
556         String[] finderParams = new String[] {  };
557         Object[] finderArgs = new Object[] {  };
558         Object result = FinderCache.getResult(finderClassName,
559                 finderMethodName, finderParams, finderArgs, getSessionFactory());
560 
561         if (result == null) {
562             Session session = null;
563 
564             try {
565                 session = openSession();
566 
567                 StringMaker query = new StringMaker();
568                 query.append("SELECT COUNT(*) ");
569                 query.append("FROM com.liferay.portal.model.OrgLabor");
570 
571                 Query q = session.createQuery(query.toString());
572                 Long count = null;
573                 Iterator itr = q.list().iterator();
574 
575                 if (itr.hasNext()) {
576                     count = (Long)itr.next();
577                 }
578 
579                 if (count == null) {
580                     count = new Long(0);
581                 }
582 
583                 FinderCache.putResult(finderClassName, finderMethodName,
584                     finderParams, finderArgs, count);
585 
586                 return count.intValue();
587             }
588             catch (Exception e) {
589                 throw HibernateUtil.processException(e);
590             }
591             finally {
592                 closeSession(session);
593             }
594         }
595         else {
596             return ((Long)result).intValue();
597         }
598     }
599 
600     protected void initDao() {
601     }
602 
603     private static Log _log = LogFactory.getLog(OrgLaborPersistenceImpl.class);
604 }