BrightSide Workbench Full Report + Source Code
ShopContext.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;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.JDOMException;
28 import org.jdom.input.SAXBuilder;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.impl.context.DefaultConstructor;
31 import org.turro.file.FileWatch;
32 import org.turro.financials.cart.delivery.DeliveryContexts;
33 import org.turro.financials.db.FinancialsPU;
34 import org.turro.financials.entity.Contract;
35 import org.zkoss.lang.Strings;
36 
41 public class ShopContext {
42 
43  private final static String
44  SHOP_FILE = "/WEB-INF/elephant/conf/shop.xml";
45 
46  private String cartPath, checkoutPath, notificationPath, OKPath, KOPath,
47  delivery, fiscal, logistic, contactPhone, locale, currency,
48  merchantCode, merchantTerminal, merchantCurrency, merchantKey,
49  merchantKeyType, merchantLanguage;
50  private boolean merchantTestScope, selfTestScope;
51  private long store, bank;
52  private DeliveryContexts deliveryContexts;
53 
54  public boolean isValid() {
55  return !Strings.isBlank(cartPath);
56  }
57 
58  public String getCartPath() {
59  return cartPath;
60  }
61 
62  public String getCheckoutPath() {
63  return checkoutPath;
64  }
65 
66  public String getNotificationPath() {
67  return notificationPath;
68  }
69 
70  public String getOKPath() {
71  return OKPath;
72  }
73 
74  public String getKOPath() {
75  return KOPath;
76  }
77 
78  public long getStore() {
79  return store;
80  }
81 
82  public long getBank() {
83  return bank;
84  }
85 
87  return new FinancialsPU().find(Contract.class, store);
88  }
89 
91  return new FinancialsPU().find(Contract.class, bank);
92  }
93 
94  public String getDelivery() {
95  return delivery;
96  }
97 
98  public String getFiscal() {
99  return fiscal;
100  }
101 
102  public String getLogistic() {
103  return logistic;
104  }
105 
106  public String getContactPhone() {
107  return contactPhone;
108  }
109 
110  public String getLocale() {
111  return locale;
112  }
113 
114  public String getCurrency() {
115  return currency;
116  }
117 
118  public String getMerchantCode() {
119  return merchantCode;
120  }
121 
122  public String getMerchantTerminal() {
123  return merchantTerminal;
124  }
125 
126  public String getMerchantCurrency() {
127  return merchantCurrency;
128  }
129 
130  public String getMerchantKey() {
131  return merchantKey;
132  }
133 
134  public String getMerchantKeyType() {
135  return merchantKeyType;
136  }
137 
138  public String getMerchantLanguage() {
139  return merchantLanguage;
140  }
141 
142  public boolean isMerchantTestScope() {
143  return merchantTestScope;
144  }
145 
146  public boolean isSelfTestScope() {
147  return selfTestScope;
148  }
149 
151  return deliveryContexts;
152  }
153 
154  private void load() {
155  Element conf = null;
156  SAXBuilder builder = new SAXBuilder();
157  Document doc;
158  try {
159  File confFile = new File(ElephantContext.getRealPath(SHOP_FILE));
160  if(confFile.exists()) {
161  doc = builder.build(confFile);
162  conf = doc.getRootElement();
163  }
164  if(conf != null) {
165  cartPath = readAttribute(conf.getChild("CartPath"), "value", "/cart");
166  checkoutPath = readAttribute(conf.getChild("CheckoutPath"), "value", "/cart/checkout");
167  notificationPath = readAttribute(conf.getChild("NotificationPath"), "value", "/cart/notification");
168  OKPath = readAttribute(conf.getChild("OKPath"), "value", "/cart/OK");
169  KOPath = readAttribute(conf.getChild("KOPath"), "value", "/cart/KO");
170  store = Long.valueOf(readAttribute(conf.getChild("Store"), "value", "0"));
171  bank = Long.valueOf(readAttribute(conf.getChild("Bank"), "value", "0"));
172  delivery = readAttribute(conf.getChild("Delivery"), "value", "Delivery");
173  fiscal = readAttribute(conf.getChild("Fiscal"), "value", "Fiscal");
174  logistic = readAttribute(conf.getChild("Logistic"), "value", "");
175  contactPhone = readAttribute(conf.getChild("ContactPhone"), "value", "Contact phone");
176  locale = readAttribute(conf.getChild("Locale"), "value", "ca_ES");
177  currency = readAttribute(conf.getChild("Currency"), "value", "EUR");
178  merchantCode = readAttribute(conf.getChild("MerchantCode"), "value", "");
179  merchantTerminal = readAttribute(conf.getChild("MerchantTerminal"), "value", "");
180  merchantCurrency = readAttribute(conf.getChild("MerchantCurrency"), "value", "");
181  merchantKey = readAttribute(conf.getChild("MerchantKey"), "value", "");
182  merchantKeyType = readAttribute(conf.getChild("MerchantKeyType"), "value", "");
183  merchantLanguage = readAttribute(conf.getChild("MerchantKeyLanguage"), "value", "");
184  merchantTestScope = "true".equals(readAttribute(conf.getChild("MerchantTestScope"), "value", "true"));
185  selfTestScope = "true".equals(readAttribute(conf.getChild("SelfTestScope"), "value", "false"));
186  deliveryContexts = new DeliveryContexts();
187  deliveryContexts.readXML(conf);
188  }
189  } catch (IOException | JDOMException ex) {
190  Logger.getLogger(DefaultConstructor.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
191  }
192  }
193 
194  private static String readAttribute(Element child, String attr, String def) {
195  return child != null ? child.getAttributeValue(attr) : def;
196  }
197 
198  private ShopContext() {}
199 
200  private static ShopContext _instance;
201  private static long _lastLoad;
202 
203  public static void reset() {
204  _instance = null;
205  }
206 
207  public static ShopContext getInstance() {
208  if(_instance == null || FileWatch.isNewer(ElephantContext.getRealPath(SHOP_FILE), _lastLoad)) {
209  _instance = new ShopContext();
210  _instance.load();
211  _lastLoad = FileWatch.getTime(ElephantContext.getRealPath(SHOP_FILE));
212  }
213  return _instance;
214  }
215 
216 }