BrightSide Workbench Full Report + Source Code
QuestionCtrl.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.logging.Level;
26 import java.util.logging.Logger;
27 import javax.servlet.ServletContext;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import org.turro.string.Strings;
31 import org.apache.commons.fileupload.FileItemIterator;
32 import org.apache.commons.fileupload.FileItemStream;
33 import org.apache.commons.fileupload.FileUploadException;
34 import org.apache.commons.fileupload.servlet.ServletFileUpload;
35 import org.apache.commons.fileupload.util.Streams;
36 import org.turro.attach.db.AttachPU;
37 import org.turro.attach.entity.AttachContent;
38 import org.turro.attach.entity.Attachment;
39 import org.turro.attach.www.AttachCtrl;
40 import org.turro.auth.Authentication;
41 import org.turro.callback.ChangedValue;
42 import org.turro.contacts.Contact;
43 import org.turro.describeit.DescribeItUtil;
44 import org.turro.dossier.db.DossierPU;
45 import org.turro.dossier.entity.Dossier;
46 import org.turro.dossier.entity.Issue;
47 import org.turro.dossier.entity.IssueComment;
48 import org.turro.dossier.entity.IssueType;
49 import org.turro.dossier.issue.IssueWrapper;
50 import org.turro.elephant.context.Application;
51 import org.turro.elephant.context.ElephantContext;
52 import org.turro.elephant.context.IConstructor;
53 import org.turro.elephant.direct.DirectContent;
54 import org.turro.elephant.direct.DirectContents;
55 import org.turro.elephant.direct.IDirectContent;
56 import org.turro.html.HtmlContent;
57 import org.turro.jpa.Dao;
58 import org.turro.log.SystemLogType;
59 import org.turro.log.SystemLogger;
60 import org.turro.marker.ElephantMarker;
61 import org.turro.plugin.contacts.IContact;
62 import org.turro.tags.TagCloud;
63 import org.turro.tags.Tags;
64 
69 @DirectContent(identifier="issue-question")
70 public class QuestionCtrl implements IDirectContent {
71 
72  public static String editQuestion(String issueId, String link, IssueType type, String template) {
73  Long id = Long.valueOf(issueId);
74  Issue issue = new DossierPU().find(Issue.class, id);
75  return editQuestion(issue, link, type, template);
76  }
77 
78  public static String editQuestion(Issue issue, String link, IssueType type, String template) {
80  if(issue == null) {
81  ElephantMarker marker = new ElephantMarker(constructor);
82  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
83  marker.put("redirect", link);
84  marker.put("type", type == null ? IssueType.TYPE_QUESTION.toString() : type.toString());
85  return marker.parse("issue", Strings.isBlank(template) ? "editQuestion" : template);
86  } else {
87  IssueWrapper wrapper = new IssueWrapper(issue);
88  if(wrapper.isFullParticipant()) {
89  String issuePath = DossierPU.getObjectPath(issue);
90  ElephantMarker marker = new ElephantMarker(constructor);
91  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
92  marker.put("redirect", link);
93  marker.put("issue", issue);
94  marker.put("webDefault", DescribeItUtil.description(DescribeItUtil.DEFAULT_ID, issuePath));
95  marker.put("tagsString", Tags.tagsString(issuePath));
96  marker.put("tagChoices", Tags.tagChoices(issuePath));
97  //marker.put("type", type == null ? IssueType.TYPE_QUESTION.toString() : type.toString());
98  AttachCtrl ac = new AttachCtrl(constructor);
99  ac.setEntityPath(issuePath);
100  ac.setPublicOnly(false);
101  marker.put("ac", ac.parseAttachments());
102  return marker.parse("issue", Strings.isBlank(template) ? "editQuestion" : template);
103  }
104  }
105  return "";
106  }
107 
108  public static String getIdentifier() {
109  return QuestionCtrl.class.getAnnotation(DirectContent.class).identifier();
110  }
111 
112  @Override
113  public boolean itsMe(String id) {
114  return getIdentifier().equals(id);
115  }
116 
117  @Override
118  public boolean myTurn(HttpServletRequest request) {
119  return DirectContents.isYourTurn(request, getIdentifier());
120  }
121 
122  @Override
123  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
124  if(ServletFileUpload.isMultipartContent(request)) {
125  try {
126  processInformation(request, response);
127  } catch (FileUploadException | IOException ex) {
128  Logger.getLogger(QuestionCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
129  }
130  }
131  }
132 
133  private String description;
134  private Issue issue = null;
135 
136  private void processInformation(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException {
138  Dao dao = new DossierPU();
139  IContact contact = Authentication.getIContact();
140  if(contact == null || !contact.isValid()) {
141  return;
142  }
143  Long dossierId = null, issueId = null;
144  String redirect = null, comment = null, webDefault = null, tags = null;
145  ServletFileUpload upload = new ServletFileUpload();
146  upload.setHeaderEncoding(ElephantContext.getEncoding());
147  FileItemIterator iter = upload.getItemIterator(request);
148  while(iter.hasNext()) {
149  FileItemStream item = iter.next();
150  String name = item.getFieldName();
151  InputStream stream = item.openStream();
152  if(item.isFormField()) {
153  switch(name) {
154  case "dossierId":
155  dossierId = Long.valueOf(Streams.asString(stream));
156  break;
157  case "issueId":
158  issueId = Long.valueOf(Streams.asString(stream));
159  issue = dao.find(Issue.class, issueId);
160  if(issue == null && dossierId > 0) {
161  Dossier dossier = dao.find(Dossier.class, dossierId);
162  issue = Issue.getDefaultIssue();
163  issue.setDossier(dossier);
164  issue.setModification(new Date());
165  issue.setType(IssueType.TYPE_QUESTION);
166  }
167  break;
168  case "redirect":
169  redirect = Streams.asString(stream, ElephantContext.getEncoding());
170  break;
171  case "type":
172  String type = Streams.asString(stream, ElephantContext.getEncoding());
173  issue.setType(IssueType.valueOf(type));
174  break;
175  case "webDefault":
176  webDefault = Streams.asString(stream, ElephantContext.getEncoding());
177  break;
178  case "description":
179  description = Streams.asString(stream, ElephantContext.getEncoding());
180  issue.setDescription(description);
181  break;
182  case "comment":
183  comment = Streams.asString(stream, ElephantContext.getEncoding());
184  break;
185  case "tags":
186  tags = Streams.asString(stream, ElephantContext.getEncoding());
187  break;
188  default:
189  if(issue != null) {
190  for(IssueComment ic : issue.getComments()) {
191  if(name.equals("comment-" + ic.getId())) {
192  String s = Streams.asString(stream, ElephantContext.getEncoding());
193  ic.setComment(s);
194  ic.setModification(new Date());
195  }
196  }
197  }
198  break;
199  }
200  } else if(issue != null) {
201  checkSave(dao);
202  Attachment attachment = new Attachment();
203  attachment.setModification(new Date());
204  attachment.setPath("/issue/" + issue.getId());
205  attachment.setOwner(contact.getId());
206  attachment.setOnlyOwner(false);
207  attachment.setComment(comment);
208  attachment.setShowKey(null);
209  attachment.setPublishable(true);
210  AttachContent ac = new AttachContent();
211  ByteArrayOutputStream baos = new ByteArrayOutputStream();
212  Streams.copy(item.openStream(), baos, true);
213  ac.setFileContent(baos.toByteArray());
214  attachment.setAttachContent(ac);
215  attachment.setFileName(convertToFileName(item.getName()));
216  attachment.setFileContentType(item.getContentType());
217  attachment.setFileSize(ac.getFileContent().length);
218  attachment.setValidated(Application.getApplication().isInRole("attach:self-validate"));
219  if(attachment.getFileSize() > 0) {
220  attachment = new AttachPU().saveObject(attachment);
221  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
222  }
223  }
224  }
225  if(issue != null) {
226  if(!Strings.isBlank(webDefault)) {
227  DescribeItUtil.addDescription(issue, DescribeItUtil.DEFAULT_ID, webDefault, (Contact) contact.getContact(), new ChangedValue<String>() {
228  @Override
229  public void onChange(String oldValue, String newValue) {
230  IssueComment ic = new IssueComment();
231  ic.setIssue(issue);
232  ic.setModification(new Date());
233  ic.setIContact(Authentication.getIContact());
234  ic.setComment(HtmlContent.text(newValue));
235  issue.getComments().add(ic);
236  }
237  });
238  }
239  Tags.setTags(issue, tags);
240  checkSave(dao);
241  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, issue, "saved",
242  issue.getId() + " - " + issue.getDescription());
243  }
244  if(!Strings.isBlank(redirect)) {
245  TagCloud.resetContext(constructor, "issue");
246  if(redirect.endsWith("=0")) {
247  redirect = redirect.substring(0, redirect.length() - 1) + issue.getId();
248  }
249  response.sendRedirect(redirect);
250  }
251  }
252 
253  private String convertToFileName(String fileName) {
254  if(fileName.contains("/")) {
255  fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
256  }
257  return fileName;
258  }
259 
260  private void checkSave(Dao dao) {
261  Date now = new Date();
262  if(issue.getId() == null) {
263  issue.addReporter(Authentication.getIContact());
265  if(!Strings.isBlank(description)) {
266  issue.setDescription(description);
267  } else if(Strings.isBlank(issue.getDescription())) {
268  issue.setDescription("********");
269  }
270  IssueComment ic = new IssueComment();
271  ic.setIssue(issue);
272  ic.setModification(now);
273  ic.setIContact(Authentication.getIContact());
274  ic.setComment(HtmlContent.text(description));
275  issue.getComments().add(ic);
276  }
277  IssueWrapper iw = new IssueWrapper(issue);
278  iw.clearEmpties();
279  issue.setModification(now);
280  issue = dao.saveObject(issue);
281  }
282 
283 }
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
static DescribeIt description(String id, Object entity)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
void setType(IssueType type)
Definition: Issue.java:295
void setDescription(String description)
Definition: Issue.java:154
static Issue getDefaultIssue()
Definition: Issue.java:503
void setModification(Date modification)
Definition: Issue.java:202
IssueParticipant addReporter(Object contact)
Definition: Issue.java:397
void setDossier(Dossier dossier)
Definition: Issue.java:162
Set< IssueComment > getComments()
Definition: Issue.java:122
IssueParticipant addResponsible(Object contact)
Definition: Issue.java:403
static String editQuestion(String issueId, String link, IssueType type, String template)
boolean myTurn(HttpServletRequest request)
static String editQuestion(Issue issue, String link, IssueType type, String template)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
static String tagsString(Object entity)
Definition: Tags.java:419
static Collection< String > tagChoices(Object entity)
Definition: Tags.java:398