BrightSide Workbench Full Report + Source Code
DossierAction.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.dossier.command;
20 
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.util.Date;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.turro.string.ObjectString;
30 import org.turro.string.Strings;
31 import org.apache.commons.mail.EmailException;
32 import org.turro.action.Actions;
33 import org.turro.action.Contacts;
34 import org.turro.auth.Authentication;
35 import org.turro.collections.KeyValueMap;
36 import org.turro.dossier.db.DossierPU;
37 import org.turro.dossier.entity.Dossier;
38 import org.turro.dossier.entity.ParticipantRequest;
39 import org.turro.dossier.entity.WorthDefinition;
40 import org.turro.dossier.entity.WorthValue;
41 import org.turro.elephant.context.ElephantContext;
42 import org.turro.elephant.context.IConstructor;
43 import org.turro.elephant.db.WhereClause;
44 import org.turro.elephant.direct.DirectContent;
45 import org.turro.elephant.direct.DirectContents;
46 import org.turro.elephant.direct.IDirectContent;
47 import org.turro.i18n.I_;
48 import org.turro.jpa.Dao;
49 import org.turro.mail.sender.MailQueue;
50 import org.turro.marker.ElephantMarker;
51 import org.turro.plugin.contacts.IContact;
52 import org.turro.util.IdGenerator;
53 
58 @DirectContent(identifier="dossier-action")
59 public class DossierAction implements IDirectContent {
60 
61  //TODO: Worth writing a DefaultAction based on direct content
62 
63  public static String createPOST(String values) {
64  return "$.post(\"" + ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "\"," +
65  "{ exrino : \"" + Actions.createRightNowParameter(values) + "\" })";
66  }
67 
68  public static String createPOST(String domId, String values) {
69  return "$.post(\"" + ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "\"," +
70  "{ exrino : \"" + Actions.createRightNowParameter(values) + "\", domid: \"" + domId + "\" })" +
71  ".done(function(data) { $(\"#" + domId + "\").html(data); });";
72  }
73 
74  public static String createURL(String values) {
75  String exrino = Actions.createRightNowAction(values);
77  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
78  "?" + exrino;
79  }
80 
81  public static String getIdentifier() {
82  return DossierAction.class.getAnnotation(DirectContent.class).identifier();
83  }
84 
85  @Override
86  public boolean itsMe(String id) {
87  return getIdentifier().equals(id);
88  }
89 
90  @Override
91  public boolean myTurn(HttpServletRequest request) {
92  return DirectContents.isYourTurn(request, getIdentifier());
93  }
94 
95  @Override
96  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
97  IConstructor constructor = ElephantContext.getConstructor(request, response);
98  KeyValueMap map = Actions.getRightNowAction(constructor);
99  if(map != null) {
100  String type = map.get("type");
101  if("worth".equals(type)) {
102  processWorth(map, constructor);
103  } else if("participate".equals(type)) {
104  processParticipate(map, constructor);
105  }
106  }
107  }
108 
109  /* Dao */
110 
111  private Dao _dao;
112 
113  private Dao getDao() {
114  if(_dao == null) {
115  _dao = new DossierPU();
116  }
117  return _dao;
118  }
119 
120  /* Worths */
121 
122  private void processWorth(KeyValueMap map, IConstructor constructor) {
123  IContact contact = Authentication.getIContact();
124  if(contact != null && contact.isWebUser()) {
125  Long defid = (Long) ObjectString.parseString(map.get("def"), Long.class, true);
126  WorthDefinition wd = getDao().find(WorthDefinition.class, defid);
127  if(wd != null) {
128  Long dosid = (Long) ObjectString.parseString(map.get("dos"), Long.class, true);
129  Dossier d = getDao().find(Dossier.class, dosid);
130  if(d != null) {
131  WorthValue wv = findValue(contact, wd, d);
132  if(wv == null) {
133  wv = new WorthValue();
134  wv.setWorthDefinition(wd);
135  wv.setDossier(d);
136  wv.setIContact(contact);
137  }
138  wv.setValue((Double) ObjectString.parseString(map.get("val"), Double.class, true));
139  if(wv.getId() != null && wv.getId() > 0) {
140  getDao().saveObject(wv);
141  } else {
142  getDao().persistObject(wv);
143  }
144  String tmpl = map.get("tmpl"),
145  root = map.get("root"),
146  grpg = map.get("grpg");
147  if(!Strings.isBlank(root) && !Strings.isBlank(tmpl)) {
148  ElephantMarker em = new ElephantMarker(constructor);
149  em.put("control", this);
150  em.put("contact", contact);
151  em.put("dossier", d);
152  em.put("root", root);
153  em.put("tmpl", tmpl);
154  em.put("grouping", grpg);
155  em.put("container", false);
156  em.put("justVoted", wd.getId());
157  em.put("controlId", map.get("controlId"));
158  constructor.getResponse().setContentType("text/html");
159  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
160  try {
161  PrintWriter out = constructor.getOut();
162  out.print(em.parse(root, tmpl));
163  } catch (IOException ex) {
164  Logger.getLogger(DossierAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
165  }
166  }
167  }
168  }
169  }
170  }
171 
172  private WorthValue findValue(IContact contact, WorthDefinition wd, Dossier d) {
173  return (WorthValue) getDao().getSingleResultOrNull(
174  "select v from WorthValue v " +
175  "where v.worthDefinition = ? " +
176  "and v.idContact = ? " +
177  "and v.dossier = ?",
178  new Object[] {
179  wd, contact.getId(), d
180  });
181  }
182 
183  public String parseWorths(IConstructor constructor, Dossier dossier, String root, String tmpl, String grpg) {
184  if(!Strings.isBlank(root) && !Strings.isBlank(tmpl)) {
185  ElephantMarker em = new ElephantMarker(constructor);
186  em.put("control", this);
187  em.put("contact", Authentication.getIContact());
188  em.put("dossier", dossier);
189  em.put("grouping", grpg);
190  em.put("container", true);
191  em.put("controlId", IdGenerator.generateHex());
192  em.put("root", root);
193  em.put("tmpl", tmpl);
194  return em.parse(root, tmpl);
195  }
196  return "";
197  }
198 
199  public boolean isMine(WorthDefinition worthDefinition, Double value) {
200  IContact contact = Authentication.getIContact();
201  if(contact != null && contact.isWebUser()) {
202  Double v = (Double) getDao().getSingleResultOrNull(
203  "select v.value from WorthValue v " +
204  "where v.dossier = ? and v.worthDefinition = ? and v.idContact = ?",
205  new Object[] {
206  worthDefinition.getDossier(), worthDefinition, contact.getId()
207  });
208  return value.equals(v);
209  }
210  return false;
211  }
212 
213  /* ParticipantRequest */
214 
215  public boolean isParticipant(Dossier dossier, IContact contact, String label) {
216  if("Beneficiary".equals(label)) {
217  return dossier.getParticipationsList().isBeneficiary(contact) || hasRequested(dossier, contact, label);
218  } else if("Offerer".equals(label)) {
219  return dossier.getParticipationsList().isOfferer(contact) || hasRequested(dossier, contact, label);
220  } else if("Support".equals(label)) {
221  return dossier.getParticipationsList().isSupport(contact) || hasRequested(dossier, contact, label);
222  } else if("Coordinator".equals(label)) {
223  return dossier.getParticipationsList().isCoordinator(contact) || hasRequested(dossier, contact, label);
224  } else if("Funding".equals(label)) {
225  return dossier.getParticipationsList().isFunding(contact) || hasRequested(dossier, contact, label);
226  } else if("Research".equals(label)) {
227  return dossier.getParticipationsList().isResearch(contact) || hasRequested(dossier, contact, label);
228  } else if("Consortium".equals(label)) {
229  return dossier.getFullParticipants().isConsortium(contact) || hasRequested(dossier, contact, label);
230  }
231  return false;
232  }
233 
234  private boolean hasRequested(Dossier dossier, IContact contact, String label) {
235  WhereClause wc = new WhereClause();
236  wc.addClause("select pr from ParticipantRequest pr");
237  wc.addClause("where idContact = :idContact");
238  wc.addNamedValue("idContact", contact.getId());
239  wc.addClause("and dossier = :dossier");
240  wc.addNamedValue("dossier", dossier);
241  if("Beneficiary".equals(label)) {
242  wc.addClause("and beneficiary = TRUE");
243  } else if("Offerer".equals(label)) {
244  wc.addClause("and offerer = TRUE");
245  } else if("Support".equals(label)) {
246  wc.addClause("and support = TRUE");
247  } else if("Coordinator".equals(label)) {
248  wc.addClause("and coordinator = TRUE");
249  } else if("Funding".equals(label)) {
250  wc.addClause("and funding = TRUE");
251  } else if("Research".equals(label)) {
252  wc.addClause("and research = TRUE");
253  } else if("Consortium".equals(label)) {
254  wc.addClause("and consortium = TRUE");
255  }
256  return !getDao().getResultList(wc).isEmpty();
257  }
258 
259  private void processParticipate(KeyValueMap map, IConstructor constructor) {
260  IContact contact = Contacts.getContactById(map.get("contact"));
261  if(contact != null && contact.isWebUser()) {
262  try {
263  Long dosid = (Long) ObjectString.parseString(map.get("dossier"), Long.class, true);
264  Dossier dossier = getDao().find(Dossier.class, dosid);
265  //String discriminator = map.get("discriminator");
266  String as = map.get("as");
267  ParticipantRequest participantRequest = new ParticipantRequest();
268  participantRequest.setIContact(contact);
269  participantRequest.setDossier(dossier);
270  if("Beneficiary".equals(as)) {
271  participantRequest.setBeneficiary(true);
272  } else if("Offerer".equals(as)) {
273  participantRequest.setOfferer(true);
274  } else if("Support".equals(as)) {
275  participantRequest.setSupport(true);
276  } else if("Coordinator".equals(as)) {
277  participantRequest.setCoordinator(true);
278  } else if("Funding".equals(as)) {
279  participantRequest.setFunding(true);
280  } else if("Research".equals(as)) {
281  participantRequest.setResearch(true);
282  } else if("Consortium".equals(as)) {
283  participantRequest.setConsortium(true);
284  }
285  //participantRequest.setDiscriminator(discriminator);
286  participantRequest.setCreation(new Date());
287  getDao().saveObject(participantRequest);
288  new MailQueue()
289  .setRoot("/projects")
290  .setReason(I_.get("Ask to participate"))
291  .addAdministrators()
292  .put("dossier", dossier)
293  .put("contact", contact)
294  .put("participateAs", I_.get(as))
295  .sendTemplate("ask-participate", I_.get("Ask to participate"));
296  } catch (EmailException ex) {
297  Logger.getLogger(DossierAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
298  }
299  }
300  }
301 
302 }
static String createRightNowParameter(String values)
Definition: Actions.java:333
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static String createRightNowAction(String values)
Definition: Actions.java:312
boolean isParticipant(Dossier dossier, IContact contact, String label)
static String createPOST(String values)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String createURL(String values)
static String createPOST(String domId, String values)
String parseWorths(IConstructor constructor, Dossier dossier, String root, String tmpl, String grpg)
boolean myTurn(HttpServletRequest request)
boolean isMine(WorthDefinition worthDefinition, Double value)
ParticipantSet< IDossierParticipant > getFullParticipants()
Definition: Dossier.java:399
DossierParticipationsList getParticipationsList()
Definition: Dossier.java:436
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
void addNamedValue(String name, Object value)
static boolean isYourTurn(HttpServletRequest request, String path)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)