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