001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
439    
440                            try {
441                                    session = openSession();
442    
443                                    StringBundler query = null;
444    
445                                    if (orderByComparator != null) {
446                                            query = new StringBundler(3 +
447                                                            (orderByComparator.getOrderByFields().length * 3));
448                                    }
449                                    else {
450                                            query = new StringBundler(3);
451                                    }
452    
453                                    query.append(_SQL_SELECT_SHOPPINGORDERITEM_WHERE);
454    
455                                    query.append(_FINDER_COLUMN_ORDERID_ORDERID_2);
456    
457                                    if (orderByComparator != null) {
458                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
459                                                    orderByComparator);
460                                    }
461    
462                                    else {
463                                            query.append(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
464                                    }
465    
466                                    String sql = query.toString();
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                                            list = new ArrayList<ShoppingOrderItem>();
483                                    }
484    
485                                    cacheResult(list);
486    
487                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_ORDERID,
488                                            finderArgs, list);
489    
490                                    closeSession(session);
491                            }
492                    }
493    
494                    return list;
495            }
496    
497            /**
498             * Finds the first shopping order item in the ordered set where orderId = &#63;.
499             *
500             * <p>
501             * 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.
502             * </p>
503             *
504             * @param orderId the order id to search with
505             * @param orderByComparator the comparator to order the set by
506             * @return the first matching shopping order item
507             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a matching shopping order item could not be found
508             * @throws SystemException if a system exception occurred
509             */
510            public ShoppingOrderItem findByOrderId_First(long orderId,
511                    OrderByComparator orderByComparator)
512                    throws NoSuchOrderItemException, SystemException {
513                    List<ShoppingOrderItem> list = findByOrderId(orderId, 0, 1,
514                                    orderByComparator);
515    
516                    if (list.isEmpty()) {
517                            StringBundler msg = new StringBundler(4);
518    
519                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
520    
521                            msg.append("orderId=");
522                            msg.append(orderId);
523    
524                            msg.append(StringPool.CLOSE_CURLY_BRACE);
525    
526                            throw new NoSuchOrderItemException(msg.toString());
527                    }
528                    else {
529                            return list.get(0);
530                    }
531            }
532    
533            /**
534             * Finds the last shopping order item in the ordered set where orderId = &#63;.
535             *
536             * <p>
537             * 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.
538             * </p>
539             *
540             * @param orderId the order id to search with
541             * @param orderByComparator the comparator to order the set by
542             * @return the last matching shopping order item
543             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a matching shopping order item could not be found
544             * @throws SystemException if a system exception occurred
545             */
546            public ShoppingOrderItem findByOrderId_Last(long orderId,
547                    OrderByComparator orderByComparator)
548                    throws NoSuchOrderItemException, SystemException {
549                    int count = countByOrderId(orderId);
550    
551                    List<ShoppingOrderItem> list = findByOrderId(orderId, count - 1, count,
552                                    orderByComparator);
553    
554                    if (list.isEmpty()) {
555                            StringBundler msg = new StringBundler(4);
556    
557                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
558    
559                            msg.append("orderId=");
560                            msg.append(orderId);
561    
562                            msg.append(StringPool.CLOSE_CURLY_BRACE);
563    
564                            throw new NoSuchOrderItemException(msg.toString());
565                    }
566                    else {
567                            return list.get(0);
568                    }
569            }
570    
571            /**
572             * Finds the shopping order items before and after the current shopping order item in the ordered set where orderId = &#63;.
573             *
574             * <p>
575             * 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.
576             * </p>
577             *
578             * @param orderItemId the primary key of the current shopping order item
579             * @param orderId the order id to search with
580             * @param orderByComparator the comparator to order the set by
581             * @return the previous, current, and next shopping order item
582             * @throws com.liferay.portlet.shopping.NoSuchOrderItemException if a shopping order item with the primary key could not be found
583             * @throws SystemException if a system exception occurred
584             */
585            public ShoppingOrderItem[] findByOrderId_PrevAndNext(long orderItemId,
586                    long orderId, OrderByComparator orderByComparator)
587                    throws NoSuchOrderItemException, SystemException {
588                    ShoppingOrderItem shoppingOrderItem = findByPrimaryKey(orderItemId);
589    
590                    Session session = null;
591    
592                    try {
593                            session = openSession();
594    
595                            ShoppingOrderItem[] array = new ShoppingOrderItemImpl[3];
596    
597                            array[0] = getByOrderId_PrevAndNext(session, shoppingOrderItem,
598                                            orderId, orderByComparator, true);
599    
600                            array[1] = shoppingOrderItem;
601    
602                            array[2] = getByOrderId_PrevAndNext(session, shoppingOrderItem,
603                                            orderId, orderByComparator, false);
604    
605                            return array;
606                    }
607                    catch (Exception e) {
608                            throw processException(e);
609                    }
610                    finally {
611                            closeSession(session);
612                    }
613            }
614    
615            protected ShoppingOrderItem getByOrderId_PrevAndNext(Session session,
616                    ShoppingOrderItem shoppingOrderItem, long orderId,
617                    OrderByComparator orderByComparator, boolean previous) {
618                    StringBundler query = null;
619    
620                    if (orderByComparator != null) {
621                            query = new StringBundler(6 +
622                                            (orderByComparator.getOrderByFields().length * 6));
623                    }
624                    else {
625                            query = new StringBundler(3);
626                    }
627    
628                    query.append(_SQL_SELECT_SHOPPINGORDERITEM_WHERE);
629    
630                    query.append(_FINDER_COLUMN_ORDERID_ORDERID_2);
631    
632                    if (orderByComparator != null) {
633                            String[] orderByFields = orderByComparator.getOrderByFields();
634    
635                            if (orderByFields.length > 0) {
636                                    query.append(WHERE_AND);
637                            }
638    
639                            for (int i = 0; i < orderByFields.length; i++) {
640                                    query.append(_ORDER_BY_ENTITY_ALIAS);
641                                    query.append(orderByFields[i]);
642    
643                                    if ((i + 1) < orderByFields.length) {
644                                            if (orderByComparator.isAscending() ^ previous) {
645                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
646                                            }
647                                            else {
648                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
649                                            }
650                                    }
651                                    else {
652                                            if (orderByComparator.isAscending() ^ previous) {
653                                                    query.append(WHERE_GREATER_THAN);
654                                            }
655                                            else {
656                                                    query.append(WHERE_LESSER_THAN);
657                                            }
658                                    }
659                            }
660    
661                            query.append(ORDER_BY_CLAUSE);
662    
663                            for (int i = 0; i < orderByFields.length; i++) {
664                                    query.append(_ORDER_BY_ENTITY_ALIAS);
665                                    query.append(orderByFields[i]);
666    
667                                    if ((i + 1) < orderByFields.length) {
668                                            if (orderByComparator.isAscending() ^ previous) {
669                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
670                                            }
671                                            else {
672                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
673                                            }
674                                    }
675                                    else {
676                                            if (orderByComparator.isAscending() ^ previous) {
677                                                    query.append(ORDER_BY_ASC);
678                                            }
679                                            else {
680                                                    query.append(ORDER_BY_DESC);
681                                            }
682                                    }
683                            }
684                    }
685    
686                    else {
687                            query.append(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
688                    }
689    
690                    String sql = query.toString();
691    
692                    Query q = session.createQuery(sql);
693    
694                    q.setFirstResult(0);
695                    q.setMaxResults(2);
696    
697                    QueryPos qPos = QueryPos.getInstance(q);
698    
699                    qPos.add(orderId);
700    
701                    if (orderByComparator != null) {
702                            Object[] values = orderByComparator.getOrderByValues(shoppingOrderItem);
703    
704                            for (Object value : values) {
705                                    qPos.add(value);
706                            }
707                    }
708    
709                    List<ShoppingOrderItem> list = q.list();
710    
711                    if (list.size() == 2) {
712                            return list.get(1);
713                    }
714                    else {
715                            return null;
716                    }
717            }
718    
719            /**
720             * Finds all the shopping order items.
721             *
722             * @return the shopping order items
723             * @throws SystemException if a system exception occurred
724             */
725            public List<ShoppingOrderItem> findAll() throws SystemException {
726                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
727            }
728    
729            /**
730             * Finds a range of all the shopping order items.
731             *
732             * <p>
733             * 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.
734             * </p>
735             *
736             * @param start the lower bound of the range of shopping order items to return
737             * @param end the upper bound of the range of shopping order items to return (not inclusive)
738             * @return the range of shopping order items
739             * @throws SystemException if a system exception occurred
740             */
741            public List<ShoppingOrderItem> findAll(int start, int end)
742                    throws SystemException {
743                    return findAll(start, end, null);
744            }
745    
746            /**
747             * Finds an ordered range of all the shopping order items.
748             *
749             * <p>
750             * 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.
751             * </p>
752             *
753             * @param start the lower bound of the range of shopping order items to return
754             * @param end the upper bound of the range of shopping order items to return (not inclusive)
755             * @param orderByComparator the comparator to order the results by
756             * @return the ordered range of shopping order items
757             * @throws SystemException if a system exception occurred
758             */
759            public List<ShoppingOrderItem> findAll(int start, int end,
760                    OrderByComparator orderByComparator) throws SystemException {
761                    Object[] finderArgs = new Object[] {
762                                    String.valueOf(start), String.valueOf(end),
763                                    String.valueOf(orderByComparator)
764                            };
765    
766                    List<ShoppingOrderItem> list = (List<ShoppingOrderItem>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
767                                    finderArgs, this);
768    
769                    if (list == null) {
770                            Session session = null;
771    
772                            try {
773                                    session = openSession();
774    
775                                    StringBundler query = null;
776                                    String sql = null;
777    
778                                    if (orderByComparator != null) {
779                                            query = new StringBundler(2 +
780                                                            (orderByComparator.getOrderByFields().length * 3));
781    
782                                            query.append(_SQL_SELECT_SHOPPINGORDERITEM);
783    
784                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
785                                                    orderByComparator);
786    
787                                            sql = query.toString();
788                                    }
789                                    else {
790                                            sql = _SQL_SELECT_SHOPPINGORDERITEM.concat(ShoppingOrderItemModelImpl.ORDER_BY_JPQL);
791                                    }
792    
793                                    Query q = session.createQuery(sql);
794    
795                                    if (orderByComparator == null) {
796                                            list = (List<ShoppingOrderItem>)QueryUtil.list(q,
797                                                            getDialect(), start, end, false);
798    
799                                            Collections.sort(list);
800                                    }
801                                    else {
802                                            list = (List<ShoppingOrderItem>)QueryUtil.list(q,
803                                                            getDialect(), start, end);
804                                    }
805                            }
806                            catch (Exception e) {
807                                    throw processException(e);
808                            }
809                            finally {
810                                    if (list == null) {
811                                            list = new ArrayList<ShoppingOrderItem>();
812                                    }
813    
814                                    cacheResult(list);
815    
816                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
817    
818                                    closeSession(session);
819                            }
820                    }
821    
822                    return list;
823            }
824    
825            /**
826             * Removes all the shopping order items where orderId = &#63; from the database.
827             *
828             * @param orderId the order id to search with
829             * @throws SystemException if a system exception occurred
830             */
831            public void removeByOrderId(long orderId) throws SystemException {
832                    for (ShoppingOrderItem shoppingOrderItem : findByOrderId(orderId)) {
833                            remove(shoppingOrderItem);
834                    }
835            }
836    
837            /**
838             * Removes all the shopping order items from the database.
839             *
840             * @throws SystemException if a system exception occurred
841             */
842            public void removeAll() throws SystemException {
843                    for (ShoppingOrderItem shoppingOrderItem : findAll()) {
844                            remove(shoppingOrderItem);
845                    }
846            }
847    
848            /**
849             * Counts all the shopping order items where orderId = &#63;.
850             *
851             * @param orderId the order id to search with
852             * @return the number of matching shopping order items
853             * @throws SystemException if a system exception occurred
854             */
855            public int countByOrderId(long orderId) throws SystemException {
856                    Object[] finderArgs = new Object[] { orderId };
857    
858                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ORDERID,
859                                    finderArgs, this);
860    
861                    if (count == null) {
862                            Session session = null;
863    
864                            try {
865                                    session = openSession();
866    
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                                    Query q = session.createQuery(sql);
876    
877                                    QueryPos qPos = QueryPos.getInstance(q);
878    
879                                    qPos.add(orderId);
880    
881                                    count = (Long)q.uniqueResult();
882                            }
883                            catch (Exception e) {
884                                    throw processException(e);
885                            }
886                            finally {
887                                    if (count == null) {
888                                            count = Long.valueOf(0);
889                                    }
890    
891                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ORDERID,
892                                            finderArgs, count);
893    
894                                    closeSession(session);
895                            }
896                    }
897    
898                    return count.intValue();
899            }
900    
901            /**
902             * Counts all the shopping order items.
903             *
904             * @return the number of shopping order items
905             * @throws SystemException if a system exception occurred
906             */
907            public int countAll() throws SystemException {
908                    Object[] finderArgs = new Object[0];
909    
910                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
911                                    finderArgs, this);
912    
913                    if (count == null) {
914                            Session session = null;
915    
916                            try {
917                                    session = openSession();
918    
919                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGORDERITEM);
920    
921                                    count = (Long)q.uniqueResult();
922                            }
923                            catch (Exception e) {
924                                    throw processException(e);
925                            }
926                            finally {
927                                    if (count == null) {
928                                            count = Long.valueOf(0);
929                                    }
930    
931                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
932                                            count);
933    
934                                    closeSession(session);
935                            }
936                    }
937    
938                    return count.intValue();
939            }
940    
941            /**
942             * Initializes the shopping order item persistence.
943             */
944            public void afterPropertiesSet() {
945                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
946                                            com.liferay.portal.util.PropsUtil.get(
947                                                    "value.object.listener.com.liferay.portlet.shopping.model.ShoppingOrderItem")));
948    
949                    if (listenerClassNames.length > 0) {
950                            try {
951                                    List<ModelListener<ShoppingOrderItem>> listenersList = new ArrayList<ModelListener<ShoppingOrderItem>>();
952    
953                                    for (String listenerClassName : listenerClassNames) {
954                                            listenersList.add((ModelListener<ShoppingOrderItem>)InstanceFactory.newInstance(
955                                                            listenerClassName));
956                                    }
957    
958                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
959                            }
960                            catch (Exception e) {
961                                    _log.error(e);
962                            }
963                    }
964            }
965    
966            public void destroy() {
967                    EntityCacheUtil.removeCache(ShoppingOrderItemImpl.class.getName());
968                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
969                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
970            }
971    
972            @BeanReference(type = ShoppingCartPersistence.class)
973            protected ShoppingCartPersistence shoppingCartPersistence;
974            @BeanReference(type = ShoppingCategoryPersistence.class)
975            protected ShoppingCategoryPersistence shoppingCategoryPersistence;
976            @BeanReference(type = ShoppingCouponPersistence.class)
977            protected ShoppingCouponPersistence shoppingCouponPersistence;
978            @BeanReference(type = ShoppingItemPersistence.class)
979            protected ShoppingItemPersistence shoppingItemPersistence;
980            @BeanReference(type = ShoppingItemFieldPersistence.class)
981            protected ShoppingItemFieldPersistence shoppingItemFieldPersistence;
982            @BeanReference(type = ShoppingItemPricePersistence.class)
983            protected ShoppingItemPricePersistence shoppingItemPricePersistence;
984            @BeanReference(type = ShoppingOrderPersistence.class)
985            protected ShoppingOrderPersistence shoppingOrderPersistence;
986            @BeanReference(type = ShoppingOrderItemPersistence.class)
987            protected ShoppingOrderItemPersistence shoppingOrderItemPersistence;
988            @BeanReference(type = ResourcePersistence.class)
989            protected ResourcePersistence resourcePersistence;
990            @BeanReference(type = UserPersistence.class)
991            protected UserPersistence userPersistence;
992            private static final String _SQL_SELECT_SHOPPINGORDERITEM = "SELECT shoppingOrderItem FROM ShoppingOrderItem shoppingOrderItem";
993            private static final String _SQL_SELECT_SHOPPINGORDERITEM_WHERE = "SELECT shoppingOrderItem FROM ShoppingOrderItem shoppingOrderItem WHERE ";
994            private static final String _SQL_COUNT_SHOPPINGORDERITEM = "SELECT COUNT(shoppingOrderItem) FROM ShoppingOrderItem shoppingOrderItem";
995            private static final String _SQL_COUNT_SHOPPINGORDERITEM_WHERE = "SELECT COUNT(shoppingOrderItem) FROM ShoppingOrderItem shoppingOrderItem WHERE ";
996            private static final String _FINDER_COLUMN_ORDERID_ORDERID_2 = "shoppingOrderItem.orderId = ?";
997            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingOrderItem.";
998            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingOrderItem exists with the primary key ";
999            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingOrderItem exists with the key {";
1000            private static Log _log = LogFactoryUtil.getLog(ShoppingOrderItemPersistenceImpl.class);
1001    }