001    /**
002     * Copyright (c) 2000-2010 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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.upload.UploadPortletRequest;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.shopping.DuplicateItemSKUException;
030    import com.liferay.portlet.shopping.ItemLargeImageNameException;
031    import com.liferay.portlet.shopping.ItemLargeImageSizeException;
032    import com.liferay.portlet.shopping.ItemMediumImageNameException;
033    import com.liferay.portlet.shopping.ItemMediumImageSizeException;
034    import com.liferay.portlet.shopping.ItemNameException;
035    import com.liferay.portlet.shopping.ItemSKUException;
036    import com.liferay.portlet.shopping.ItemSmallImageNameException;
037    import com.liferay.portlet.shopping.ItemSmallImageSizeException;
038    import com.liferay.portlet.shopping.NoSuchCategoryException;
039    import com.liferay.portlet.shopping.NoSuchItemException;
040    import com.liferay.portlet.shopping.model.ShoppingItem;
041    import com.liferay.portlet.shopping.model.ShoppingItemField;
042    import com.liferay.portlet.shopping.model.ShoppingItemPrice;
043    import com.liferay.portlet.shopping.model.ShoppingItemPriceConstants;
044    import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
045    import com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldUtil;
046    import com.liferay.portlet.shopping.service.persistence.ShoppingItemPriceUtil;
047    
048    import java.io.File;
049    
050    import java.util.ArrayList;
051    import java.util.List;
052    
053    import javax.portlet.ActionRequest;
054    import javax.portlet.ActionResponse;
055    import javax.portlet.PortletConfig;
056    import javax.portlet.RenderRequest;
057    import javax.portlet.RenderResponse;
058    
059    import org.apache.struts.action.ActionForm;
060    import org.apache.struts.action.ActionForward;
061    import org.apache.struts.action.ActionMapping;
062    
063    /**
064     * @author Brian Wing Shun Chan
065     */
066    public class EditItemAction extends PortletAction {
067    
068            public void processAction(
069                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070                            ActionRequest actionRequest, ActionResponse actionResponse)
071                    throws Exception {
072    
073                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
074    
075                    try {
076                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
077                                    updateItem(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.DELETE)) {
080                                    deleteItem(actionRequest);
081                            }
082    
083                            if (Validator.isNotNull(cmd)) {
084                                    sendRedirect(actionRequest, actionResponse);
085                            }
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchCategoryException ||
089                                    e instanceof NoSuchItemException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(actionRequest, e.getClass().getName());
093    
094                                    setForward(actionRequest, "portlet.shopping.error");
095                            }
096                            else if (e instanceof DuplicateItemSKUException ||
097                                             e instanceof ItemLargeImageNameException ||
098                                             e instanceof ItemLargeImageSizeException ||
099                                             e instanceof ItemMediumImageNameException ||
100                                             e instanceof ItemMediumImageSizeException ||
101                                             e instanceof ItemNameException ||
102                                             e instanceof ItemSKUException ||
103                                             e instanceof ItemSmallImageNameException ||
104                                             e instanceof ItemSmallImageSizeException) {
105    
106                                    SessionErrors.add(actionRequest, e.getClass().getName());
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112            }
113    
114            public ActionForward render(
115                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
116                            RenderRequest renderRequest, RenderResponse renderResponse)
117                    throws Exception {
118    
119                    try {
120                            ActionUtil.getItem(renderRequest);
121                    }
122                    catch (Exception e) {
123                            if (e instanceof NoSuchItemException ||
124                                    e instanceof PrincipalException) {
125    
126                                    SessionErrors.add(renderRequest, e.getClass().getName());
127    
128                                    return mapping.findForward("portlet.shopping.error");
129                            }
130                            else {
131                                    throw e;
132                            }
133                    }
134    
135                    return mapping.findForward(
136                            getForward(renderRequest, "portlet.shopping.edit_item"));
137            }
138    
139            protected void deleteItem(ActionRequest actionRequest) throws Exception {
140                    long itemId = ParamUtil.getLong(actionRequest, "itemId");
141    
142                    ShoppingItemServiceUtil.deleteItem(itemId);
143            }
144    
145            protected void updateItem(ActionRequest actionRequest) throws Exception {
146                    UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
147                            actionRequest);
148    
149                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150                            WebKeys.THEME_DISPLAY);
151    
152                    long itemId = ParamUtil.getLong(uploadRequest, "itemId");
153    
154                    long groupId = themeDisplay.getScopeGroupId();
155                    long categoryId = ParamUtil.getLong(uploadRequest, "categoryId");
156                    String sku = ParamUtil.getString(uploadRequest, "sku");
157                    String name = ParamUtil.getString(uploadRequest, "name");
158                    String description = ParamUtil.getString(uploadRequest, "description");
159                    String properties = ParamUtil.getString(uploadRequest, "properties");
160    
161                    int fieldsCount = ParamUtil.getInteger(uploadRequest, "fieldsCount", 1);
162    
163                    List<ShoppingItemField> itemFields = new ArrayList<ShoppingItemField>();
164    
165                    for (int i = 0; i < fieldsCount; i ++) {
166                            String fieldName = ParamUtil.getString(
167                                    uploadRequest, "fieldName" + i);
168                            String fieldValues = ParamUtil.getString(
169                                    uploadRequest, "fieldValues" + i);
170                            String fieldDescription = ParamUtil.getString(
171                                    uploadRequest, "fieldDescription" + i);
172    
173                            ShoppingItemField itemField = ShoppingItemFieldUtil.create(0);
174    
175                            itemField.setName(fieldName);
176                            itemField.setValues(fieldValues);
177                            itemField.setDescription(fieldDescription);
178    
179                            itemFields.add(itemField);
180                    }
181    
182                    String fieldsQuantities = ParamUtil.getString(
183                            uploadRequest, "fieldsQuantities");
184    
185                    int pricesCount = ParamUtil.getInteger(uploadRequest, "pricesCount", 1);
186    
187                    List<ShoppingItemPrice> itemPrices = new ArrayList<ShoppingItemPrice>();
188    
189                    for (int i = 0; i < pricesCount; i ++) {
190                            int minQuantity = ParamUtil.getInteger(
191                                    uploadRequest, "minQuantity" + i);
192                            int maxQuantity = ParamUtil.getInteger(
193                                    uploadRequest, "maxQuantity" + i);
194                            double price = ParamUtil.getDouble(uploadRequest, "price" + i);
195                            double discount = ParamUtil.getDouble(
196                                    uploadRequest, "discount" + i) / 100;
197                            boolean taxable = ParamUtil.getBoolean(
198                                    uploadRequest, "taxable" + i);
199                            double shipping = ParamUtil.getDouble(
200                                    uploadRequest, "shipping" + i);
201                            boolean useShippingFormula = ParamUtil.getBoolean(
202                                    uploadRequest, "useShippingFormula" + i);
203                            boolean active = ParamUtil.getBoolean(uploadRequest, "active" + i);
204                            int defaultPrice = ParamUtil.getInteger(
205                                    uploadRequest, "defaultPrice");
206    
207                            int status = ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT;
208    
209                            if ((defaultPrice != i) && active) {
210                                    status = ShoppingItemPriceConstants.STATUS_ACTIVE;
211                            }
212                            else if ((defaultPrice != i) && !active) {
213                                    status = ShoppingItemPriceConstants.STATUS_INACTIVE;
214                            }
215    
216                            ShoppingItemPrice itemPrice = ShoppingItemPriceUtil.create(0);
217    
218                            itemPrice.setMinQuantity(minQuantity);
219                            itemPrice.setMaxQuantity(maxQuantity);
220                            itemPrice.setPrice(price);
221                            itemPrice.setDiscount(discount);
222                            itemPrice.setTaxable(taxable);
223                            itemPrice.setShipping(shipping);
224                            itemPrice.setUseShippingFormula(useShippingFormula);
225                            itemPrice.setStatus(status);
226    
227                            itemPrices.add(itemPrice);
228                    }
229    
230                    boolean requiresShipping = ParamUtil.getBoolean(
231                            uploadRequest, "requiresShipping");
232                    int stockQuantity = ParamUtil.getInteger(
233                            uploadRequest, "stockQuantity");
234    
235                    boolean featured = ParamUtil.getBoolean(uploadRequest, "featured");
236                    Boolean sale = null;
237    
238                    boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
239                    String smallImageURL = ParamUtil.getString(
240                            uploadRequest, "smallImageURL");
241                    File smallFile = uploadRequest.getFile("smallFile");
242    
243                    boolean mediumImage = ParamUtil.getBoolean(
244                            uploadRequest, "mediumImage");
245                    String mediumImageURL = ParamUtil.getString(
246                            uploadRequest, "mediumImageURL");
247                    File mediumFile = uploadRequest.getFile("mediumFile");
248    
249                    boolean largeImage = ParamUtil.getBoolean(uploadRequest, "largeImage");
250                    String largeImageURL = ParamUtil.getString(
251                            uploadRequest, "largeImageURL");
252                    File largeFile = uploadRequest.getFile("largeFile");
253    
254                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
255                            ShoppingItem.class.getName(), actionRequest);
256    
257                    if (itemId <= 0) {
258    
259                            // Add item
260    
261                            ShoppingItemServiceUtil.addItem(
262                                    groupId, categoryId, sku, name, description, properties,
263                                    fieldsQuantities, requiresShipping, stockQuantity, featured,
264                                    sale, smallImage, smallImageURL, smallFile, mediumImage,
265                                    mediumImageURL, mediumFile, largeImage, largeImageURL,
266                                    largeFile, itemFields, itemPrices, serviceContext);
267                    }
268                    else {
269    
270                            // Update item
271    
272                            ShoppingItemServiceUtil.updateItem(
273                                    itemId, groupId, categoryId, sku, name, description, properties,
274                                    fieldsQuantities, requiresShipping, stockQuantity, featured,
275                                    sale, smallImage, smallImageURL, smallFile, mediumImage,
276                                    mediumImageURL, mediumFile, largeImage, largeImageURL,
277                                    largeFile, itemFields, itemPrices, serviceContext);
278                    }
279            }
280    
281    }