BrightSide Workbench Full Report + Source Code
FinancialsParser.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.parser;
20 
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.ObjectString;
26 import org.turro.annotation.ExternalParser;
27 import org.turro.auth.Authentication;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.util.DecimalFormats;
31 import org.turro.financials.cart.ShoppingCart;
32 import org.turro.financials.cart.www.Checkout;
33 import org.turro.financials.content.ContractContentIterator;
34 import org.turro.financials.content.ProductContentIterator;
35 import org.turro.financials.db.FinancialsPU;
36 import org.turro.financials.entity.Product;
37 import org.turro.financials.product.www.ProductIterator;
38 import org.turro.html.HTMLGenerator;
39 import org.turro.jpa.Dao;
40 import org.turro.util.PhraseBuilder;
41 
47 public class FinancialsParser extends AbstractParser {
48 
49  @Override
50  public String getParserName() {
51  return "Financials";
52  }
53 
54  @Override
55  protected boolean doExecute() {
56  boolean done = false;
57  if("contracts".equals(getMacroName()) || "contract-list".equals(getMacroName())) {
58  //{@contracts:context:count:intervention}
59  boolean grouped = "contracts".equals(getMacroName());
61  Authentication.getIContact(), false,
62  ElephantContext.getRootWebPath() + getToken(1, "/user/contracts"));
63  ci.setNavigatorTop(grouped);
64  ci.setNavigatorBottom(grouped);
65  ci.setPage((Integer) ObjectString.parseNativeString(getToken(2), Integer.class, true));
66  ci.setCheckParticipation("true".equals(getToken(3)));
67  ci.setAllowAttachments(true);
68  ci.load();
69  ci.render();
70  done = true;
71  } else if("contract-type".equals(getMacroName()) || "contract-type-list".equals(getMacroName())) {
72  boolean grouped = "contract-type".equals(tokens[0]);
73  //{@contract-type:context:count:intervention:type[:template]}
75  Authentication.getIContact(), false,
76  ElephantContext.getRootWebPath() + getToken(1, "/user/contracts"));
77  ci.setNavigatorTop(grouped);
78  ci.setNavigatorBottom(grouped);
79  ci.setPage((Integer) ObjectString.parseNativeString(getToken(2), Integer.class, true));
80  ci.setType((Integer) ObjectString.parseNativeString(getToken(4), Integer.class, true));
81  ci.setCheckParticipation("true".equals(getToken(3)));
82  ci.setAllowAttachments(true);
84  ci.load();
85  ci.render();
86  done = true;
87  } else if("checkout".equals(getMacroName())) {
88  Checkout checkout = new Checkout(constructor);
89  checkout.renderCheckout();
90  done = true;
91  } else if("clear-cart".equals(getMacroName())) {
93  done = true;
94  } else if("products".equals(getMacroName()) || "product-list".equals(getMacroName()) ||
95  "promoted".equals(getMacroName()) || "promoted-list".equals(getMacroName())) {
96  boolean grouped = "products".equals(getMacroName()) || "promoted".equals(getMacroName());
97  boolean promoted = "promoted".equals(getMacroName()) || "promoted-list".equals(getMacroName());
99  Authentication.getIContact(), false,
100  ElephantContext.getRootWebPath() + getToken(1, "/user/products"));
101  pci.setNavigatorTop(grouped);
102  pci.setNavigatorBottom(grouped);
103  pci.setPage((Integer) ObjectString.parseNativeString(getToken(2), Integer.class, true));
104  pci.setOnlyPromoted(promoted);
105  pci.setAllowInterest(true);
106  pci.setAllowDescriptions(true);
107  pci.setAllowComments(true);
108  pci.setAllowAttachments(true);
109  pci.setAllowFiles(true);
110  pci.setAllowGroups(grouped);
112  pci.setFullTemplate(getToken(4));
113  pci.setPublishable(true);
114  pci.setFullSearch(true);
115  pci.load();
116  pci.render();
117 // out.write(getPublished(grouped, constructor,
118 // (Integer) ObjectString.parseNativeString(getToken(2), Integer.class, true),
119 // getToken(3),
120 // getToken(4),
121 // getToken(1),
122 // promoted));
123  done = true;
124  } else if("product-name".equals(getMacroName()) || "product-name-list".equals(getMacroName())) {
125  boolean anchor = "product-name".equals(getMacroName());
126  out.write(createLinks(anchor, constructor, getToken(1), getToken(2)));
127  done = true;
128  } else if("product-price".equals(getMacroName())) {
129  out.write(createPrice(constructor, getToken(1), getToken(2)));
130  done = true;
131  } else if("cart".equals(getMacroName())) {
133  sc.renderCart();
134  done = true;
135  }
136  return done;
137  }
138 
139  @Override
140  protected void explainMacros(ParserMacroSet macros) {
141  macros.addMacro("contracts")
142  .addParameter("context", true, "/user/contracts")
143  .addParameter("count", true, "10")
144  .addParameter("intervention", true, "true");
145  macros.addMacro("contract-list")
146  .copyParametersFrom("contracts", macros);
147  macros.addMacro("contract-type")
148  .addParameter("context", true, "/user/contracts")
149  .addParameter("count", true, "10")
150  .addParameter("intervention", true, "true")
151  .addParameter("type", true, "0")
152  .addParameter("template", true, "summary");
153  macros.addMacro("contract-type-list")
154  .copyParametersFrom("contract-type", macros);
155  macros.addMacro("checkout");
156  macros.addMacro("clean-cart");
157  macros.addMacro("products")
158  .addParameter("context", false)
159  .addParameter("count", true, "10")
160  .addParameter("summaryTemplate", true, "summary")
161  .addParameter("fullTemplate", true, "full");
162  macros.addMacro("product-list")
163  .copyParametersFrom("products", macros);
164  macros.addMacro("promoted")
165  .copyParametersFrom("products", macros);
166  macros.addMacro("promoted-list")
167  .copyParametersFrom("products", macros);
168  macros.addMacro("product-name")
169  .addParameter("context", false)
170  .addParameter("ids", false, null, true);
171  macros.addMacro("product-name-list")
172  .copyParametersFrom("product-name", macros);
173  macros.addMacro("product-price")
174  .addParameter("format", false)
175  .addParameter("ids", false, null, true);
176  macros.addMacro("cart")
177  .addParameter("shopping", false)
178  .addParameter("template", true, "cart");
179  }
180 
181  public String getPublished(boolean grouped, IConstructor constructor, int count,
182  String summaryTemplate, String fullTemplate, String context, boolean promoted) {
183  try {
185  ProductIterator pi = new ProductIterator(constructor, context, null);
186  pi.setAllowInterest(true);
187  pi.setAllowVotes(false);
188  pi.setAllowComments(true);
189  pi.setShowDescriptions(true);
190  pi.setShowAttachments(true);
191  pi.setAllowGroups(grouped);
192  pi.setNavigatorTop(true);
193  pi.setNavigatorBottom(true);
194  pi.setFullTemplate(fullTemplate);
195  pi.setSummaryTemplate(summaryTemplate);
196  pi.setPage(count);
197  pi.setOnlyPromoted(promoted);
198  pi.setPublishable(true);
199  pi.load();
200  pi.render();
201  constructor.setOut(null);
202  return constructor.getOutputString();
203  } catch (IOException ex) {
204  Logger.getLogger(FinancialsParser.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
205  }
206  return null;
207  }
208 
209  private String createLinks(boolean anchor, IConstructor constructor, String context, String ids) {
210  String[] aids = ids.split("\\s*,\\s*");
211  ArrayList<FinancialsParser.ProductQuantity> products = new ArrayList<>();
212  Dao dao = new FinancialsPU();
213  for(String id : aids) {
214  String[] aid = id.split("\\s*\\*\\s*");
215  Product product = dao.find(Product.class, Long.parseLong(aid[0]));
216  if(product != null) {
217  products.add(new FinancialsParser.ProductQuantity(product, aid.length == 2 ? Double.parseDouble(aid[1]) : 1.0));
218  }
219  }
220  if(anchor) {
221  PhraseBuilder pb = new PhraseBuilder();
222  for(FinancialsParser.ProductQuantity pq : products) {
223  pb.addWord("<a href='" + ElephantContext.getRootWebPath() + context + "?item=" + pq.product.getId() + "'>" +
224  (pq.quantity > 1 ? ((int) pq.quantity) + " " : "") + pq.product.getDescription() +
225  "</a>");
226  pb.addPendingSeparator(", ");
227  }
228  return pb.toString();
229  } else {
230  HTMLGenerator html = new HTMLGenerator();
231  html.startTag("ul");
232  for(FinancialsParser.ProductQuantity pq : products) {
233  html.startTag("li");
234  html.startAnchor(ElephantContext.getRootWebPath() + context + "?item=" + pq.product.getId(), null);
235  html.write((pq.quantity > 1 ? ((int) pq.quantity) + " " : "") + pq.product.getDescription());
236  html.endTag().endTag();
237  }
238  html.endAllTags();
239  return html.toString();
240  }
241  }
242 
243  private String createPrice(IConstructor constructor, String format, String ids) {
244  String[] aids = ids.split("\\s*,\\s*");
245  double price = 0.0;
246  Dao dao = new FinancialsPU();
247  for(String id : aids) {
248  String[] aid = id.split("\\s*\\*\\s*");
249  Product product = dao.find(Product.class, Long.parseLong(aid[0]));
250  if(product != null) {
251  price += product.getPrice() * (aid.length == 2 ? Double.parseDouble(aid[1]) : 1.0);
252  }
253  }
254  return DecimalFormats.format(price, format);
255  }
256 
257  private class ProductQuantity {
258  Product product;
259  double quantity;
260 
261  public ProductQuantity(Product product, double quantity) {
262  this.product = product;
263  this.quantity = quantity;
264  }
265  }
266 
267 }
static void removeCart(IConstructor constructor)
void setShowDescriptions(boolean showDescriptions)
void setAllowAttachments(boolean allowAttachments)
void setAllowInterest(boolean allowInterest)
void setFullTemplate(String fullTemplate)
void setAllowDescriptions(boolean allowDescriptions)
void setAllowComments(boolean allowComments)
void setNavigatorBottom(boolean navigatorBottom)
void setSummaryTemplate(String summaryTemplate)
void setNavigatorTop(boolean navigatorTop)
void explainMacros(ParserMacroSet macros)
String getPublished(boolean grouped, IConstructor constructor, int count, String summaryTemplate, String fullTemplate, String context, boolean promoted)
ParserMacro addMacro(String macroName)
void copyParametersFrom(String macroName, ParserMacroSet macros)
ParserMacro addParameter(String name, boolean optional)