001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.shopping.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.portal.security.auth.PrincipalException;
027: import com.liferay.portal.struts.PortletAction;
028: import com.liferay.portal.util.PortalUtil;
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.ShoppingItemField;
041: import com.liferay.portlet.shopping.model.ShoppingItemPrice;
042: import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceImpl;
043: import com.liferay.portlet.shopping.service.ShoppingItemServiceUtil;
044: import com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldUtil;
045: import com.liferay.portlet.shopping.service.persistence.ShoppingItemPriceUtil;
046: import com.liferay.util.servlet.SessionErrors;
047: import com.liferay.util.servlet.UploadPortletRequest;
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: * <a href="EditItemAction.java.html"><b><i>View Source</i></b></a>
066: *
067: * @author Brian Wing Shun Chan
068: *
069: */
070: public class EditItemAction extends PortletAction {
071:
072: public void processAction(ActionMapping mapping, ActionForm form,
073: PortletConfig config, ActionRequest req, ActionResponse res)
074: throws Exception {
075:
076: String cmd = ParamUtil.getString(req, Constants.CMD);
077:
078: try {
079: if (cmd.equals(Constants.ADD)
080: || cmd.equals(Constants.UPDATE)) {
081: updateItem(req);
082: } else if (cmd.equals(Constants.DELETE)) {
083: deleteItem(req);
084: }
085:
086: if (Validator.isNotNull(cmd)) {
087: sendRedirect(req, res);
088: }
089: } catch (Exception e) {
090: if (e instanceof NoSuchCategoryException
091: || e instanceof NoSuchItemException
092: || e instanceof PrincipalException) {
093:
094: SessionErrors.add(req, e.getClass().getName());
095:
096: setForward(req, "portlet.shopping.error");
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(req, e.getClass().getName());
108: } else {
109: throw e;
110: }
111: }
112: }
113:
114: public ActionForward render(ActionMapping mapping, ActionForm form,
115: PortletConfig config, RenderRequest req, RenderResponse res)
116: throws Exception {
117:
118: try {
119: ActionUtil.getItem(req);
120: } catch (Exception e) {
121: if (e instanceof NoSuchItemException
122: || e instanceof PrincipalException) {
123:
124: SessionErrors.add(req, e.getClass().getName());
125:
126: return mapping.findForward("portlet.shopping.error");
127: } else {
128: throw e;
129: }
130: }
131:
132: return mapping.findForward(getForward(req,
133: "portlet.shopping.edit_item"));
134: }
135:
136: protected void deleteItem(ActionRequest req) throws Exception {
137: long itemId = ParamUtil.getLong(req, "itemId");
138:
139: ShoppingItemServiceUtil.deleteItem(itemId);
140: }
141:
142: protected void updateItem(ActionRequest req) throws Exception {
143: UploadPortletRequest uploadReq = PortalUtil
144: .getUploadPortletRequest(req);
145:
146: long itemId = ParamUtil.getLong(uploadReq, "itemId");
147:
148: long categoryId = ParamUtil.getLong(uploadReq, "categoryId");
149: String sku = ParamUtil.getString(uploadReq, "sku");
150: String name = ParamUtil.getString(uploadReq, "name");
151: String description = ParamUtil.getString(uploadReq,
152: "description");
153: String properties = ParamUtil
154: .getString(uploadReq, "properties");
155:
156: int fieldsCount = ParamUtil.getInteger(uploadReq,
157: "fieldsCount", 1);
158:
159: List itemFields = new ArrayList();
160:
161: for (int i = 0; i < fieldsCount; i++) {
162: String fieldName = ParamUtil.getString(uploadReq,
163: "fieldName" + i);
164: String fieldValues = ParamUtil.getString(uploadReq,
165: "fieldValues" + i);
166: String fieldDescription = ParamUtil.getString(uploadReq,
167: "fieldDescription" + i);
168:
169: ShoppingItemField itemField = ShoppingItemFieldUtil
170: .create(0);
171:
172: itemField.setName(fieldName);
173: itemField.setValues(fieldValues);
174: itemField.setDescription(fieldDescription);
175:
176: itemFields.add(itemField);
177: }
178:
179: String fieldsQuantities = ParamUtil.getString(uploadReq,
180: "fieldsQuantities");
181:
182: int pricesCount = ParamUtil.getInteger(uploadReq,
183: "pricesCount", 1);
184:
185: List itemPrices = new ArrayList();
186:
187: for (int i = 0; i < pricesCount; i++) {
188: int minQuantity = ParamUtil.getInteger(uploadReq,
189: "minQuantity" + i);
190: int maxQuantity = ParamUtil.getInteger(uploadReq,
191: "maxQuantity" + i);
192: double price = ParamUtil.getDouble(uploadReq, "price" + i);
193: double discount = ParamUtil.getDouble(uploadReq, "discount"
194: + i) / 100;
195: boolean taxable = ParamUtil.getBoolean(uploadReq, "taxable"
196: + i);
197: double shipping = ParamUtil.getDouble(uploadReq, "shipping"
198: + i);
199: boolean useShippingFormula = ParamUtil.getBoolean(
200: uploadReq, "useShippingFormula" + i);
201: boolean active = ParamUtil.getBoolean(uploadReq, "active"
202: + i);
203: int defaultPrice = ParamUtil.getInteger(uploadReq,
204: "defaultPrice");
205:
206: int status = ShoppingItemPriceImpl.STATUS_ACTIVE_DEFAULT;
207:
208: if ((defaultPrice != i) && active) {
209: status = ShoppingItemPriceImpl.STATUS_ACTIVE;
210: } else if ((defaultPrice != i) && !active) {
211: status = ShoppingItemPriceImpl.STATUS_INACTIVE;
212: }
213:
214: ShoppingItemPrice itemPrice = ShoppingItemPriceUtil
215: .create(0);
216:
217: itemPrice.setMinQuantity(minQuantity);
218: itemPrice.setMaxQuantity(maxQuantity);
219: itemPrice.setPrice(price);
220: itemPrice.setDiscount(discount);
221: itemPrice.setTaxable(taxable);
222: itemPrice.setShipping(shipping);
223: itemPrice.setUseShippingFormula(useShippingFormula);
224: itemPrice.setStatus(status);
225:
226: itemPrices.add(itemPrice);
227: }
228:
229: boolean requiresShipping = ParamUtil.getBoolean(uploadReq,
230: "requiresShipping");
231: int stockQuantity = ParamUtil.getInteger(uploadReq,
232: "stockQuantity");
233:
234: boolean featured = ParamUtil.getBoolean(uploadReq, "featured");
235: Boolean sale = null;
236:
237: boolean smallImage = ParamUtil.getBoolean(uploadReq,
238: "smallImage");
239: String smallImageURL = ParamUtil.getString(uploadReq,
240: "smallImageURL");
241: File smallFile = uploadReq.getFile("smallFile");
242:
243: boolean mediumImage = ParamUtil.getBoolean(uploadReq,
244: "mediumImage");
245: String mediumImageURL = ParamUtil.getString(uploadReq,
246: "mediumImageURL");
247: File mediumFile = uploadReq.getFile("mediumFile");
248:
249: boolean largeImage = ParamUtil.getBoolean(uploadReq,
250: "largeImage");
251: String largeImageURL = ParamUtil.getString(uploadReq,
252: "largeImageURL");
253: File largeFile = uploadReq.getFile("largeFile");
254:
255: String[] communityPermissions = req
256: .getParameterValues("communityPermissions");
257: String[] guestPermissions = req
258: .getParameterValues("guestPermissions");
259:
260: if (itemId <= 0) {
261:
262: // Add item
263:
264: ShoppingItemServiceUtil.addItem(categoryId, sku, name,
265: description, properties, fieldsQuantities,
266: requiresShipping, stockQuantity, featured, sale,
267: smallImage, smallImageURL, smallFile, mediumImage,
268: mediumImageURL, mediumFile, largeImage,
269: largeImageURL, largeFile, itemFields, itemPrices,
270: communityPermissions, guestPermissions);
271: } else {
272:
273: // Update item
274:
275: ShoppingItemServiceUtil.updateItem(itemId, categoryId, sku,
276: name, description, properties, fieldsQuantities,
277: requiresShipping, stockQuantity, featured, sale,
278: smallImage, smallImageURL, smallFile, mediumImage,
279: mediumImageURL, mediumFile, largeImage,
280: largeImageURL, largeFile, itemFields, itemPrices);
281: }
282: }
283:
284: }
|