KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > order_manager > action > ViewProductsAction


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

5 package com.dotmarketing.portlets.order_manager.action;
6
7 import java.net.URLDecoder JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.portlet.ActionRequest;
11 import javax.portlet.ActionResponse;
12 import javax.portlet.PortletConfig;
13 import javax.servlet.http.HttpSession JavaDoc;
14
15 import org.apache.struts.action.ActionForm;
16 import org.apache.struts.action.ActionMapping;
17
18 import com.dotmarketing.db.DotHibernate;
19 import com.dotmarketing.factories.InodeFactory;
20 import com.dotmarketing.portal.struts.DotPortletAction;
21 import com.dotmarketing.portlets.categories.model.Category;
22 import com.dotmarketing.portlets.entities.factories.EntityFactory;
23 import com.dotmarketing.portlets.entities.model.Entity;
24 import com.dotmarketing.util.Logger;
25 import com.dotmarketing.util.UtilMethods;
26 import com.dotmarketing.util.WebKeys;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.struts.ActionException;
29 import com.liferay.portal.util.Constants;
30 import com.liferay.portlet.ActionRequestImpl;
31 import com.liferay.util.servlet.SessionMessages;
32 import com.dotmarketing.cms.product.model.ShoppingCart;
33 import com.dotmarketing.cms.product.struts.ProductsForm;
34 import com.dotmarketing.portlets.discountcode.factories.DiscountCodeFactory;
35 import com.dotmarketing.portlets.discountcode.model.DiscountCode;
36 import com.dotmarketing.portlets.product.factories.ProductFactory;
37 import com.dotmarketing.portlets.product.model.Product;
38 import com.dotmarketing.portlets.product.model.ProductFormat;
39
40
41 /**
42  * @author Salvador Di Nardo
43  *
44  */

45 public class ViewProductsAction extends DotPortletAction {
46     public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig config,
48              ActionRequest req, ActionResponse res)
49          throws Exception JavaDoc {
50         Logger.debug(this,"START LOAD PRODUCTS ACTION");
51         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : "";
52         String JavaDoc referrer = req.getParameter("referrer");
53         if (!UtilMethods.isSet(referrer))
54         {
55             referrer = req.getParameter("referer");
56         }
57         ProductsForm productsForm = (ProductsForm) form;
58         
59         HttpSession JavaDoc session = ((ActionRequestImpl)req).getHttpServletRequest().getSession();
60         
61         if ((referrer!=null) && (referrer.length()!=0))
62         {
63             referrer = URLDecoder.decode(referrer,"UTF-8");
64         }
65         
66         DotHibernate.startTransaction();
67         User user = _getUser(req);
68         
69         try
70         {
71             _retrieveProducts(req, res, config, form, user);
72         }
73         catch (Exception JavaDoc ae)
74         {
75             _handleException(ae, req);
76         }
77         /*
78          * Save the format occurrence
79          */

80         if ((cmd != null) && cmd.equals(Constants.ADD)) {
81             try
82             {
83                 _addToCard(mapping,form,config,req,res,session);
84                 _sendToReferral(req,res,referrer);
85             }
86             catch (Exception JavaDoc ae)
87             {
88                 _handleException(ae, req);
89             }
90         }
91         else if ((cmd != null) && cmd.equals(Constants.UPDATE)) {
92             try
93             {
94                 _updateCard(mapping,form,config,req,res,session);
95                 _sendToReferral(req,res,referrer);
96             }
97             catch (Exception JavaDoc ae)
98             {
99                 _handleException(ae, req);
100             }
101         }
102         /*
103          * If we are deleting the event,
104          * run the delete action and return to the list
105          *
106          */

107         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
108             try {
109                 _removeFromCard(mapping,form,config,req,res,session);
110                 _sendToReferral(req,res,referrer);
111             } catch (ActionException ae) {
112                 _handleException(ae, req);
113             }
114         }
115         else if ((cmd != null) && cmd.equals(Constants.CANCEL))
116         {
117             _sendToReferral(req,res,referrer);
118         }
119         else if ((cmd != null) && cmd.equals(Constants.VIEW))
120         {
121             setForward(req,"portlet.ext.order_manager.view_cart");
122             return;
123         }
124         else if ((cmd != null) && cmd.equals("addDiscount"))
125         {
126             _addDiscount(mapping,form,config,req,res,session);
127             _sendToReferral(req,res,referrer);
128         }
129         else if ((cmd != null) && cmd.equals("removeDiscount"))
130         {
131             _removeDiscount(mapping,form,config,req,res,session);
132             _sendToReferral(req,res,referrer);
133         }
134         if((cmd != null) && cmd.equals("new"))
135         {
136             session.removeAttribute(WebKeys.SHOPPING_CART);
137         }
138         DotHibernate.commitTransaction();
139         
140         setForward(req, "portlet.ext.order_manager.view_products");
141         Logger.debug(this,"END LOAD PRODUCTS ACTION");
142    }
143     
144     private void _retrieveProducts(ActionRequest req,ActionResponse res,PortletConfig config,ActionForm form,User user)
145     {
146         _loadProductByCategory(form,req);
147         _loadTypeProducts(form,req);
148     }
149     
150     private void _loadProductByCategory(ActionForm lf,ActionRequest request)
151     {
152         ProductsForm productsForm = (ProductsForm) lf;
153         int categoryInode = productsForm.getCategoryInode();
154         String JavaDoc orderBy = productsForm.getOrderBy();
155         orderBy = (UtilMethods.isSet(orderBy) ? orderBy : "title");
156         int page = productsForm.getPage();
157         int pageSize = productsForm.getPageSize();
158         String JavaDoc direction = productsForm.getDirection();
159         String JavaDoc filter = productsForm.getFilter();
160         
161         //Retrieve the products
162
List JavaDoc<Product> listProducts = ProductFactory.getAllProductsByCategory(categoryInode,orderBy,filter,page,pageSize,direction,"");
163         
164         //Save the list of products
165
productsForm.setListProducts(listProducts);
166     }
167     
168     private void _loadTypeProducts(ActionForm lf,ActionRequest request)
169     {
170         ProductsForm productsForm = (ProductsForm) lf;
171         
172         String JavaDoc typeProductEntityName = WebKeys.PRODUCT_PRODUCTS_TYPE;
173         Entity entity = EntityFactory.getEntity(typeProductEntityName);
174             
175         //Retrieve the products
176
List JavaDoc<Category> listTypeProducts = InodeFactory.getParentsOfClass(entity,Category.class);
177         //This is not a generic methods, it supose it has only one master category and only one level
178
Category masterCategory = listTypeProducts.get(0);
179         listTypeProducts = InodeFactory.getChildrenClass(masterCategory,Category.class);
180         
181         //Save the list of products
182
productsForm.setListTypeProducts(listTypeProducts);
183     }
184     
185     private void _addToCard(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response,HttpSession JavaDoc session)
186     {
187         ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART);
188         if (shoppingCart == null)
189         {
190             shoppingCart = new ShoppingCart();
191             session.setAttribute(WebKeys.SHOPPING_CART,shoppingCart);
192         }
193         
194         ProductsForm productsForm = (ProductsForm) form;
195         List JavaDoc<Product> products = productsForm.getListProducts();
196         for(int i = 0;i < products.size();i++)
197         {
198             Product product = productsForm.getListProducts().get(i);
199             List JavaDoc<ProductFormat> formats = product.getFormats();
200             for(int j = 0;j < formats.size();j++)
201             {
202                 ProductFormat format = formats.get(j);
203                 String JavaDoc selectedParameter = product.getInode() + "|" + format.getInode() + "|" + "ADD";
204                 
205                 if (UtilMethods.isSet(request.getParameter(selectedParameter)) &&
206                         request.getParameter(selectedParameter).equals("on"))
207                 {
208                     String JavaDoc quantityParameter = product.getInode() + "|" + format.getInode() + "|" + "QUANTITY";
209                     quantityParameter = request.getParameter(quantityParameter);
210                     int quantity = (UtilMethods.isSet(quantityParameter) && com.dotmarketing.util.UtilMethods.isInt(quantityParameter) ? Integer.parseInt(quantityParameter) : 0);
211                     //Add the items to the shoppingCart
212
if(quantity > 0)
213                     {
214                         shoppingCart.addItem(quantity,format.getInode());
215                     }
216                 }
217             }
218         }
219         SessionMessages.add(request, "message","message.order_manager.shopping_cart_added");
220     }
221     
222     private void _updateCard(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response,HttpSession JavaDoc session)
223     {
224         ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART);
225         if (shoppingCart == null)
226         {
227             shoppingCart = new ShoppingCart();
228             session.setAttribute(WebKeys.SHOPPING_CART,shoppingCart);
229         }
230         
231         ProductsForm productsForm = (ProductsForm) form;
232         List JavaDoc<Product> products = productsForm.getListProducts();
233         for(int i = 0;i < products.size();i++)
234         {
235             Product product = productsForm.getListProducts().get(i);
236             List JavaDoc<ProductFormat> formats = product.getFormats();
237             for(int j = 0;j < formats.size();j++)
238             {
239                 ProductFormat format = formats.get(j);
240                 String JavaDoc quantityParameter = product.getInode() + "|" + format.getInode() + "|" + "QUANTITY";
241                 quantityParameter = request.getParameter(quantityParameter);
242                 int quantity = (UtilMethods.isSet(quantityParameter) && com.dotmarketing.util.UtilMethods.isInt(quantityParameter) ? Integer.parseInt(quantityParameter) : -1);
243                 //Add the items to the shoppingCart
244
if (quantity >= 0)
245                 {
246                     if(quantity > 0)
247                     {
248                         shoppingCart.setQuantityItem(quantity,format.getInode());
249                     }
250                     else
251                     {
252                         shoppingCart.removeItem(format.getInode());
253                     }
254                 }
255             }
256         }
257         SessionMessages.add(request, "message","message.order_manager.shopping_cart_updated");
258     }
259         
260     
261     private void _removeFromCard(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response,HttpSession JavaDoc session)
262     {
263         String JavaDoc formatInodeString = request.getParameter("formatInode");
264         ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute(WebKeys.SHOPPING_CART);
265         if (UtilMethods.isLong(formatInodeString))
266         {
267             long formatInode = Long.parseLong(formatInodeString);
268             shoppingCart.removeItem(formatInode);
269             SessionMessages.add(request, "message","message.order_manager.shopping_cart_deleted");
270         }
271     }
272     
273     private void _addDiscount(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response,HttpSession JavaDoc session)
274     {
275         ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART);
276         if (shoppingCart == null)
277         {
278             shoppingCart = new ShoppingCart();
279             session.setAttribute(WebKeys.SHOPPING_CART,shoppingCart);
280         }
281         String JavaDoc discountString = request.getParameter("discount");
282         String JavaDoc[] discounts = discountString.split(":");
283         
284         for(String JavaDoc discount : discounts)
285         {
286             discount = discount.trim();
287             DiscountCode discountCode = DiscountCodeFactory.getDiscountCodeById(discount);
288             if(discountCode.getInode() != 0)
289             {
290                 shoppingCart.deleteDiscountCode(discount);
291                 shoppingCart.addDiscount(discountCode);
292             }
293         }
294         SessionMessages.add(request, "message","message.order_manager.shopping_cart_discount_added");
295     }
296     
297     private void _removeDiscount(ActionMapping mapping, ActionForm form, PortletConfig config,ActionRequest request, ActionResponse response,HttpSession JavaDoc session)
298     {
299         ShoppingCart shoppingCart = (ShoppingCart)session.getAttribute(WebKeys.SHOPPING_CART);
300         if (shoppingCart == null)
301         {
302             shoppingCart = new ShoppingCart();
303             session.setAttribute(WebKeys.SHOPPING_CART,shoppingCart);
304         }
305         String JavaDoc discountString = request.getParameter("discountId");
306                 
307         shoppingCart.deleteDiscountCode(discountString);
308                 
309         SessionMessages.add(request, "message","message.order_manager.shopping_cart_discount_removed");
310     }
311 }
312
Popular Tags