BrightSide Workbench Full Report + Source Code
MyAgreementsDashboard.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.agreements;
20 
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
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.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IElement;
31 import org.turro.elephant.db.ElephantPU;
32 import org.turro.elephant.entities.db.AgreementSignature;
33 import org.turro.elephant.impl.abstracts.AbstractElement;
34 import org.turro.elephant.layout.IManageable;
35 import org.turro.elephant.layout.IRenderable;
36 import org.turro.elephant.security.IDefendable;
37 import org.turro.i18n.I_;
38 import org.turro.marker.ElephantMarker;
39 import org.turro.plugin.contacts.IContact;
40 
46 
47  protected String sclass, fullTemplate, summaryTemplate, agreementPath;
48  protected int count;
49  protected boolean showDescriptions;
50  protected Long item;
52  protected IContact contact;
53 
54  @Override
55  public void loadData() throws ServletException, IOException {
56  sclass = getAttributes().getAttributeValue("attrib:sclass", "");
57  count = getAttributes().getAttributeIntegerValue("attrib:count", 10);
58  showDescriptions = getAttributes().getAttributeBooleanValue("attrib:showDescriptions", true);
59  fullTemplate = getAttributes().getAttributeValue("attrib:fullTemplate", "");
60  summaryTemplate = getAttributes().getAttributeValue("attrib:summaryTemplate", "");
61  agreementPath = getAttributes().getAttributeValue("attrib:agreementPath", null);
62 
64 
65  item = (Long) ObjectString.parseString(getConstructor().getParameter("item"), Long.class, true);
66 
67  if(item > 0) {
68  signature = new ElephantPU().find(AgreementSignature.class, item);
69  if(!signature.getIdContact().equals(contact.getId())) {
70  signature = null;
71  }
72  }
73 
74  if(signature != null) {
76  } else {
77  item = 0L;
78  }
79  }
80 
81  @Override
82  public void startConstruction() throws ServletException, IOException {
83  PrintWriter out = getConstructor().getOut();
84  out.print(getStart());
85  out.print(getStartBody());
86 
88  if(!Strings.isBlank(agreementPath)) {
89  marker.put("agreementPath", ElephantContext.getRootWebPath() + agreementPath);
90  }
91  if(item > 0) {
92  prepareValues(marker, signature, 0);
93  marker.process("agreement", getFullTemplate());
94  } else {
95  AgreementsUtil agreements = new AgreementsUtil(contact);
96  if(agreements.getSignatures(null).isEmpty()) {
97  out.write("<h3>" + I_.get("Empty list") + "</h3>");
98  } else {
99  int index = 0;
100  for(AgreementSignature asig : agreements.getSignatures(null)) {
101  marker.put("divider", index > 0);
102  marker.put("header", index == 0);
103  marker.put("footer", index == agreements.getSignatures(null).size() - 1);
104  prepareValues(marker, asig, 0);
105  marker.process("agreement", getSummaryTemplate());
106  index++;
107  }
108  }
109  }
110 
111  out.print(getEndBody());
112  out.print(getEnd());
113  }
114 
115  @Override
116  public String getStart() {
117  return "<div class='" + sclass + "agreement-web'>";
118  }
119 
120  @Override
121  public String getEnd() {
122  return "</div>";
123  }
124 
125  private void prepareValues(ElephantMarker marker, AgreementSignature signature, int page) {
126 
127  try {
128  marker.put("signature", signature);
129  marker.put("acceptUrl", AgreementAction.createURL(getConstructor(), signature, contact, true));
130  marker.put("declineUrl", AgreementAction.createURL(getConstructor(), signature, contact, false));
131 
132  marker.put("name", signature.getAgreement().getTitle());
133  marker.put("text", signature.getAgreement().getText());
134 
135  String link = getItemLink(signature, page);
136  if(link != null) {
137  marker.put("path", link);
138  marker.put("all", getPageLink());
139  }
140  marker.put("readall", getItemLink(signature, page));
141  marker.put("showDescriptions", showDescriptions);
142  } catch (Exception ex) {
143  Logger.getLogger(MyAgreementsDashboard.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
144  }
145 
146  }
147 
148  private String getItemLink(AgreementSignature signature, int page) {
149  return getContext().getFullPath() + "?item=" + signature.getId();
150  }
151 
152  private String getPageLink() {
153  return getContext().getFullPath();
154  }
155 
156  private String getSummaryTemplate() {
157  return Strings.isBlank(summaryTemplate) ? "mySummary" : summaryTemplate;
158  }
159 
160  private String getFullTemplate() {
161  return Strings.isBlank(fullTemplate) ? "myFull" : fullTemplate;
162  }
163 
164 }
165 
static String createURL(IConstructor constructor, AgreementSignature signature, IContact contact, boolean accept)
List< AgreementSignature > getSignatures(String action)
static String get(String msg)
Definition: I_.java:41
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)