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