BrightSide Workbench Full Report + Source Code
ShoppingCart.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.financials.cart;
20 
21 import java.util.Date;
22 import org.turro.string.ObjectString;
23 import org.turro.string.Strings;
24 import org.turro.action.Actions;
25 import org.turro.action.IRegisterCallback;
26 import org.turro.auth.Authentication;
27 import org.turro.collections.KeyValueMap;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.financials.db.FinancialsPU;
31 import org.turro.financials.entity.Product;
32 import org.turro.marker.ElephantMarker;
33 import org.turro.plugin.contacts.IContact;
34 
40 public class ShoppingCart {
41 
42  private Cart cart;
43  private final IConstructor constructor;
44  private final String shopping, template;
45 
46  public ShoppingCart(IConstructor constructor, String shopping, String template) {
47  this.constructor = constructor;
48  this.shopping = shopping;
49  this.template = template;
50  checkCart();
51  }
52 
53  public void renderCart() {
54  ElephantMarker marker = new ElephantMarker(constructor);
55  marker.put("shoppingCart", this);
56  marker.put("cart", cart);
57  marker.process("cart", getTemplate());
58  }
59 
60  public String getUrlForRemoveFromCart(int order) {
61  return Actions.createRightNowParameter("delFromCart=" + order);
62  }
63 
64  public String getUrlForChangeFromCart(int order) {
65  return Actions.createRightNowParameter("changeFromCart=" + order);
66  }
67 
68  public String getUrlForShopping() {
70  shopping;
71  }
72 
73  public String getUrlForCheckout() {
75  if(contact == null || !contact.isWebUser()) {
76  return ElephantContext.getRootWebPath() + "/user";
77  } else {
79  }
80  }
81 
82  private void checkCart() {
83  cart = getCart(constructor);
84  KeyValueMap values = Actions.getRightNowAction(constructor);
85  if(values != null) {
86  Long product = (Long) ObjectString.parseString(values.get("addToCart"), Long.class, true);
87  if(product > 0) {
88  Double quantity = (Double) ObjectString.parseString(values.get("quantity"), Double.class, true);
89  Double price = (Double) ObjectString.parseString(values.get("price"), Double.class, true);
90  if(quantity != 0.0) {
91  Double stock = getStock(cart, product);
92  if((stock == null) || ((stock - quantity) >= 0.0)) {
93  cart.addItem(product, quantity, 0, price);
94  }
95  }
96  }
97  int order = (Integer) ObjectString.parseString(values.get("delFromCart"), Integer.class, true);
98  if(order > 0) {
99  cart.removeItem(order);
100  }
101  order = (Integer) ObjectString.parseString(values.get("changeFromCart"), Integer.class, true);
102  if(order > 0) {
103  Double quantity = (Double) ObjectString.parseString(constructor.getParameter("q"), Double.class, true);
104  if(quantity != 0.0) {
105  CartItem ci = cart.getItem(order);
106  if(ci != null) {
107  Double stock = getStock(cart, ci.getProduct());
108  if((stock == null) || ((stock - quantity + ci.getQuantity()) >= 0.0)) {
109  cart.changeItem(order, quantity);
110  }
111  }
112  }
113  String concept = constructor.getParameter("c");
114  if(!Strings.isBlank(concept)) {
115  cart.changeItem(order, concept);
116  }
117  }
118  }
119  }
120 
121  private String getTemplate() {
122  return Strings.isBlank(template) ? "cart" : template;
123  }
124 
125  public static Cart getCart(IConstructor constructor) {
127  IContact contact = Authentication.getIContact();
128  if(cart == null) {
129  cart = new Cart();
131  if(contact == null) {
133  }
134  }
135  if(contact != null && cart.getItems().isEmpty()) {
136  Cart serCart = Cart.deserializeFor(contact);
137  if(serCart != null) {
138  cart = serCart;
140  Cart.removeFor(contact);
141  }
142  }
143  return cart;
144  }
145 
146  public static void removeCart(IConstructor constructor) {
148  }
149 
150  public static String getAddToCart(String product, String quantity, String price) {
152  "addToCart=" + product +
153  ";quantity=" + quantity +
154  ";price=" + price);
155  }
156 
157  public static Double getStockForProduct(IConstructor constructor, long id) {
158  Cart cart = getCart(constructor);
159  return getStock(cart, id);
160  }
161 
162  public static Double getStockForProduct(IConstructor constructor, Product product) {
163  Cart cart = getCart(constructor);
164  return getStock(cart, product);
165  }
166 
167  private static Double getStock(Cart cart, Long id) {
168  return getStock(cart, new FinancialsPU().find(Product.class, id));
169  }
170 
171  private static Double getStock(Cart cart, Product product) {
172  if(product != null && !product.isService()) {
173  Double stock = 0.0;
174  for(CartItem ci : cart.getProducts(product.getId())) {
175  stock -= ci.getQuantity();
176  }
177  stock += product.getProductStock(new Date(), ShopContext.getInstance().getContractStore());
178  return stock;
179  }
180  return null;
181  }
182 
183 }
static String createRightNowParameter(String values)
Definition: Actions.java:333
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
void changeItem(Integer order, double quantity)
Definition: Cart.java:151
static void removeFor(IContact contact)
Definition: Cart.java:261
boolean addItem(long productId, double quantity, double tax, double price)
Definition: Cart.java:128
void removeItem(Integer order)
Definition: Cart.java:140
CartItemSet getItems()
Definition: Cart.java:54
static Cart deserializeFor(IContact contact)
Definition: Cart.java:246
CartItem getItem(Integer order)
Definition: Cart.java:175
static Cart getCart(IConstructor constructor)
ShoppingCart(IConstructor constructor, String shopping, String template)
static String getAddToCart(String product, String quantity, String price)
static Double getStockForProduct(IConstructor constructor, long id)
static void removeCart(IConstructor constructor)
static Double getStockForProduct(IConstructor constructor, Product product)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void setSessionAttribute(String key, Object value)