BrightSide Workbench Full Report + Source Code
AgreementAction.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.agreements;
20 
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLDecoder;
24 import java.net.URLEncoder;
25 import java.util.HashMap;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31 import org.turro.string.ObjectString;
32 import org.turro.action.Actions;
33 import org.turro.collections.KeyValueMap;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.context.IConstructor;
36 import org.turro.elephant.direct.DirectContent;
37 import org.turro.elephant.direct.DirectContents;
38 import org.turro.elephant.direct.IDirectContent;
39 import org.turro.elephant.entities.db.AgreementSignature;
40 import org.turro.elephant.security.IUser;
41 import org.turro.plugin.contacts.IContact;
42 
47 @DirectContent(identifier="agreement-action")
48 public class AgreementAction implements IDirectContent {
49 
50  public static String createURL(IConstructor constructor, AgreementSignature signature, IContact contact, boolean accept) throws UnsupportedEncodingException, Exception {
51  HashMap<String, String> values = new HashMap<>();
52  values.put(Actions.USER_PAR, contact.getConnector(IUser.CONNECTOR_EMAIL));
53  values.put(Actions.REDIR_PAR, URLEncoder.encode(ElephantContext.getRootWebPath() + "/user/myagreements?item=" + signature.getId(), "UTF-8"));
54  values.put("agreementId", ObjectString.formatNativeObject(signature.getAgreement().getId(), false));
55  values.put("contactId", contact.getId());
56  values.put("accept", ObjectString.formatNativeObject(Boolean.valueOf(accept), false));
58  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
59  "?" + Actions.createAction(values, 7, false);
60  }
61 
62  public static String getIdentifier() {
63  return AgreementAction.class.getAnnotation(DirectContent.class).identifier();
64  }
65 
66  @Override
67  public boolean itsMe(String id) {
68  return getIdentifier().equals(id);
69  }
70 
71  @Override
72  public boolean myTurn(HttpServletRequest request) {
73  return DirectContents.isYourTurn(request, getIdentifier());
74  }
75 
76  @Override
77  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
78  try {
79  IConstructor constructor = ElephantContext.getConstructor(request, response);
80  KeyValueMap map = Actions.getAction(constructor);
81  if(Actions.prepareActions(constructor, map)) {
82  AgreementsUtil agreements = new AgreementsUtil(map.get("contactId"));
83  agreements.sign(
84  (Long) ObjectString.parseNativeString(map.get("agreementId"), Long.class, true),
85  (Boolean) ObjectString.parseNativeString(map.get("accept"), Boolean.class, true));
86  response.sendRedirect(URLDecoder.decode(map.get(Actions.REDIR_PAR), "UTF-8"));
87  }
88  } catch (IOException ex) {
89  Logger.getLogger(AgreementAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  }
91  }
92 
93 }
94 
static String createAction(String email, String redir)
Definition: Actions.java:90
static final String USER_PAR
Definition: Actions.java:66
static KeyValueMap getAction(IConstructor constructor)
Definition: Actions.java:252
static boolean prepareActions(IConstructor constructor, KeyValueMap values)
Definition: Actions.java:173
static String createURL(IConstructor constructor, AgreementSignature signature, IContact contact, boolean accept)
boolean myTurn(HttpServletRequest request)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
void sign(Agreement agreement, boolean agreed)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27