1 package org.itracker.web.actions.admin.report;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4 import org.apache.log4j.Logger;
5 import org.apache.struts.action.*;
6 import org.itracker.model.PermissionType;
7 import org.itracker.model.Report;
8 import org.itracker.model.util.UserUtilities;
9 import org.itracker.services.ReportService;
10 import org.itracker.web.actions.base.ItrackerBaseAction;
11 import org.itracker.web.forms.ReportForm;
12 import org.itracker.web.util.Constants;
13 import org.itracker.web.util.RequestHelper;
14 import org.itracker.web.util.ServletContextUtils;
15 import org.w3c.dom.Document;
16 import org.xml.sax.SAXException;
17
18 import javax.servlet.ServletException;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21 import javax.servlet.http.HttpSession;
22 import javax.xml.parsers.DocumentBuilderFactory;
23 import javax.xml.parsers.ParserConfigurationException;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerConfigurationException;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.TransformerFactory;
28 import javax.xml.transform.dom.DOMSource;
29 import javax.xml.transform.stream.StreamResult;
30 import java.io.*;
31 import java.lang.reflect.InvocationTargetException;
32 import java.util.Map;
33 import java.util.Set;
34
35
36
37 public class EditReportFormAction extends ItrackerBaseAction {
38 private static final Logger log = Logger.getLogger(EditReportFormAction.class);
39
40 public EditReportFormAction() {
41 }
42
43 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44 ActionMessages errors = new ActionMessages();
45
46 try {
47 ReportService reportService = ServletContextUtils.getItrackerServices().getReportService();
48
49 HttpSession session = request.getSession(true);
50 String action = request.getParameter("action");
51
52 Report report;
53
54 report = null;
55
56 ReportForm/../../../org/itracker/web/forms/ReportForm.html#ReportForm">ReportForm reportForm = (ReportForm) form;
57 if (reportForm == null) {
58 reportForm = new ReportForm();
59 }
60
61
62 if ("create".equals(action)) {
63 report = new Report();
64 report.setId(-1);
65 reportForm.setAction("create");
66 reportForm.setId(report.getId());
67 } else if ("update".equals(action)) {
68 Integer reportId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
69 report = reportService.getReportDAO().findByPrimaryKey(reportId);
70 if (report == null) {
71 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
72 } else {
73 reportForm.setAction("update");
74 reportForm.setId(report.getId());
75 reportForm.setName(report.getName());
76 reportForm.setNameKey(report.getNameKey());
77 reportForm.setDescription(report.getDescription());
78 reportForm.setClassName(report.getClassName());
79 if (null != report.getFileData()) {
80 String fileDataString = getAsString(report.getFileData(), errors);
81 reportForm.setFileData(fileDataString);
82 }
83
84 }
85 } else {
86 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidaction"));
87 }
88
89 if (errors.isEmpty()) {
90 request.setAttribute("reportForm", reportForm);
91 session.setAttribute(Constants.REPORT_KEY, report);
92 saveToken(request);
93 return mapping.getInputForward();
94 }
95 } catch (RuntimeException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
96 log.error("Exception while creating edit report form.", e);
97 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
98 }
99
100 if (!errors.isEmpty()) {
101 saveErrors(request, errors);
102 }
103
104 return mapping.findForward("error");
105 }
106
107 public static String getAsString(byte[] xmlBytes, ActionMessages errors) {
108
109 try {
110 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
111 new ByteArrayInputStream(xmlBytes));
112 StringWriter w = new StringWriter();
113
114 Transformer t = TransformerFactory.newInstance().newTransformer();
115 t.transform(new DOMSource(doc), new StreamResult(w));
116
117 return w.toString();
118
119 } catch (SAXException e) {
120 log.debug("Exception while creating edit report form.", e);
121 } catch (ParserConfigurationException | TransformerException | IOException e) {
122 log.error("Exception while creating edit report form.", e);
123 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
124 }
125 return null;
126 }
127
128 public static byte[] getAsXmlBytes (String xmlString, ActionMessages errors) {
129
130 try {
131 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
132 new ByteArrayInputStream(xmlString.getBytes("utf-8")));
133 ByteArrayOutputStream os = new ByteArrayOutputStream();
134 OutputStreamWriter w = new OutputStreamWriter(os, "utf-8");
135
136 Transformer t = TransformerFactory.newInstance().newTransformer();
137 t.transform(new DOMSource(doc), new StreamResult(w));
138
139 return os.toByteArray();
140
141 } catch (SAXException | ParserConfigurationException e) {
142 log.debug("Exception while creating edit report form.", e);
143 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.details", e.getMessage()));
144 } catch (TransformerException | IOException e) {
145 log.error("Exception while creating edit report form.", e);
146 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
147 }
148 return null;
149 }
150
151 }
152