001    /**
002     * Copyright (c) 2000-2012 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            @Override
069            public void processAction(
070                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
071                            ActionRequest actionRequest, ActionResponse actionResponse)
072                    throws Exception {
073    
074                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
075    
076                    try {
077                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
078                                    updateItem(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.DELETE)) {
081                                    deleteItem(actionRequest);
082                            }
083    
084                            if (Validator.isNotNull(cmd)) {
085                                    sendRedirect(actionRequest, actionResponse);
086                            }
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchCategoryException ||
090                                    e instanceof NoSuchItemException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(actionRequest, e.getClass().getName());
094    
095                                    setForward(actionRequest, "portlet.shopping.error");
096                            }
097                            else if (e instanceof DuplicateItemSKUException ||
098                                             e instanceof ItemLargeImageNameException ||
099                                             e instanceof ItemLargeImageSizeException ||
100                                             e instanceof ItemMediumImageNameException ||
101                                             e instanceof ItemMediumImageSizeException ||
102                                             e instanceof ItemNameException ||
103                                             e instanceof ItemSKUException ||
104                                             e instanceof ItemSmallImageNameException ||
105                                             e instanceof ItemSmallImageSizeException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass().getName());
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113            }
114    
115            @Override
116            public ActionForward render(
117                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
118                            RenderRequest renderRequest, RenderResponse renderResponse)
119                    throws Exception {
120    
121                    try {
122                            ActionUtil.getItem(renderRequest);
123                    }
124                    catch (Exception e) {
125                            if (e instanceof NoSuchItemException ||
126                                    e instanceof PrincipalException) {
127    
128                                    SessionErrors.add(renderRequest, e.getClass().getName());
129    
130                                    return mapping.findForward("portlet.shopping.error");
131                            }
132                            else {
133                                    throw e;
134                            }
135                    }
136    
137                    return mapping.findForward(
138                            getForward(renderRequest, "portlet.shopping.edit_item"));
139            }
140    
141            protected void deleteItem(ActionRequest actionRequest) throws Exception {
142                    long itemId = ParamUtil.getLong(actionRequest, "itemId");
143    
144                    ShoppingItemServiceUtil.deleteItem(itemId);
145            }
146    
147            protected void updateItem(ActionRequest actionRequest) throws Exception {
148                    UploadPortletRequest uploadPortletRequest =
149                            PortalUtil.getUploadPortletRequest(actionRequest);
150    
151                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
152                            WebKeys.THEME_DISPLAY);
153    
154                    long itemId = ParamUtil.getLong(uploadPortletRequest, "itemId");
155    
156                    long groupId = themeDisplay.getScopeGroupId();
157                    long categoryId = ParamUtil.getLong(uploadPortletRequest, "categoryId");
158                    String sku = ParamUtil.getString(uploadPortletRequest, "sku");
159                    String name = ParamUtil.getString(uploadPortletRequest, "name");
160                    String description = ParamUtil.getString(
161                            uploadPortletRequest, "description");
162                    String properties = ParamUtil.getString(
163                            uploadPortletRequest, "properties");
164    
165                    int fieldsCount = ParamUtil.getInteger(
166                            uploadPortletRequest, "fieldsCount", 1);
167    
168                    List<ShoppingItemField> itemFields = new ArrayList<ShoppingItemField>();
169    
170                    for (int i = 0; i < fieldsCount; i ++) {
171                            String fieldName = ParamUtil.getString(
172                                    uploadPortletRequest, "fieldName" + i);
173                            String fieldValues = ParamUtil.getString(
174                                    uploadPortletRequest, "fieldValues" + i);
175                            String fieldDescription = ParamUtil.getString(
176                                    uploadPortletRequest, "fieldDescription" + i);
177    
178                            ShoppingItemField itemField = ShoppingItemFieldUtil.create(0);
179    
180                            itemField.setName(fieldName);
181                            itemField.setValues(fieldValues);
182                            itemField.setDescription(fieldDescription);
183    
184                            itemFields.add(itemField);
185                    }
186    
187                    String fieldsQuantities = ParamUtil.getString(
188                            uploadPortletRequest, "fieldsQuantities");
189    
190                    int pricesCount = ParamUtil.getInteger(
191                            uploadPortletRequest, "pricesCount", 1);
192    
193                    List<ShoppingItemPrice> itemPrices = new ArrayList<ShoppingItemPrice>();
194    
195                    for (int i = 0; i < pricesCount; i ++) {
196                            int minQuantity = ParamUtil.getInteger(
197                                    uploadPortletRequest, "minQuantity" + i);
198                            int maxQuantity = ParamUtil.getInteger(
199                                    uploadPortletRequest, "maxQuantity" + i);
200                            double price = ParamUtil.getDouble(
201                                    uploadPortletRequest, "price" + i);
202                            double discount = ParamUtil.getDouble(
203                                    uploadPortletRequest, "discount" + i) / 100;
204                            boolean taxable = ParamUtil.getBoolean(
205                                    uploadPortletRequest, "taxable" + i);
206                            double shipping = ParamUtil.getDouble(
207                                    uploadPortletRequest, "shipping" + i);
208                            boolean useShippingFormula = ParamUtil.getBoolean(
209                                    uploadPortletRequest, "useShippingFormula" + i);
210                            boolean active = ParamUtil.getBoolean(
211                                    uploadPortletRequest, "active" + i);
212                            int defaultPrice = ParamUtil.getInteger(
213                                    uploadPortletRequest, "defaultPrice");
214    
215                            int status = ShoppingItemPriceConstants.STATUS_ACTIVE_DEFAULT;
216    
217                            if ((defaultPrice != i) && active) {
218                                    status = ShoppingItemPriceConstants.STATUS_ACTIVE;
219                            }
220                            else if ((defaultPrice != i) && !active) {
221                                    status = ShoppingItemPriceConstants.STATUS_INACTIVE;
222                            }
223    
224                            ShoppingItemPrice itemPrice = ShoppingItemPriceUtil.create(0);
225    
226                            itemPrice.setMinQuantity(minQuantity);
227                            itemPrice.setMaxQuantity(maxQuantity);
228                            itemPrice.setPrice(price);
229                            itemPrice.setDiscount(discount);
230                            itemPrice.setTaxable(taxable);
231                            itemPrice.setShipping(shipping);
232                            itemPrice.setUseShippingFormula(useShippingFormula);
233                            itemPrice.setStatus(status);
234    
235                            itemPrices.add(itemPrice);
236                    }
237    
238                    boolean requiresShipping = ParamUtil.getBoolean(
239                            uploadPortletRequest, "requiresShipping");
240                    int stockQuantity = ParamUtil.getInteger(
241                            uploadPortletRequest, "stockQuantity");
242    
243                    boolean featured = ParamUtil.getBoolean(
244                            uploadPortletRequest, "featured");
245                    Boolean sale = null;
246    
247                    boolean smallImage = ParamUtil.getBoolean(
248                            uploadPortletRequest, "smallImage");
249                    String smallImageURL = ParamUtil.getString(
250                            uploadPortletRequest, "smallImageURL");
251                    File smallFile = uploadPortletRequest.getFile("smallFile");
252    
253                    boolean mediumImage = ParamUtil.getBoolean(
254                            uploadPortletRequest, "mediumImage");
255                    String mediumImageURL = ParamUtil.getString(
256                            uploadPortletRequest, "mediumImageURL");
257                    File mediumFile = uploadPortletRequest.getFile("mediumFile");
258    
259                    boolean largeImage = ParamUtil.getBoolean(
260                            uploadPortletRequest, "largeImage");
261                    String largeImageURL = ParamUtil.getString(
262                            uploadPortletRequest, "largeImageURL");
263                    File largeFile = uploadPortletRequest.getFile("largeFile");
264    
265                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
266                            ShoppingItem.class.getName(), actionRequest);
267    
268                    if (itemId <= 0) {
269    
270                            // Add item
271    
272                            ShoppingItemServiceUtil.addItem(
273                                    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                    else {
280    
281                            // Update item
282    
283                            ShoppingItemServiceUtil.updateItem(
284                                    itemId, groupId, categoryId, sku, name, description, properties,
285                                    fieldsQuantities, requiresShipping, stockQuantity, featured,
286                                    sale, smallImage, smallImageURL, smallFile, mediumImage,
287                                    mediumImageURL, mediumFile, largeImage, largeImageURL,
288                                    largeFile, itemFields, itemPrices, serviceContext);
289                    }
290            }
291    
292    }