BrightSide Workbench Full Report + Source Code
EditDossierCtrl.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.contacts.Contact;
42 import org.turro.describeit.DescribeItUtil;
43 import org.turro.dossier.db.DossierPU;
44 import org.turro.dossier.dossier.DossierWrapper;
45 import org.turro.dossier.entity.Dossier;
46 import org.turro.elephant.context.Application;
47 import org.turro.elephant.context.ElephantContext;
48 import org.turro.elephant.context.IConstructor;
49 import org.turro.elephant.direct.DirectContent;
50 import org.turro.elephant.direct.DirectContents;
51 import org.turro.elephant.direct.IDirectContent;
52 import org.turro.html.HTMLEntities;
53 import org.turro.jpa.Dao;
54 import org.turro.log.SystemLogType;
55 import org.turro.log.SystemLogger;
56 import org.turro.marker.ElephantMarker;
57 import org.turro.plugin.contacts.IContact;
58 
63 @DirectContent(identifier="edit-dossier")
64 public class EditDossierCtrl implements IDirectContent {
65 
66  public static String editDossier(String dossierId, String link, String template) {
67  Long id = Long.valueOf(dossierId);
68  Dossier dossier = new DossierPU().find(Dossier.class, id);
69  return editDossier(dossier, link, template);
70  }
71 
72  public static String editDossier(Dossier dossier, String link, String template) {
74  if(dossier != null) {
75  DossierWrapper wrapper = new DossierWrapper(dossier);
76  if(wrapper.isParticipant()) {
77  ElephantMarker marker = new ElephantMarker(constructor);
78  marker.put("action", DirectContents.createRelativeURL(getIdentifier()));
79  marker.put("redirect", link);
80  marker.put("webDefault", DescribeItUtil.descriptionString(DescribeItUtil.DEFAULT_ID, dossier));
81  marker.put("dossier", dossier);
82  AttachCtrl ac = new AttachCtrl(constructor);
84  ac.setPublicOnly(false);
85  marker.put("ac", ac.parseAttachments());
86  return marker.parse("dossier", Strings.isBlank(template) ? "editDossier" : template);
87  }
88  }
89  return "";
90  }
91 
92  public static String getIdentifier() {
93  return EditDossierCtrl.class.getAnnotation(DirectContent.class).identifier();
94  }
95 
96  @Override
97  public boolean itsMe(String id) {
98  return getIdentifier().equals(id);
99  }
100 
101  @Override
102  public boolean myTurn(HttpServletRequest request) {
103  return DirectContents.isYourTurn(request, getIdentifier());
104  }
105 
106  @Override
107  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
108  if(ServletFileUpload.isMultipartContent(request)) {
109  try {
110  processInformation(request, response);
111  } catch (FileUploadException | IOException ex) {
112  Logger.getLogger(EditDossierCtrl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115  }
116 
117  private void processInformation(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException {
118  Dao dao = new DossierPU();
119  IContact contact = Authentication.getIContact();
120  Dossier dossier = null;
121  if(contact == null || !contact.isValid()) {
122  return;
123  }
124  Long dossierId = null;
125  String redirect = null, comment = null, webDefault = null;
126  ServletFileUpload upload = new ServletFileUpload();
127  upload.setHeaderEncoding(ElephantContext.getEncoding());
128  FileItemIterator iter = upload.getItemIterator(request);
129  while(iter.hasNext()) {
130  FileItemStream item = iter.next();
131  String name = item.getFieldName();
132  InputStream stream = item.openStream();
133  if(item.isFormField()) {
134  switch(name) {
135  case "dossierId":
136  dossierId = Long.valueOf(Streams.asString(stream));
137  dossier = dao.find(Dossier.class, dossierId);
138  break;
139  case "webDefault":
140  webDefault = Streams.asString(stream, ElephantContext.getEncoding());
141  break;
142  case "redirect":
143  redirect = Streams.asString(stream, ElephantContext.getEncoding());
144  break;
145  case "comment":
146  comment = Streams.asString(stream, ElephantContext.getEncoding());
147  comment = HTMLEntities.plainWhenPossible(comment);
148  break;
149  }
150  } else if(dossier != null) {
151  Attachment attachment = new Attachment();
152  attachment.setModification(new Date());
153  attachment.setPath("/dossier/" + dossier.getId());
154  attachment.setOwner(contact.getId());
155  attachment.setOnlyOwner(false);
156  attachment.setComment(comment);
157  attachment.setShowKey(null);
158  attachment.setPublishable(true);
159  AttachContent ac = new AttachContent();
160  ByteArrayOutputStream baos = new ByteArrayOutputStream();
161  Streams.copy(item.openStream(), baos, true);
162  ac.setFileContent(baos.toByteArray());
163  attachment.setAttachContent(ac);
164  attachment.setFileName(convertToFileName(item.getName()));
165  attachment.setFileContentType(item.getContentType());
166  attachment.setFileSize(ac.getFileContent().length);
167  attachment.setValidated(Application.getApplication().isInRole("attach:self-validate"));
168  if(attachment.getFileSize() > 0) {
169  attachment = new AttachPU().saveObject(attachment);
170  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, attachment, "uploaded", null);
171  }
172  }
173  }
174  if(dossier != null && !Strings.isBlank(webDefault)) {
175  DescribeItUtil.addDescription(dossier, DescribeItUtil.DEFAULT_ID, webDefault, (Contact) contact.getContact(), null);
176  }
177  if(!Strings.isBlank(redirect)) {
178  if(redirect.endsWith("=0")) {
179  redirect = redirect.substring(0, redirect.length() - 1) + dossier.getId();
180  }
181  response.sendRedirect(redirect);
182  }
183  }
184 
185  private String convertToFileName(String fileName) {
186  if(fileName.contains("/")) {
187  fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
188  }
189  return fileName;
190  }
191 
192 }
void setPublicOnly(boolean publicOnly)
Definition: AttachCtrl.java:77
void setEntityPath(String entityPath)
Definition: AttachCtrl.java:45
static String descriptionString(String id, Object entity)
static String getObjectPath(Object object)
Definition: DossierPU.java:66
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
boolean myTurn(HttpServletRequest request)
static String editDossier(Dossier dossier, String link, String template)
static String editDossier(String dossierId, String link, String template)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)