BrightSide Workbench Full Report + Source Code
IssueAction.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.dossier.command;
20 
21 import java.util.Date;
22 import javax.servlet.ServletContext;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import org.turro.string.ObjectString;
26 import org.turro.action.Actions;
27 import org.turro.action.Contacts;
28 import org.turro.auth.Authentication;
29 import org.turro.collections.KeyValueMap;
30 import org.turro.dossier.db.DossierPU;
31 import org.turro.dossier.entity.Issue;
32 import org.turro.dossier.entity.IssueComment;
33 import org.turro.dossier.entity.IssueParticipant;
34 import org.turro.dossier.entity.IssueParticipantRole;
35 import org.turro.dossier.issue.IssueMail;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.elephant.direct.DirectContent;
39 import org.turro.elephant.direct.DirectContents;
40 import org.turro.elephant.direct.IDirectContent;
41 import org.turro.i18n.I_;
42 import org.turro.jpa.Dao;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.registry.ChangeCategory;
45 import org.turro.registry.Changes;
46 import org.turro.registry.UniqueString;
47 
52 @DirectContent(identifier="issue-action")
53 public class IssueAction implements IDirectContent {
54 
55  public static String getIdentifier() {
56  return IssueAction.class.getAnnotation(DirectContent.class).identifier();
57  }
58 
59  @Override
60  public boolean itsMe(String id) {
61  return getIdentifier().equals(id);
62  }
63 
64  @Override
65  public boolean myTurn(HttpServletRequest request) {
66  return DirectContents.isYourTurn(request, getIdentifier());
67  }
68 
69  @Override
70  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
71  IConstructor constructor = ElephantContext.getConstructor(request, response);
72  KeyValueMap map = Actions.getRightNowAction(constructor);
73  if(map != null) {
74  String type = map.get("type");
75  if("assist".equals(type)) {
76  processAssist(map, constructor);
77  } else if("unassist".equals(type)) {
78  processUnassist(map, constructor);
79  }
80  }
81  }
82 
83  /* Dao */
84 
85  private Dao _dao;
86 
87  private Dao getDao() {
88  if(_dao == null) {
89  _dao = new DossierPU();
90  }
91  return _dao;
92  }
93 
94  /* Participates */
95 
96  private void processAssist(KeyValueMap map, IConstructor constructor) {
97  IContact contact = Contacts.getContactById(map.get("contact"));
98  if(contact != null && contact.isWebUser()) {
99  Long issueId = (Long) ObjectString.parseString(map.get("issue"), Long.class, true);
100  Issue issue = getDao().find(Issue.class, issueId);
101  if(issue != null && !issue.getIssueParticipants().isParticipant(contact)) {
102  Changes changes = new Changes();
103  ChangeCategory cc = new ChangeCategory(4, I_.get("Participants"));
104  IssueParticipant ip = issue.addParticipant(contact, IssueParticipantRole.ISSUE_ASSISTANT);
105  changes.addChange(new UniqueString(cc, I_.byKey(ip.getRole().toString()), ip.getName()));
106  IssueComment ic = new IssueComment();
107  ic.setIssue(issue);
108  ic.setModification(new Date());
109  ic.setIContact(Authentication.getIContact());
110  ic.setComment(changes.getChangesString());
111  ic.setExpenses(0);
112  ic.setHours(0);
113  ic.setPrice(0);
114  ic.setProcessed(true);
115  issue.getComments().add(ic);
116  issue = getDao().saveObject(issue);
117  new IssueMail(changes, issue, 0, 0, 0).sendMail();
118  }
119  }
120  }
121 
122  private void processUnassist(KeyValueMap map, IConstructor constructor) {
123  IContact contact = Contacts.getContactById(map.get("contact"));
124  if(contact != null && contact.isWebUser()) {
125  Long issueId = (Long) ObjectString.parseString(map.get("issue"), Long.class, true);
126  Issue issue = getDao().find(Issue.class, issueId);
127  if(issue != null) {
128  IssueParticipant ip = issue.removeParticipant(contact, IssueParticipantRole.ISSUE_ASSISTANT);
129  if(ip != null) {
130  Changes changes = new Changes();
131  ChangeCategory cc = new ChangeCategory(4, I_.get("Participants"));
132  changes.addChange(new UniqueString(cc, I_.byKey(ip.getRole().toString()), "-" + ip.getName()));
133  IssueComment ic = new IssueComment();
134  ic.setIssue(issue);
135  ic.setModification(new Date());
136  ic.setIContact(Authentication.getIContact());
137  ic.setComment(changes.getChangesString());
138  ic.setExpenses(0);
139  ic.setHours(0);
140  ic.setPrice(0);
141  ic.setProcessed(true);
142  issue.getComments().add(ic);
143  issue = getDao().saveObject(issue);
144  new IssueMail(changes, issue, 0, 0, 0).sendMail();
145  }
146  }
147  }
148  }
149 
150 }
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
boolean myTurn(HttpServletRequest request)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)