KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > product > action > EditPriceAction


1 /*
2  * Created on 19/10/2004
3  *
4  */

5 package com.dotmarketing.portlets.product.action;
6
7 import java.net.URLDecoder JavaDoc;
8
9 import javax.portlet.ActionRequest;
10 import javax.portlet.ActionResponse;
11 import javax.portlet.PortletConfig;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
36  * @author Salvador Di Nardo
37  *
38  */

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 JavaDoc {
47         Logger.debug(this,"START EDIT PRICE ACTION");
48         ProductFormatForm productFormatForm = (ProductFormatForm) form;
49         long formatInode = productFormatForm.getInode();
50         String JavaDoc cmd = ((req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : "");
51         String JavaDoc 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         /*
68          * Save the format occurrence
69          */

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 JavaDoc input = mapping.getInput();
84                     setForward(req,input);
85                 }
86             } catch (ActionException ae) {
87                 _handleException(ae, req);
88             }
89         }
90         /*
91          * If we are deleting the event,
92          * run the delete action and return to the list
93          *
94          */

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         //_loadForm(req,res,config,form,user);
121
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     ///// ************** ALL METHODS HERE *************************** ////////
131
private void _retrievePrice(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
132     throws Exception JavaDoc {
133         ProductFormatForm productFormatForm = (ProductFormatForm) form;
134         String JavaDoc 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 JavaDoc {
143         
144         ProductFormatForm productFormatForm = (ProductFormatForm) form;
145         ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE);
146         //BeanUtils.copyProperties(productPrice,productFormatForm);
147
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         //add message
155
//wraps request to get session object
156
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
157         HttpServletRequest JavaDoc 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 JavaDoc
164     {
165         ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE);
166         ProductPriceFactory.deleteProductPrice(productPrice);
167
168         //add message
169
//wraps request to get session object
170
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
171         HttpServletRequest JavaDoc 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 JavaDoc
178     {
179         ProductPrice productPrice = (ProductPrice) req.getAttribute(WebKeys.PRODUCT_PRODUCT_PRICE);
180         ProductPriceFactory.copyProductPrice(productPrice);
181         //add message
182
//wraps request to get session object
183
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
184         HttpServletRequest JavaDoc 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 JavaDoc ex)
198         {
199             Logger.debug(this,ex.toString());
200         }
201     }
202 }
203
204
Popular Tags