BrightSide Workbench Full Report + Source Code
PetitionCtrl.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.acceptance;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import javax.servlet.ServletContext;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.turro.string.Strings;
28 import org.turro.action.Contacts;
29 import org.turro.action.IAcceptances;
30 import org.turro.action.Plugins;
31 import org.turro.auth.Authentication;
32 import org.turro.elephant.context.Application;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.elephant.context.IConstructor;
35 import org.turro.elephant.direct.DirectContent;
36 import org.turro.elephant.direct.DirectContents;
37 import org.turro.elephant.direct.IDirectContent;
38 import org.turro.marker.ElephantMarker;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.util.IdGenerator;
41 
46 @DirectContent(identifier="petition-create")
47 public class PetitionCtrl implements IDirectContent {
48 
49  public static String createPetition(String path, IContact contact, String link) {
50  return createPetition(path, contact, link, null);
51  }
52 
53  public static String createPetition(String path, IContact contact, String link, String template) {
55  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
56  marker.put("uniqueId", IdGenerator.generateHex());
57  marker.put("path", path);
58  marker.put("contact", contact);
59  marker.put("petitioner", Authentication.getIContact());
60  marker.put("redirect", link);
61  return marker.parse("acceptance", Strings.isBlank(template) ? "petition" : template);
62  }
63 
64  public static String getIdentifier() {
65  return PetitionCtrl.class.getAnnotation(DirectContent.class).identifier();
66  }
67 
68  @Override
69  public boolean itsMe(String id) {
70  return getIdentifier().equals(id);
71  }
72 
73  @Override
74  public boolean myTurn(HttpServletRequest request) {
75  return DirectContents.isYourTurn(request, getIdentifier());
76  }
77 
78  @Override
79  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
81  IContact petitioner = Contacts.getContactById(request.getParameter("petitioner"));
82  IContact contact = Contacts.getContactById(request.getParameter("contact"));
83  String path = request.getParameter("path");
84  String comment = request.getParameter("comment");
85  String redirect = request.getParameter("redirect");
86  if(petitioner.isValid()) {
87  if(contact.isValid() && !Strings.isBlank(path) && !Strings.isBlank(comment)) {
89  new AcceptanceMail(acceptances.request(petitioner, contact, path, comment)).sendMail();
90  }
91  } else {
92  redirect = "/user";
93  }
94  if(!Strings.isBlank(redirect)) {
95  try {
96  constructor.redirect(redirect);
97  } catch (IOException ex) {
98  Logger.getLogger(PetitionCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  }
100  }
101  }
102 
103 }
static String createPetition(String path, IContact contact, String link, String template)
boolean myTurn(HttpServletRequest request)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String createPetition(String path, IContact contact, String link)
static IContact getContactById(String id)
Definition: Contacts.java:72
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
IAcceptance request(IContact petitioner, IContact contact, String path, String comment)