View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.actions.admin.report;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.apache.struts.upload.FormFile;
24  import org.itracker.core.resources.ITrackerResources;
25  import org.itracker.model.Language;
26  import org.itracker.model.PermissionType;
27  import org.itracker.model.Report;
28  import org.itracker.model.util.UserUtilities;
29  import org.itracker.services.ConfigurationService;
30  import org.itracker.services.ReportService;
31  import org.itracker.web.actions.base.ItrackerBaseAction;
32  import org.itracker.web.forms.ReportForm;
33  import org.itracker.web.util.Constants;
34  import org.itracker.web.util.RequestHelper;
35  import org.itracker.web.util.ServletContextUtils;
36  
37  import javax.servlet.ServletException;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.http.HttpSession;
41  import java.io.IOException;
42  import java.util.Map;
43  import java.util.Set;
44  
45  
46  public class EditReportAction extends ItrackerBaseAction {
47      private static final Logger log = Logger.getLogger(EditReportAction.class);
48  
49      public EditReportAction() {
50      }
51  
52      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
53  
54          final ActionMessages errors = new ActionMessages();
55  
56          if (!isTokenValid(request)) {
57              log.debug("Invalid request token while editing report.");
58              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
59                      "itracker.web.error.transaction"));
60              saveErrors(request, errors);
61              return mapping.findForward("listreports");
62          }
63          resetToken(request);
64  
65          final ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
66          final ReportService reportService = ServletContextUtils.getItrackerServices().getReportService();
67          final ReportForm/../../../org/itracker/web/forms/ReportForm.html#ReportForm">ReportForm reportForm = (ReportForm) form;
68          if (reportForm == null)
69              return mapping.findForward("listreportsadmin");
70  
71          HttpSession session = request.getSession(true);
72          Report editreport;
73          try {
74  
75              if (null != reportForm.getId() && reportForm.getId() != -1) {
76                  // TODO cleanup the service.
77                  editreport = reportService.getReportDAO().findByPrimaryKey(reportForm.getId());
78              } else {
79                  editreport = new Report();
80              }
81              editreport.setName(reportForm.getName());
82              editreport.setNameKey(reportForm.getNameKey());
83              editreport.setDescription(reportForm.getDescription());
84              FormFile fileData = reportForm.getFileDataFile();
85              try {
86                  if (fileData.getFileData() == null ||
87                          fileData.getFileData().length == 0) {
88                      // should be validated already-
89                      if (null == reportForm.getId()) {
90                          errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingdatafile"));
91                      }
92                  } else {
93                      EditReportFormAction.getAsString(fileData.getFileData(), errors);
94                      editreport.setFileData(fileData.getFileData());
95                  }
96  
97              } catch (Exception e) {
98                  log.error("Exception while verifying import data.", e);
99                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.missingdatafile"));
100             }
101 
102 
103             String action = request.getParameter("action");
104             if (errors.isEmpty()) {
105                 reportService.getReportDAO().saveOrUpdate(editreport);
106             }
107 
108             if ("create".equals(action) && editreport != null) {
109                 // If it was a create, add a new language key in the base for it.
110                 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, editreport.getNameKey(), editreport.getName()));
111                 ITrackerResources.clearKeyFromBundles(editreport.getNameKey(), true);
112             }
113         } catch (Exception e) {
114             log.error("Exception processing form data", e);
115             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.save"));
116         }
117 
118         if (!errors.isEmpty()) {
119             saveErrors(request, errors);
120             saveToken(request);
121             return mapping.getInputForward();
122         }
123 
124         session.removeAttribute(Constants.REPORT_KEY);
125         return mapping.findForward("listreportsadmin");
126     }
127 
128 }
129