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.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;
78 public static String
addInformation(String issueId, String link, String
template) {
79 Long
id = Long.valueOf(issueId);
81 return addInformation(
new IssueWrapper(issue), link,
template);
85 return addInformation(wrapper, link,
null);
90 if(constructor.
isInRole(
"issue:edit")) {
94 marker.
put(
"wrapper", wrapper);
96 marker.
put(
"redirect", link);
97 if(constructor.
isInRole(
"issue:dossier")) {
99 if(dossiers.stream().anyMatch(d -> d.getId().equals(wrapper.
getIssue().
getId()))) {
100 marker.
put(
"dossiers", dossiers);
104 return marker.
parse(
"issue", Strings.isBlank(
template) ?
"addinfo" :
template);
115 return getIdentifier().equals(
id);
119 public boolean myTurn(HttpServletRequest request) {
124 public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
125 if(ServletFileUpload.isMultipartContent(request)) {
127 processInformation(request, response);
128 }
catch (FileUploadException | IOException ex) {
134 private void processInformation(HttpServletRequest request, HttpServletResponse response)
throws FileUploadException, IOException {
140 if(contact ==
null || !contact.
isValid()) {
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"));
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()) {
161 issueId = Long.valueOf(Streams.asString(stream));
162 issue = dao.find(Issue.class, issueId);
163 wrapper =
new IssueWrapper(issue);
166 redirect = Streams.asString(stream, ElephantContext.getEncoding());
169 Long dossierId = Long.valueOf(Streams.asString(stream));
171 Dossier dossier = dao.find(Dossier.class, dossierId);
173 changes.addChange(
new StringChange(issueCat, I_.get(
"Dossier"),
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"),
189 comment = Streams.asString(stream, ElephantContext.getEncoding());
190 if(!Strings.isBlank(comment)) {
191 comment = HTMLEntities.plainWhenPossible(comment);
192 changes.addComment(commentCat, comment);
196 expenses = NumberUtils.toDouble(Streams.asString(stream), 0);
199 hours = NumberUtils.toDouble(Streams.asString(stream), 0);
202 price = NumberUtils.toDouble(Streams.asString(stream), 0);
206 issue.
setStatus(IssueStatus.STATUS_INCOMPLETE);
211 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_RESOLVED,
false));
212 issue.
setStatus(IssueStatus.STATUS_RESOLVED);
214 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_CLOSED,
false));
215 issue.
setStatus(IssueStatus.STATUS_CLOSED);
221 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_RESOLVED,
false));
222 issue.
setStatus(IssueStatus.STATUS_RESOLVED);
224 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_CLOSED,
false));
225 issue.
setStatus(IssueStatus.STATUS_CLOSED);
229 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_REOPENED,
false));
230 issue.
setStatus(IssueStatus.STATUS_REOPENED);
235 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_VERIFIED,
false));
236 issue.
setStatus(IssueStatus.STATUS_VERIFIED);
238 changes.addChange(
new EnumChange(issueCat, I_.get(
"Status"), issue.
getStatus(), IssueStatus.
STATUS_CLOSED,
false));
239 issue.
setStatus(IssueStatus.STATUS_CLOSED);
243 cancelEmails = Boolean.parseBoolean(Streams.asString(stream, ElephantContext.getEncoding()));
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));
271 changes.clearEquals();
272 if(changes.stringSize() > 0) {
273 Date now =
new Date();
275 IssueComment ic =
new IssueComment();
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);
284 changes.addChange(
new UniqueNumber(issueCat, I_.get(
"Hours"), hours));
288 changes.addChange(
new UniqueNumber(issueCat, I_.get(
"Price"), price));
291 ic.setComment(changes.getChangesString());
294 issue = dao.saveObject(issue);
295 SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, issue,
"saved",
298 new IssueMail(changes, issue, 0, 0, 0).sendMail();
300 IssueWrapper.checkPendingRevision(issue);
303 if(!Strings.isBlank(redirect)) {
308 private String convertToFileName(String fileName) {
309 if(fileName.contains(
"/")) {
310 fileName = fileName.substring(fileName.lastIndexOf(
"/") + 1);
static IContact getIContact()
IssueResolution getResolution()
void setResolution(IssueResolution resolution)
void setDescription(String description)
void setModification(Date modification)
void setStatus(IssueStatus status)
void setDossier(Dossier dossier)
Set< IssueComment > getComments()
void setDuplicated(Issue duplicated)
java.util.List< Dossier > getDossierList()
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)
void redirect(String path)
boolean isInRole(String role)