BrightSide Workbench Full Report + Source Code
DeliveryContexts.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.delivery;
20 
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import org.jdom.Element;
26 import org.turro.contacts.Address;
27 import org.turro.financials.entity.Product;
28 
33 public class DeliveryContexts extends ArrayList<DeliveryContext> {
34 
36  for(DeliveryContext dc : this) {
37  if(dc instanceof StreetMapContext) {
38  if(dc.match(address.getAddressString())) {
39  Product p = dc.getDeliveryProduct();
40  if(p != null) {
41  return p;
42  }
43  }
44  } else if(dc instanceof ZipcodeContext) {
45  if(dc.match(address.getZipCode())) {
46  Product p = dc.getDeliveryProduct();
47  if(p != null) {
48  return p;
49  }
50  }
51  } else if(dc instanceof ProvinceContext) {
52  if(dc.match(address.getProvince())) {
53  Product p = dc.getDeliveryProduct();
54  if(p != null) {
55  return p;
56  }
57  }
58  }
59  }
60  return null;
61  }
62 
63  public Product getDeliveryProduct(String value) {
64  for(DeliveryContext dc : this) {
65  if(dc instanceof ValueContext) {
66  if(dc.match(value)) {
67  Product p = dc.getDeliveryProduct();
68  if(p != null) {
69  return p;
70  }
71  }
72  }
73  }
74  return null;
75  }
76 
77  public Set<String> getDeliveryValues() {
78  Set<String> values = new HashSet<>();
79  for(DeliveryContext dc : this) {
80  if(dc instanceof ValueContext) {
81  values.addAll(((ValueContext) dc).getValues());
82  }
83  }
84  return values;
85  }
86 
87  public List<StreetItem> getDeliveryStreets(String search) {
88  List<StreetItem> streets = new ArrayList<>();
89  for(DeliveryContext dc : this) {
90  if(dc instanceof StreetMapContext) {
91  streets.addAll(((StreetMapContext) dc).getStreets(search));
92  }
93  }
94  return streets;
95  }
96 
97  public boolean isStreetBased() {
98  return this.stream().anyMatch((dc) -> (dc instanceof StreetMapContext));
99  }
100 
101  public void readXML(Element root) {
102  List<Element> children = root.getChildren("DeliveryByStreetMap");
103  for(Element el : children) {
104  add(new StreetMapContext(el.getAttributeValue("value"),
105  Long.valueOf(el.getAttributeValue("product")),
106  Long.valueOf(el.getAttributeValue("store")),
107  el.getAttributeValue("logistic")));
108  }
109  children = root.getChildren("DeliveryByZipcode");
110  for(Element el : children) {
111  add(new ZipcodeContext(el.getAttributeValue("value"),
112  Long.valueOf(el.getAttributeValue("product")),
113  Long.valueOf(el.getAttributeValue("store")),
114  el.getAttributeValue("logistic")));
115  }
116  children = root.getChildren("DeliveryByProvince");
117  for(Element el : children) {
118  add(new ProvinceContext(el.getAttributeValue("value"),
119  Long.valueOf(el.getAttributeValue("product")),
120  Long.valueOf(el.getAttributeValue("store")),
121  el.getAttributeValue("logistic")));
122  }
123  children = root.getChildren("DeliveryByValue");
124  for(Element el : children) {
125  add(new ValueContext(el.getAttributeValue("value"),
126  Long.valueOf(el.getAttributeValue("product")),
127  Long.valueOf(el.getAttributeValue("store")),
128  el.getAttributeValue("logistic")));
129  }
130  }
131 
132 }
List< StreetItem > getDeliveryStreets(String search)