BrightSide Workbench Full Report + Source Code
AnonymousCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.anonymous;
20 
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.Actions;
28 import org.turro.action.Contacts;
29 import org.turro.action.MailSenders;
30 import org.turro.annotation.ElephantPlugin;
31 import org.turro.auth.Authentication;
32 import org.turro.collections.KeyValueMap;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.direct.AbstractDirectContentCtrl;
36 import org.turro.elephant.direct.DirectContent;
37 import org.turro.entities.Entities;
38 import org.turro.json.Jsons;
39 import org.turro.marker.ElephantMarker;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.util.IdGenerator;
42 import org.turro.ws.service.WsConstants;
43 import org.turro.ws.service.p2p.P2PServer;
44 import org.turro.ws.service.p2p.P2Ps;
45 import org.zkoss.lang.Strings;
46 
51 @ElephantPlugin(label="anonymous-ctrl")
52 @DirectContent(identifier="anonymous-action")
54 
55  public AnonymousCtrl() {
56  super("widgets/anonymous");
57  setNeedsUser(true);
58  }
59 
60  public String renderMailContainer(IConstructor constructor, IContact recipe, Object entity) {
61  return renderQuestionContainer(constructor, recipe, entity, "mail");
62  }
63 
64  public String renderMailContainer(IConstructor constructor, IContact recipe, Object entity, String template) {
65  return renderMailContainer(constructor, recipe.getName(), recipe.getEmail(), entity, template);
66  }
67 
68  public String renderMailContainer(IConstructor constructor, String recipeName, String recipeMail, Object entity) {
69  return renderMailContainer(constructor, recipeName, recipeMail, entity, "mail");
70  }
71 
72  public String renderMailContainer(IConstructor constructor, String recipeName, String recipeMail, Object entity, String template) {
74  ElephantMarker marker = new ElephantMarker(constructor);
75  String containerId = "cic_" + IdGenerator.generate();
76  marker.put("containerId", containerId);
77  KeyValueMap values = new KeyValueMap();
78  values.put("type", "mail-form");
79  values.put("senderName", contact.getName());
80  values.put("senderMail", contact.getEmail());
81  values.put("recipeName", recipeName);
82  values.put("recipeMail", recipeMail);
83  values.put("entityPath", Entities.getController(entity).getPath());
84  values.put("containerId", containerId);
85  marker.put("askLink", getAjaxUrl(containerId, values));
86  return marker.parse(getTmplRoot(), template);
87  }
88 
89  public String renderQuestionContainer(IConstructor constructor, IContact recipe, Object entity) {
90  return renderQuestionContainer(constructor, recipe, entity, "question");
91  }
92 
93  public String renderQuestionContainer(IConstructor constructor, IContact recipe, Object entity, String template) {
94  ElephantMarker marker = new ElephantMarker(constructor);
95  String containerId = "cic_" + IdGenerator.generate();
96  marker.put("containerId", containerId);
97  KeyValueMap values = new KeyValueMap();
98  values.put("type", "question-form");
99  values.put("answerer", recipe.getId());
100  values.put("questioner", Authentication.getIContact().getId());
101  values.put("entityPath", Entities.getController(entity).getPath());
102  values.put("containerId", containerId);
103  marker.put("askLink", getAjaxUrl(containerId, values));
104  return marker.parse(getTmplRoot(), template);
105  }
106 
107  public void renderAnswerForm(IConstructor constructor) {
108  execute(constructor);
109  }
110 
111  public String createAnswerLink(IConstructor constructor, IContact recipe, Object entity) {
112  KeyValueMap values = new KeyValueMap();
113  values.put("type", "render-answer");
114  values.put("answerer", Authentication.getIContact().getId());
115  values.put("questioner", recipe.getId());
116  values.put("entityPath", Entities.getController(entity).getPath());
117  return createLinkTo("/user/widgets/anonymous", values);
118  }
119 
120  /* IEntityCtrl */
121 
122  @Override
123  protected void prepareCleanMarker(ElephantMarker marker, KeyValueMap map) {
124  // nothing to do
125  }
126 
127  @Override
128  protected void prepareMarker(ElephantMarker marker) {
129  // nothing to do
130  }
131 
132  /* IDirectContent */
133 
134  @Override
135  protected String getIdentifier() {
136  return AnonymousCtrl.class.getAnnotation(DirectContent.class).identifier();
137  }
138 
139  @Override
140  protected void doExecute(IConstructor constructor, KeyValueMap map) {
141  String type = map.get("type");
142  if("question-form".equals(type)) {
143  renderQuestionForm(constructor, map);
144  } else if("answer-form".equals(type)) {
145  renderAnswerForm(constructor, map);
146  } else if("send-question".equals(type)) {
147  try {
148  processQuestion(constructor, map);
149  } catch (EmailException ex) {
150  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
151  }
152  } else if("send-answer".equals(type)) {
153  try {
154  processAnswer(constructor, map);
155  } catch (EmailException ex) {
156  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
157  }
158  } else if("mail-form".equals(type)) {
159  renderMailForm(constructor, map);
160  } else if("send-mail".equals(type)) {
161  try {
162  processMail(constructor, map);
163  } catch (EmailException ex) {
164  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
165  }
166  } else if("p2pmail-form".equals(type)) {
167  renderP2PMailForm(constructor, map);
168  } else if("send-p2pmail".equals(type)) {
169  try {
170  processP2PMail(constructor, map);
171  } catch (EmailException ex) {
172  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
173  }
174  }
175  }
176 
177  private void renderMailForm(IConstructor constructor, KeyValueMap map) {
178  ElephantMarker marker = getCleanMarkerFrom(constructor);
179  addContainerId(marker, false);
180  marker.putAll(map);
181  marker.put("newMailLink", getAjaxSubmitUrl(map.get("containerId")));
182  try {
183  constructor.getResponse().setContentType("text/html");
184  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
185  marker.process(getTmplRoot(), "mail-form", constructor.getResponse().getWriter());
186  } catch (IOException ex) {
187  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
188  }
189  }
190 
191  private void renderP2PMailForm(IConstructor constructor, KeyValueMap map) {
192  ElephantMarker marker = getCleanMarkerFrom(constructor);
193  addContainerId(marker, false);
194  marker.putAll(map);
195  marker.put("newP2PMailLink", getAjaxSubmitUrl(map.get("containerId")));
196  try {
197  constructor.getResponse().setContentType("text/html");
198  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
199  marker.process(getTmplRoot(), "p2pmail-form", constructor.getResponse().getWriter());
200  } catch (IOException ex) {
201  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
202  }
203  }
204 
205  private void renderQuestionForm(IConstructor constructor, KeyValueMap map) {
206  ElephantMarker marker = getCleanMarkerFrom(constructor);
207  addContainerId(marker, false);
208  marker.putAll(map);
209  marker.put("newQuestionLink", getAjaxSubmitUrl(map.get("containerId")));
210  try {
211  constructor.getResponse().setContentType("text/html");
212  constructor.getResponse().setCharacterEncoding(ElephantContext.getEncoding());
213  marker.process(getTmplRoot(), "question-form", constructor.getResponse().getWriter());
214  } catch (IOException ex) {
215  Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
216  }
217  }
218 
219  private void renderAnswerForm(IConstructor constructor, KeyValueMap map) {
220  ElephantMarker marker = getCleanMarkerFrom(constructor);
221  addContainerId(marker, false);
222  marker.putAll(map);
223  marker.process(getTmplRoot(), "answer-form");
224  }
225 
226  private void processMail(IConstructor constructor, KeyValueMap map) throws EmailException {
227  String reason = Entities.getController(map.get("entityPath")).getName();
228  String comment = map.get("comment");
229  if(!Strings.isBlank(comment)) {
230  Map args = new HashMap();
231  args.put("reason", reason);
232  args.put("comment", comment);
233  args.put("senderName", map.get("senderMame"));
234  args.put("senderMail", map.get("senderMail"));
235  MailSenders.getPool()
236  .addUser(map.get("recipeName"), map.get("recipeMail"))
237  .setRoot("/anonymous")
238  .putAll(args)
239  .sendTemplate("send-mail", ElephantContext.getSiteName() + ": " + reason);
240  }
241  }
242 
243  private void processP2PMail(IConstructor constructor, KeyValueMap map) throws EmailException {
244  String comment = map.get("comment");
245  if(!Strings.isBlank(comment)) {
246  Map args = new HashMap();
247  args.put("reason", map.get("reason"));
248  args.put("comment", comment);
249  args.put("senderName", map.get("senderMame"));
250  args.put("senderMail", map.get("senderMail"));
251  // Change to Jsons
252  Jsons ud = P2Ps.getP2PData(map.get("serverDomain"), map.get(Long.class, "memberId"),
253  P2PServer.P2P_SERVICE, WsConstants.USER_DATA, Jsons.object().add("userId", map.get("userId")));
254  MailSenders.getPool()
255  .addUser(map.get("recipeName"), ud.getString("userEmail"))
256  .setRoot("/anonymous")
257  .putAll(args)
258  .sendTemplate("send-p2pmail", ElephantContext.getSiteName() + ": " + map.get("reason"));
259  }
260  }
261 
262 
263  private void processQuestion(IConstructor constructor, KeyValueMap map) throws EmailException {
264  String reason = Entities.getController(map.get("entityPath")).getName();
265  IContact answerer = Contacts.getContactById(map.get("answerer"));
266  String comment = map.get("comment");
267  if(!Strings.isBlank(comment)) {
268  Map args = new HashMap();
269  args.put("reason", reason);
270  args.put("comment", comment);
271  map.put("type", "answer-form");
272  args.put("answerLink", createLinkTo("/user/widgets/anonymous",
273  new KeyValueMap(Actions.addEmailAndDays(map, answerer.getEmail(), 15))));
274  MailSenders.getPool()
275  .addContact(answerer)
276  .setRoot("/anonymous")
277  .putAll(args)
278  .sendTemplate("send-question", ElephantContext.getSiteName() + ": " + reason);
279  }
280  }
281 
282  private void processAnswer(IConstructor constructor, KeyValueMap map) throws EmailException {
283  String reason = Entities.getController(map.get("entityPath")).getName();
284  String comment = map.get("comment");
285  if(!Strings.isBlank(comment)) {
286  MailSenders.getPool()
287  .addContact(Contacts.getContactById(map.get("questioner")))
288  .setRoot("/anonymous")
289  .put("reason", reason)
290  .put("comment", comment)
291  .sendTemplate("send-answer", ElephantContext.getSiteName() + ": " + reason);
292  }
293  }
294 
295 }
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
String renderMailContainer(IConstructor constructor, IContact recipe, Object entity, String template)
String renderMailContainer(IConstructor constructor, IContact recipe, Object entity)
String renderQuestionContainer(IConstructor constructor, IContact recipe, Object entity, String template)
void renderAnswerForm(IConstructor constructor)
String createAnswerLink(IConstructor constructor, IContact recipe, Object entity)
void doExecute(IConstructor constructor, KeyValueMap map)
void prepareMarker(ElephantMarker marker)
String renderMailContainer(IConstructor constructor, String recipeName, String recipeMail, Object entity, String template)
String renderMailContainer(IConstructor constructor, String recipeName, String recipeMail, Object entity)
String renderQuestionContainer(IConstructor constructor, IContact recipe, Object entity)
static IElephantEntity getController(String path)
Definition: Entities.java:78
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)