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 ShoppingItemImpl() {
037            }
038    
039            @Override
040            public int compareTo(ShoppingItem item) {
041                    return new ItemNameComparator(true).compare(this, item);
042            }
043    
044            @Override
045            public ShoppingCategory getCategory() {
046                    ShoppingCategory category = null;
047    
048                    if (getCategoryId() > 0) {
049                            try {
050                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
051                                            getCategoryId());
052                            }
053                            catch (Exception e) {
054                                    category = new ShoppingCategoryImpl();
055    
056                                    category.setGroupId(getGroupId());
057    
058                                    _log.error(e);
059                            }
060                    }
061                    else {
062                            category = new ShoppingCategoryImpl();
063    
064                            category.setGroupId(getGroupId());
065                    }
066    
067                    return category;
068            }
069    
070            @Override
071            public String[] getFieldsQuantitiesArray() {
072                    return _fieldsQuantitiesArray;
073            }
074    
075            @Override
076            public List<ShoppingItemPrice> getItemPrices()
077                    throws PortalException, SystemException {
078    
079                    return ShoppingItemPriceLocalServiceUtil.getItemPrices(getItemId());
080            }
081    
082            @Override
083            public void setFieldsQuantities(String fieldsQuantities) {
084                    _fieldsQuantitiesArray = StringUtil.split(fieldsQuantities);
085    
086                    super.setFieldsQuantities(fieldsQuantities);
087            }
088    
089            @Override
090            public void setFieldsQuantitiesArray(String[] fieldsQuantitiesArray) {
091                    _fieldsQuantitiesArray = fieldsQuantitiesArray;
092    
093                    super.setFieldsQuantities(StringUtil.merge(fieldsQuantitiesArray));
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(ShoppingItemImpl.class);
097    
098            private String[] _fieldsQuantitiesArray;
099    
100    }