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