001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.annotation.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.model.ModelListener;
037    import com.liferay.portal.service.persistence.BatchSessionUtil;
038    import com.liferay.portal.service.persistence.ResourcePersistence;
039    import com.liferay.portal.service.persistence.UserPersistence;
040    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041    
042    import com.liferay.portlet.shopping.NoSuchOrderItemException;
043    import com.liferay.portlet.shopping.model.ShoppingOrderItem;
044    import com.liferay.portlet.shopping.model.impl.ShoppingOrderItemImpl;
045    import com.liferay.portlet.shopping.model.impl.ShoppingOrderItemModelImpl;
046    
047    import java.io.Serializable;
048    
049    import java.util.ArrayList;
050    import java.util.Collections;
051    import java.util.List;
052    
053    /**
054     * The persistence implementation for the shopping order item service.
055     *
056     * <p>
057     * Never modify or reference this class directly. Always use {@link ShoppingOrderItemUtil} to access the shopping order item persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
058     * </p>
059     *
060     * <p>
061     * Caching information and settings can be found in <code>portal.properties</code>
062     * </p>
063     *
064     * @author Brian Wing Shun Chan
065     * @see ShoppingOrderItemPersistence
066     * @see ShoppingOrderItemUtil
067     * @generated
068     */
069    public class ShoppingOrderItemPersistenceImpl extends BasePersistenceImpl<ShoppingOrderItem>
070            implements ShoppingOrderItemPersistence {
071            public static final String FINDER_CLASS_NAME_ENTITY = ShoppingOrderItemImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
073                    ".List";
074            public static final FinderPath FINDER_PATH_FIND_BY_ORDERID = new FinderPath(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
075                            ShoppingOrderItemModelImpl.FINDER_CACHE_ENABLED,
076                            FINDER_CLASS_NAME_LIST, "findByOrderId",
077                            new String[] {
078                                    Long.class.getName(),
079                                    
080                            "java.lang.Integer", "java.lang.Integer",
081                                    "com.liferay.portal.kernel.util.OrderByComparator"
082                            });
083            public static final FinderPath FINDER_PATH_COUNT_BY_ORDERID = new FinderPath(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
084                            ShoppingOrderItemModelImpl.FINDER_CACHE_ENABLED,
085                            FINDER_CLASS_NAME_LIST, "countByOrderId",
086                            new String[] { Long.class.getName() });
087            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
088                            ShoppingOrderItemModelImpl.FINDER_CACHE_ENABLED,
089                            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
090            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
091                            ShoppingOrderItemModelImpl.FINDER_CACHE_ENABLED,
092                            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
093    
094            /**
095             * Caches the shopping order item in the entity cache if it is enabled.
096             *
097             * @param shoppingOrderItem the shopping order item to cache
098             */
099            public void cacheResult(ShoppingOrderItem shoppingOrderItem) {
100                    EntityCacheUtil.putResult(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
101                            ShoppingOrderItemImpl.class, shoppingOrderItem.getPrimaryKey(),
102                            shoppingOrderItem);
103            }
104    
105            /**
106             * Caches the shopping order items in the entity cache if it is enabled.
107             *
108             * @param shoppingOrderItems the shopping order items to cache
109             */
110            public void cacheResult(List<ShoppingOrderItem> shoppingOrderItems) {
111                    for (ShoppingOrderItem shoppingOrderItem : shoppingOrderItems) {
112                            if (EntityCacheUtil.getResult(
113                                                    ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
114                                                    ShoppingOrderItemImpl.class,
115                                                    shoppingOrderItem.getPrimaryKey(), this) == null) {
116                                    cacheResult(shoppingOrderItem);
117                            }
118                    }
119            }
120    
121            /**
122             * Clears the cache for all shopping order items.
123             *
124             * <p>
125             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
126             * </p>
127             */
128            public void clearCache() {
129                    CacheRegistryUtil.clear(ShoppingOrderItemImpl.class.getName());
130                    EntityCacheUtil.clearCache(ShoppingOrderItemImpl.class.getName());
131                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
133            }
134    
135            /**
136             * Clears the cache for the shopping order item.
137             *
138             * <p>
139             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
140             * </p>
141             */
142            public void clearCache(ShoppingOrderItem shoppingOrderItem) {
143                    EntityCacheUtil.removeResult(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
144                            ShoppingOrderItemImpl.class, shoppingOrderItem.getPrimaryKey());
145            }
146    
147            /**
148             * Creates a new shopping order item with the primary key. Does not add the shopping order item to the database.
149             *
150             * @param orderItemId the primary key for the new shopping order item
151             * @return the new shopping order item
152             */
153            public ShoppingOrderItem create(long orderItemId) {
154                    ShoppingOrderItem shoppingOrderItem = new ShoppingOrderItemImpl();
155    
156                    shoppingOrderItem.setNew(true);
157                    shoppingOrderItem.setPrimaryKey(orderItemId);
158    
159                    return shoppingOrderItem;
160            }
161    
162            /**
163             * Removes the shopping order item with the primary key from the database. Also notifies the appropriate model listeners.
164             *
165             * @param primaryKey the primary key of the shopping order item to remove
166             * @return the shopping order item that was removed
167             * @throws com.liferay.portal.NoSuchModelException if a shopping order item with the primary key could not be found
168             * @throws SystemException if a system exception occurred
169             */
170            public ShoppingOrderItem remove(Serializable primaryKey)
171                    throws NoSuchModelException, SystemException {
172                    return remove(((Long)primaryKey).longValue());
173            }
174    
175            /**
176             * Removes the shopping order item with the primary key from the database. Also notifies the appropriate model listeners.
177             *
178             * @param orderItemId the primary key of the shopping order item to remove
179             * @return the shopping order item that was removed
180             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a shopping order item with the primary key could not be found
181             * @throws SystemException if a system exception occurred
182             */
183            public ShoppingOrderItem remove(long orderItemId)
184                    throws NoSuchOrderItemException, SystemException {
185                    Session session = null;
186    
187                    try {
188                            session = openSession();
189    
190                            ShoppingOrderItem shoppingOrderItem = (ShoppingOrderItem)session.get(ShoppingOrderItemImpl.class,
191                                            new Long(orderItemId));
192    
193                            if (shoppingOrderItem == null) {
194                                    if (_log.isWarnEnabled()) {
195                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + orderItemId);
196                                    }
197    
198                                    throw new NoSuchOrderItemException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
199                                            orderItemId);
200                            }
201    
202                            return remove(shoppingOrderItem);
203                    }
204                    catch (NoSuchOrderItemException nsee) {
205                            throw nsee;
206                    }
207                    catch (Exception e) {
208                            throw processException(e);
209                    }
210                    finally {
211                            closeSession(session);
212                    }
213            }
214    
215            protected ShoppingOrderItem removeImpl(ShoppingOrderItem shoppingOrderItem)
216                    throws SystemException {
217                    shoppingOrderItem = toUnwrappedModel(shoppingOrderItem);
218    
219                    Session session = null;
220    
221                    try {
222                            session = openSession();
223    
224                            BatchSessionUtil.delete(session, shoppingOrderItem);
225                    }
226                    catch (Exception e) {
227                            throw processException(e);
228                    }
229                    finally {
230                            closeSession(session);
231                    }
232    
233                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
234    
235                    EntityCacheUtil.removeResult(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
236                            ShoppingOrderItemImpl.class, shoppingOrderItem.getPrimaryKey());
237    
238                    return shoppingOrderItem;
239            }
240    
241            public ShoppingOrderItem updateImpl(
242                    com.liferay.portlet.shopping.model.ShoppingOrderItem shoppingOrderItem,
243                    boolean merge) throws SystemException {
244                    shoppingOrderItem = toUnwrappedModel(shoppingOrderItem);
245    
246                    Session session = null;
247    
248                    try {
249                            session = openSession();
250    
251                            BatchSessionUtil.update(session, shoppingOrderItem, merge);
252    
253                            shoppingOrderItem.setNew(false);
254                    }
255                    catch (Exception e) {
256                            throw processException(e);
257                    }
258                    finally {
259                            closeSession(session);
260                    }
261    
262                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
263    
264                    EntityCacheUtil.putResult(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
265                            ShoppingOrderItemImpl.class, shoppingOrderItem.getPrimaryKey(),
266                            shoppingOrderItem);
267    
268                    return shoppingOrderItem;
269            }
270    
271            protected ShoppingOrderItem toUnwrappedModel(
272                    ShoppingOrderItem shoppingOrderItem) {
273                    if (shoppingOrderItem instanceof ShoppingOrderItemImpl) {
274                            return shoppingOrderItem;
275                    }
276    
277                    ShoppingOrderItemImpl shoppingOrderItemImpl = new ShoppingOrderItemImpl();
278    
279                    shoppingOrderItemImpl.setNew(shoppingOrderItem.isNew());
280                    shoppingOrderItemImpl.setPrimaryKey(shoppingOrderItem.getPrimaryKey());
281    
282                    shoppingOrderItemImpl.setOrderItemId(shoppingOrderItem.getOrderItemId());
283                    shoppingOrderItemImpl.setOrderId(shoppingOrderItem.getOrderId());
284                    shoppingOrderItemImpl.setItemId(shoppingOrderItem.getItemId());
285                    shoppingOrderItemImpl.setSku(shoppingOrderItem.getSku());
286                    shoppingOrderItemImpl.setName(shoppingOrderItem.getName());
287                    shoppingOrderItemImpl.setDescription(shoppingOrderItem.getDescription());
288                    shoppingOrderItemImpl.setProperties(shoppingOrderItem.getProperties());
289                    shoppingOrderItemImpl.setPrice(shoppingOrderItem.getPrice());
290                    shoppingOrderItemImpl.setQuantity(shoppingOrderItem.getQuantity());
291                    shoppingOrderItemImpl.setShippedDate(shoppingOrderItem.getShippedDate());
292    
293                    return shoppingOrderItemImpl;
294            }
295    
296            /**
297             * Finds the shopping order item with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
298             *
299             * @param primaryKey the primary key of the shopping order item to find
300             * @return the shopping order item
301             * @throws com.liferay.portal.NoSuchModelException if a shopping order item with the primary key could not be found
302             * @throws SystemException if a system exception occurred
303             */
304            public ShoppingOrderItem findByPrimaryKey(Serializable primaryKey)
305                    throws NoSuchModelException, SystemException {
306                    return findByPrimaryKey(((Long)primaryKey).longValue());
307            }
308    
309            /**
310             * Finds the shopping order item with the primary key or throws a {@link com.liferay.portlet.shopping.NoSuchOrderItemException} if it could not be found.
311             *
312             * @param orderItemId the primary key of the shopping order item to find
313             * @return the shopping order item
314             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a shopping order item with the primary key could not be found
315             * @throws SystemException if a system exception occurred
316             */
317            public ShoppingOrderItem findByPrimaryKey(long orderItemId)
318                    throws NoSuchOrderItemException, SystemException {
319                    ShoppingOrderItem shoppingOrderItem = fetchByPrimaryKey(orderItemId);
320    
321                    if (shoppingOrderItem == null) {
322                            if (_log.isWarnEnabled()) {
323                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + orderItemId);
324                            }
325    
326                            throw new NoSuchOrderItemException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
327                                    orderItemId);
328                    }
329    
330                    return shoppingOrderItem;
331            }
332    
333            /**
334             * Finds the shopping order item with the primary key or returns <code>null</code> if it could not be found.
335             *
336             * @param primaryKey the primary key of the shopping order item to find
337             * @return the shopping order item, or <code>null</code> if a shopping order item with the primary key could not be found
338             * @throws SystemException if a system exception occurred
339             */
340            public ShoppingOrderItem fetchByPrimaryKey(Serializable primaryKey)
341                    throws SystemException {
342                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
343            }
344    
345            /**
346             * Finds the shopping order item with the primary key or returns <code>null</code> if it could not be found.
347             *
348             * @param orderItemId the primary key of the shopping order item to find
349             * @return the shopping order item, or <code>null</code> if a shopping order item with the primary key could not be found
350             * @throws SystemException if a system exception occurred
351             */
352            public ShoppingOrderItem fetchByPrimaryKey(long orderItemId)
353                    throws SystemException {
354                    ShoppingOrderItem shoppingOrderItem = (ShoppingOrderItem)EntityCacheUtil.getResult(ShoppingOrderItemModelImpl.ENTITY_CACHE_ENABLED,
355                                    ShoppingOrderItemImpl.class, orderItemId, this);
356    
357                    if (shoppingOrderItem == null) {
358                            Session session = null;
359    
360                            try {
361                                    session = openSession();
362    
363                                    shoppingOrderItem = (ShoppingOrderItem)session.get(ShoppingOrderItemImpl.class,
364                                                    new Long(orderItemId));
365                            }
366                            catch (Exception e) {
367                                    throw processException(e);
368                            }
369                            finally {
370                                    if (shoppingOrderItem != null) {
371                                            cacheResult(shoppingOrderItem);
372                                    }
373    
374                                    closeSession(session);
375                            }
376                    }
377    
378                    return shoppingOrderItem;
379            }
380    
381            /**
382             * Finds all the shopping order items where orderId = &#63;.
383             *
384             * @param orderId the order id to search with
385             * @return the matching shopping order items
386             * @throws SystemException if a system exception occurred
387             */
388            public List<ShoppingOrderItem> findByOrderId(long orderId)
389                    throws SystemException {
390                    return findByOrderId(orderId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
391            }
392    
393            /**
394             * Finds a range of all the shopping order items where orderId = &#63;.
395             *
396             * <p>
397             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
398             * </p>
399             *
400             * @param orderId the order id to search with
401             * @param start the lower bound of the range of shopping order items to return
402             * @param end the upper bound of the range of shopping order items to return (not inclusive)
403             * @return the range of matching shopping order items
404             * @throws SystemException if a system exception occurred
405             */
406            public List<ShoppingOrderItem> findByOrderId(long orderId, int start,
407                    int end) throws SystemException {
408                    return findByOrderId(orderId, start, end, null);
409            }
410    
411            /**
412             * Finds an ordered range of all the shopping order items where orderId = &#63;.
413             *
414             * <p>
415             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
416             * </p>
417             *
418             * @param orderId the order id to search with
419             * @param start the lower bound of the range of shopping order items to return
420             * @param end the upper bound of the range of shopping order items to return (not inclusive)
421             * @param orderByComparator the comparator to order the results by
422             * @return the ordered range of matching shopping order items
423             * @throws SystemException if a system exception occurred
424             */
425            public List<ShoppingOrderItem> findByOrderId(long orderId, int start,
426                    int end, OrderByComparator orderByComparator) throws SystemException {
427                    Object[] finderArgs = new Object[] {
428                                    orderId,
429                                    
430                                    String.valueOf(start), String.valueOf(end),
431                                    String.valueOf(orderByComparator)
432                            };
433    
434                    List<ShoppingOrderItem> list = (List<ShoppingOrderItem>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_ORDERID,
435                                    finderArgs, this);
436    
437                    if (list == null) {
438                            StringBundler query = null;
439    
440                            if (orderByComparator != null) {
441                                    query = new StringBundler(3 +
442                                                    (orderByComparator.getOrderByFields().length * 3));
443                            }
444                            else {
445                                    query = new StringBundler(3);
446                            }
447    
448                            query.append(_SQL_SELECT_SHOPPINGORDERITEM_WHERE);
449    
450                            query.append(_FINDER_COLUMN_ORDERID_ORDERID_2);
451    
452                            if (orderByComparator != null) {
453                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
454                                            orderByComparator);
455                            }
456    
457                            else {
458                                    query.append(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
459                            }
460    
461                            String sql = query.toString();
462    
463                            Session session = null;
464    
465                            try {
466                                    session = openSession();
467    
468                                    Query q = session.createQuery(sql);
469    
470                                    QueryPos qPos = QueryPos.getInstance(q);
471    
472                                    qPos.add(orderId);
473    
474                                    list = (List<ShoppingOrderItem>)QueryUtil.list(q, getDialect(),
475                                                    start, end);
476                            }
477                            catch (Exception e) {
478                                    throw processException(e);
479                            }
480                            finally {
481                                    if (list == null) {
482                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_ORDERID,
483                                                    finderArgs);
484                                    }
485                                    else {
486                                            cacheResult(list);
487    
488                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_ORDERID,
489                                                    finderArgs, list);
490                                    }
491    
492                                    closeSession(session);
493                            }
494                    }
495    
496                    return list;
497            }
498    
499            /**
500             * Finds the first shopping order item in the ordered set where orderId = &#63;.
501             *
502             * <p>
503             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
504             * </p>
505             *
506             * @param orderId the order id to search with
507             * @param orderByComparator the comparator to order the set by
508             * @return the first matching shopping order item
509             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a matching shopping order item could not be found
510             * @throws SystemException if a system exception occurred
511             */
512            public ShoppingOrderItem findByOrderId_First(long orderId,
513                    OrderByComparator orderByComparator)
514                    throws NoSuchOrderItemException, SystemException {
515                    List<ShoppingOrderItem> list = findByOrderId(orderId, 0, 1,
516                                    orderByComparator);
517    
518                    if (list.isEmpty()) {
519                            StringBundler msg = new StringBundler(4);
520    
521                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
522    
523                            msg.append("orderId=");
524                            msg.append(orderId);
525    
526                            msg.append(StringPool.CLOSE_CURLY_BRACE);
527    
528                            throw new NoSuchOrderItemException(msg.toString());
529                    }
530                    else {
531                            return list.get(0);
532                    }
533            }
534    
535            /**
536             * Finds the last shopping order item in the ordered set where orderId = &#63;.
537             *
538             * <p>
539             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
540             * </p>
541             *
542             * @param orderId the order id to search with
543             * @param orderByComparator the comparator to order the set by
544             * @return the last matching shopping order item
545             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a matching shopping order item could not be found
546             * @throws SystemException if a system exception occurred
547             */
548            public ShoppingOrderItem findByOrderId_Last(long orderId,
549                    OrderByComparator orderByComparator)
550                    throws NoSuchOrderItemException, SystemException {
551                    int count = countByOrderId(orderId);
552    
553                    List<ShoppingOrderItem> list = findByOrderId(orderId, count - 1, count,
554                                    orderByComparator);
555    
556                    if (list.isEmpty()) {
557                            StringBundler msg = new StringBundler(4);
558    
559                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
560    
561                            msg.append("orderId=");
562                            msg.append(orderId);
563    
564                            msg.append(StringPool.CLOSE_CURLY_BRACE);
565    
566                            throw new NoSuchOrderItemException(msg.toString());
567                    }
568                    else {
569                            return list.get(0);
570                    }
571            }
572    
573            /**
574             * Finds the shopping order items before and after the current shopping order item in the ordered set where orderId = &#63;.
575             *
576             * <p>
577             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
578             * </p>
579             *
580             * @param orderItemId the primary key of the current shopping order item
581             * @param orderId the order id to search with
582             * @param orderByComparator the comparator to order the set by
583             * @return the previous, current, and next shopping order item
584             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a shopping order item with the primary key could not be found
585             * @throws SystemException if a system exception occurred
586             */
587            public ShoppingOrderItem[] findByOrderId_PrevAndNext(long orderItemId,
588                    long orderId, OrderByComparator orderByComparator)
589                    throws NoSuchOrderItemException, SystemException {
590                    ShoppingOrderItem shoppingOrderItem = findByPrimaryKey(orderItemId);
591    
592                    Session session = null;
593    
594                    try {
595                            session = openSession();
596    
597                            ShoppingOrderItem[] array = new ShoppingOrderItemImpl[3];
598    
599                            array[0] = getByOrderId_PrevAndNext(session, shoppingOrderItem,
600                                            orderId, orderByComparator, true);
601    
602                            array[1] = shoppingOrderItem;
603    
604                            array[2] = getByOrderId_PrevAndNext(session, shoppingOrderItem,
605                                            orderId, orderByComparator, false);
606    
607                            return array;
608                    }
609                    catch (Exception e) {
610                            throw processException(e);
611                    }
612                    finally {
613                            closeSession(session);
614                    }
615            }
616    
617            protected ShoppingOrderItem getByOrderId_PrevAndNext(Session session,
618                    ShoppingOrderItem shoppingOrderItem, long orderId,
619                    OrderByComparator orderByComparator, boolean previous) {
620                    StringBundler query = null;
621    
622                    if (orderByComparator != null) {
623                            query = new StringBundler(6 +
624                                            (orderByComparator.getOrderByFields().length * 6));
625                    }
626                    else {
627                            query = new StringBundler(3);
628                    }
629    
630                    query.append(_SQL_SELECT_SHOPPINGORDERITEM_WHERE);
631    
632                    query.append(_FINDER_COLUMN_ORDERID_ORDERID_2);
633    
634                    if (orderByComparator != null) {
635                            String[] orderByFields = orderByComparator.getOrderByFields();
636    
637                            if (orderByFields.length > 0) {
638                                    query.append(WHERE_AND);
639                            }
640    
641                            for (int i = 0; i < orderByFields.length; i++) {
642                                    query.append(_ORDER_BY_ENTITY_ALIAS);
643                                    query.append(orderByFields[i]);
644    
645                                    if ((i + 1) < orderByFields.length) {
646                                            if (orderByComparator.isAscending() ^ previous) {
647                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
648                                            }
649                                            else {
650                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
651                                            }
652                                    }
653                                    else {
654                                            if (orderByComparator.isAscending() ^ previous) {
655                                                    query.append(WHERE_GREATER_THAN);
656                                            }
657                                            else {
658                                                    query.append(WHERE_LESSER_THAN);
659                                            }
660                                    }
661                            }
662    
663                            query.append(ORDER_BY_CLAUSE);
664    
665                            for (int i = 0; i < orderByFields.length; i++) {
666                                    query.append(_ORDER_BY_ENTITY_ALIAS);
667                                    query.append(orderByFields[i]);
668    
669                                    if ((i + 1) < orderByFields.length) {
670                                            if (orderByComparator.isAscending() ^ previous) {
671                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
672                                            }
673                                            else {
674                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
675                                            }
676                                    }
677                                    else {
678                                            if (orderByComparator.isAscending() ^ previous) {
679                                                    query.append(ORDER_BY_ASC);
680                                            }
681                                            else {
682                                                    query.append(ORDER_BY_DESC);
683                                            }
684                                    }
685                            }
686                    }
687    
688                    else {
689                            query.append(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
690                    }
691    
692                    String sql = query.toString();
693    
694                    Query q = session.createQuery(sql);
695    
696                    q.setFirstResult(0);
697                    q.setMaxResults(2);
698    
699                    QueryPos qPos = QueryPos.getInstance(q);
700    
701                    qPos.add(orderId);
702    
703                    if (orderByComparator != null) {
704                            Object[] values = orderByComparator.getOrderByValues(shoppingOrderItem);
705    
706                            for (Object value : values) {
707                                    qPos.add(value);
708                            }
709                    }
710    
711                    List<ShoppingOrderItem> list = q.list();
712    
713                    if (list.size() == 2) {
714                            return list.get(1);
715                    }
716                    else {
717                            return null;
718                    }
719            }
720    
721            /**
722             * Finds all the shopping order items.
723             *
724             * @return the shopping order items
725             * @throws SystemException if a system exception occurred
726             */
727            public List<ShoppingOrderItem> findAll() throws SystemException {
728                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
729            }
730    
731            /**
732             * Finds a range of all the shopping order items.
733             *
734             * <p>
735             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
736             * </p>
737             *
738             * @param start the lower bound of the range of shopping order items to return
739             * @param end the upper bound of the range of shopping order items to return (not inclusive)
740             * @return the range of shopping order items
741             * @throws SystemException if a system exception occurred
742             */
743            public List<ShoppingOrderItem> findAll(int start, int end)
744                    throws SystemException {
745                    return findAll(start, end, null);
746            }
747    
748            /**
749             * Finds an ordered range of all the shopping order items.
750             *
751             * <p>
752             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
753             * </p>
754             *
755             * @param start the lower bound of the range of shopping order items to return
756             * @param end the upper bound of the range of shopping order items to return (not inclusive)
757             * @param orderByComparator the comparator to order the results by
758             * @return the ordered range of shopping order items
759             * @throws SystemException if a system exception occurred
760             */
761            public List<ShoppingOrderItem> findAll(int start, int end,
762                    OrderByComparator orderByComparator) throws SystemException {
763                    Object[] finderArgs = new Object[] {
764                                    String.valueOf(start), String.valueOf(end),
765                                    String.valueOf(orderByComparator)
766                            };
767    
768                    List<ShoppingOrderItem> list = (List<ShoppingOrderItem>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
769                                    finderArgs, this);
770    
771                    if (list == null) {
772                            StringBundler query = null;
773                            String sql = null;
774    
775                            if (orderByComparator != null) {
776                                    query = new StringBundler(2 +
777                                                    (orderByComparator.getOrderByFields().length * 3));
778    
779                                    query.append(_SQL_SELECT_SHOPPINGORDERITEM);
780    
781                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
782                                            orderByComparator);
783    
784                                    sql = query.toString();
785                            }
786                            else {
787                                    sql = _SQL_SELECT_SHOPPINGORDERITEM.concat(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
788                            }
789    
790                            Session session = null;
791    
792                            try {
793                                    session = openSession();
794    
795                                    Query q = session.createQuery(sql);
796    
797                                    if (orderByComparator == null) {
798                                            list = (List<ShoppingOrderItem>)QueryUtil.list(q,
799                                                            getDialect(), start, end, false);
800    
801                                            Collections.sort(list);
802                                    }
803                                    else {
804                                            list = (List<ShoppingOrderItem>)QueryUtil.list(q,
805                                                            getDialect(), start, end);
806                                    }
807                            }
808                            catch (Exception e) {
809                                    throw processException(e);
810                            }
811                            finally {
812                                    if (list == null) {
813                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
814                                                    finderArgs);
815                                    }
816                                    else {
817                                            cacheResult(list);
818    
819                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
820                                                    list);
821                                    }
822    
823                                    closeSession(session);
824                            }
825                    }
826    
827                    return list;
828            }
829    
830            /**
831             * Removes all the shopping order items where orderId = &#63; from the database.
832             *
833             * @param orderId the order id to search with
834             * @throws SystemException if a system exception occurred
835             */
836            public void removeByOrderId(long orderId) throws SystemException {
837                    for (ShoppingOrderItem shoppingOrderItem : findByOrderId(orderId)) {
838                            remove(shoppingOrderItem);
839                    }
840            }
841    
842            /**
843             * Removes all the shopping order items from the database.
844             *
845             * @throws SystemException if a system exception occurred
846             */
847            public void removeAll() throws SystemException {
848                    for (ShoppingOrderItem shoppingOrderItem : findAll()) {
849                            remove(shoppingOrderItem);
850                    }
851            }
852    
853            /**
854             * Counts all the shopping order items where orderId = &#63;.
855             *
856             * @param orderId the order id to search with
857             * @return the number of matching shopping order items
858             * @throws SystemException if a system exception occurred
859             */
860            public int countByOrderId(long orderId) throws SystemException {
861                    Object[] finderArgs = new Object[] { orderId };
862    
863                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ORDERID,
864                                    finderArgs, this);
865    
866                    if (count == null) {
867                            StringBundler query = new StringBundler(2);
868    
869                            query.append(_SQL_COUNT_SHOPPINGORDERITEM_WHERE);
870    
871                            query.append(_FINDER_COLUMN_ORDERID_ORDERID_2);
872    
873                            String sql = query.toString();
874    
875                            Session session = null;
876    
877                            try {
878                                    session = openSession();
879    
880                                    Query q = session.createQuery(sql);
881    
882                                    QueryPos qPos = QueryPos.getInstance(q);
883    
884                                    qPos.add(orderId);
885    
886                                    count = (Long)q.uniqueResult();
887                            }
888                            catch (Exception e) {
889                                    throw processException(e);
890                            }
891                            finally {
892                                    if (count == null) {
893                                            count = Long.valueOf(0);
894                                    }
895    
896                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ORDERID,
897                                            finderArgs, count);
898    
899                                    closeSession(session);
900                            }
901                    }
902    
903                    return count.intValue();
904            }
905    
906            /**
907             * Counts all the shopping order items.
908             *
909             * @return the number of shopping order items
910             * @throws SystemException if a system exception occurred
911             */
912            public int countAll() throws SystemException {
913                    Object[] finderArgs = new Object[0];
914    
915                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
916                                    finderArgs, this);
917    
918                    if (count == null) {
919                            Session session = null;
920    
921                            try {
922                                    session = openSession();
923    
924                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGORDERITEM);
925    
926                                    count = (Long)q.uniqueResult();
927                            }
928                            catch (Exception e) {
929                                    throw processException(e);
930                            }
931                            finally {
932                                    if (count == null) {
933                                            count = Long.valueOf(0);
934                                    }
935    
936                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
937                                            count);
938    
939                                    closeSession(session);
940                            }
941                    }
942    
943                    return count.intValue();
944            }
945    
946            /**
947             * Initializes the shopping order item persistence.
948             */
949            public void afterPropertiesSet() {
950                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
951                                            com.liferay.portal.util.PropsUtil.get(
952                                                    "value.object.listener.com.liferay.portlet.shopping.model.ShoppingOrderItem")));
953    
954                    if (listenerClassNames.length > 0) {
955                            try {
956                                    List<ModelListener<ShoppingOrderItem>> listenersList = new ArrayList<ModelListener<ShoppingOrderItem>>();
957    
958                                    for (String listenerClassName : listenerClassNames) {
959                                            listenersList.add((ModelListener<ShoppingOrderItem>)InstanceFactory.newInstance(
960                                                            listenerClassName));
961                                    }
962    
963                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
964                            }
965                            catch (Exception e) {
966                                    _log.error(e);
967                            }
968                    }
969            }
970    
971            public void destroy() {
972                    EntityCacheUtil.removeCache(ShoppingOrderItemImpl.class.getName());
973                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
974                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
975            }
976    
977            @BeanReference(type = ShoppingCartPersistence.class)
978            protected ShoppingCartPersistence shoppingCartPersistence;
979            @BeanReference(type = ShoppingCategoryPersistence.class)
980            protected ShoppingCategoryPersistence shoppingCategoryPersistence;
981            @BeanReference(type = ShoppingCouponPersistence.class)
982            protected ShoppingCouponPersistence shoppingCouponPersistence;
983            @BeanReference(type = ShoppingItemPersistence.class)
984            protected ShoppingItemPersistence shoppingItemPersistence;
985            @BeanReference(type = ShoppingItemFieldPersistence.class)
986            protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
987            @BeanReference(type = ShoppingItemPricePersistence.class)
988            protected ShoppingItemPricePersistence shoppingItemPricePersistence;
989            @BeanReference(type = ShoppingOrderPersistence.class)
990            protected ShoppingOrderPersistence shoppingOrderPersistence;
991            @BeanReference(type = ShoppingOrderItemPersistence.class)
992            protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
993            @BeanReference(type = ResourcePersistence.class)
994            protected ResourcePersistence resourcePersistence;
995            @BeanReference(type = UserPersistence.class)
996            protected UserPersistence userPersistence;
997            private static final String _SQL_SELECT_SHOPPINGORDERITEM = "SELECT shoppingOrderItem FROM ShoppingOrderItem shoppingOrderItem";
998            private static final String _SQL_SELECT_SHOPPINGORDERITEM_WHERE = "SELECT shoppingOrderItem FROM ShoppingOrderItem shoppingOrderItem WHERE ";
999            private static final String _SQL_COUNT_SHOPPINGORDERITEM = "SELECT COUNT(shoppingOrderItem) FROM ShoppingOrderItem shoppingOrderItem";
1000            private static final String _SQL_COUNT_SHOPPINGORDERITEM_WHERE = "SELECT COUNT(shoppingOrderItem) FROM ShoppingOrderItem shoppingOrderItem WHERE ";
1001            private static final String _FINDER_COLUMN_ORDERID_ORDERID_2 = "shoppingOrderItem.orderId = ?";
1002            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingOrderItem.";
1003            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingOrderItem exists with the primary key ";
1004            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingOrderItem exists with the key {";
1005            private static Log _log = LogFactoryUtil.getLog(ShoppingOrderItemPersistenceImpl.class);
1006    }