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