001    /**
002     * Copyright (c) 2000-2013 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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portlet.shopping.model.ShoppingCategory;
023    import com.liferay.portlet.shopping.model.ShoppingItem;
024    import com.liferay.portlet.shopping.model.ShoppingItemPrice;
025    import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
026    import com.liferay.portlet.shopping.service.ShoppingItemPriceLocalServiceUtil;
027    import com.liferay.portlet.shopping.util.comparator.ItemNameComparator;
028    
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class ShoppingItemImpl extends ShoppingItemBaseImpl {
035    
036            public static final int STOCK_QUANTITY_INFINITE_STOCK = -1;
037    
038            public ShoppingItemImpl() {
039            }
040    
041            @Override
042            public int compareTo(ShoppingItem item) {
043                    return new ItemNameComparator(true).compare(this, item);
044            }
045    
046            @Override
047            public ShoppingCategory getCategory() {
048                    ShoppingCategory category = null;
049    
050                    if (getCategoryId() > 0) {
051                            try {
052                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
053                                            getCategoryId());
054                            }
055                            catch (Exception e) {
056                                    category = new ShoppingCategoryImpl();
057    
058                                    category.setGroupId(getGroupId());
059    
060                                    _log.error(e);
061                            }
062                    }
063                    else {
064                            category = new ShoppingCategoryImpl();
065    
066                            category.setGroupId(getGroupId());
067                    }
068    
069                    return category;
070            }
071    
072            @Override
073            public String[] getFieldsQuantitiesArray() {
074                    return _fieldsQuantitiesArray;
075            }
076    
077            @Override
078            public List<ShoppingItemPrice> getItemPrices()
079                    throws PortalException, SystemException {
080    
081                    return ShoppingItemPriceLocalServiceUtil.getItemPrices(getItemId());
082            }
083    
084            @Override
085            public boolean isInfiniteStock() {
086                    if (getStockQuantity() == STOCK_QUANTITY_INFINITE_STOCK) {
087                            return true;
088                    }
089    
090                    return false;
091            }
092    
093            @Override
094            public void setFieldsQuantities(String fieldsQuantities) {
095                    _fieldsQuantitiesArray = StringUtil.split(fieldsQuantities);
096    
097                    super.setFieldsQuantities(fieldsQuantities);
098            }
099    
100            @Override
101            public void setFieldsQuantitiesArray(String[] fieldsQuantitiesArray) {
102                    _fieldsQuantitiesArray = fieldsQuantitiesArray;
103    
104                    super.setFieldsQuantities(StringUtil.merge(fieldsQuantitiesArray));
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(ShoppingItemImpl.class);
108    
109            private String[] _fieldsQuantitiesArray;
110    
111    }