1 5 package com.dotmarketing.portlets.product.action; 6 7 import java.net.URLDecoder ; 8 9 import javax.portlet.ActionRequest; 10 import javax.portlet.ActionResponse; 11 import javax.portlet.PortletConfig; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import org.apache.commons.beanutils.BeanUtils; 15 import org.apache.struts.Globals; 16 import org.apache.struts.action.ActionErrors; 17 import org.apache.struts.action.ActionForm; 18 import org.apache.struts.action.ActionMapping; 19 20 import com.dotmarketing.db.DotHibernate; 21 import com.dotmarketing.portal.struts.DotPortletAction; 22 import com.dotmarketing.util.Logger; 23 import com.dotmarketing.util.WebKeys; 24 import com.liferay.portal.model.User; 25 import com.liferay.portal.struts.ActionException; 26 import com.liferay.portal.util.Constants; 27 import com.liferay.portlet.ActionRequestImpl; 28 import com.liferay.util.servlet.SessionMessages; 29 import com.dotmarketing.portlets.product.factories.ProductPriceFactory; 30 import com.dotmarketing.portlets.product.model.ProductPrice; 31 import com.dotmarketing.portlets.product.struts.ProductFormatForm; 32 import com.dotmarketing.portlets.product.struts.ProductPriceForm; 33 34 35 39 public class EditPriceAction extends DotPortletAction { 40 41 public static boolean debug = false; 42 43 public void processAction( 44 ActionMapping mapping, ActionForm form, PortletConfig config, 45 ActionRequest req, ActionResponse res) 46 throws Exception { 47 Logger.debug(this,"START EDIT PRICE ACTION"); 48 ProductFormatForm productFormatForm = (ProductFormatForm) form; 49 long formatInode = productFormatForm.getInode(); 50 String cmd = ((req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : ""); 51 String referrer = req.getParameter("referrer"); 52 53 if ((referrer!=null) && (referrer.length()!=0)) 54 { 55 referrer = URLDecoder.decode(referrer,"UTF-8"); 56 } 57 58 DotHibernate.startTransaction(); 59 User user = _getUser(req); 60 61 try { 62 _retrievePrice(req, res, config, form, user); 63 64 } catch (ActionException ae) { 65 _handleException(ae, req); 66 } 67 70 if ((cmd != null) && cmd.equals(Constants.SAVE)) { 71 try { 72 73 ActionErrors ae = null; 74 ae = productFormatForm.validatePrice(mapping,req); 75 if (ae.size() == 0) 76 { 77 _savePrice(req, res, config, form, user); 78 _sendToReferral(req,res,referrer); 79 } 80 else 81 { 82 req.setAttribute(Globals.ERROR_KEY,ae); 83 String input = mapping.getInput(); 84 setForward(req,input); 85 } 86 } catch (ActionException ae) { 87 _handleException(ae, req); 88 } 89 } 90 95 else if ((cmd != null) && cmd.equals(Constants.DELETE)) 96 { 97 try { 98 _deletePrice(req, res, config, form, user); 99 _sendToReferral(req,res,referrer); 100 } catch (ActionException ae) { 101 _handleException(ae, req); 102 } 103 } 104 else if ((cmd != null) && cmd.equals(Constants.CANCEL)) 105 { 106 _sendToReferral(req,res,referrer); 107 } 108 else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.COPY)) 109 { 110 try 111 { 112 _copyPrice(req,res,config,form,user); 113 _sendToReferral(req,res,referrer); 114 } catch (ActionException ae) 115 { 116 _handleException(ae, req); 117 } 118 } 119 DotHibernate.commitTransaction(); 120 if (formatInode != 0) 122 { 123 productFormatForm.setInode(formatInode); 124 } 125 setForward(req, "portlet.ext.product.edit_format"); 126 Logger.debug(this,"END EDIT PRICE ACTION"); 127 } 128 129 130 private void _retrievePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 132 throws Exception { 133 ProductFormatForm productFormatForm = (ProductFormatForm) form; 134 String inode = (req.getParameter("priceInode") != null) ? req.getParameter("priceInode") : "0"; 135 ProductPrice productPrice = null; 136 productPrice = ProductPriceFactory.getProductPrice(inode); 137 productPrice.setProductFormatInode(productFormatForm.getInode()); 138 req.setAttribute(WebKeys.PRODUCT_PRODUCT_PRICE,productPrice); 139 } 140 141 private void _savePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 142 throws Exception { 143 144 ProductFormatForm productFormatForm = (ProductFormatForm) form; 145 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 146 productPrice.setMinQty(productFormatForm.getMinQty()); 148 productPrice.setMaxQty(productFormatForm.getMaxQty()); 149 productPrice.setRetailPrice(productFormatForm.getRetailPrice()); 150 productPrice.setPartnerPrice(productFormatForm.getPartnerPrice()); 151 productPrice.setProductFormatInode(productFormatForm.getInode()); 152 ProductPriceFactory.saveProductPrice(productPrice); 153 154 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 157 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 158 159 SessionMessages.add(httpReq, "message", "message.product.price.saved"); 160 } 161 162 private void _deletePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 163 throws Exception 164 { 165 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 166 ProductPriceFactory.deleteProductPrice(productPrice); 167 168 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 171 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 172 173 SessionMessages.add(httpReq, "message", "message.product.price.deleted"); 174 } 175 176 private void _copyPrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 177 throws Exception 178 { 179 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 180 ProductPriceFactory.copyProductPrice(productPrice); 181 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 184 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 185 186 SessionMessages.add(httpReq, "message", "message.product.price.copied"); 187 } 188 189 private void _loadForm(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 190 { 191 ProductPriceForm productPriceForm = (ProductPriceForm) form; 192 ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE); 193 try 194 { 195 BeanUtils.copyProperties(productPriceForm,productPrice); 196 } 197 catch(Exception ex) 198 { 199 Logger.debug(this,ex.toString()); 200 } 201 } 202 } 203 204 | Popular Tags |