1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.shopping.DuplicateItemSKUException;
32  import com.liferay.portlet.shopping.ItemLargeImageNameException;
33  import com.liferay.portlet.shopping.ItemLargeImageSizeException;
34  import com.liferay.portlet.shopping.ItemMediumImageNameException;
35  import com.liferay.portlet.shopping.ItemMediumImageSizeException;
36  import com.liferay.portlet.shopping.ItemNameException;
37  import com.liferay.portlet.shopping.ItemSKUException;
38  import com.liferay.portlet.shopping.ItemSmallImageNameException;
39  import com.liferay.portlet.shopping.ItemSmallImageSizeException;
40  import com.liferay.portlet.shopping.NoSuchCategoryException;
41  import com.liferay.portlet.shopping.NoSuchItemException;
42  import com.liferay.portlet.shopping.model.ShoppingItemField;
43  import com.liferay.portlet.shopping.model.ShoppingItemPrice;
44  import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceImpl;
45  import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
46  import com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldUtil;
47  import com.liferay.portlet.shopping.service.persistence.ShoppingItemPriceUtil;
48  import com.liferay.util.servlet.SessionErrors;
49  import com.liferay.util.servlet.UploadPortletRequest;
50  
51  import java.io.File;
52  
53  import java.util.ArrayList;
54  import java.util.List;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.RenderRequest;
60  import javax.portlet.RenderResponse;
61  
62  import org.apache.struts.action.ActionForm;
63  import org.apache.struts.action.ActionForward;
64  import org.apache.struts.action.ActionMapping;
65  
66  /**
67   * <a href="EditItemAction.java.html"><b><i>View Source</i></b></a>
68   *
69   * @author Brian Wing Shun Chan
70   *
71   */
72  public class EditItemAction extends PortletAction {
73  
74      public void processAction(
75              ActionMapping mapping, ActionForm form, PortletConfig config,
76              ActionRequest req, ActionResponse res)
77          throws Exception {
78  
79          String cmd = ParamUtil.getString(req, Constants.CMD);
80  
81          try {
82              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
83                  updateItem(req);
84              }
85              else if (cmd.equals(Constants.DELETE)) {
86                  deleteItem(req);
87              }
88  
89              if (Validator.isNotNull(cmd)) {
90                  sendRedirect(req, res);
91              }
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchCategoryException ||
95                  e instanceof NoSuchItemException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(req, e.getClass().getName());
99  
100                 setForward(req, "portlet.shopping.error");
101             }
102             else if (e instanceof DuplicateItemSKUException ||
103                      e instanceof ItemLargeImageNameException ||
104                      e instanceof ItemLargeImageSizeException ||
105                      e instanceof ItemMediumImageNameException ||
106                      e instanceof ItemMediumImageSizeException ||
107                      e instanceof ItemNameException ||
108                      e instanceof ItemSKUException ||
109                      e instanceof ItemSmallImageNameException ||
110                      e instanceof ItemSmallImageSizeException) {
111 
112                 SessionErrors.add(req, e.getClass().getName());
113             }
114             else {
115                 throw e;
116             }
117         }
118     }
119 
120     public ActionForward render(
121             ActionMapping mapping, ActionForm form, PortletConfig config,
122             RenderRequest req, RenderResponse res)
123         throws Exception {
124 
125         try {
126             ActionUtil.getItem(req);
127         }
128         catch (Exception e) {
129             if (e instanceof NoSuchItemException ||
130                 e instanceof PrincipalException) {
131 
132                 SessionErrors.add(req, e.getClass().getName());
133 
134                 return mapping.findForward("portlet.shopping.error");
135             }
136             else {
137                 throw e;
138             }
139         }
140 
141         return mapping.findForward(
142             getForward(req, "portlet.shopping.edit_item"));
143     }
144 
145     protected void deleteItem(ActionRequest req) throws Exception {
146         long itemId = ParamUtil.getLong(req, "itemId");
147 
148         ShoppingItemServiceUtil.deleteItem(itemId);
149     }
150 
151     protected void updateItem(ActionRequest req) throws Exception {
152         UploadPortletRequest uploadReq =
153             PortalUtil.getUploadPortletRequest(req);
154 
155         long itemId = ParamUtil.getLong(uploadReq, "itemId");
156 
157         long categoryId = ParamUtil.getLong(uploadReq, "categoryId");
158         String sku = ParamUtil.getString(uploadReq, "sku");
159         String name = ParamUtil.getString(uploadReq, "name");
160         String description = ParamUtil.getString(uploadReq, "description");
161         String properties = ParamUtil.getString(uploadReq, "properties");
162 
163         int fieldsCount = ParamUtil.getInteger(uploadReq, "fieldsCount", 1);
164 
165         List itemFields = new ArrayList();
166 
167         for (int i = 0; i < fieldsCount; i ++) {
168             String fieldName = ParamUtil.getString(uploadReq, "fieldName" + i);
169             String fieldValues = ParamUtil.getString(
170                 uploadReq, "fieldValues" + i);
171             String fieldDescription = ParamUtil.getString(
172                 uploadReq, "fieldDescription" + i);
173 
174             ShoppingItemField itemField = ShoppingItemFieldUtil.create(0);
175 
176             itemField.setName(fieldName);
177             itemField.setValues(fieldValues);
178             itemField.setDescription(fieldDescription);
179 
180             itemFields.add(itemField);
181         }
182 
183         String fieldsQuantities = ParamUtil.getString(
184             uploadReq, "fieldsQuantities");
185 
186         int pricesCount = ParamUtil.getInteger(uploadReq, "pricesCount", 1);
187 
188         List itemPrices = new ArrayList();
189 
190         for (int i = 0; i < pricesCount; i ++) {
191             int minQuantity = ParamUtil.getInteger(
192                 uploadReq, "minQuantity" + i);
193             int maxQuantity = ParamUtil.getInteger(
194                 uploadReq, "maxQuantity" + i);
195             double price = ParamUtil.getDouble(uploadReq, "price" + i);
196             double discount = ParamUtil.getDouble(
197                 uploadReq, "discount" + i) / 100;
198             boolean taxable = ParamUtil.getBoolean(uploadReq, "taxable" + i);
199             double shipping = ParamUtil.getDouble(uploadReq, "shipping" + i);
200             boolean useShippingFormula = ParamUtil.getBoolean(
201                 uploadReq, "useShippingFormula" + i);
202             boolean active = ParamUtil.getBoolean(uploadReq, "active" + i);
203             int defaultPrice = ParamUtil.getInteger(uploadReq, "defaultPrice");
204 
205             int status = ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT;
206 
207             if ((defaultPrice != i) && active) {
208                 status = ShoppingItemPriceImpl.STATUS_ACTIVE;
209             }
210             else if ((defaultPrice != i) && !active) {
211                 status = ShoppingItemPriceImpl.STATUS_INACTIVE;
212             }
213 
214             ShoppingItemPrice itemPrice = ShoppingItemPriceUtil.create(0);
215 
216             itemPrice.setMinQuantity(minQuantity);
217             itemPrice.setMaxQuantity(maxQuantity);
218             itemPrice.setPrice(price);
219             itemPrice.setDiscount(discount);
220             itemPrice.setTaxable(taxable);
221             itemPrice.setShipping(shipping);
222             itemPrice.setUseShippingFormula(useShippingFormula);
223             itemPrice.setStatus(status);
224 
225             itemPrices.add(itemPrice);
226         }
227 
228         boolean requiresShipping = ParamUtil.getBoolean(
229             uploadReq, "requiresShipping");
230         int stockQuantity = ParamUtil.getInteger(uploadReq, "stockQuantity");
231 
232         boolean featured = ParamUtil.getBoolean(uploadReq, "featured");
233         Boolean sale = null;
234 
235         boolean smallImage = ParamUtil.getBoolean(uploadReq, "smallImage");
236         String smallImageURL = ParamUtil.getString(uploadReq, "smallImageURL");
237         File smallFile = uploadReq.getFile("smallFile");
238 
239         boolean mediumImage = ParamUtil.getBoolean(uploadReq, "mediumImage");
240         String mediumImageURL = ParamUtil.getString(
241             uploadReq, "mediumImageURL");
242         File mediumFile = uploadReq.getFile("mediumFile");
243 
244         boolean largeImage = ParamUtil.getBoolean(uploadReq, "largeImage");
245         String largeImageURL = ParamUtil.getString(uploadReq, "largeImageURL");
246         File largeFile = uploadReq.getFile("largeFile");
247 
248         String[] communityPermissions = req.getParameterValues(
249             "communityPermissions");
250         String[] guestPermissions = req.getParameterValues(
251             "guestPermissions");
252 
253         if (itemId <= 0) {
254 
255             // Add item
256 
257             ShoppingItemServiceUtil.addItem(
258                 categoryId, sku, name, description, properties,
259                 fieldsQuantities, requiresShipping, stockQuantity, featured,
260                 sale, smallImage, smallImageURL, smallFile, mediumImage,
261                 mediumImageURL, mediumFile, largeImage, largeImageURL,
262                 largeFile, itemFields, itemPrices, communityPermissions,
263                 guestPermissions);
264         }
265         else {
266 
267             // Update item
268 
269             ShoppingItemServiceUtil.updateItem(
270                 itemId, categoryId, sku, name, description, properties,
271                 fieldsQuantities, requiresShipping, stockQuantity, featured,
272                 sale, smallImage, smallImageURL, smallFile, mediumImage,
273                 mediumImageURL, mediumFile, largeImage, largeImageURL,
274                 largeFile, itemFields, itemPrices);
275         }
276     }
277 
278 }