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.annotation.BeanReference;
24  import com.liferay.portal.kernel.cache.CacheRegistry;
25  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
26  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderPath;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.log.Log;
34  import com.liferay.portal.kernel.log.LogFactoryUtil;
35  import com.liferay.portal.kernel.util.GetterUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.service.persistence.BatchSessionUtil;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.shopping.NoSuchCategoryException;
44  import com.liferay.portlet.shopping.model.ShoppingCategory;
45  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
46  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryModelImpl;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.List;
51  
52  /**
53   * <a href="ShoppingCategoryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class ShoppingCategoryPersistenceImpl extends BasePersistenceImpl
59      implements ShoppingCategoryPersistence {
60      public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCategoryImpl.class.getName();
61      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
62          ".List";
63      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
64              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
65              FINDER_CLASS_NAME_LIST, "findByGroupId",
66              new String[] { Long.class.getName() });
67      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
68              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
69              FINDER_CLASS_NAME_LIST, "findByGroupId",
70              new String[] {
71                  Long.class.getName(),
72                  
73              "java.lang.Integer", "java.lang.Integer",
74                  "com.liferay.portal.kernel.util.OrderByComparator"
75              });
76      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
77              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
78              FINDER_CLASS_NAME_LIST, "countByGroupId",
79              new String[] { Long.class.getName() });
80      public static final FinderPath FINDER_PATH_FIND_BY_G_P = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
81              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
82              FINDER_CLASS_NAME_LIST, "findByG_P",
83              new String[] { Long.class.getName(), Long.class.getName() });
84      public static final FinderPath FINDER_PATH_FIND_BY_OBC_G_P = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
85              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
86              FINDER_CLASS_NAME_LIST, "findByG_P",
87              new String[] {
88                  Long.class.getName(), Long.class.getName(),
89                  
90              "java.lang.Integer", "java.lang.Integer",
91                  "com.liferay.portal.kernel.util.OrderByComparator"
92              });
93      public static final FinderPath FINDER_PATH_COUNT_BY_G_P = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
94              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
95              FINDER_CLASS_NAME_LIST, "countByG_P",
96              new String[] { Long.class.getName(), Long.class.getName() });
97      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
98              ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
99              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
100     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
101             ShoppingCategoryModelImpl.FINDER_CACHE_ENABLED,
102             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
103 
104     public void cacheResult(ShoppingCategory shoppingCategory) {
105         EntityCacheUtil.putResult(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
106             ShoppingCategoryImpl.class, shoppingCategory.getPrimaryKey(),
107             shoppingCategory);
108     }
109 
110     public void cacheResult(List<ShoppingCategory> shoppingCategories) {
111         for (ShoppingCategory shoppingCategory : shoppingCategories) {
112             if (EntityCacheUtil.getResult(
113                         ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
114                         ShoppingCategoryImpl.class,
115                         shoppingCategory.getPrimaryKey(), this) == null) {
116                 cacheResult(shoppingCategory);
117             }
118         }
119     }
120 
121     public void clearCache() {
122         CacheRegistry.clear(ShoppingCategoryImpl.class.getName());
123         EntityCacheUtil.clearCache(ShoppingCategoryImpl.class.getName());
124         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
125         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
126     }
127 
128     public ShoppingCategory create(long categoryId) {
129         ShoppingCategory shoppingCategory = new ShoppingCategoryImpl();
130 
131         shoppingCategory.setNew(true);
132         shoppingCategory.setPrimaryKey(categoryId);
133 
134         return shoppingCategory;
135     }
136 
137     public ShoppingCategory remove(long categoryId)
138         throws NoSuchCategoryException, SystemException {
139         Session session = null;
140 
141         try {
142             session = openSession();
143 
144             ShoppingCategory shoppingCategory = (ShoppingCategory)session.get(ShoppingCategoryImpl.class,
145                     new Long(categoryId));
146 
147             if (shoppingCategory == null) {
148                 if (_log.isWarnEnabled()) {
149                     _log.warn(
150                         "No ShoppingCategory exists with the primary key " +
151                         categoryId);
152                 }
153 
154                 throw new NoSuchCategoryException(
155                     "No ShoppingCategory exists with the primary key " +
156                     categoryId);
157             }
158 
159             return remove(shoppingCategory);
160         }
161         catch (NoSuchCategoryException nsee) {
162             throw nsee;
163         }
164         catch (Exception e) {
165             throw processException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172     public ShoppingCategory remove(ShoppingCategory shoppingCategory)
173         throws SystemException {
174         for (ModelListener<ShoppingCategory> listener : listeners) {
175             listener.onBeforeRemove(shoppingCategory);
176         }
177 
178         shoppingCategory = removeImpl(shoppingCategory);
179 
180         for (ModelListener<ShoppingCategory> listener : listeners) {
181             listener.onAfterRemove(shoppingCategory);
182         }
183 
184         return shoppingCategory;
185     }
186 
187     protected ShoppingCategory removeImpl(ShoppingCategory shoppingCategory)
188         throws SystemException {
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             if (shoppingCategory.isCachedModel() ||
195                     BatchSessionUtil.isEnabled()) {
196                 Object staleObject = session.get(ShoppingCategoryImpl.class,
197                         shoppingCategory.getPrimaryKeyObj());
198 
199                 if (staleObject != null) {
200                     session.evict(staleObject);
201                 }
202             }
203 
204             session.delete(shoppingCategory);
205 
206             session.flush();
207         }
208         catch (Exception e) {
209             throw processException(e);
210         }
211         finally {
212             closeSession(session);
213         }
214 
215         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
216 
217         EntityCacheUtil.removeResult(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
218             ShoppingCategoryImpl.class, shoppingCategory.getPrimaryKey());
219 
220         return shoppingCategory;
221     }
222 
223     /**
224      * @deprecated Use <code>update(ShoppingCategory shoppingCategory, boolean merge)</code>.
225      */
226     public ShoppingCategory update(ShoppingCategory shoppingCategory)
227         throws SystemException {
228         if (_log.isWarnEnabled()) {
229             _log.warn(
230                 "Using the deprecated update(ShoppingCategory shoppingCategory) method. Use update(ShoppingCategory shoppingCategory, boolean merge) instead.");
231         }
232 
233         return update(shoppingCategory, false);
234     }
235 
236     /**
237      * Add, update, or merge, the entity. This method also calls the model
238      * listeners to trigger the proper events associated with adding, deleting,
239      * or updating an entity.
240      *
241      * @param        shoppingCategory the entity to add, update, or merge
242      * @param        merge boolean value for whether to merge the entity. The
243      *                default value is false. Setting merge to true is more
244      *                expensive and should only be true when shoppingCategory is
245      *                transient. See LEP-5473 for a detailed discussion of this
246      *                method.
247      * @return        true if the portlet can be displayed via Ajax
248      */
249     public ShoppingCategory update(ShoppingCategory shoppingCategory,
250         boolean merge) throws SystemException {
251         boolean isNew = shoppingCategory.isNew();
252 
253         for (ModelListener<ShoppingCategory> listener : listeners) {
254             if (isNew) {
255                 listener.onBeforeCreate(shoppingCategory);
256             }
257             else {
258                 listener.onBeforeUpdate(shoppingCategory);
259             }
260         }
261 
262         shoppingCategory = updateImpl(shoppingCategory, merge);
263 
264         for (ModelListener<ShoppingCategory> listener : listeners) {
265             if (isNew) {
266                 listener.onAfterCreate(shoppingCategory);
267             }
268             else {
269                 listener.onAfterUpdate(shoppingCategory);
270             }
271         }
272 
273         return shoppingCategory;
274     }
275 
276     public ShoppingCategory updateImpl(
277         com.liferay.portlet.shopping.model.ShoppingCategory shoppingCategory,
278         boolean merge) throws SystemException {
279         Session session = null;
280 
281         try {
282             session = openSession();
283 
284             BatchSessionUtil.update(session, shoppingCategory, merge);
285 
286             shoppingCategory.setNew(false);
287         }
288         catch (Exception e) {
289             throw processException(e);
290         }
291         finally {
292             closeSession(session);
293         }
294 
295         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
296 
297         EntityCacheUtil.putResult(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
298             ShoppingCategoryImpl.class, shoppingCategory.getPrimaryKey(),
299             shoppingCategory);
300 
301         return shoppingCategory;
302     }
303 
304     public ShoppingCategory findByPrimaryKey(long categoryId)
305         throws NoSuchCategoryException, SystemException {
306         ShoppingCategory shoppingCategory = fetchByPrimaryKey(categoryId);
307 
308         if (shoppingCategory == null) {
309             if (_log.isWarnEnabled()) {
310                 _log.warn("No ShoppingCategory exists with the primary key " +
311                     categoryId);
312             }
313 
314             throw new NoSuchCategoryException(
315                 "No ShoppingCategory exists with the primary key " +
316                 categoryId);
317         }
318 
319         return shoppingCategory;
320     }
321 
322     public ShoppingCategory fetchByPrimaryKey(long categoryId)
323         throws SystemException {
324         ShoppingCategory shoppingCategory = (ShoppingCategory)EntityCacheUtil.getResult(ShoppingCategoryModelImpl.ENTITY_CACHE_ENABLED,
325                 ShoppingCategoryImpl.class, categoryId, this);
326 
327         if (shoppingCategory == null) {
328             Session session = null;
329 
330             try {
331                 session = openSession();
332 
333                 shoppingCategory = (ShoppingCategory)session.get(ShoppingCategoryImpl.class,
334                         new Long(categoryId));
335             }
336             catch (Exception e) {
337                 throw processException(e);
338             }
339             finally {
340                 if (shoppingCategory != null) {
341                     cacheResult(shoppingCategory);
342                 }
343 
344                 closeSession(session);
345             }
346         }
347 
348         return shoppingCategory;
349     }
350 
351     public List<ShoppingCategory> findByGroupId(long groupId)
352         throws SystemException {
353         Object[] finderArgs = new Object[] { new Long(groupId) };
354 
355         List<ShoppingCategory> list = (List<ShoppingCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
356                 finderArgs, this);
357 
358         if (list == null) {
359             Session session = null;
360 
361             try {
362                 session = openSession();
363 
364                 StringBuilder query = new StringBuilder();
365 
366                 query.append(
367                     "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
368 
369                 query.append("shoppingCategory.groupId = ?");
370 
371                 query.append(" ");
372 
373                 query.append("ORDER BY ");
374 
375                 query.append("shoppingCategory.parentCategoryId ASC, ");
376                 query.append("shoppingCategory.name ASC");
377 
378                 Query q = session.createQuery(query.toString());
379 
380                 QueryPos qPos = QueryPos.getInstance(q);
381 
382                 qPos.add(groupId);
383 
384                 list = q.list();
385             }
386             catch (Exception e) {
387                 throw processException(e);
388             }
389             finally {
390                 if (list == null) {
391                     list = new ArrayList<ShoppingCategory>();
392                 }
393 
394                 cacheResult(list);
395 
396                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
397                     finderArgs, list);
398 
399                 closeSession(session);
400             }
401         }
402 
403         return list;
404     }
405 
406     public List<ShoppingCategory> findByGroupId(long groupId, int start, int end)
407         throws SystemException {
408         return findByGroupId(groupId, start, end, null);
409     }
410 
411     public List<ShoppingCategory> findByGroupId(long groupId, int start,
412         int end, OrderByComparator obc) throws SystemException {
413         Object[] finderArgs = new Object[] {
414                 new Long(groupId),
415                 
416                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
417             };
418 
419         List<ShoppingCategory> list = (List<ShoppingCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
420                 finderArgs, this);
421 
422         if (list == null) {
423             Session session = null;
424 
425             try {
426                 session = openSession();
427 
428                 StringBuilder query = new StringBuilder();
429 
430                 query.append(
431                     "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
432 
433                 query.append("shoppingCategory.groupId = ?");
434 
435                 query.append(" ");
436 
437                 if (obc != null) {
438                     query.append("ORDER BY ");
439 
440                     String[] orderByFields = obc.getOrderByFields();
441 
442                     for (int i = 0; i < orderByFields.length; i++) {
443                         query.append("shoppingCategory.");
444                         query.append(orderByFields[i]);
445 
446                         if (obc.isAscending()) {
447                             query.append(" ASC");
448                         }
449                         else {
450                             query.append(" DESC");
451                         }
452 
453                         if ((i + 1) < orderByFields.length) {
454                             query.append(", ");
455                         }
456                     }
457                 }
458 
459                 else {
460                     query.append("ORDER BY ");
461 
462                     query.append("shoppingCategory.parentCategoryId ASC, ");
463                     query.append("shoppingCategory.name ASC");
464                 }
465 
466                 Query q = session.createQuery(query.toString());
467 
468                 QueryPos qPos = QueryPos.getInstance(q);
469 
470                 qPos.add(groupId);
471 
472                 list = (List<ShoppingCategory>)QueryUtil.list(q, getDialect(),
473                         start, end);
474             }
475             catch (Exception e) {
476                 throw processException(e);
477             }
478             finally {
479                 if (list == null) {
480                     list = new ArrayList<ShoppingCategory>();
481                 }
482 
483                 cacheResult(list);
484 
485                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
486                     finderArgs, list);
487 
488                 closeSession(session);
489             }
490         }
491 
492         return list;
493     }
494 
495     public ShoppingCategory findByGroupId_First(long groupId,
496         OrderByComparator obc) throws NoSuchCategoryException, SystemException {
497         List<ShoppingCategory> list = findByGroupId(groupId, 0, 1, obc);
498 
499         if (list.isEmpty()) {
500             StringBuilder msg = new StringBuilder();
501 
502             msg.append("No ShoppingCategory exists with the key {");
503 
504             msg.append("groupId=" + groupId);
505 
506             msg.append(StringPool.CLOSE_CURLY_BRACE);
507 
508             throw new NoSuchCategoryException(msg.toString());
509         }
510         else {
511             return list.get(0);
512         }
513     }
514 
515     public ShoppingCategory findByGroupId_Last(long groupId,
516         OrderByComparator obc) throws NoSuchCategoryException, SystemException {
517         int count = countByGroupId(groupId);
518 
519         List<ShoppingCategory> list = findByGroupId(groupId, count - 1, count,
520                 obc);
521 
522         if (list.isEmpty()) {
523             StringBuilder msg = new StringBuilder();
524 
525             msg.append("No ShoppingCategory exists with the key {");
526 
527             msg.append("groupId=" + groupId);
528 
529             msg.append(StringPool.CLOSE_CURLY_BRACE);
530 
531             throw new NoSuchCategoryException(msg.toString());
532         }
533         else {
534             return list.get(0);
535         }
536     }
537 
538     public ShoppingCategory[] findByGroupId_PrevAndNext(long categoryId,
539         long groupId, OrderByComparator obc)
540         throws NoSuchCategoryException, SystemException {
541         ShoppingCategory shoppingCategory = findByPrimaryKey(categoryId);
542 
543         int count = countByGroupId(groupId);
544 
545         Session session = null;
546 
547         try {
548             session = openSession();
549 
550             StringBuilder query = new StringBuilder();
551 
552             query.append(
553                 "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
554 
555             query.append("shoppingCategory.groupId = ?");
556 
557             query.append(" ");
558 
559             if (obc != null) {
560                 query.append("ORDER BY ");
561 
562                 String[] orderByFields = obc.getOrderByFields();
563 
564                 for (int i = 0; i < orderByFields.length; i++) {
565                     query.append("shoppingCategory.");
566                     query.append(orderByFields[i]);
567 
568                     if (obc.isAscending()) {
569                         query.append(" ASC");
570                     }
571                     else {
572                         query.append(" DESC");
573                     }
574 
575                     if ((i + 1) < orderByFields.length) {
576                         query.append(", ");
577                     }
578                 }
579             }
580 
581             else {
582                 query.append("ORDER BY ");
583 
584                 query.append("shoppingCategory.parentCategoryId ASC, ");
585                 query.append("shoppingCategory.name ASC");
586             }
587 
588             Query q = session.createQuery(query.toString());
589 
590             QueryPos qPos = QueryPos.getInstance(q);
591 
592             qPos.add(groupId);
593 
594             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
595                     shoppingCategory);
596 
597             ShoppingCategory[] array = new ShoppingCategoryImpl[3];
598 
599             array[0] = (ShoppingCategory)objArray[0];
600             array[1] = (ShoppingCategory)objArray[1];
601             array[2] = (ShoppingCategory)objArray[2];
602 
603             return array;
604         }
605         catch (Exception e) {
606             throw processException(e);
607         }
608         finally {
609             closeSession(session);
610         }
611     }
612 
613     public List<ShoppingCategory> findByG_P(long groupId, long parentCategoryId)
614         throws SystemException {
615         Object[] finderArgs = new Object[] {
616                 new Long(groupId), new Long(parentCategoryId)
617             };
618 
619         List<ShoppingCategory> list = (List<ShoppingCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_P,
620                 finderArgs, this);
621 
622         if (list == null) {
623             Session session = null;
624 
625             try {
626                 session = openSession();
627 
628                 StringBuilder query = new StringBuilder();
629 
630                 query.append(
631                     "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
632 
633                 query.append("shoppingCategory.groupId = ?");
634 
635                 query.append(" AND ");
636 
637                 query.append("shoppingCategory.parentCategoryId = ?");
638 
639                 query.append(" ");
640 
641                 query.append("ORDER BY ");
642 
643                 query.append("shoppingCategory.parentCategoryId ASC, ");
644                 query.append("shoppingCategory.name ASC");
645 
646                 Query q = session.createQuery(query.toString());
647 
648                 QueryPos qPos = QueryPos.getInstance(q);
649 
650                 qPos.add(groupId);
651 
652                 qPos.add(parentCategoryId);
653 
654                 list = q.list();
655             }
656             catch (Exception e) {
657                 throw processException(e);
658             }
659             finally {
660                 if (list == null) {
661                     list = new ArrayList<ShoppingCategory>();
662                 }
663 
664                 cacheResult(list);
665 
666                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_P, finderArgs,
667                     list);
668 
669                 closeSession(session);
670             }
671         }
672 
673         return list;
674     }
675 
676     public List<ShoppingCategory> findByG_P(long groupId,
677         long parentCategoryId, int start, int end) throws SystemException {
678         return findByG_P(groupId, parentCategoryId, start, end, null);
679     }
680 
681     public List<ShoppingCategory> findByG_P(long groupId,
682         long parentCategoryId, int start, int end, OrderByComparator obc)
683         throws SystemException {
684         Object[] finderArgs = new Object[] {
685                 new Long(groupId), new Long(parentCategoryId),
686                 
687                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
688             };
689 
690         List<ShoppingCategory> list = (List<ShoppingCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_G_P,
691                 finderArgs, this);
692 
693         if (list == null) {
694             Session session = null;
695 
696             try {
697                 session = openSession();
698 
699                 StringBuilder query = new StringBuilder();
700 
701                 query.append(
702                     "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
703 
704                 query.append("shoppingCategory.groupId = ?");
705 
706                 query.append(" AND ");
707 
708                 query.append("shoppingCategory.parentCategoryId = ?");
709 
710                 query.append(" ");
711 
712                 if (obc != null) {
713                     query.append("ORDER BY ");
714 
715                     String[] orderByFields = obc.getOrderByFields();
716 
717                     for (int i = 0; i < orderByFields.length; i++) {
718                         query.append("shoppingCategory.");
719                         query.append(orderByFields[i]);
720 
721                         if (obc.isAscending()) {
722                             query.append(" ASC");
723                         }
724                         else {
725                             query.append(" DESC");
726                         }
727 
728                         if ((i + 1) < orderByFields.length) {
729                             query.append(", ");
730                         }
731                     }
732                 }
733 
734                 else {
735                     query.append("ORDER BY ");
736 
737                     query.append("shoppingCategory.parentCategoryId ASC, ");
738                     query.append("shoppingCategory.name ASC");
739                 }
740 
741                 Query q = session.createQuery(query.toString());
742 
743                 QueryPos qPos = QueryPos.getInstance(q);
744 
745                 qPos.add(groupId);
746 
747                 qPos.add(parentCategoryId);
748 
749                 list = (List<ShoppingCategory>)QueryUtil.list(q, getDialect(),
750                         start, end);
751             }
752             catch (Exception e) {
753                 throw processException(e);
754             }
755             finally {
756                 if (list == null) {
757                     list = new ArrayList<ShoppingCategory>();
758                 }
759 
760                 cacheResult(list);
761 
762                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_G_P,
763                     finderArgs, list);
764 
765                 closeSession(session);
766             }
767         }
768 
769         return list;
770     }
771 
772     public ShoppingCategory findByG_P_First(long groupId,
773         long parentCategoryId, OrderByComparator obc)
774         throws NoSuchCategoryException, SystemException {
775         List<ShoppingCategory> list = findByG_P(groupId, parentCategoryId, 0,
776                 1, obc);
777 
778         if (list.isEmpty()) {
779             StringBuilder msg = new StringBuilder();
780 
781             msg.append("No ShoppingCategory exists with the key {");
782 
783             msg.append("groupId=" + groupId);
784 
785             msg.append(", ");
786             msg.append("parentCategoryId=" + parentCategoryId);
787 
788             msg.append(StringPool.CLOSE_CURLY_BRACE);
789 
790             throw new NoSuchCategoryException(msg.toString());
791         }
792         else {
793             return list.get(0);
794         }
795     }
796 
797     public ShoppingCategory findByG_P_Last(long groupId, long parentCategoryId,
798         OrderByComparator obc) throws NoSuchCategoryException, SystemException {
799         int count = countByG_P(groupId, parentCategoryId);
800 
801         List<ShoppingCategory> list = findByG_P(groupId, parentCategoryId,
802                 count - 1, count, obc);
803 
804         if (list.isEmpty()) {
805             StringBuilder msg = new StringBuilder();
806 
807             msg.append("No ShoppingCategory exists with the key {");
808 
809             msg.append("groupId=" + groupId);
810 
811             msg.append(", ");
812             msg.append("parentCategoryId=" + parentCategoryId);
813 
814             msg.append(StringPool.CLOSE_CURLY_BRACE);
815 
816             throw new NoSuchCategoryException(msg.toString());
817         }
818         else {
819             return list.get(0);
820         }
821     }
822 
823     public ShoppingCategory[] findByG_P_PrevAndNext(long categoryId,
824         long groupId, long parentCategoryId, OrderByComparator obc)
825         throws NoSuchCategoryException, SystemException {
826         ShoppingCategory shoppingCategory = findByPrimaryKey(categoryId);
827 
828         int count = countByG_P(groupId, parentCategoryId);
829 
830         Session session = null;
831 
832         try {
833             session = openSession();
834 
835             StringBuilder query = new StringBuilder();
836 
837             query.append(
838                 "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory WHERE ");
839 
840             query.append("shoppingCategory.groupId = ?");
841 
842             query.append(" AND ");
843 
844             query.append("shoppingCategory.parentCategoryId = ?");
845 
846             query.append(" ");
847 
848             if (obc != null) {
849                 query.append("ORDER BY ");
850 
851                 String[] orderByFields = obc.getOrderByFields();
852 
853                 for (int i = 0; i < orderByFields.length; i++) {
854                     query.append("shoppingCategory.");
855                     query.append(orderByFields[i]);
856 
857                     if (obc.isAscending()) {
858                         query.append(" ASC");
859                     }
860                     else {
861                         query.append(" DESC");
862                     }
863 
864                     if ((i + 1) < orderByFields.length) {
865                         query.append(", ");
866                     }
867                 }
868             }
869 
870             else {
871                 query.append("ORDER BY ");
872 
873                 query.append("shoppingCategory.parentCategoryId ASC, ");
874                 query.append("shoppingCategory.name ASC");
875             }
876 
877             Query q = session.createQuery(query.toString());
878 
879             QueryPos qPos = QueryPos.getInstance(q);
880 
881             qPos.add(groupId);
882 
883             qPos.add(parentCategoryId);
884 
885             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
886                     shoppingCategory);
887 
888             ShoppingCategory[] array = new ShoppingCategoryImpl[3];
889 
890             array[0] = (ShoppingCategory)objArray[0];
891             array[1] = (ShoppingCategory)objArray[1];
892             array[2] = (ShoppingCategory)objArray[2];
893 
894             return array;
895         }
896         catch (Exception e) {
897             throw processException(e);
898         }
899         finally {
900             closeSession(session);
901         }
902     }
903 
904     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
905         throws SystemException {
906         Session session = null;
907 
908         try {
909             session = openSession();
910 
911             dynamicQuery.compile(session);
912 
913             return dynamicQuery.list();
914         }
915         catch (Exception e) {
916             throw processException(e);
917         }
918         finally {
919             closeSession(session);
920         }
921     }
922 
923     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
924         int start, int end) throws SystemException {
925         Session session = null;
926 
927         try {
928             session = openSession();
929 
930             dynamicQuery.setLimit(start, end);
931 
932             dynamicQuery.compile(session);
933 
934             return dynamicQuery.list();
935         }
936         catch (Exception e) {
937             throw processException(e);
938         }
939         finally {
940             closeSession(session);
941         }
942     }
943 
944     public List<ShoppingCategory> findAll() throws SystemException {
945         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
946     }
947 
948     public List<ShoppingCategory> findAll(int start, int end)
949         throws SystemException {
950         return findAll(start, end, null);
951     }
952 
953     public List<ShoppingCategory> findAll(int start, int end,
954         OrderByComparator obc) throws SystemException {
955         Object[] finderArgs = new Object[] {
956                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
957             };
958 
959         List<ShoppingCategory> list = (List<ShoppingCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
960                 finderArgs, this);
961 
962         if (list == null) {
963             Session session = null;
964 
965             try {
966                 session = openSession();
967 
968                 StringBuilder query = new StringBuilder();
969 
970                 query.append(
971                     "SELECT shoppingCategory FROM ShoppingCategory shoppingCategory ");
972 
973                 if (obc != null) {
974                     query.append("ORDER BY ");
975 
976                     String[] orderByFields = obc.getOrderByFields();
977 
978                     for (int i = 0; i < orderByFields.length; i++) {
979                         query.append("shoppingCategory.");
980                         query.append(orderByFields[i]);
981 
982                         if (obc.isAscending()) {
983                             query.append(" ASC");
984                         }
985                         else {
986                             query.append(" DESC");
987                         }
988 
989                         if ((i + 1) < orderByFields.length) {
990                             query.append(", ");
991                         }
992                     }
993                 }
994 
995                 else {
996                     query.append("ORDER BY ");
997 
998                     query.append("shoppingCategory.parentCategoryId ASC, ");
999                     query.append("shoppingCategory.name ASC");
1000                }
1001
1002                Query q = session.createQuery(query.toString());
1003
1004                if (obc == null) {
1005                    list = (List<ShoppingCategory>)QueryUtil.list(q,
1006                            getDialect(), start, end, false);
1007
1008                    Collections.sort(list);
1009                }
1010                else {
1011                    list = (List<ShoppingCategory>)QueryUtil.list(q,
1012                            getDialect(), start, end);
1013                }
1014            }
1015            catch (Exception e) {
1016                throw processException(e);
1017            }
1018            finally {
1019                if (list == null) {
1020                    list = new ArrayList<ShoppingCategory>();
1021                }
1022
1023                cacheResult(list);
1024
1025                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1026
1027                closeSession(session);
1028            }
1029        }
1030
1031        return list;
1032    }
1033
1034    public void removeByGroupId(long groupId) throws SystemException {
1035        for (ShoppingCategory shoppingCategory : findByGroupId(groupId)) {
1036            remove(shoppingCategory);
1037        }
1038    }
1039
1040    public void removeByG_P(long groupId, long parentCategoryId)
1041        throws SystemException {
1042        for (ShoppingCategory shoppingCategory : findByG_P(groupId,
1043                parentCategoryId)) {
1044            remove(shoppingCategory);
1045        }
1046    }
1047
1048    public void removeAll() throws SystemException {
1049        for (ShoppingCategory shoppingCategory : findAll()) {
1050            remove(shoppingCategory);
1051        }
1052    }
1053
1054    public int countByGroupId(long groupId) throws SystemException {
1055        Object[] finderArgs = new Object[] { new Long(groupId) };
1056
1057        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1058                finderArgs, this);
1059
1060        if (count == null) {
1061            Session session = null;
1062
1063            try {
1064                session = openSession();
1065
1066                StringBuilder query = new StringBuilder();
1067
1068                query.append("SELECT COUNT(shoppingCategory) ");
1069                query.append("FROM ShoppingCategory shoppingCategory WHERE ");
1070
1071                query.append("shoppingCategory.groupId = ?");
1072
1073                query.append(" ");
1074
1075                Query q = session.createQuery(query.toString());
1076
1077                QueryPos qPos = QueryPos.getInstance(q);
1078
1079                qPos.add(groupId);
1080
1081                count = (Long)q.uniqueResult();
1082            }
1083            catch (Exception e) {
1084                throw processException(e);
1085            }
1086            finally {
1087                if (count == null) {
1088                    count = Long.valueOf(0);
1089                }
1090
1091                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1092                    finderArgs, count);
1093
1094                closeSession(session);
1095            }
1096        }
1097
1098        return count.intValue();
1099    }
1100
1101    public int countByG_P(long groupId, long parentCategoryId)
1102        throws SystemException {
1103        Object[] finderArgs = new Object[] {
1104                new Long(groupId), new Long(parentCategoryId)
1105            };
1106
1107        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_P,
1108                finderArgs, this);
1109
1110        if (count == null) {
1111            Session session = null;
1112
1113            try {
1114                session = openSession();
1115
1116                StringBuilder query = new StringBuilder();
1117
1118                query.append("SELECT COUNT(shoppingCategory) ");
1119                query.append("FROM ShoppingCategory shoppingCategory WHERE ");
1120
1121                query.append("shoppingCategory.groupId = ?");
1122
1123                query.append(" AND ");
1124
1125                query.append("shoppingCategory.parentCategoryId = ?");
1126
1127                query.append(" ");
1128
1129                Query q = session.createQuery(query.toString());
1130
1131                QueryPos qPos = QueryPos.getInstance(q);
1132
1133                qPos.add(groupId);
1134
1135                qPos.add(parentCategoryId);
1136
1137                count = (Long)q.uniqueResult();
1138            }
1139            catch (Exception e) {
1140                throw processException(e);
1141            }
1142            finally {
1143                if (count == null) {
1144                    count = Long.valueOf(0);
1145                }
1146
1147                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_P, finderArgs,
1148                    count);
1149
1150                closeSession(session);
1151            }
1152        }
1153
1154        return count.intValue();
1155    }
1156
1157    public int countAll() throws SystemException {
1158        Object[] finderArgs = new Object[0];
1159
1160        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1161                finderArgs, this);
1162
1163        if (count == null) {
1164            Session session = null;
1165
1166            try {
1167                session = openSession();
1168
1169                Query q = session.createQuery(
1170                        "SELECT COUNT(shoppingCategory) FROM ShoppingCategory shoppingCategory");
1171
1172                count = (Long)q.uniqueResult();
1173            }
1174            catch (Exception e) {
1175                throw processException(e);
1176            }
1177            finally {
1178                if (count == null) {
1179                    count = Long.valueOf(0);
1180                }
1181
1182                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1183                    count);
1184
1185                closeSession(session);
1186            }
1187        }
1188
1189        return count.intValue();
1190    }
1191
1192    public void afterPropertiesSet() {
1193        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1194                    com.liferay.portal.util.PropsUtil.get(
1195                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCategory")));
1196
1197        if (listenerClassNames.length > 0) {
1198            try {
1199                List<ModelListener<ShoppingCategory>> listenersList = new ArrayList<ModelListener<ShoppingCategory>>();
1200
1201                for (String listenerClassName : listenerClassNames) {
1202                    listenersList.add((ModelListener<ShoppingCategory>)Class.forName(
1203                            listenerClassName).newInstance());
1204                }
1205
1206                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1207            }
1208            catch (Exception e) {
1209                _log.error(e);
1210            }
1211        }
1212    }
1213
1214    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence.impl")
1215    protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1216    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence.impl")
1217    protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1218    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence.impl")
1219    protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1220    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence.impl")
1221    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1222    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence.impl")
1223    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1224    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence.impl")
1225    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1226    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence.impl")
1227    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1228    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence.impl")
1229    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1230    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1231    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1232    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1233    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1234    private static Log _log = LogFactoryUtil.getLog(ShoppingCategoryPersistenceImpl.class);
1235}