BrightSide Workbench Full Report + Source Code
Cart.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.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Currency;
25 import java.util.Iterator;
26 import org.turro.action.Contacts;
27 import org.turro.action.IRegisterCallback;
28 import org.turro.elephant.impl.util.Serializer;
29 import org.turro.elephant.security.IUser;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.util.IdGenerator;
32 
37 public class Cart implements Serializable, IRegisterCallback {
38 
39  private boolean wantBill = false, pickup = false;
40  private int status = 0;
41  private final CartItemSet items;
42  private String order, contactId, deliveryValue;
43  private transient IContact contact;
44 
45  public Cart() {
46  this.items = new CartItemSet();
47  }
48 
49  public String getResetOrder() {
50  order = IdGenerator.generateLong().substring(2, 14);
51  return order;
52  }
53 
54  public CartItemSet getItems() {
55  return items;
56  }
57 
58  public String getOrder() {
59  return order;
60  }
61 
62  public int getStatus() {
63  return status;
64  }
65 
66  public void setStatus(int status) {
67  this.status = status;
68  }
69 
70  public boolean isWantBill() {
71  return wantBill;
72  }
73 
74  public void setWantBill(boolean wantBill) {
75  this.wantBill = wantBill;
76  }
77 
78  public boolean isPickup() {
79  return pickup;
80  }
81 
82  public void setPickup(boolean pickup) {
83  this.pickup = pickup;
84  }
85 
86  public String getDeliveryValue() {
87  return deliveryValue;
88  }
89 
90  public void setDeliveryValue(String deliveryValue) {
91  this.deliveryValue = deliveryValue;
92  }
93 
94  public boolean isEmpty() {
95  return items.isEmpty();
96  }
97 
98  public boolean needsDelivery() {
99  Iterator<CartItem> it = items.iterator();
100  while(it.hasNext()) {
101  if(it.next().isNeedsDelivery()) {
102  return true;
103  }
104  }
105  return false;
106  }
107 
108  public void removeDeliveryItem() {
109  Iterator<CartItem> it = items.iterator();
110  while(it.hasNext()) {
111  if(it.next().isDeliveryItem()) {
112  it.remove();
113  }
114  }
115  }
116 
117  public boolean addDeliveryItem(long productId, double quantity, double tax, double price) {
118  CartItem ci = new CartItem();
119  ci.setProductId(productId);
120  ci.setQuantity(quantity);
121  ci.setTax(tax);
122  ci.setPrice(price);
123  ci.makeValid();
124  ci.setDeliveryItem(true);
125  return items.addItem(ci);
126  }
127 
128  public boolean addItem(long productId, double quantity, double tax, double price) {
129  setStatus(0);
130  CartItem ci = new CartItem();
131  ci.setProductId(productId);
132  ci.setQuantity(quantity);
133  ci.setTax(tax);
134  ci.setPrice(price);
135  ci.makeValid();
136  ci.setDeliveryItem(false);
137  return items.addItem(ci);
138  }
139 
140  public void removeItem(Integer order) {
141  setStatus(0);
142  Iterator<CartItem> it = items.iterator();
143  while(it.hasNext()) {
144  if(it.next().getOrder() == order) {
145  it.remove();
146  break;
147  }
148  }
149  }
150 
151  public void changeItem(Integer order, double quantity) {
152  setStatus(0);
153  Iterator<CartItem> it = items.iterator();
154  while(it.hasNext()) {
155  CartItem ci = it.next();
156  if(ci.getOrder() == order) {
157  ci.setQuantity(quantity);
158  break;
159  }
160  }
161  }
162 
163  public void changeItem(Integer order, String concept) {
164  setStatus(0);
165  Iterator<CartItem> it = items.iterator();
166  while(it.hasNext()) {
167  CartItem ci = it.next();
168  if(ci.getOrder() == order) {
169  ci.setConcept(concept);
170  break;
171  }
172  }
173  }
174 
175  public CartItem getItem(Integer order) {
176  Iterator<CartItem> it = items.iterator();
177  while(it.hasNext()) {
178  CartItem ci = it.next();
179  if(ci.getOrder() == order) {
180  return ci;
181  }
182  }
183  return null;
184  }
185 
186  public Collection<CartItem> getProducts(long id) {
187  ArrayList<CartItem> list = new ArrayList<>();
188  for(CartItem ci : items) {
189  if(ci.getProductId() == id) {
190  list.add(ci);
191  }
192  }
193  return list;
194  }
195 
196  public double getTotalAmount() {
197  double value = 0.0;
198  for(CartItem ci : items) {
199  value += ci.getAmount();
200  }
201  return value;
202  }
203 
204  public double getTotalTaxable() {
205  double value = 0.0;
206  for(CartItem ci : items) {
207  value += ci.getTaxable();
208  }
209  return value;
210  }
211 
212  public double getTotalTax() {
213  double value = 0.0;
214  for(CartItem ci : items) {
215  value += ci.getTaxAmount();
216  }
217  return value;
218  }
219 
221  return new CartAmounts(this);
222  }
223 
224  public IContact getContact() {
225  if(contact == null) {
226  contact = Contacts.getContactById(contactId);
227  }
228  return contact;
229  }
230 
231 public void serializeFor(String email) {
232  setStatus(0);
233  Serializer.serialize("/cart/" + email + ".ser", this);
234  }
235 
236  public void serializeFor(IContact contact) {
237  setStatus(0);
238  Serializer.serialize("/cart/" + contact.getId() + ".ser", this);
239  }
240 
241  public void serializeForOrder(IContact contact) {
242  contactId = contact.getId();
243  Serializer.serialize("/cart/" + getOrder() + ".ser", this);
244  }
245 
246  public static Cart deserializeFor(IContact contact) {
247  Cart cart = (Cart) Serializer.deserialize("/cart/" + contact.getId() + ".ser");
248  if(cart == null) {
249  String mailFile = "/cart/" + contact.getConnector(IUser.CONNECTOR_EMAIL) + ".ser";
250  cart = (Cart) Serializer.deserialize(mailFile);
251  }
252  return cart;
253  }
254 
255  public static Cart deserializeForOrder(String order) {
256  Cart cart = (Cart) Serializer.deserialize("/cart/" + order + ".ser");
257  Serializer.remove("/cart/" + order + ".ser");
258  return cart;
259  }
260 
261  public static void removeFor(IContact contact) {
262  Serializer.remove("/cart/" + contact.getId() + ".ser");
263  Serializer.remove("/cart/" + contact.getConnector(IUser.CONNECTOR_EMAIL) + ".ser");
264  }
265 
266  @Override
267  public void onRegister(String email) {
268  serializeFor(email);
269  }
270 
271  private Currency _currency;
272 
273  public Currency getCurrency() {
274  if(_currency == null) {
275  _currency = Currency.getInstance(ShopContext.getInstance().getCurrency());
276  }
277  return _currency;
278  }
279 
280 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static void serialize(String fileName, Object instance)
Definition: Serializer.java:49
static void remove(String fileName)
Definition: Serializer.java:69
static Object deserialize(String fileName)
Definition: Serializer.java:60
boolean addItem(CartItem cartItem)
void setProductId(long productId)
Definition: CartItem.java:51
void setQuantity(double quantity)
Definition: CartItem.java:59
void setConcept(String concept)
Definition: CartItem.java:83
void setDeliveryItem(boolean deliveryItem)
Definition: CartItem.java:95
void changeItem(Integer order, double quantity)
Definition: Cart.java:151
Collection< CartItem > getProducts(long id)
Definition: Cart.java:186
static void removeFor(IContact contact)
Definition: Cart.java:261
void onRegister(String email)
Definition: Cart.java:267
static Cart deserializeForOrder(String order)
Definition: Cart.java:255
boolean addItem(long productId, double quantity, double tax, double price)
Definition: Cart.java:128
void setDeliveryValue(String deliveryValue)
Definition: Cart.java:90
void removeItem(Integer order)
Definition: Cart.java:140
void setStatus(int status)
Definition: Cart.java:66
CartItemSet getItems()
Definition: Cart.java:54
void changeItem(Integer order, String concept)
Definition: Cart.java:163
CartAmounts getAmounts()
Definition: Cart.java:220
static Cart deserializeFor(IContact contact)
Definition: Cart.java:246
boolean addDeliveryItem(long productId, double quantity, double tax, double price)
Definition: Cart.java:117
void setPickup(boolean pickup)
Definition: Cart.java:82
void setWantBill(boolean wantBill)
Definition: Cart.java:74
void serializeFor(IContact contact)
Definition: Cart.java:236
void serializeForOrder(IContact contact)
Definition: Cart.java:241
CartItem getItem(Integer order)
Definition: Cart.java:175
void serializeFor(String email)
Definition: Cart.java:231
static final String CONNECTOR_EMAIL
Definition: IUser.java:27