BrightSide Workbench Full Report + Source Code
AddInformationCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.www;
20 
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Date;
25 import java.util.List;
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.Strings;
32 import org.apache.commons.fileupload.FileItemIterator;
33 import org.apache.commons.fileupload.FileItemStream;
34 import org.apache.commons.fileupload.FileUploadException;
35 import org.apache.commons.fileupload.servlet.ServletFileUpload;
36 import org.apache.commons.fileupload.util.Streams;
37 import org.apache.commons.lang3.math.NumberUtils;
38 import org.turro.attach.db.AttachPU;
39 import org.turro.attach.entity.AttachContent;
40 import org.turro.attach.entity.Attachment;
41 import org.turro.auth.Authentication;
42 import org.turro.dossier.db.DossierPU;
43 import org.turro.dossier.dossier.ProjectContext;
44 import org.turro.dossier.entity.Dossier;
45 import org.turro.dossier.entity.Issue;
46 import org.turro.dossier.entity.IssueComment;
47 import org.turro.dossier.entity.IssueResolution;
48 import org.turro.dossier.entity.IssueStatus;
49 import org.turro.dossier.issue.IssueMail;
50 import org.turro.dossier.issue.IssueWrapper;
51 import org.turro.dossier.search.DossierResults;
52 import org.turro.elephant.context.Application;
53 import org.turro.elephant.context.ElephantContext;
54 import org.turro.elephant.context.IConstructor;
55 import org.turro.elephant.direct.DirectContent;
56 import org.turro.elephant.direct.DirectContents;
57 import org.turro.elephant.direct.IDirectContent;
58 import org.turro.html.HTMLEntities;
59 import org.turro.i18n.I_;
60 import org.turro.jpa.Dao;
61 import org.turro.log.SystemLogType;
62 import org.turro.log.SystemLogger;
63 import org.turro.marker.ElephantMarker;
64 import org.turro.plugin.contacts.IContact;
65 import org.turro.registry.ChangeCategory;
66 import org.turro.registry.Changes;
67 import org.turro.registry.EnumChange;
68 import org.turro.registry.StringChange;
69 import org.turro.registry.UniqueNumber;
70 
75 @DirectContent(identifier="issue-add-info")
76 public class AddInformationCtrl implements IDirectContent {
77 
78  public static String addInformation(String issueId, String link, String template) {
79  Long id = Long.valueOf(issueId);
80  Issue issue = new DossierPU().find(Issue.class, id);
81  return addInformation(new IssueWrapper(issue), link, template);
82  }
83 
84  public static String addInformation(IssueWrapper wrapper, String link) {
85  return addInformation(wrapper, link, null);
86  }
87 
88  public static String addInformation(IssueWrapper wrapper, String link, String template) {
90  if(constructor.isInRole("issue:edit")) {
91  ElephantMarker marker = new ElephantMarker(constructor);
92  marker.put("projectContext", new ProjectContext());
93  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
94  marker.put("wrapper", wrapper);
95  marker.put("issue", wrapper.getIssue());
96  marker.put("redirect", link);
97  if(constructor.isInRole("issue:dossier")) {
98  List<Dossier> dossiers = new DossierResults().getDossierList();
99  if(dossiers.stream().anyMatch(d -> d.getId().equals(wrapper.getIssue().getId()))) {
100  marker.put("dossiers", dossiers);
101  }
102  }
103  //marker.put("responsibles", Contacts.getWebUsers(Contacts.getByRole("issue:edit")));
104  return marker.parse("issue", Strings.isBlank(template) ? "addinfo" : template);
105  }
106  return "";
107  }
108 
109  public static String getIdentifier() {
110  return AddInformationCtrl.class.getAnnotation(DirectContent.class).identifier();
111  }
112 
113  @Override
114  public boolean itsMe(String id) {
115  return getIdentifier().equals(id);
116  }
117 
118  @Override
119  public boolean myTurn(HttpServletRequest request) {
120  return DirectContents.isYourTurn(request, getIdentifier());
121  }
122 
123  @Override
124  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
125  if(ServletFileUpload.isMultipartContent(request)) {
126  try {
127  processInformation(request, response);
128  } catch (FileUploadException | IOException ex) {
129  Logger.getLogger(AddInformationCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
130  }
131  }
132  }
133 
134  private void processInformation(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException {
136  Dao dao = new DossierPU();
137  IContact contact = Authentication.getIContact();
138  Issue issue = null;
139  IssueWrapper wrapper = null;
140  if(contact == null || !contact.isValid()) {
141  return;
142  }
143  Changes changes = new Changes();
144  ChangeCategory issueCat = new ChangeCategory(2, I_.get("Changes"));
145  ChangeCategory attachCat = new ChangeCategory(3, I_.get("Attachments"));
146  ChangeCategory commentCat = new ChangeCategory(5, I_.get("Comment"));
147  Long issueId = null;
148  Boolean cancelEmails = false;
149  Double hours = 0.0d, expenses = 0.0d, price = 0.0d;
150  String redirect = null, comment = null, description = null;
151  ServletFileUpload upload = new ServletFileUpload();
152  upload.setHeaderEncoding(ElephantContext.getEncoding());
153  FileItemIterator iter = upload.getItemIterator(request);
154  while(iter.hasNext()) {
155  FileItemStream item = iter.next();
156  String name = item.getFieldName();
157  InputStream stream = item.openStream();
158  if(item.isFormField()) {
159  switch(name) {
160  case "issueId":
161  issueId = Long.valueOf(Streams.asString(stream));
162  issue = dao.find(Issue.class, issueId);
163  wrapper = new IssueWrapper(issue);
164  break;
165  case "redirect":
166  redirect = Streams.asString(stream, ElephantContext.getEncoding());
167  break;
168  case "dossierId":
169  Long dossierId = Long.valueOf(Streams.asString(stream));
170  if(dossierId > 0) {
171  Dossier dossier = dao.find(Dossier.class, dossierId);
172  if(dossier != null && dossier.getId() != issue.getDossier().getId()) {
173  changes.addChange(new StringChange(issueCat, I_.get("Dossier"),
174  issue.getDossier().getDescription(), dossier.getDescription()));
175  issue.setDossier(dossier);
176  }
177  }
178  break;
179  case "description":
180  description = Streams.asString(stream, ElephantContext.getEncoding());
181  if(!Strings.isBlank(description)) {
182  description = HTMLEntities.plainWhenPossible(description);
183  changes.addChange(new StringChange(issueCat, I_.get("Description"),
184  issue.getDescription(), description));
185  issue.setDescription(description);
186  }
187  break;
188  case "comment":
189  comment = Streams.asString(stream, ElephantContext.getEncoding());
190  if(!Strings.isBlank(comment)) {
191  comment = HTMLEntities.plainWhenPossible(comment);
192  changes.addComment(commentCat, comment);
193  }
194  break;
195  case "expenses":
196  expenses = NumberUtils.toDouble(Streams.asString(stream), 0);
197  break;
198  case "hours":
199  hours = NumberUtils.toDouble(Streams.asString(stream), 0);
200  break;
201  case "price":
202  price = NumberUtils.toDouble(Streams.asString(stream), 0);
203  break;
204  case "status1":
205  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_INCOMPLETE, false));
206  issue.setStatus(IssueStatus.STATUS_INCOMPLETE);
207  break;
208  case "status2":
209  changes.addChange(new EnumChange(issueCat, I_.get("Resolution"), issue.getResolution(), IssueResolution.RESOLUTION_FIXED));
210  issue.setResolution(IssueResolution.RESOLUTION_FIXED);
211  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_RESOLVED, false));
212  issue.setStatus(IssueStatus.STATUS_RESOLVED);
213  if(wrapper.getCanClose()) {
214  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_CLOSED, false));
215  issue.setStatus(IssueStatus.STATUS_CLOSED);
216  }
217  break;
218  case "status3":
219  changes.addChange(new EnumChange(issueCat, I_.get("Resolution"), issue.getResolution(), IssueResolution.RESOLUTION_WONTFIX));
220  issue.setResolution(IssueResolution.RESOLUTION_WONTFIX);
221  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_RESOLVED, false));
222  issue.setStatus(IssueStatus.STATUS_RESOLVED);
223  if(wrapper.getCanClose()) {
224  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_CLOSED, false));
225  issue.setStatus(IssueStatus.STATUS_CLOSED);
226  }
227  break;
228  case "status4":
229  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_REOPENED, false));
230  issue.setStatus(IssueStatus.STATUS_REOPENED);
231  issue.setResolution(IssueResolution.RESOLUTION_NONE);
232  issue.setDuplicated(null);
233  break;
234  case "status5":
235  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_VERIFIED, false));
236  issue.setStatus(IssueStatus.STATUS_VERIFIED);
237  if(wrapper.getCanClose()) {
238  changes.addChange(new EnumChange(issueCat, I_.get("Status"), issue.getStatus(), IssueStatus.STATUS_CLOSED, false));
239  issue.setStatus(IssueStatus.STATUS_CLOSED);
240  }
241  break;
242  case "cancelEmails":
243  cancelEmails = Boolean.parseBoolean(Streams.asString(stream, ElephantContext.getEncoding()));
244  break;
245  }
246  } else if(issueId != null && issueId > 0) {
247  Attachment attachment = new Attachment();
248  attachment.setModification(new Date());
249  attachment.setPath("/issue/" + issueId);
250  attachment.setOwner(contact.getId());
251  attachment.setOnlyOwner(false);
252  attachment.setComment("");
253  attachment.setShowKey(null);
254  attachment.setPublishable(true);
255  AttachContent ac = new AttachContent();
256  ByteArrayOutputStream baos = new ByteArrayOutputStream();
257  Streams.copy(item.openStream(), baos, true);
258  ac.setFileContent(baos.toByteArray());
259  attachment.setAttachContent(ac);
260  attachment.setFileName(convertToFileName(item.getName()));
261  attachment.setFileContentType(item.getContentType());
262  attachment.setFileSize(ac.getFileContent().length);
263  attachment.setValidated(Application.getApplication().isInRole("attach:self-validate"));
264  if(attachment.getFileSize() > 0) {
265  attachment = new AttachPU().saveObject(attachment);
266  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
267  changes.addChange(new StringChange(attachCat, I_.get("Upload"), attachment.getFileName(), null, true));
268  }
269  }
270  }
271  changes.clearEquals();
272  if(changes.stringSize() > 0) {
273  Date now = new Date();
274  if(issue != null) {
275  IssueComment ic = new IssueComment();
276  ic.setIssue(issue);
277  ic.setModification(now);
278  ic.setIContact(Authentication.getIContact());
279  if(expenses != 0.0d) {
280  changes.addChange(new UniqueNumber(issueCat, I_.get("Expenses"), expenses));
281  ic.setExpenses(expenses);
282  }
283  if(hours != 0.0d) {
284  changes.addChange(new UniqueNumber(issueCat, I_.get("Hours"), hours));
285  ic.setHours(hours);
286  }
287  if(price != 0.0d) {
288  changes.addChange(new UniqueNumber(issueCat, I_.get("Price"), price));
289  ic.setPrice(price);
290  }
291  ic.setComment(changes.getChangesString());
292  issue.getComments().add(ic);
293  issue.setModification(now);
294  issue = dao.saveObject(issue);
295  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, issue, "saved",
296  issue.getId() + " - " + issue.getDescription());
297  if(!cancelEmails) {
298  new IssueMail(changes, issue, 0, 0, 0).sendMail();
299  }
300  IssueWrapper.checkPendingRevision(issue);
301  }
302  }
303  if(!Strings.isBlank(redirect)) {
304  constructor.redirect(redirect);
305  }
306  }
307 
308  private String convertToFileName(String fileName) {
309  if(fileName.contains("/")) {
310  fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
311  }
312  return fileName;
313  }
314 
315 }
IssueResolution getResolution()
Definition: Issue.java:226
void setResolution(IssueResolution resolution)
Definition: Issue.java:230
void setDescription(String description)
Definition: Issue.java:154
void setModification(Date modification)
Definition: Issue.java:202
void setStatus(IssueStatus status)
Definition: Issue.java:246
void setDossier(Dossier dossier)
Definition: Issue.java:162
Set< IssueComment > getComments()
Definition: Issue.java:122
void setDuplicated(Issue duplicated)
Definition: Issue.java:170
java.util.List< Dossier > getDossierList()
static String addInformation(IssueWrapper wrapper, String link)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static String addInformation(IssueWrapper wrapper, String link, String template)
static String addInformation(String issueId, String link, String template)
boolean myTurn(HttpServletRequest request)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)