1   /**
2    * Copyright (c) 2000-2008 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.servlet.SessionErrors;
26  import com.liferay.portal.kernel.upload.UploadPortletRequest;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.shopping.DuplicateItemSKUException;
34  import com.liferay.portlet.shopping.ItemLargeImageNameException;
35  import com.liferay.portlet.shopping.ItemLargeImageSizeException;
36  import com.liferay.portlet.shopping.ItemMediumImageNameException;
37  import com.liferay.portlet.shopping.ItemMediumImageSizeException;
38  import com.liferay.portlet.shopping.ItemNameException;
39  import com.liferay.portlet.shopping.ItemSKUException;
40  import com.liferay.portlet.shopping.ItemSmallImageNameException;
41  import com.liferay.portlet.shopping.ItemSmallImageSizeException;
42  import com.liferay.portlet.shopping.NoSuchCategoryException;
43  import com.liferay.portlet.shopping.NoSuchItemException;
44  import com.liferay.portlet.shopping.model.ShoppingItemField;
45  import com.liferay.portlet.shopping.model.ShoppingItemPrice;
46  import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceImpl;
47  import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
48  import com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldUtil;
49  import com.liferay.portlet.shopping.service.persistence.ShoppingItemPriceUtil;
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 portletConfig,
76              ActionRequest actionRequest, ActionResponse actionResponse)
77          throws Exception {
78  
79          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
80  
81          try {
82              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
83                  updateItem(actionRequest);
84              }
85              else if (cmd.equals(Constants.DELETE)) {
86                  deleteItem(actionRequest);
87              }
88  
89              if (Validator.isNotNull(cmd)) {
90                  sendRedirect(actionRequest, actionResponse);
91              }
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchCategoryException ||
95                  e instanceof NoSuchItemException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(actionRequest, e.getClass().getName());
99  
100                 setForward(actionRequest, "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(actionRequest, e.getClass().getName());
113             }
114             else {
115                 throw e;
116             }
117         }
118     }
119 
120     public ActionForward render(
121             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
122             RenderRequest renderRequest, RenderResponse renderResponse)
123         throws Exception {
124 
125         try {
126             ActionUtil.getItem(renderRequest);
127         }
128         catch (Exception e) {
129             if (e instanceof NoSuchItemException ||
130                 e instanceof PrincipalException) {
131 
132                 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.shopping.edit_item"));
143     }
144 
145     protected void deleteItem(ActionRequest actionRequest) throws Exception {
146         long itemId = ParamUtil.getLong(actionRequest, "itemId");
147 
148         ShoppingItemServiceUtil.deleteItem(itemId);
149     }
150 
151     protected void updateItem(ActionRequest actionRequest) throws Exception {
152         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
153             actionRequest);
154 
155         long itemId = ParamUtil.getLong(uploadRequest, "itemId");
156 
157         long categoryId = ParamUtil.getLong(uploadRequest, "categoryId");
158         String sku = ParamUtil.getString(uploadRequest, "sku");
159         String name = ParamUtil.getString(uploadRequest, "name");
160         String description = ParamUtil.getString(uploadRequest, "description");
161         String properties = ParamUtil.getString(uploadRequest, "properties");
162 
163         int fieldsCount = ParamUtil.getInteger(uploadRequest, "fieldsCount", 1);
164 
165         List<ShoppingItemField> itemFields = new ArrayList<ShoppingItemField>();
166 
167         for (int i = 0; i < fieldsCount; i ++) {
168             String fieldName = ParamUtil.getString(
169                 uploadRequest, "fieldName" + i);
170             String fieldValues = ParamUtil.getString(
171                 uploadRequest, "fieldValues" + i);
172             String fieldDescription = ParamUtil.getString(
173                 uploadRequest, "fieldDescription" + i);
174 
175             ShoppingItemField itemField = ShoppingItemFieldUtil.create(0);
176 
177             itemField.setName(fieldName);
178             itemField.setValues(fieldValues);
179             itemField.setDescription(fieldDescription);
180 
181             itemFields.add(itemField);
182         }
183 
184         String fieldsQuantities = ParamUtil.getString(
185             uploadRequest, "fieldsQuantities");
186 
187         int pricesCount = ParamUtil.getInteger(uploadRequest, "pricesCount", 1);
188 
189         List<ShoppingItemPrice> itemPrices = new ArrayList<ShoppingItemPrice>();
190 
191         for (int i = 0; i < pricesCount; i ++) {
192             int minQuantity = ParamUtil.getInteger(
193                 uploadRequest, "minQuantity" + i);
194             int maxQuantity = ParamUtil.getInteger(
195                 uploadRequest, "maxQuantity" + i);
196             double price = ParamUtil.getDouble(uploadRequest, "price" + i);
197             double discount = ParamUtil.getDouble(
198                 uploadRequest, "discount" + i) / 100;
199             boolean taxable = ParamUtil.getBoolean(
200                 uploadRequest, "taxable" + i);
201             double shipping = ParamUtil.getDouble(
202                 uploadRequest, "shipping" + i);
203             boolean useShippingFormula = ParamUtil.getBoolean(
204                 uploadRequest, "useShippingFormula" + i);
205             boolean active = ParamUtil.getBoolean(uploadRequest, "active" + i);
206             int defaultPrice = ParamUtil.getInteger(
207                 uploadRequest, "defaultPrice");
208 
209             int status = ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT;
210 
211             if ((defaultPrice != i) && active) {
212                 status = ShoppingItemPriceImpl.STATUS_ACTIVE;
213             }
214             else if ((defaultPrice != i) && !active) {
215                 status = ShoppingItemPriceImpl.STATUS_INACTIVE;
216             }
217 
218             ShoppingItemPrice itemPrice = ShoppingItemPriceUtil.create(0);
219 
220             itemPrice.setMinQuantity(minQuantity);
221             itemPrice.setMaxQuantity(maxQuantity);
222             itemPrice.setPrice(price);
223             itemPrice.setDiscount(discount);
224             itemPrice.setTaxable(taxable);
225             itemPrice.setShipping(shipping);
226             itemPrice.setUseShippingFormula(useShippingFormula);
227             itemPrice.setStatus(status);
228 
229             itemPrices.add(itemPrice);
230         }
231 
232         boolean requiresShipping = ParamUtil.getBoolean(
233             uploadRequest, "requiresShipping");
234         int stockQuantity = ParamUtil.getInteger(
235             uploadRequest, "stockQuantity");
236 
237         boolean featured = ParamUtil.getBoolean(uploadRequest, "featured");
238         Boolean sale = null;
239 
240         boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
241         String smallImageURL = ParamUtil.getString(
242             uploadRequest, "smallImageURL");
243         File smallFile = uploadRequest.getFile("smallFile");
244 
245         boolean mediumImage = ParamUtil.getBoolean(
246             uploadRequest, "mediumImage");
247         String mediumImageURL = ParamUtil.getString(
248             uploadRequest, "mediumImageURL");
249         File mediumFile = uploadRequest.getFile("mediumFile");
250 
251         boolean largeImage = ParamUtil.getBoolean(uploadRequest, "largeImage");
252         String largeImageURL = ParamUtil.getString(
253             uploadRequest, "largeImageURL");
254         File largeFile = uploadRequest.getFile("largeFile");
255 
256         String[] communityPermissions = actionRequest.getParameterValues(
257             "communityPermissions");
258         String[] guestPermissions = actionRequest.getParameterValues(
259             "guestPermissions");
260 
261         if (itemId <= 0) {
262 
263             // Add item
264 
265             ShoppingItemServiceUtil.addItem(
266                 categoryId, sku, name, description, properties,
267                 fieldsQuantities, requiresShipping, stockQuantity, featured,
268                 sale, smallImage, smallImageURL, smallFile, mediumImage,
269                 mediumImageURL, mediumFile, largeImage, largeImageURL,
270                 largeFile, itemFields, itemPrices, communityPermissions,
271                 guestPermissions);
272         }
273         else {
274 
275             // Update item
276 
277             ShoppingItemServiceUtil.updateItem(
278                 itemId, categoryId, sku, name, description, properties,
279                 fieldsQuantities, requiresShipping, stockQuantity, featured,
280                 sale, smallImage, smallImageURL, smallFile, mediumImage,
281                 mediumImageURL, mediumFile, largeImage, largeImageURL,
282                 largeFile, itemFields, itemPrices);
283         }
284     }
285 
286 }