19 package org.turro.dossier.www;
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;
69 @DirectContent(identifier=
"issue-question")
73 Long
id = Long.valueOf(issueId);
75 return editQuestion(issue, link, type,
template);
83 marker.
put(
"redirect", link);
85 return marker.
parse(
"issue", Strings.isBlank(
template) ?
"editQuestion" :
template);
92 marker.
put(
"redirect", link);
93 marker.
put(
"issue", issue);
102 return marker.
parse(
"issue", Strings.isBlank(
template) ?
"editQuestion" :
template);
114 return getIdentifier().equals(
id);
118 public boolean myTurn(HttpServletRequest request) {
123 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
124 if(ServletFileUpload.isMultipartContent(request)) {
126 processInformation(request, response);
127 }
catch (FileUploadException | IOException ex) {
133 private String description;
134 private Issue issue =
null;
136 private void processInformation(HttpServletRequest request, HttpServletResponse response)
throws FileUploadException, IOException {
140 if(contact ==
null || !contact.
isValid()) {
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()) {
155 dossierId = Long.valueOf(Streams.asString(stream));
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);
165 issue.
setType(IssueType.TYPE_QUESTION);
169 redirect = Streams.asString(stream, ElephantContext.getEncoding());
172 String type = Streams.asString(stream, ElephantContext.getEncoding());
173 issue.
setType(IssueType.valueOf(type));
176 webDefault = Streams.asString(stream, ElephantContext.getEncoding());
179 description = Streams.asString(stream, ElephantContext.getEncoding());
183 comment = Streams.asString(stream, ElephantContext.getEncoding());
186 tags = Streams.asString(stream, ElephantContext.getEncoding());
191 if(name.equals(
"comment-" + ic.getId())) {
192 String s = Streams.asString(stream, ElephantContext.getEncoding());
194 ic.setModification(
new Date());
200 }
else if(issue !=
null) {
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);
226 if(!Strings.isBlank(webDefault)) {
227 DescribeItUtil.addDescription(issue, DescribeItUtil.DEFAULT_ID, webDefault, (Contact) contact.
getContact(),
new ChangedValue<String>() {
229 public void onChange(String oldValue, String newValue) {
230 IssueComment ic = new IssueComment();
232 ic.setModification(new Date());
233 ic.setIContact(Authentication.getIContact());
234 ic.setComment(HtmlContent.text(newValue));
235 issue.getComments().add(ic);
239 Tags.setTags(issue, tags);
241 SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, issue,
"saved",
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();
249 response.sendRedirect(redirect);
253 private String convertToFileName(String fileName) {
254 if(fileName.contains(
"/")) {
255 fileName = fileName.substring(fileName.lastIndexOf(
"/") + 1);
260 private void checkSave(Dao dao) {
261 Date now =
new Date();
262 if(issue.
getId() ==
null) {
265 if(!Strings.isBlank(description)) {
270 IssueComment ic =
new IssueComment();
272 ic.setModification(now);
273 ic.setIContact(Authentication.getIContact());
274 ic.setComment(HtmlContent.text(description));
277 IssueWrapper iw =
new IssueWrapper(issue);
280 issue = dao.saveObject(issue);
String parseAttachments()
void setPublicOnly(boolean publicOnly)
void setEntityPath(String entityPath)
static IContact getIContact()
static final String DEFAULT_ID
static DescribeIt description(String id, Object entity)
static String getObjectPath(Object object)
IContact getDefaultResponsible()
void setType(IssueType type)
void setDescription(String description)
static Issue getDefaultIssue()
void setModification(Date modification)
IssueParticipant addReporter(Object contact)
void setDossier(Dossier dossier)
Set< IssueComment > getComments()
IssueParticipant addResponsible(Object contact)
boolean isFullParticipant()
static String editQuestion(String issueId, String link, IssueType type, String template)
boolean myTurn(HttpServletRequest request)
static String getIdentifier()
static String editQuestion(Issue issue, String link, IssueType type, String template)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
IConstructor getConstructor()
static Application getApplication()
static String logMsg(String msg)
static boolean isYourTurn(HttpServletRequest request, String path)
static String createRelativeURL(String id)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)