KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > product > action > CheckOutAction


1 package com.dotmarketing.cms.product.action;
2
3
4 import java.util.Date JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.http.HttpSession JavaDoc;
10
11 import org.apache.struts.action.ActionErrors;
12 import org.apache.struts.action.ActionForm;
13 import org.apache.struts.action.ActionForward;
14 import org.apache.struts.action.ActionMapping;
15 import org.apache.struts.actions.DispatchAction;
16
17 import com.dotmarketing.beans.UserProxy;
18 import com.dotmarketing.cms.factories.PublicAddressFactory;
19 import com.dotmarketing.cms.product.model.Holder;
20 import com.dotmarketing.cms.product.model.ShoppingCart;
21 import com.dotmarketing.db.DotHibernate;
22 import com.dotmarketing.factories.InodeFactory;
23 import com.dotmarketing.factories.UserProxyFactory;
24 import com.dotmarketing.portlets.discountcode.model.DiscountCode;
25 import com.dotmarketing.portlets.order_manager.factories.OrderFactory;
26 import com.dotmarketing.portlets.order_manager.factories.OrderItemFactory;
27 import com.dotmarketing.portlets.order_manager.model.Order;
28 import com.dotmarketing.portlets.order_manager.model.OrderItem;
29 import com.dotmarketing.portlets.order_manager.struts.OrderForm;
30 import com.dotmarketing.portlets.organization.model.Organization;
31 import com.dotmarketing.portlets.product.model.ProductFormat;
32 import com.dotmarketing.portlets.product.model.ProductPrice;
33 import com.dotmarketing.util.Config;
34 import com.dotmarketing.util.UtilMethods;
35 import com.dotmarketing.util.WebKeys;
36 import com.dotmarketing.viewtools.ProductWebAPI;
37 import com.liferay.portal.ejb.UserLocalManagerUtil;
38 import com.liferay.portal.model.Address;
39 import com.liferay.portal.model.User;
40
41 public class CheckOutAction extends DispatchAction
42 {
43     public ActionForward unspecified(ActionMapping mapping, ActionForm lf,
44             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
45     throws Exception JavaDoc {
46         ActionForward af = null;
47         User userSession = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
48         if (userSession == null) {
49             //UserProxy proxy = UserProxyFactory.getUserProxy(userSession);
50
//request.setAttribute("userProxyInode", proxy.getInode());
51
request.setAttribute("from", "purchaseOrder");
52             String JavaDoc url = request.getRequestURI() + "?dispatch=afterLogin";
53             request.setAttribute("referrer",url);
54             request.getSession().setAttribute(WebKeys.REDIRECT_AFTER_LOGIN,url);
55             af = mapping.findForward("registrantInfoPage");
56             return af;
57         }
58         else
59         {
60             
61             return afterLogin(mapping,lf,request,response);
62         }
63     }
64     
65     public ActionForward afterLogin(ActionMapping mapping, ActionForm lf, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
66     {
67         User userSession = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
68         OrderForm orderForm = (OrderForm) lf;
69         //Getting the user from the session
70
userSession = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
71         String JavaDoc userId = userSession.getUserId();
72         String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
73         
74         //get liferay user
75
User user = UserLocalManagerUtil.getUserById(companyId, userId);
76         //List addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
77
//Address address = (Address) addresses.get(0);
78

79         UserProxy userProxy = UserProxyFactory.getUserProxy(user);
80         Organization organization = (Organization) InodeFactory.getParentOfClass(userProxy,Organization.class);
81         List JavaDoc addresses;
82         Address address;
83         boolean haveOrganization = false;
84         if (organization.getInode() > 0){
85             haveOrganization = true;
86             //Billing Address
87
orderForm.setBillingAddressType("Work");
88             orderForm.setBillingAddress1(organization.getStreet1());
89             orderForm.setBillingAddress2(organization.getStreet2());
90             orderForm.setBillingCity(organization.getCity());
91             orderForm.setBillingCountry(Config.getStringProperty("DEFAULT_COUNTRY_CODE"));
92             orderForm.setBillingState(organization.getState());
93             orderForm.setBillingZip(organization.getZip());
94             orderForm.setBillingContactName(user.getFullName());
95             orderForm.setBillingContactPhone(organization.getPhone());
96             orderForm.setBillingContactEmail(user.getEmailAddress());
97             
98             //Work Address
99
orderForm.setWorkAddress1(organization.getStreet1());
100             orderForm.setWorkAddress2(organization.getStreet2());
101             orderForm.setWorkCity(organization.getCity());
102             orderForm.setWorkCountry("United States of America");
103             orderForm.setWorkState(organization.getState());
104             orderForm.setWorkZip(organization.getZip());
105             orderForm.setWorkContactName(user.getFullName());
106             orderForm.setWorkContactPhone(organization.getPhone());
107             orderForm.setWorkContactEmail(user.getEmailAddress());
108             orderForm.setWorkLabel(organization.getTitle()!=null?organization.getTitle():"");
109             
110             //Shipping Address
111
orderForm.setShippingAddressType("Work");
112             orderForm.setShippingAddress1(organization.getStreet1());
113             orderForm.setShippingAddress2(organization.getStreet2());
114             orderForm.setShippingCity(organization.getCity());
115             orderForm.setShippingCountry(Config.getStringProperty("DEFAULT_COUNTRY_CODE"));
116             orderForm.setShippingState(organization.getState());
117             orderForm.setShippingZip(organization.getZip());
118             orderForm.setShippingPhone(organization.getPhone());
119             orderForm.setShippingFax(organization.getFax());
120             orderForm.setShippingLabel(organization.getTitle()!=null?organization.getTitle():"");
121         }
122         
123         addresses = PublicAddressFactory.getAddressesByUserId(user.getUserId());
124         int posAux = -1;
125         for (int i=0; i<addresses.size();i++){
126             address = (Address) addresses.get(i);
127             if ((address.getDescription() != null) && (address.getDescription().toLowerCase().equals("home"))){
128                 orderForm.setHomeAddress1(address.getStreet1());
129                 orderForm.setHomeAddress2(address.getStreet2());
130                 orderForm.setHomeCity(address.getCity());
131                 orderForm.setHomeCountry(address.getCountry());
132                 if (address.getCountry()==null || address.getCountry().equals("United States of America")) {
133                     orderForm.setHomeCountry("United States of America");
134                     orderForm.setHomeState(address.getState());
135                 }
136                 else{
137                     orderForm.setHomeState("otherCountry");
138                     orderForm.setHomeStateOtherCountryText(address.getState());
139                 }
140                 orderForm.setHomeZip(address.getZip());
141                 orderForm.setHomeContactName(user.getFullName());
142                 orderForm.setHomeContactPhone(address.getPhone());
143                 orderForm.setHomeContactEmail(user.getEmailAddress());
144                 orderForm.setHomePhone(address.getPhone());
145                 orderForm.setHomeFax(address.getFax());
146             }
147             if ((address.getDescription() != null) && (address.getDescription().toLowerCase().equals("work"))){
148                 posAux = i;
149                 if (!(haveOrganization)){
150                     orderForm.setWorkAddress1(address.getStreet1());
151                     orderForm.setWorkAddress2(address.getStreet2());
152                     orderForm.setWorkCity(address.getCity());
153                     orderForm.setWorkCountry(address.getCountry());
154                     if (address.getCountry()==null || address.getCountry().equals("United States of America")) {
155                         orderForm.setWorkCountry("United States of America");
156                         orderForm.setWorkState(address.getState());
157                     }
158                     else{
159                         orderForm.setWorkState("otherCountry");
160                         orderForm.setWorkStateOtherCountryText(address.getState());
161                     }
162                     orderForm.setWorkZip(address.getZip());
163                     orderForm.setWorkContactName(user.getFullName());
164                     orderForm.setWorkContactPhone(address.getPhone());
165                     orderForm.setWorkContactEmail(user.getEmailAddress());
166                     orderForm.setWorkPhone(address.getPhone());
167                     orderForm.setWorkFax(address.getFax());
168                 }
169             }
170         }
171         if (!(haveOrganization)){
172             if (posAux == -1)
173                 posAux = 0;
174             address = (Address) addresses.get(posAux);
175             
176             //Billing Address
177
orderForm.setBillingAddressType(address.getDescription());
178             orderForm.setBillingAddress1(address.getStreet1());
179             orderForm.setBillingAddress2(address.getStreet2());
180             orderForm.setBillingCity(address.getCity());
181             orderForm.setBillingCountry(address.getCountry());
182             if (address.getCountry()==null || address.getCountry().equals(Config.getStringProperty("DEFAULT_COUNTRY_CODE"))) {
183                 orderForm.setBillingState(address.getState());
184                 orderForm.setBillingCountry(Config.getStringProperty("DEFAULT_COUNTRY_CODE"));
185             }
186             else{
187                 orderForm.setBillingState("otherCountry");
188                 orderForm.setBillingStateOtherCountryText(address.getState());
189             }
190             orderForm.setBillingZip(address.getZip());
191             orderForm.setBillingContactName(user.getFullName());
192             orderForm.setBillingContactPhone(address.getPhone());
193             orderForm.setBillingContactEmail(user.getEmailAddress());
194             
195             //Shipping Address
196
orderForm.setShippingAddressType(address.getDescription());
197             orderForm.setShippingAddress1(address.getStreet1());
198             orderForm.setShippingAddress2(address.getStreet2());
199             orderForm.setShippingCity(address.getCity());
200             orderForm.setShippingCountry(address.getCountry());
201             if (address.getCountry()==null || address.getCountry().equals(Config.getStringProperty("DEFAULT_COUNTRY_CODE"))) {
202                 orderForm.setShippingState(address.getState());
203                 orderForm.setShippingCountry(Config.getStringProperty("DEFAULT_COUNTRY_CODE"));
204             }
205             else{
206                 orderForm.setShippingState("otherCountry");
207                 orderForm.setShippingStateOtherCountryText(address.getState());
208             }
209             orderForm.setShippingZip(address.getZip());
210             orderForm.setShippingPhone(address.getPhone());
211             orderForm.setShippingFax(address.getFax());
212             orderForm.setShippingLabel("");
213         }
214         
215         //save the order----------------------------------
216
HttpSession JavaDoc session = request.getSession();
217         ShoppingCart shoppingCart = (ShoppingCart) session.getAttribute(WebKeys.SHOPPING_CART);
218         DotHibernate.startTransaction();
219         Date JavaDoc now = new Date JavaDoc();
220         Order order;
221         List JavaDoc<Holder> holders;
222         String JavaDoc newOrderInode = (String JavaDoc) session.getAttribute("newOrderInode");
223         if (newOrderInode == null)
224         {
225             order = new Order();
226         }
227         else
228         {
229             order = OrderFactory.getOrderById(newOrderInode);
230             List JavaDoc<OrderItem> orderItems = OrderItemFactory.getAllOrderItemsByParentOrder(order);
231             for(OrderItem orderItem : orderItems)
232             {
233                 OrderItemFactory.deleteOrderItem(orderItem);
234             }
235         }
236         order.setUserInode(userProxy.getInode());
237         order.setDatePosted(now);
238         order.setLastModDate(now);
239         //Billing
240
order.setBillingAddress1(orderForm.getBillingAddress1());
241         order.setBillingAddress2(orderForm.getBillingAddress2());
242         order.setBillingCity(orderForm.getBillingCity());
243         if (orderForm.getBillingState() != null){
244         if (orderForm.getBillingState().equals("otherCountry")){
245             order.setBillingState(orderForm.getBillingStateOtherCountryText());
246             orderForm.setBillingState(orderForm.getBillingStateOtherCountryText());
247         }
248         else
249             order.setBillingState(orderForm.getBillingState());
250         }
251         order.setBillingZip(orderForm.getBillingZip());
252         order.setBillingCountry(orderForm.getBillingCountry());
253         order.setBillingPhone(orderForm.getBillingPhone());
254         order.setBillingFax(orderForm.getBillingFax());
255         order.setBillingContactName(orderForm.getBillingContactName());
256         order.setBillingContactPhone(orderForm.getBillingContactPhone());
257         order.setBillingContactEmail(orderForm.getBillingContactPhone());
258         //Shipping
259
order.setShippingAddress1(orderForm.getShippingAddress1());
260         order.setShippingAddress2(orderForm.getShippingAddress2());
261         order.setShippingLabel(orderForm.getShippingLabel());
262         order.setShippingCity(orderForm.getShippingCity());
263         if (orderForm.getShippingState().equals("otherCountry"))
264             order.setShippingState(orderForm.getShippingStateOtherCountryText());
265         else
266             order.setShippingState(orderForm.getShippingState());
267         order.setShippingZip(orderForm.getShippingZip());
268         order.setShippingCountry(orderForm.getShippingCountry());
269         order.setShippingPhone(orderForm.getShippingPhone());
270         order.setShippingFax(orderForm.getShippingFax());
271         int orderStatus = Config.getIntProperty("ECOM_ORDER_INCOMPLETE");
272         order.setOrderStatus(orderStatus);
273         int paymentStatus = Config.getIntProperty("ECOM_PAY_WAITING");
274         order.setPaymentStatus(paymentStatus);
275         //Set Discount Codes
276
List JavaDoc<DiscountCode> discounts = shoppingCart.getDiscountCodes();
277         String JavaDoc discountCodes = "";
278         for(DiscountCode discount : discounts)
279         {
280             discountCodes += discount.getCodeId() + ":";
281         }
282         if (UtilMethods.isSet(discountCodes))
283         {
284             discountCodes = discountCodes.substring(0,discountCodes.lastIndexOf(":"));
285         }
286         order.setDiscountCodes(discountCodes);
287         orderForm.setDiscountCodes(discountCodes);
288         
289         order.setPaymentType("1");
290         order.setNameOnCard("");
291         order.setCardType("");
292         order.setCardNumber("");
293         order.setCardExpMonth(0);
294         order.setCardExpYear(0);
295         order.setCardVerificationValue("");
296         order.setOrderTotalPaid(0);
297         order.setOrderShipType(0);
298         
299         //Save the Order
300
OrderFactory.saveOrder(order);
301         orderForm.setInode(order.getInode());
302         boolean isPartner = false;
303         if (session.getAttribute("isPartner") != null &&
304             session.getAttribute("isPartner").equals("true"))
305         {
306             isPartner = true;
307         }
308
309         
310         float orderSubTotal = 0F;
311         holders = shoppingCart.getHolders();
312         for(Holder holder : holders)
313         {
314             ProductFormat format = holder.getFormat();
315             OrderItem orderItem = new OrderItem();
316             orderItem.setOrderInode(order.getInode());
317             orderItem.setProductInode(format.getInode());
318             orderItem.setItemQty(holder.getQuantity());
319             int quantity = holder.getQuantity();
320             ProductPrice productPrice = format.getQuantityPrice(quantity,discounts);
321             float price = (isPartner) ? productPrice.getPartnerPrice() : productPrice.getRetailPrice();
322             orderItem.setItemPrice(price);
323             float lineTotal = quantity * price;
324             orderSubTotal += lineTotal;
325             OrderItemFactory.saveOrderItem(orderItem);
326             holder.setInode(orderItem.getInode());
327         }
328         //Discount
329
float orderDiscount = 0;
330         ProductWebAPI pwa = new ProductWebAPI();
331         orderDiscount = pwa.getTotalApplicableDiscount(holders,discounts,isPartner);
332         orderForm.setOrderDiscount(orderDiscount);
333         order.setOrderDiscount(orderDiscount);
334         //SubTotal - Discount
335
float orderSubTotalDiscount = orderSubTotal - orderDiscount;
336         orderSubTotalDiscount = (orderSubTotalDiscount < 0 ? 0 : orderSubTotalDiscount);
337         orderForm.setOrderSubTotalDiscount(orderSubTotalDiscount);
338         //float orderSubTotalDiscount = orderSubTotal;
339
orderForm.setOrderSubTotalDiscount(orderSubTotalDiscount);
340         order.setOrderTotalDue(orderSubTotal);
341         order.setOrderSubTotal(orderSubTotal);
342         order.setOrderTotal(orderSubTotal);
343         //Save the Order
344
OrderFactory.saveOrder(order);
345         session.setAttribute("newOrderInode",String.valueOf(order.getInode()));
346         //------------------------------------------------
347
DotHibernate.commitTransaction();
348         ActionForward af = mapping.findForward("checkOut");
349         return af;
350     }
351     
352     
353     public ActionForward reload(ActionMapping mapping, ActionForm lf, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
354     {
355         ActionForward af = mapping.findForward("checkOut");
356         return af;
357     }
358     
359     public ActionForward validate(ActionMapping mapping, ActionForm lf, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc
360     {
361         OrderForm orderForm = (OrderForm) lf;
362         ActionErrors ae = orderForm.validate();
363         if(ae != null && ae.size() > 0)
364         {
365             saveMessages(request, ae);
366             ActionForward af = mapping.getInputForward();
367             return af;
368         }
369         
370         ActionForward af = mapping.findForward("reviewOrder");
371         return af;
372     }
373 }
Popular Tags