Update of /cvsroot/itracker/itracker/src/cowsultants/itracker/web/actions/admin/report In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2817/src/cowsultants/itracker/web/actions/admin/report Added Files: Tag: itrackerhibernate EditReportAction.java EditReportFormAction.java RemoveReportAction.java ExportReportAction.java Log Message: Modularizing the Struts actions to be able to get a modular system that is easier to read for everybody. --- NEW FILE: RemoveReportAction.java --- /* * This software was designed and created by Jason Carroll. * Copyright (c) 2002, 2003, 2004 Jason Carroll. * The author can be reached at jca...@co... * ITracker website: http://www.cowsultants.com * ITracker forums: http://www.cowsultants.com/phpBB/index.php * * This program is free software; you can redistribute it and/or modify * it only under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ package cowsultants.itracker.web.actions.admin.report; import cowsultants.itracker.ejb.beans.session.ReportHandlerBean; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import cowsultants.itracker.ejb.client.interfaces.ReportHandler; import cowsultants.itracker.ejb.client.util.Logger; import cowsultants.itracker.ejb.client.util.UserUtilities; import cowsultants.itracker.web.actions.notyetsorted.ITrackerAction; import cowsultants.itracker.web.util.Constants; public class RemoveReportAction extends ITrackerAction { public RemoveReportAction() { } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionErrors errors = new ActionErrors(); if(! isLoggedIn(request, response)) { return mapping.findForward("login"); } try { HttpSession session = request.getSession(true); HashMap userPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY); if(! UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_USER_ADMIN)) { return mapping.findForward("unauthorized"); } ReportHandler rh = new ReportHandlerBean(); Integer reportId = new Integer((request.getParameter("id") == null ? "-1" : request.getParameter("id"))); if(reportId.intValue() > 0) { rh.removeReport(reportId); return mapping.findForward("listreportsadmin"); } } catch(NumberFormatException nfe) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport")); Logger.logDebug("Invalid report id " + request.getParameter("id") + " specified."); } catch(Exception e) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); Logger.logError("System Error.", e); } if(! errors.isEmpty()) { saveErrors(request, errors); } return mapping.findForward("error"); } } --- NEW FILE: EditReportAction.java --- /* * This software was designed and created by Jason Carroll. * Copyright (c) 2002, 2003, 2004 Jason Carroll. * The author can be reached at jca...@co... * ITracker website: http://www.cowsultants.com * ITracker forums: http://www.cowsultants.com/phpBB/index.php * * This program is free software; you can redistribute it and/or modify * it only under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ package cowsultants.itracker.web.actions.admin.report; import cowsultants.itracker.ejb.beans.session.ReportHandlerBean; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import cowsultants.itracker.ejb.client.interfaces.ReportHandler; import cowsultants.itracker.ejb.client.interfaces.SystemConfiguration; import cowsultants.itracker.ejb.client.models.LanguageModel; import cowsultants.itracker.ejb.client.models.ReportModel; import cowsultants.itracker.ejb.client.resources.ITrackerResources; import cowsultants.itracker.ejb.client.util.Logger; import cowsultants.itracker.ejb.client.util.ReportUtilities; import cowsultants.itracker.ejb.client.util.UserUtilities; import cowsultants.itracker.web.actions.notyetsorted.ITrackerAction; import cowsultants.itracker.web.util.Constants; public class EditReportAction extends ITrackerAction { public EditReportAction() { } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionErrors errors = new ActionErrors(); if(! isLoggedIn(request, response)) { return mapping.findForward("login"); } if(! isTokenValid(request)) { Logger.logDebug("Invalid request token while editing report."); return mapping.findForward("listreports"); } resetToken(request); HttpSession session = request.getSession(true); ReportModel report = null; try { ReportHandler rh = new ReportHandlerBean(); HashMap userPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY); if(! UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_USER_ADMIN)) { return mapping.findForward("unauthorized"); } report = (ReportModel) session.getAttribute(Constants.REPORT_KEY); report = new ReportModel(); report.setId((Integer) PropertyUtils.getSimpleProperty(form, "id")); report.setName((String) PropertyUtils.getSimpleProperty(form, "name")); report.setNameKey((String) PropertyUtils.getSimpleProperty(form, "nameKey")); report.setDescription((String) PropertyUtils.getSimpleProperty(form, "description")); report.setDataType(((Integer) PropertyUtils.getSimpleProperty(form, "dataType") != null ? ((Integer) PropertyUtils.getSimpleProperty(form, "dataType")).intValue() : ReportUtilities.DATATYPE_ISSUE)); report.setReportType(((Integer) PropertyUtils.getSimpleProperty(form, "reportType") != null ? ((Integer) PropertyUtils.getSimpleProperty(form, "reportType")).intValue() : ReportUtilities.REPORTTYPE_JFREE)); try { FormFile file = (FormFile) PropertyUtils.getSimpleProperty(form, "fileData"); report.setFileData(file.getFileData()); } catch(Exception e) { Logger.logError("Exception while verifying import data.", e); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.missingdatafile")); } String className = (String) PropertyUtils.getSimpleProperty(form, "className"); if(className == null || className.equals("")) { report.setClassName(""); } else { report.setClassName((String) PropertyUtils.getSimpleProperty(form, "className")); } String action = (String) request.getParameter("action"); if("create".equals(action)) { report = rh.createReport(report); } else if ("update".equals(action)) { report = rh.updateReport(report); } if(report == null) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.save")); } else if("create".equals(action)) { // If it was a create, add a new language key in the base for it. SystemConfiguration sc = getITrackerServices().getSystemConfiguration(); sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, report.getNameKey(), report.getName())); ITrackerResources.clearKeyFromBundles(report.getNameKey(), true); } } catch(Exception e) { Logger.logError("Exception processing form data", e); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.save")); } if(! errors.isEmpty()) { saveErrors(request, errors); saveToken(request); return mapping.getInputForward(); } session.removeAttribute(Constants.REPORT_KEY); return mapping.findForward("listreportsadmin"); } } --- NEW FILE: EditReportFormAction.java --- /* * This software was designed and created by Jason Carroll. * Copyright (c) 2002, 2003, 2004 Jason Carroll. * The author can be reached at jca...@co... * ITracker website: http://www.cowsultants.com * ITracker forums: http://www.cowsultants.com/phpBB/index.php * * This program is free software; you can redistribute it and/or modify * it only under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ package cowsultants.itracker.web.actions.admin.report; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.validator.DynaValidatorForm; import cowsultants.itracker.ejb.client.interfaces.ReportHandler; import cowsultants.itracker.ejb.client.models.ReportModel; import cowsultants.itracker.ejb.client.models.UserModel; import cowsultants.itracker.ejb.client.util.Logger; import cowsultants.itracker.ejb.client.util.UserUtilities; import cowsultants.itracker.web.actions.notyetsorted.ITrackerAction; import cowsultants.itracker.web.util.Constants; public class EditReportFormAction extends ITrackerAction { public EditReportFormAction() { } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionErrors errors = new ActionErrors(); if(! isLoggedIn(request, response)) { return mapping.findForward("login"); } try { ReportHandler rh = getITrackerServices().getReportHandler(); HttpSession session = request.getSession(true); HashMap userPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY); UserModel user = (UserModel) session.getAttribute(Constants.USER_KEY); if(! UserUtilities.hasPermission(userPermissions, UserUtilities.PERMISSION_USER_ADMIN)) { return mapping.findForward("unauthorized"); } ReportModel report = null; DynaValidatorForm reportForm = (DynaValidatorForm) form; if(reportForm == null) { reportForm = new DynaValidatorForm(); } String action = (String) request.getParameter("action"); if("create".equals(action)) { report = new ReportModel(); report.setId(new Integer(-1)); reportForm.set("action", "create"); reportForm.set("id", report.getId()); } else if ("update".equals(action)) { Integer reportId = (Integer) PropertyUtils.getSimpleProperty(form, "id"); report = rh.getReport(reportId); if(report == null) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport")); } else { reportForm.set("action", "update"); reportForm.set("id", report.getId()); reportForm.set("name", report.getName()); reportForm.set("nameKey", report.getNameKey()); reportForm.set("description", report.getDescription()); reportForm.set("reportType", new Integer(report.getReportType())); reportForm.set("dataType", new Integer(report.getDataType())); reportForm.set("className", report.getClassName()); } } else { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidaction")); } if(errors.isEmpty()) { request.setAttribute("reportForm", reportForm); session.setAttribute(Constants.REPORT_KEY, report); saveToken(request); return mapping.getInputForward(); } } catch(Exception e) { Logger.logError("Exception while creating edit report form.", e); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); } if(! errors.isEmpty()) { saveErrors(request, errors); } return mapping.findForward("error"); } } --- NEW FILE: ExportReportAction.java --- /* * This software was designed and created by Jason Carroll. * Copyright (c) 2002, 2003, 2004 Jason Carroll. * The author can be reached at jca...@co... * ITracker website: http://www.cowsultants.com * ITracker forums: http://www.cowsultants.com/phpBB/index.php * * This program is free software; you can redistribute it and/or modify * it only under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ package cowsultants.itracker.web.actions.admin.report; import cowsultants.itracker.ejb.beans.session.ReportHandlerBean; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import cowsultants.itracker.ejb.client.interfaces.ReportHandler; import cowsultants.itracker.ejb.client.models.ReportModel; import cowsultants.itracker.ejb.client.util.Base64; import cowsultants.itracker.ejb.client.util.Logger; import cowsultants.itracker.ejb.client.util.UserUtilities; import cowsultants.itracker.web.actions.notyetsorted.ITrackerAction; public class ExportReportAction extends ITrackerAction { public ExportReportAction () { } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionErrors errors = new ActionErrors(); if(! isLoggedIn(request, response)) { return mapping.findForward("login"); } if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) { return mapping.findForward("unauthorized"); } try { Integer reportId = (Integer) PropertyUtils.getSimpleProperty(form, "id"); if(reportId == null || reportId.intValue() < 0) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport")); } else { ReportHandler rh = new ReportHandlerBean(); ReportModel report = rh.getReport(reportId); if(report != null) { report.setFileData(rh.getReportFile(reportId)); response.setContentType("application/x-itracker-report-export"); response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_report_" + report.getId() + ".itr\""); ServletOutputStream out = response.getOutputStream(); Logger.logDebug("Attempting export for: " + report); out.println("# Exported ITracker report " + report.getName()); out.println("id=" + report.getId()); out.println("name=" + report.getName()); out.println("namekey=" + report.getNameKey()); out.println("dataType=" + report.getDataType()); out.println("reportType=" + report.getReportType()); if(report.getClassName() != null && ! report.getClassName().equals("")) { out.println("className=" + report.getClassName()); } out.println("description=" + Base64.encodeObject(report.getDescription(), Base64.DONT_BREAK_LINES)); out.println("definition=" + Base64.encodeBytes(report.getFileData(), Base64.DONT_BREAK_LINES)); out.flush(); out.close(); return null; } Logger.logDebug("Unknown report " + reportId + " specified for export"); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport")); } } catch(Exception e) { Logger.logError("Exception while exporting a report.", e); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); } if(! errors.isEmpty()) { saveErrors(request, errors); } return mapping.findForward("error"); } } |