1 5 package com.dotmarketing.portlets.product.action; 6 7 import java.net.URLDecoder ; 8 import java.util.List ; 9 10 import javax.portlet.ActionRequest; 11 import javax.portlet.ActionResponse; 12 import javax.portlet.PortletConfig; 13 import javax.servlet.http.HttpServletRequest ; 14 15 import org.apache.commons.beanutils.BeanUtils; 16 import org.apache.struts.Globals; 17 import org.apache.struts.action.ActionErrors; 18 import org.apache.struts.action.ActionForm; 19 import org.apache.struts.action.ActionMapping; 20 21 import com.dotmarketing.db.DotHibernate; 22 import com.dotmarketing.portal.struts.DotPortletAction; 23 import com.dotmarketing.util.Logger; 24 import com.dotmarketing.util.WebKeys; 25 import com.liferay.portal.model.User; 26 import com.liferay.portal.struts.ActionException; 27 import com.liferay.portal.util.Constants; 28 import com.liferay.portlet.ActionRequestImpl; 29 import com.liferay.util.servlet.SessionMessages; 30 import com.dotmarketing.portlets.product.factories.ProductFormatFactory; 31 import com.dotmarketing.portlets.product.factories.ProductPriceFactory; 32 import com.dotmarketing.portlets.product.model.ProductFormat; 33 import com.dotmarketing.portlets.product.model.ProductPrice; 34 import com.dotmarketing.portlets.product.struts.ProductFormatForm; 35 import com.dotmarketing.portlets.product.struts.ProductPriceForm; 36 37 38 42 public class EditFormatAction extends DotPortletAction { 43 44 public static boolean debug = false; 45 46 47 public void processAction( 48 ActionMapping mapping, ActionForm form, PortletConfig config, 49 ActionRequest req, ActionResponse res) 50 throws Exception { 51 Logger.debug(this,"START EDIT FORMAT ACTION"); 52 ProductFormatForm productFormatForm = (ProductFormatForm) form; 53 long productInode = productFormatForm.getProductInode(); 54 String cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : ""; 55 String referrer = req.getParameter("referrer"); 56 57 if ((referrer!=null) && (referrer.length()!=0)) 58 { 59 referrer = URLDecoder.decode(referrer,"UTF-8"); 60 } 61 62 DotHibernate.startTransaction(); 63 User user = _getUser(req); 64 65 try { 66 _retrieveFormat(req, res, config, form, user); 67 68 } catch (ActionException ae) { 69 _handleException(ae, req); 70 } 71 74 if ((cmd != null) && cmd.equals(Constants.SAVE)) { 75 try { 76 ActionErrors ae = null; 77 ae = productFormatForm.validate(mapping,req); 78 if (ae.size() == 0) 79 { 80 _saveFormat(req, res, config, form, user); 81 } else 83 { 84 req.setAttribute(Globals.ERROR_KEY,ae); 85 String input = mapping.getInput(); 86 setForward(req,input); 87 } 89 } catch (ActionException ae) { 90 _handleException(ae, req); 91 } 92 } 93 98 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 99 try { 100 _deleteFormat(req, res, config, form, user); 101 _sendToReferral(req,res,referrer); 102 } catch (ActionException ae) { 103 _handleException(ae, req); 104 } 105 } 106 else if ((cmd != null) && cmd.equals(Constants.CANCEL)) { 107 _sendToReferral(req,res,referrer); 108 } 109 else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.COPY)) { 110 try { 111 _copyFormat(req, res, config, form, user); 112 _sendToReferral(req,res,referrer); 113 } catch (ActionException ae) { 114 _handleException(ae, req); 115 } 116 } 117 else if ((cmd != null) && cmd.equals("savePrice")) { 118 ActionErrors ae = null; 119 ae = productFormatForm.validatePrice(mapping,req); 120 if (ae.size() == 0) 121 { 122 _savePrice(req, res, config, form, user); 123 } 125 else 126 { 127 req.setAttribute(Globals.ERROR_KEY,ae); 128 String input = mapping.getInput(); 129 setForward(req,input); 130 } 131 } 132 else if ((cmd != null) && cmd.equals("deletePrice")) { 133 try { 134 _deletePrice(req, res, config, form, user); 135 } catch (ActionException ae) { 137 _handleException(ae, req); 138 } 139 } 140 DotHibernate.commitTransaction(); 141 _loadForm(req,res,config,form,user); 142 if (productInode != 0) 143 { 144 productFormatForm.setProductInode(productInode); 145 } 146 setForward(req, "portlet.ext.product.edit_format"); 147 Logger.debug(this,"END EDIT FORMAT ACTION"); 148 } 149 150 151 private void _retrieveFormat(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 153 throws Exception { 154 155 String inode = (req.getParameter("inode") != null) ? req.getParameter("inode") : "0"; 156 String priceInode = (req.getParameter("priceInode") != null) ? req.getParameter("priceInode") : "0"; 157 ProductFormat productFormat = null; 158 ProductPrice productPrice = null; 159 productFormat = ProductFormatFactory.getProductFormat(inode); 160 productPrice = ProductPriceFactory.getProductPrice(priceInode); 161 req.setAttribute(WebKeys.PRODUCT_PRODUCT_FORMAT,productFormat); 162 req.setAttribute(WebKeys.PRODUCT_PRODUCT_PRICE,productPrice); 163 } 164 165 private void _saveFormat(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 166 throws Exception { 167 168 ProductFormatForm productFormatForm = (ProductFormatForm) form; 169 ProductFormat productFormat = (ProductFormat) req.getAttribute(WebKeys.PRODUCT_PRODUCT_FORMAT); 170 BeanUtils.copyProperties(productFormat,productFormatForm); 171 ProductFormatFactory.saveProductFormat(productFormat); 172 173 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 176 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 177 178 SessionMessages.add(httpReq, "message", "message.product.format.saved"); 179 } 180 181 private void _deleteFormat(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 182 throws Exception 183 { 184 ProductFormat productFormat = (ProductFormat) req.getAttribute(WebKeys.PRODUCT_PRODUCT_FORMAT); 185 ProductFormatFactory.deleteProductFormat(productFormat); 186 187 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 190 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 191 192 SessionMessages.add(httpReq, "message", "message.product.format.deleted"); 193 } 194 195 private void _copyFormat(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 196 throws Exception 197 { 198 ProductFormat productFormat = (ProductFormat) req.getAttribute(WebKeys.PRODUCT_PRODUCT_FORMAT); 199 ProductFormatFactory.copyProductFormat(productFormat); 200 201 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 204 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 205 206 SessionMessages.add(httpReq, "message", "message.product.format.copied"); 207 } 208 209 private void _loadForm(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 210 { 211 ProductFormatForm productFormatForm = (ProductFormatForm) form; 212 ProductFormat productFormat = (ProductFormat) req.getAttribute(WebKeys.PRODUCT_PRODUCT_FORMAT); 213 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 214 ProductPriceForm productPriceForm = new ProductPriceForm (); 215 216 try 217 { 218 BeanUtils.copyProperties(productFormatForm,productPrice); 219 BeanUtils.copyProperties(productFormatForm,productFormat); 220 BeanUtils.copyProperties(productPriceForm,productPrice); 221 } 222 catch(Exception ex) 223 { 224 Logger.debug(this,ex.toString()); 225 } 226 227 List <ProductPrice> formatPrice = ProductPriceFactory.getAllProductPricesByFormat(productFormat); 228 productFormatForm.setPrices(formatPrice); 229 req.setAttribute("productPriceForm", productPriceForm); 230 } 231 232 private void _savePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 233 throws Exception { 234 235 ProductFormatForm productFormatForm = (ProductFormatForm) form; 236 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 237 productPrice.setMinQty(productFormatForm.getMinQty()); 239 productPrice.setMaxQty(productFormatForm.getMaxQty()); 240 productPrice.setRetailPrice(productFormatForm.getRetailPrice()); 241 productPrice.setPartnerPrice(productFormatForm.getPartnerPrice()); 242 productPrice.setProductFormatInode(productFormatForm.getInode()); 243 ProductPriceFactory.saveProductPrice(productPrice); 244 req.setAttribute(WebKeys.PRODUCT_PRODUCT_PRICE,new ProductPrice()); 245 productFormatForm.setPriceInode(0); 246 247 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 250 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 251 252 SessionMessages.add(httpReq, "message", "message.product.price.saved"); 253 } 254 255 private void _deletePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 256 throws Exception 257 { 258 ProductFormatForm productFormatForm = (ProductFormatForm) form; 259 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 260 ProductPriceFactory.deleteProductPrice(productPrice); 261 req.setAttribute(WebKeys.PRODUCT_PRODUCT_PRICE,new ProductPrice()); 262 productFormatForm.setPriceInode(0); 263 264 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 267 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 268 269 SessionMessages.add(httpReq, "message", "message.product.price.deleted"); 270 } 271 } 272 273 | Popular Tags |