BrightSide Workbench Full Report + Source Code
LogisticDashboard.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.www;
20 
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.Date;
24 import java.util.List;
25 import javax.servlet.ServletException;
26 import org.turro.string.ObjectString;
27 import org.turro.string.Strings;
28 import org.turro.auth.Authentication;
29 import org.turro.contacts.Contact;
30 import org.turro.elephant.context.IElement;
31 import org.turro.elephant.impl.abstracts.AbstractElement;
32 import org.turro.elephant.layout.IManageable;
33 import org.turro.elephant.layout.IRenderable;
34 import org.turro.elephant.security.IDefendable;
35 import org.turro.financials.cart.FollowUps;
36 import org.turro.financials.cart.ShopContext;
37 import org.turro.financials.db.FinancialsPU;
38 import org.turro.financials.entity.FollowUp;
39 import org.turro.i18n.I_;
40 import org.turro.jpa.Dao;
41 import org.turro.marker.ElephantMarker;
42 import org.turro.plugin.contacts.IContact;
43 
49 
50  protected String sclass, fullTemplate, summaryTemplate;
51  protected Long item;
52  protected IContact contact;
53  protected FollowUp followUp;
54 
55  @Override
56  public void loadData() throws ServletException, IOException {
57  sclass = getAttributes().getAttributeValue("attrib:sclass", "");
58  fullTemplate = getAttributes().getAttributeValue("attrib:fullTemplate", "");
59  summaryTemplate = getAttributes().getAttributeValue("attrib:summaryTemplate", "");
60 
61  processActions();
62 
63  item = (Long) ObjectString.parseString(getConstructor().getParameter("item"), Long.class, true);
64 
65  if(item > 0) {
66  followUp = new FinancialsPU().find(FollowUp.class, item);
67  }
68 
69  if(followUp != null) {
71  } else {
72  item = 0L;
73  }
74 
76  }
77 
78  @Override
79  public void startConstruction() throws ServletException, IOException {
80  PrintWriter out = getConstructor().getOut();
81 
83  if(item > 0) {
84  prepareValues(marker, followUp, 0);
85  marker.process("followup", getFullTemplate());
86  } else {
87  int index = 0;
88  List<FollowUp> lfu = FollowUps.notPrepared(contact);
89  for(FollowUp fu : lfu) {
90  marker.put("divider", index > 0);
91  marker.put("header", index == 0);
92  marker.put("title", I_.get("Not prepared"));
93  marker.put("footer", index == lfu.size() - 1);
94  prepareValues(marker, fu, 0);
95  marker.process("followup", getSummaryTemplate());
96  index++;
97  }
98  index = 0;
100  for(FollowUp fu : lfu) {
101  marker.put("divider", index > 0);
102  marker.put("header", index == 0);
103  marker.put("title", I_.get("Not delivered"));
104  marker.put("footer", index == lfu.size() - 1);
105  prepareValues(marker, fu, 0);
106  marker.process("followup", getSummaryTemplate());
107  index++;
108  }
109  }
110 
111  }
112 
113  @Override
114  public String getStart() {
115  return "<div class='" + sclass + "dossier-web'>";
116  }
117 
118  @Override
119  public String getEnd() {
120  return "</div>";
121  }
122 
123  private void prepareValues(ElephantMarker marker, FollowUp fu, int page) {
124 
125  marker.put("followUp", fu);
126 
127  String link = getItemLink(fu, page);
128  if(link != null) {
129  marker.put("path", link);
130  marker.put("all", getPageLink(page));
131  }
132  marker.put("readall", getItemLink(fu, page));
133 
134  marker.put("document", fu.getDocument());
135  marker.put("contract", fu.getDocument().getContract());
136  Contact contractor = (Contact) fu.getDocument().getContract().getIContractor().getContact();
137  marker.put("address", contractor.getAddressMap().get(ShopContext.getInstance().getDelivery()));
138  marker.put("mobile", contractor.getConnectorMap().get(ShopContext.getInstance().getContactPhone()));
139 
140  marker.put("prepared", fu.getPreparedDate() != null);
141  marker.put("iprepare", fu.getIPrepare());
142  marker.put("ideliver", fu.getIDeliver());
143  }
144 
145  private String getItemLink(FollowUp fu, int page) {
146  return getContext().getFullPath() + "?item=" + fu.getId() + "&page=" + page;
147  }
148 
149  private String getPageLink(int page) {
150  return getContext().getFullPath() + "?page=" + page;
151  }
152 
153  private String getSummaryTemplate() {
154  return Strings.isBlank(summaryTemplate) ? "mySummary" : summaryTemplate;
155  }
156 
157  private String getFullTemplate() {
158  return Strings.isBlank(fullTemplate) ? "myFull" : fullTemplate;
159  }
160 
161  private void processActions() {
162  Long fuid = (Long) ObjectString.parseNativeString(getConstructor().getParameter("fuid"), Long.class, false);
163 
164  if(fuid != null && fuid > 0) {
165  String prepared = getConstructor().getParameter("prepared"),
166  delivered = getConstructor().getParameter("delivered");
167  if("true".equals(prepared)) {
168  Dao dao = new FinancialsPU();
169  FollowUp fu = dao.find(FollowUp.class, fuid);
170  if(fu != null) {
171  fu.setPreparedDate(new Date());
172  dao.saveObject(fu);
173  }
174  } else if("true".equals(delivered)) {
175  Dao dao = new FinancialsPU();
176  FollowUp fu = dao.find(FollowUp.class, fuid);
177  if(fu != null) {
178  fu.setDeliveredDate(new Date());
179  fu.setReceiver(getConstructor().getParameter("receiver"));
180  fu.setReceiverId(getConstructor().getParameter("receiverId"));
181  dao.saveObject(fu);
182  }
183  }
184  }
185  }
186 
187 }
188 
static List< FollowUp > notDelivered(IContact contact)
Definition: FollowUps.java:47
static List< FollowUp > notPrepared(IContact contact)
Definition: FollowUps.java:35
static String get(String msg)
Definition: I_.java:41
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)