BrightSide Workbench Full Report + Source Code
PollAction.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.polls;
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.PollOption;
40 import org.turro.elephant.security.IUser;
41 import org.turro.plugin.contacts.IContact;
42 
47 @DirectContent(identifier="poll-action")
48 public class PollAction implements IDirectContent {
49 
50  public static String createURL(PollOption option, IContact contact, int vote) 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/mypolls?item=" + option.getIdPoll(), "UTF-8"));
54  values.put("pollId", ObjectString.formatNativeObject(option.getIdPoll(), false));
55  values.put("pollData", option.getPollData());
56  values.put("contactId", contact.getId());
57  values.put("vote", ObjectString.formatNativeObject(Integer.valueOf(vote), false));
59  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
60  "?" + Actions.createAction(values, 7, false);
61  }
62 
63  public static String getIdentifier() {
64  return PollAction.class.getAnnotation(DirectContent.class).identifier();
65  }
66 
67  @Override
68  public boolean itsMe(String id) {
69  return getIdentifier().equals(id);
70  }
71 
72  @Override
73  public boolean myTurn(HttpServletRequest request) {
74  return DirectContents.isYourTurn(request, getIdentifier());
75  }
76 
77  @Override
78  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
79  try {
80  IConstructor constructor = ElephantContext.getConstructor(request, response);
81  KeyValueMap map = Actions.getAction(constructor);
82  if(Actions.prepareActions(constructor, map)) {
83  PollsUtil polls = new PollsUtil(map.get("contactId"));
84  polls.vote(
85  (Long) ObjectString.parseNativeString(map.get("pollId"), Long.class, true),
86  (String) map.get("pollData"),
87  (Integer) ObjectString.parseNativeString(map.get("vote"), Integer.class, true));
88  response.sendRedirect(URLDecoder.decode(map.get(Actions.REDIR_PAR), "UTF-8"));
89  }
90  } catch (IOException ex) {
91  Logger.getLogger(PollAction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
92  }
93  }
94 
95 }
96 
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 IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
boolean itsMe(String id)
Definition: PollAction.java:68
static String createURL(PollOption option, IContact contact, int vote)
Definition: PollAction.java:50
static String getIdentifier()
Definition: PollAction.java:63
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
Definition: PollAction.java:78
boolean myTurn(HttpServletRequest request)
Definition: PollAction.java:73
boolean vote(Long pollId, String pollData, Integer vote)
Definition: PollsUtil.java:56
static final String CONNECTOR_EMAIL
Definition: IUser.java:27