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