19 package org.turro.anonymous;
21 import java.io.IOException;
22 import java.util.HashMap;
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;
56 super(
"widgets/anonymous");
61 return renderQuestionContainer(constructor, recipe, entity,
"mail");
65 return renderMailContainer(constructor, recipe.
getName(), recipe.
getEmail(), entity,
template);
69 return renderMailContainer(constructor, recipeName, recipeMail, entity,
"mail");
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);
84 values.put(
"containerId", containerId);
85 marker.
put(
"askLink", getAjaxUrl(containerId, values));
86 return marker.
parse(getTmplRoot(),
template);
90 return renderQuestionContainer(constructor, recipe, entity,
"question");
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());
102 values.put(
"containerId", containerId);
103 marker.
put(
"askLink", getAjaxUrl(containerId, values));
104 return marker.
parse(getTmplRoot(),
template);
108 execute(constructor);
112 KeyValueMap values =
new KeyValueMap();
113 values.put(
"type",
"render-answer");
115 values.put(
"questioner", recipe.
getId());
117 return createLinkTo(
"/user/widgets/anonymous", values);
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)) {
148 processQuestion(constructor, map);
149 }
catch (EmailException ex) {
152 }
else if(
"send-answer".equals(type)) {
154 processAnswer(constructor, map);
155 }
catch (EmailException ex) {
158 }
else if(
"mail-form".equals(type)) {
159 renderMailForm(constructor, map);
160 }
else if(
"send-mail".equals(type)) {
162 processMail(constructor, map);
163 }
catch (EmailException ex) {
166 }
else if(
"p2pmail-form".equals(type)) {
167 renderP2PMailForm(constructor, map);
168 }
else if(
"send-p2pmail".equals(type)) {
170 processP2PMail(constructor, map);
171 }
catch (EmailException ex) {
177 private void renderMailForm(
IConstructor constructor, KeyValueMap map) {
179 addContainerId(marker,
false);
181 marker.
put(
"newMailLink", getAjaxSubmitUrl(map.get(
"containerId")));
183 constructor.
getResponse().setContentType(
"text/html");
186 }
catch (IOException ex) {
187 Logger.getLogger(AnonymousCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
191 private void renderP2PMailForm(IConstructor constructor, KeyValueMap map) {
192 ElephantMarker marker = getCleanMarkerFrom(constructor);
193 addContainerId(marker,
false);
195 marker.put(
"newP2PMailLink", getAjaxSubmitUrl(map.get(
"containerId")));
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);
205 private void renderQuestionForm(IConstructor constructor, KeyValueMap map) {
206 ElephantMarker marker = getCleanMarkerFrom(constructor);
207 addContainerId(marker,
false);
209 marker.put(
"newQuestionLink", getAjaxSubmitUrl(map.get(
"containerId")));
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);
219 private void renderAnswerForm(IConstructor constructor, KeyValueMap map) {
220 ElephantMarker marker = getCleanMarkerFrom(constructor);
221 addContainerId(marker,
false);
223 marker.process(getTmplRoot(),
"answer-form");
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")
239 .sendTemplate(
"send-mail", ElephantContext.getSiteName() +
": " + reason);
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"));
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")
258 .sendTemplate(
"send-p2pmail", ElephantContext.getSiteName() +
": " + map.get(
"reason"));
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")
278 .sendTemplate(
"send-question", ElephantContext.getSiteName() +
": " + reason);
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);
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 IContact getIContact()
static String logMsg(String msg)
static String getEncoding()
static IElephantEntity getController(String path)
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
HttpServletResponse getResponse()