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            @Override
041            public int compareTo(ShoppingItem item) {
042                    return new ItemNameComparator(true).compare(this, item);
043            }
044    
045            @Override
046            public ShoppingCategory getCategory() {
047                    ShoppingCategory category = null;
048    
049                    if (getCategoryId() > 0) {
050                            try {
051                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
052                                            getCategoryId());
053                            }
054                            catch (Exception e) {
055                                    category = new ShoppingCategoryImpl();
056    
057                                    category.setGroupId(getGroupId());
058    
059                                    _log.error(e);
060                            }
061                    }
062                    else {
063                            category = new ShoppingCategoryImpl();
064    
065                            category.setGroupId(getGroupId());
066                    }
067    
068                    return category;
069            }
070    
071            @Override
072            public String[] getFieldsQuantitiesArray() {
073                    return _fieldsQuantitiesArray;
074            }
075    
076            @Override
077            public List<ShoppingItemPrice> getItemPrices() throws PortalException {
078                    return ShoppingItemPriceLocalServiceUtil.getItemPrices(getItemId());
079            }
080    
081            @Override
082            public String getShoppingItemImageURL(ThemeDisplay themeDisplay) {
083                    if (!isSmallImage()) {
084                            return null;
085                    }
086    
087                    if (Validator.isNotNull(getSmallImageURL())) {
088                            return getSmallImageURL();
089                    }
090    
091                    return themeDisplay.getPathImage() + "/shopping/item?img_id=" +
092                            getSmallImageId() + "&t=" +
093                                    WebServerServletTokenUtil.getToken(getSmallImageId());
094            }
095    
096            @Override
097            public boolean isInfiniteStock() {
098                    if (getStockQuantity() == STOCK_QUANTITY_INFINITE_STOCK) {
099                            return true;
100                    }
101    
102                    return false;
103            }
104    
105            @Override
106            public void setFieldsQuantities(String fieldsQuantities) {
107                    _fieldsQuantitiesArray = StringUtil.split(fieldsQuantities);
108    
109                    super.setFieldsQuantities(fieldsQuantities);
110            }
111    
112            @Override
113            public void setFieldsQuantitiesArray(String[] fieldsQuantitiesArray) {
114                    _fieldsQuantitiesArray = fieldsQuantitiesArray;
115    
116                    super.setFieldsQuantities(StringUtil.merge(fieldsQuantitiesArray));
117            }
118    
119            private static final Log _log = LogFactoryUtil.getLog(
120                    ShoppingItemImpl.class);
121    
122            private String[] _fieldsQuantitiesArray;
123    
124    }