KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > discountcode > action > EditDiscountCodeAction


1 /*
2  * Created on Jun 20, 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.dotmarketing.portlets.discountcode.action;
8
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.net.URLDecoder JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.portlet.ActionRequest;
16 import javax.portlet.ActionResponse;
17 import javax.portlet.PortletConfig;
18
19 import org.apache.struts.Globals;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionMapping;
22 import org.apache.struts.action.ActionMessages;
23
24 import com.dotmarketing.db.DotHibernate;
25 import com.dotmarketing.factories.InodeFactory;
26 import com.dotmarketing.portal.struts.DotPortletAction;
27 import com.dotmarketing.util.Logger;
28 import com.dotmarketing.util.UtilMethods;
29 import com.dotmarketing.util.WebKeys;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.util.Constants;
32 import com.liferay.util.servlet.SessionMessages;
33 import com.dotmarketing.portlets.discountcode.factories.DiscountCodeFactory;
34 import com.dotmarketing.portlets.discountcode.model.DiscountCode;
35 import com.dotmarketing.portlets.discountcode.struts.DiscountCodeForm;
36 import com.dotmarketing.portlets.product.factories.ProductFormatFactory;
37 import com.dotmarketing.portlets.product.model.ProductFormat;
38
39 /**
40  * @author David
41  *
42  * TODO To change the template for this generated type comment go to
43  * Window - Preferences - Java - Code Style - Code Templates
44  */

45 public class EditDiscountCodeAction extends DotPortletAction
46 {
47     public void processAction(ActionMapping mapping, ActionForm form,PortletConfig config, ActionRequest req, ActionResponse res) throws Exception JavaDoc
48     {
49         DiscountCodeForm discountCodeForm = (DiscountCodeForm) form;
50         String JavaDoc cmd = req.getParameter(Constants.CMD);
51         String JavaDoc referer = req.getParameter("referer");
52         
53
54         if ((referer != null) && (referer.length() != 0))
55         {
56             referer = URLDecoder.decode(referer, "UTF-8");
57         }
58                 
59         DotHibernate.startTransaction();
60         _loadDiscount(req, form);
61
62         User user = _getUser(req);
63         
64         if ((cmd != null) && cmd.equals(Constants.ADD))
65         {
66             try
67             {
68                 Logger.debug(this, "Calling Add Method");
69                 ActionMessages ae = discountCodeForm.validateEdit(mapping,req);
70                 if (ae != null && ae.size() > 0)
71                 {
72                     req.setAttribute(Globals.ERROR_KEY, ae);
73                     setForward(req, "portlet.ext.discountcode.edit_discountcode");
74                     return;
75                 }
76                 
77                 _saveWebAsset(req, res, config, form, user);
78                 
79                 _sendToReferral(req, res, referer);
80                 
81                 
82                 return;
83             }
84             catch (Exception JavaDoc ae)
85             {
86                 _handleException(ae, req);
87                 return;
88             }
89         }
90         else if ((cmd != null) && cmd.equals(Constants.DELETE))
91         {
92             try
93             {
94                 _deleteWebAsset(req,res,config,form,user);
95                 
96                 
97                 _sendToReferral(req, res, referer);
98                 
99                 
100                 return;
101             }
102             catch (Exception JavaDoc ae)
103             {
104                 _handleException(ae, req);
105                 return;
106             }
107         }
108         DotHibernate.commitTransaction();
109         _loadForm(req,form);
110         setForward(req, "portlet.ext.discountcode.edit_discountcode");
111     }
112       
113     public void _saveWebAsset(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user)
114             throws Exception JavaDoc
115     {
116         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(WebKeys.DateFormats.SHORTDATE);
117         DiscountCode discountCode = (DiscountCode) req.getAttribute(WebKeys.DISCOUNTCODE_DISCOUNTS);
118         
119         DiscountCodeForm discountCodeForm = (DiscountCodeForm) form;
120         String JavaDoc codeId = discountCodeForm.getCodeId();
121         String JavaDoc codeDesc = discountCodeForm.getCodeDescription();
122         Date JavaDoc startDate = null;
123         if (UtilMethods.isSet(discountCodeForm.getStartDate()))
124         {
125             startDate = df.parse(discountCodeForm.getStartDate());
126         }
127         Date JavaDoc endDate = null;
128         if (UtilMethods.isSet(discountCodeForm.getEndDate()))
129         {
130             endDate = df.parse(discountCodeForm.getEndDate());
131         }
132         int discountType = discountCodeForm.getDiscountType();
133         boolean freeShipping = discountCodeForm.isFreeShipping();
134         boolean noBulkDiscount = discountCodeForm.isNoBulkDisc();
135         float discountAmount = discountCodeForm.getDiscountAmount();
136         int minOrder = discountCodeForm.getMinOrder();
137         
138         discountCode.setCodeId(codeId);
139         discountCode.setCodeDescription(codeDesc);
140         discountCode.setStartDate(startDate);
141         discountCode.setEndDate(endDate);
142         discountCode.setDiscountType(discountType);
143         discountCode.setFreeShipping(freeShipping);
144         discountCode.setNoBulkDisc(noBulkDiscount);
145         discountCode.setDiscountAmount(discountAmount);
146         discountCode.setMinOrder(minOrder);
147         
148         DiscountCodeFactory.saveDiscountCode(discountCode);
149         
150         //Delete the old categories
151
List JavaDoc<ProductFormat> oldProductList = InodeFactory.getParentsOfClass(discountCode,ProductFormat.class);
152         for(ProductFormat format: oldProductList)
153         {
154             discountCode.deleteParent(format,WebKeys.DISCOUNTCODE_PRODUCT_FORMAT);
155         }
156         
157         String JavaDoc[] idProductList = discountCodeForm.getProducts();
158         if (idProductList != null)
159         {
160             for(int i=0;i< idProductList.length;i++)
161             {
162                 String JavaDoc idProduct = String.valueOf(idProductList[i]);
163                 ProductFormat format = ProductFormatFactory.getProductFormat(idProduct);
164                 discountCode.addParent(format,WebKeys.DISCOUNTCODE_PRODUCT_FORMAT);
165             }
166         }
167         else
168         {
169             List JavaDoc<ProductFormat> allProductFormats = ProductFormatFactory.getAllProductFormats();
170             for(ProductFormat format : allProductFormats)
171             {
172                 discountCode.addParent(format,WebKeys.DISCOUNTCODE_PRODUCT_FORMAT);
173             }
174         }
175         
176         String JavaDoc message = "message.discountcode.save";
177         SessionMessages.add(req, "message",message);
178     }
179        
180     private DiscountCode _loadDiscount(ActionRequest request, ActionForm form) throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc
181     {
182         DiscountCode discountCode;
183         String JavaDoc inode = request.getParameter("inode");
184         long longInode = 0;
185         if(UtilMethods.isSet(inode))
186         {
187             longInode = Long.parseLong(inode);
188         }
189         discountCode = DiscountCodeFactory.getDiscountCode(longInode);
190         request.setAttribute(WebKeys.DISCOUNTCODE_DISCOUNTS,discountCode);
191         return discountCode;
192     }
193     
194     private void _loadForm(ActionRequest request, ActionForm form)
195     {
196         SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(WebKeys.DateFormats.SHORTDATE);
197         DiscountCodeForm discountCodeForm = (DiscountCodeForm) form;
198         DiscountCode discountCode;
199         discountCode = (DiscountCode) request.getAttribute(WebKeys.DISCOUNTCODE_DISCOUNTS);
200         //Get the bean values
201
String JavaDoc codeId = discountCode.getCodeId();
202         String JavaDoc codeDesc = discountCode.getCodeDescription();
203         Date JavaDoc startDate = discountCode.getStartDate();
204         Date JavaDoc endDate = discountCode.getEndDate();
205         int discountType = discountCode.getDiscountType();
206         boolean freeShipping = discountCode.getFreeShipping();
207         boolean noBulkDiscount = discountCode.getNoBulkDisc();
208         float discountAmount = discountCode.getDiscountAmount();
209         int minOrder = discountCode.getMinOrder();
210         
211         //Set the values in the form
212
discountCodeForm.setCodeId(codeId);
213         discountCodeForm.setCodeDescription(codeDesc);
214         if(UtilMethods.isSet(startDate))
215         {
216             discountCodeForm.setStartDate(df.format(startDate));
217         }
218         if(UtilMethods.isSet(endDate))
219         {
220             discountCodeForm.setEndDate(df.format(endDate));
221         }
222         discountCodeForm.setDiscountType(discountType);
223         discountCodeForm.setFreeShipping(freeShipping);
224         discountCodeForm.setNoBulkDisc(noBulkDiscount);
225         discountCodeForm.setDiscountAmount(discountAmount);
226         discountCodeForm.setMinOrder(minOrder);
227         
228         List JavaDoc productList = InodeFactory.getParentsOfClassByRelationType(discountCode, ProductFormat.class,WebKeys.DISCOUNTCODE_PRODUCT_FORMAT);
229         String JavaDoc[] idProductList = new String JavaDoc[productList.size()];
230         for(int i=0; i < productList.size();i++)
231         {
232             ProductFormat format = (ProductFormat) productList.get(i);
233             idProductList[i] = String.valueOf(format.getInode());
234         }
235         discountCodeForm.setProducts(idProductList);
236     }
237
238     public void _deleteWebAsset(ActionRequest req, ActionResponse rest, PortletConfig config, ActionForm form, User user) throws Exception JavaDoc
239     {
240         DiscountCode discountCode;
241         String JavaDoc inode = req.getParameter("inode");
242         long longInode = 0;
243         if(UtilMethods.isSet(inode))
244         {
245             longInode = Long.parseLong(inode);
246         }
247         discountCode = DiscountCodeFactory.getDiscountCode(longInode);
248         DiscountCodeFactory.deleteDiscountCode(discountCode);
249         
250         String JavaDoc message = "message.discountcode.delete";
251         SessionMessages.add(req, "message",message);
252     }
253 }
254
Popular Tags