19 package org.turro.dossier.command;
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;
58 @DirectContent(identifier=
"dossier-action")
68 public static String
createPOST(String domId, String values) {
71 ".done(function(data) { $(\"#" + domId +
"\").html(data); });";
86 public boolean itsMe(String
id) {
87 return getIdentifier().equals(
id);
91 public boolean myTurn(HttpServletRequest request) {
96 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
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);
113 private Dao getDao() {
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);
128 Long dosid = (Long) ObjectString.parseString(map.get(
"dos"), Long.class,
true);
129 Dossier d = getDao().find(Dossier.class, dosid);
131 WorthValue wv = findValue(contact, wd, d);
133 wv =
new WorthValue();
134 wv.setWorthDefinition(wd);
136 wv.setIContact(contact);
138 wv.setValue((Double) ObjectString.parseString(map.get(
"val"), Double.class,
true));
139 if(wv.getId() !=
null && wv.getId() > 0) {
140 getDao().saveObject(wv);
142 getDao().persistObject(wv);
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());
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);
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 = ? " +
179 wd, contact.getId(), d
184 if(!Strings.isBlank(root) && !Strings.isBlank(tmpl)) {
186 em.
put(
"control",
this);
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);
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 = ?",
216 if(
"Beneficiary".equals(label)) {
218 }
else if(
"Offerer".equals(label)) {
220 }
else if(
"Support".equals(label)) {
222 }
else if(
"Coordinator".equals(label)) {
224 }
else if(
"Funding".equals(label)) {
226 }
else if(
"Research".equals(label)) {
228 }
else if(
"Consortium".equals(label)) {
229 return dossier.
getFullParticipants().isConsortium(contact) || hasRequested(dossier, contact, label);
234 private boolean hasRequested(
Dossier dossier,
IContact contact, String label) {
236 wc.
addClause(
"select pr from ParticipantRequest pr");
237 wc.
addClause(
"where idContact = :idContact");
241 if(
"Beneficiary".equals(label)) {
243 }
else if(
"Offerer".equals(label)) {
245 }
else if(
"Support".equals(label)) {
247 }
else if(
"Coordinator".equals(label)) {
249 }
else if(
"Funding".equals(label)) {
251 }
else if(
"Research".equals(label)) {
253 }
else if(
"Consortium".equals(label)) {
256 return !getDao().getResultList(wc).isEmpty();
259 private void processParticipate(KeyValueMap map, IConstructor constructor) {
260 IContact contact = Contacts.getContactById(map.get(
"contact"));
261 if(contact !=
null && contact.isWebUser()) {
263 Long dosid = (Long) ObjectString.parseString(map.get(
"dossier"), Long.class,
true);
264 Dossier dossier = getDao().find(Dossier.class, dosid);
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);
286 participantRequest.setCreation(
new Date());
287 getDao().saveObject(participantRequest);
289 .setRoot(
"/projects")
290 .setReason(I_.get(
"Ask to participate"))
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);
static String createRightNowParameter(String values)
static KeyValueMap getRightNowAction(IConstructor constructor)
static String createRightNowAction(String values)
static IContact getIContact()
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)
static String getIdentifier()
boolean isMine(WorthDefinition worthDefinition, Double value)
boolean isBeneficiary(IContact contact)
boolean isFunding(IContact contact)
boolean isOfferer(IContact contact)
boolean isCoordinator(IContact contact)
boolean isSupport(IContact contact)
boolean isResearch(IContact contact)
ParticipantSet< IDossierParticipant > getFullParticipants()
DossierParticipationsList getParticipationsList()
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static String getRootWebPath()
void addClause(String clause)
void addNamedValue(String name, Object value)
static boolean isYourTurn(HttpServletRequest request, String path)
static final String DIRECT_CONTENT_PATH
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
boolean equals(Object obj)