0001: package org.tigris.scarab.actions;
0002:
0003: /* ================================================================
0004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
0005: *
0006: * Redistribution and use in source and binary forms, with or without
0007: * modification, are permitted provided that the following conditions are
0008: * met:
0009: *
0010: * 1. Redistributions of source code must retain the above copyright
0011: * notice, this list of conditions and the following disclaimer.
0012: *
0013: * 2. Redistributions in binary form must reproduce the above copyright
0014: * notice, this list of conditions and the following disclaimer in the
0015: * documentation and/or other materials provided with the distribution.
0016: *
0017: * 3. The end-user documentation included with the redistribution, if
0018: * any, must include the following acknowlegement: "This product includes
0019: * software developed by Collab.Net <http://www.Collab.Net/>."
0020: * Alternately, this acknowlegement may appear in the software itself, if
0021: * and wherever such third-party acknowlegements normally appear.
0022: *
0023: * 4. The hosted project names must not be used to endorse or promote
0024: * products derived from this software without prior written
0025: * permission. For written permission, please contact info@collab.net.
0026: *
0027: * 5. Products derived from this software may not use the "Tigris" or
0028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
0029: * prior written permission of Collab.Net.
0030: *
0031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
0035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
0037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
0039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
0040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0042: *
0043: * ====================================================================
0044: *
0045: * This software consists of voluntary contributions made by many
0046: * individuals on behalf of Collab.Net.
0047: */
0048:
0049: import java.util.ArrayList;
0050: import java.util.Calendar;
0051: import java.util.HashMap;
0052: import java.util.Iterator;
0053: import java.util.List;
0054: import java.util.Map;
0055:
0056: import org.apache.commons.collections.MapIterator;
0057: import org.apache.commons.collections.map.LinkedMap;
0058: import org.apache.commons.lang.StringUtils;
0059: import org.apache.fulcrum.intake.Intake;
0060: import org.apache.fulcrum.intake.model.Group;
0061: import org.apache.fulcrum.parser.ParameterParser;
0062: import org.apache.torque.om.NumberKey;
0063: import org.apache.turbine.RunData;
0064: import org.apache.turbine.TemplateContext;
0065: import org.tigris.scarab.actions.base.RequireLoginFirstAction;
0066: import org.tigris.scarab.om.AttributeValue;
0067: import org.tigris.scarab.om.Report;
0068: import org.tigris.scarab.om.ReportManager;
0069: import org.tigris.scarab.om.ReportPeer;
0070: import org.tigris.scarab.om.ScarabUser;
0071: import org.tigris.scarab.reports.ReportAxis;
0072: import org.tigris.scarab.reports.ReportBridge;
0073: import org.tigris.scarab.reports.ReportDate;
0074: import org.tigris.scarab.reports.ReportDefinition;
0075: import org.tigris.scarab.reports.ReportGroup;
0076: import org.tigris.scarab.reports.ReportHeading;
0077: import org.tigris.scarab.reports.ReportOptionAttribute;
0078: import org.tigris.scarab.reports.ReportUserAttribute;
0079: import org.tigris.scarab.tools.ScarabLocalizationTool;
0080: import org.tigris.scarab.tools.ScarabRequestTool;
0081: import org.tigris.scarab.tools.localization.L10NKeySet;
0082: import org.tigris.scarab.tools.localization.L10NMessage;
0083: import org.tigris.scarab.tools.localization.LocalizationKey;
0084: import org.tigris.scarab.util.ScarabConstants;
0085: import org.tigris.scarab.util.ScarabUtil;
0086: import org.tigris.scarab.util.export.ExportFormat;
0087: import org.tigris.scarab.util.word.IssueSearch;
0088: import org.tigris.scarab.util.word.IssueSearchFactory;
0089:
0090: /**
0091: * This class is responsible for report generation forms
0092: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
0093: * @version $Id: ConfigureReport.java 10379 2006-12-11 00:37:12Z dabbous $
0094: */
0095: public class ConfigureReport extends RequireLoginFirstAction {
0096: static final String NO_PERMISSION_MESSAGE = "NoPermissionToEditReport";
0097:
0098: private static final String ADD_USER = "add_user";
0099: private static final String SELECTED_USER = "select_user";
0100:
0101: ScarabLocalizationTool l10n;
0102: ScarabRequestTool scarabR;
0103: ReportBridge report;
0104: Intake intake;
0105: ParameterParser params;
0106: ScarabUser user;
0107:
0108: private void setup(RunData data, TemplateContext context)
0109: throws Exception {
0110: l10n = getLocalizationTool(context);
0111: scarabR = getScarabRequestTool(context);
0112: report = scarabR.getReport();
0113: intake = getIntakeTool(context);
0114: params = data.getParameters();
0115: user = (ScarabUser) data.getUser();
0116: }
0117:
0118: public void doSaveinfo(RunData data, TemplateContext context)
0119: throws Exception {
0120: setup(data, context);
0121:
0122: if (!report.isEditable((ScarabUser) data.getUser())) {
0123: setNoPermissionMessage();
0124: setTarget(data, "reports,ReportList.vm");
0125: } else if (intake.isAllValid()) {
0126: Group intakeReport = getIntakeReportGroup(intake, report);
0127: if (intakeReport != null) {
0128: intakeReport.setValidProperties(report);
0129:
0130: if (report.isReadyForCalculation()) {
0131: String msg = report.isNew() ? l10n
0132: .get(L10NKeySet.ReportUpdated) : l10n
0133: .get(L10NKeySet.ReportUpdatedNotSaved);
0134: scarabR.setConfirmMessage(msg);
0135: setTarget(data, "reports,Info.vm");
0136: } else {
0137: String msg = report.isNew() ? l10n
0138: .get("ReportUpdatedPleaseAddRowAndColumnCriteria")
0139: : l10n
0140: .get("ReportUpdatedNotSavedPleaseAddRowAndColumnCriteria");
0141: scarabR.setConfirmMessage(msg);
0142: setTarget(data, "reports,AxisConfiguration.vm");
0143: }
0144: } else {
0145: // FIXME! i don't know that the intakeReport should ever be
0146: // null, but since the conditional was here, don't fail silently
0147: scarabR
0148: .setAlertMessage(L10NKeySet.ThisShouldNotHappenPleaseContactAdmin);
0149: setTarget(data, "reports,Info.vm");
0150: }
0151: } else {
0152: scarabR.setAlertMessage(L10NKeySet.InvalidData);
0153: setTarget(data, "reports,Info.vm");
0154: }
0155: }
0156:
0157: public void doSelectheading(RunData data, TemplateContext context)
0158: throws Exception {
0159: setup(data, context);
0160: // the form will carry over the selected heading. just make sure
0161: // to remove old intake data
0162:
0163: intake.removeAll();
0164: // give the user a message if they are already on the selected
0165: // heading, in the event they are confused.
0166:
0167: int level = params.getInt("heading", -1);
0168: int prevLevel = params.getInt("prevheading", -2);
0169: if (level == prevLevel) {
0170: getScarabRequestTool(context).setInfoMessage(
0171: getLocalizationTool(context).format(
0172: "AlreadyEditingSelectedHeading",
0173: new Integer(level + 1)));
0174: }
0175: }
0176:
0177: public void doSettype(RunData data, TemplateContext context)
0178: throws Exception {
0179: setup(data, context);
0180: int axis = params.getInt("axis", 0); // 0=row; 1=column
0181: int level = params.getInt("heading", -1);
0182: int type = params.getInt("headingtype", 0);
0183:
0184: // remove any old data
0185: if (level != -1) {
0186:
0187: ReportDefinition reportDefn = report.getReportDefinition();
0188: ReportAxis reportAxis = reportDefn.getAxis(axis);
0189: ReportHeading heading = reportAxis.getHeading(level);
0190: int currentType = heading.calculateType();
0191: if (type != currentType) {
0192: if (currentType == 2
0193: && !reportDefn.allowMoreHeadings(reportAxis)) {
0194: scarabR
0195: .setAlertMessage(L10NKeySet.ThisAxisMustBeDatesUnlessHeadingsAreRemoved);
0196: params.setString("headingtype", "2");
0197: } else {
0198: LocalizationKey msg;
0199: if (heading.size() > 0) {
0200: heading.reset();
0201: msg = L10NKeySet.HeadingTypeChangedOldDataDiscarded;
0202: } else {
0203: msg = L10NKeySet.HeadingTypeChanged;
0204: }
0205: scarabR.setConfirmMessage(msg);
0206: }
0207: }
0208: }
0209: }
0210:
0211: public void doAddoptions(RunData data, TemplateContext context)
0212: throws Exception {
0213: setup(data, context);
0214: if (!report.isEditable((ScarabUser) data.getUser())) {
0215: setNoPermissionMessage();
0216: setTarget(data, "reports,ReportList.vm");
0217: } else if (intake.isAllValid()) {
0218:
0219: int axis = params.getInt("axis", 0); // 0=row; 1=column
0220: int level = params.getInt("heading", -1);
0221: int type = params.getInt("headingtype", 0);
0222:
0223: // we are using an IssueSearch object to gather the data to
0224: // create the ReportOptionAttribute objects.
0225: IssueSearch search = null;
0226:
0227: try {
0228: search = scarabR.getNewSearch();
0229: // Set intake properties
0230: //Group searchGroup = intake.get("SearchIssue", search.getQueryKey());
0231: //searchGroup.setProperties(search);
0232:
0233: // Set attribute values to search on
0234: LinkedMap avMap = search.getCommonAttributeValuesMap();
0235: for (MapIterator i = avMap.mapIterator(); i.hasNext();) {
0236: i.next();
0237: AttributeValue aval = (AttributeValue) i.getValue();
0238: Group group = intake.get("AttributeValue", aval
0239: .getQueryKey());
0240: if (group != null) {
0241: group.setProperties(aval);
0242: }
0243: }
0244:
0245: // remove unset AttributeValues
0246: List setAttValues = removeUnsetValues(search
0247: .getAttributeValues());
0248:
0249: ReportHeading heading = report.getReportDefinition()
0250: .getAxis(axis).getHeading(level);
0251: if (type != heading.calculateType()) {
0252: scarabR
0253: .setAlertMessage(L10NKeySet.ChangeOfTypeMessage);
0254: }
0255:
0256: // we are going to delete the old heading data and reconstruct it
0257: // so if there is any group info, we need to get it first
0258: List groups = heading.getReportGroups();
0259: Map optionGroupMap = null;
0260: if (groups != null && !groups.isEmpty()) {
0261: optionGroupMap = new HashMap();
0262: for (Iterator i = groups.iterator(); i.hasNext();) {
0263: ReportGroup group = (ReportGroup) i.next();
0264: List options = group
0265: .getReportOptionAttributes();
0266: if (options != null && !options.isEmpty()) {
0267: for (Iterator j = options.iterator(); j
0268: .hasNext();) {
0269: optionGroupMap.put(j.next(), group);
0270: }
0271: }
0272: }
0273: }
0274: heading.reset();
0275:
0276: //convert to ReportOptionAttributes
0277: for (Iterator i = setAttValues.iterator(); i.hasNext();) {
0278: AttributeValue av = (AttributeValue) i.next();
0279: //pull any chained values out to create a flat list
0280: List chainedValues = av.getValueList();
0281: for (Iterator j = chainedValues.iterator(); j
0282: .hasNext();) {
0283: ReportOptionAttribute roa = new ReportOptionAttribute();
0284: Integer id = Integer
0285: .valueOf(((AttributeValue) j.next())
0286: .getOptionId().toString());
0287: roa.setOptionId(id);
0288:
0289: if (optionGroupMap == null) {
0290: heading.addReportOptionAttribute(roa);
0291: } else {
0292: ReportGroup group = (ReportGroup) optionGroupMap
0293: .get(roa);
0294: if (group == null) {
0295: // add it to the first group
0296: ((ReportGroup) groups.get(0))
0297: .addReportOptionAttribute(roa);
0298: } else {
0299: group.addReportOptionAttribute(roa);
0300: }
0301: }
0302: }
0303: }
0304: if (level == -1) {
0305: params.setString("heading", "0");
0306: }
0307:
0308: String msg = getLocalizedHeadingConfirmMessage(report,
0309: l10n);
0310: scarabR.setConfirmMessage(msg);
0311:
0312: } catch (Exception e) {
0313: throw e;
0314: } finally {
0315: if (search != null) {
0316: IssueSearchFactory.INSTANCE.notifyDone();
0317: }
0318: }
0319:
0320: /*
0321: //testing
0322: java.io.FileWriter fw = new java.io.FileWriter("/tmp/Report.xml");
0323: BeanWriter bw = new BeanWriter(fw);
0324: bw.writeXmlDeclaration("<?xml version='1.0' encoding='UTF-8' ?>");
0325: bw.write(report.getReportDefinition());
0326: bw.flush();
0327: bw.close();
0328: */
0329: }
0330: }
0331:
0332: private static String getLocalizedHeadingConfirmMessage(
0333: ReportBridge report, ScarabLocalizationTool l10n) {
0334: LocalizationKey key;
0335: if (report.isReadyForCalculation()) {
0336: key = report.isNew() ? L10NKeySet.ReportUpdatedDoMoreOrCalculate
0337: : L10NKeySet.ReportUpdatedNotSavedDoMoreOrCalculate;
0338: } else {
0339: key = report.isNew() ? L10NKeySet.ReportUpdatedDoMore
0340: : L10NKeySet.ReportUpdatedNotSavedDoMore;
0341: }
0342: L10NMessage msg = new L10NMessage(key);
0343: return msg.getMessage(l10n);
0344: }
0345:
0346: /**
0347: * remove unset AttributeValues. this method is c/p from IssueSearch
0348: *
0349: * @param attValues a <code>List</code> value
0350: */
0351: private List removeUnsetValues(List attValues) {
0352: int size = attValues.size();
0353: List setAVs = new ArrayList(size);
0354: for (int i = 0; i < size; i++) {
0355: AttributeValue attVal = (AttributeValue) attValues.get(i);
0356: if (attVal.getOptionId() != null
0357: || attVal.getValue() != null
0358: || attVal.getUserId() != null) {
0359: setAVs.add(attVal);
0360: }
0361: }
0362: return setAVs;
0363: }
0364:
0365: /**
0366: * Adds users to the current header.
0367: */
0368: public void doAddusers(RunData data, TemplateContext context)
0369: throws Exception {
0370: setup(data, context);
0371: if (!report.isEditable(user)) {
0372: setNoPermissionMessage();
0373: setTarget(data, "reports,ReportList.vm");
0374: return;
0375: }
0376:
0377: int axis = params.getInt("axis", 0); // 0=row; 1=column
0378: int level = params.getInt("heading", -1);
0379: int type = params.getInt("headingtype", 0);
0380:
0381: // not too elegant for editing, but if groups have been defined
0382: // we remove them for now until the rest of the reports is
0383: // more code complete !FIXME!
0384: ReportHeading heading = report.getReportDefinition().getAxis(
0385: axis).getHeading(level);
0386: if (type != heading.calculateType()
0387: || heading.getReportGroups() != null) {
0388: if (heading.getReportGroups() != null) {
0389: scarabR
0390: .setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
0391: }
0392: heading.reset();
0393: }
0394:
0395: String[] userIds = params.getStrings(ADD_USER);
0396: if (userIds != null && userIds.length > 0) {
0397: for (int i = 0; i < userIds.length; i++) {
0398: String userId = userIds[i];
0399: ReportUserAttribute rua = new ReportUserAttribute();
0400: rua.setUserId(new Integer(userId));
0401: rua.setAttributeId(new Integer(params
0402: .getString("user_attr_" + userId)));
0403: heading.addReportUserAttribute(rua);
0404: }
0405: if (level == -1) {
0406: params.setString("heading", "0");
0407: }
0408:
0409: String msg = getLocalizedHeadingConfirmMessage(report, l10n);
0410: scarabR.setConfirmMessage(msg);
0411: } else {
0412: scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
0413: }
0414: }
0415:
0416: /**
0417: * Removes users from temporary working list.
0418: */
0419: public void doRemoveusers(RunData data, TemplateContext context)
0420: throws Exception {
0421: setup(data, context);
0422: if (!report.isEditable(user)) {
0423: setNoPermissionMessage();
0424: setTarget(data, "reports,ReportList.vm");
0425: return;
0426: }
0427:
0428: int axis = params.getInt("axis", 0); // 0=row; 1=column
0429: int level = params.getInt("heading", -1);
0430: int type = params.getInt("headingtype", 0);
0431:
0432: // not too elegant for editing, but if groups have been defined
0433: // we remove them for now until the rest of the reports is
0434: // more code complete !FIXME!
0435: ReportHeading heading = report.getReportDefinition().getAxis(
0436: axis).getHeading(level);
0437: if (type != heading.calculateType()
0438: || heading.getReportGroups() != null) {
0439: heading.reset();
0440: scarabR
0441: .setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
0442: } else {
0443: String[] userIds = params.getStrings(SELECTED_USER);
0444: if (userIds != null && userIds.length > 0) {
0445: for (int i = 0; i < userIds.length; i++) {
0446: String userId = userIds[i];
0447: ReportUserAttribute rua = new ReportUserAttribute();
0448: rua.setUserId(new Integer(userId));
0449: rua.setAttributeId(new Integer(params
0450: .getString("old_attr_" + userId)));
0451:
0452: List ruas = heading.getReportUserAttributes();
0453: if (ruas != null) {
0454: ruas.remove(rua);
0455: }
0456: }
0457: scarabR
0458: .setConfirmMessage(L10NKeySet.SelectedUsersWereRemoved);
0459: } else {
0460: scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
0461: }
0462: }
0463: }
0464:
0465: /**
0466: * Changes the user attribute a user is associated with.
0467: */
0468: public void doUpdateusers(RunData data, TemplateContext context)
0469: throws Exception {
0470:
0471: setup(data, context);
0472: if (!report.isEditable(user)) {
0473: setNoPermissionMessage();
0474: setTarget(data, "reports,ReportList.vm");
0475: return;
0476: }
0477:
0478: int axis = params.getInt("axis", 0); // 0=row; 1=column
0479: int level = params.getInt("heading", -1);
0480: int type = params.getInt("headingtype", 0);
0481:
0482: // not too elegant for editing, but if groups have been defined
0483: // we remove them for now until the rest of the reports is
0484: // more code complete !FIXME!
0485: ReportHeading heading = report.getReportDefinition().getAxis(
0486: axis).getHeading(level);
0487: if (type != heading.calculateType()
0488: || heading.getReportGroups() != null) {
0489: heading.reset();
0490: scarabR
0491: .setAlertMessage(L10NKeySet.CouldNotMakeRequestedChange);
0492: } else {
0493: String[] userIds = params.getStrings(SELECTED_USER);
0494: if (userIds != null && userIds.length > 0) {
0495: for (int i = 0; i < userIds.length; i++) {
0496: String userId = userIds[i];
0497: ReportUserAttribute rua = new ReportUserAttribute();
0498: rua.setUserId(new Integer(userId));
0499: rua.setAttributeId(new Integer(params
0500: .getString("old_attr_" + userId)));
0501:
0502: List ruas = heading.getReportUserAttributes();
0503: if (ruas != null) {
0504: /* this will make it hard to find dupes
0505: for (Iterator i=ruas.iterator(); i.hasNext();)
0506: {
0507: Object obj = i.next();
0508: if (obj.equals(rua))
0509: {
0510: ((ReportUserAttribute)obj).setAttributeId();
0511: }
0512: }
0513: */
0514: ruas.remove(rua);
0515: }
0516:
0517: rua = new ReportUserAttribute();
0518: rua.setUserId(new Integer(userId));
0519: rua.setAttributeId(new Integer(params
0520: .getString("asso_user_{" + userId + "}")));
0521: heading.addReportUserAttribute(rua);
0522: }
0523: scarabR
0524: .setConfirmMessage(L10NKeySet.SelectedUsersWereModified);
0525: } else {
0526: scarabR.setAlertMessage(L10NKeySet.NoUsersSelected);
0527: }
0528: }
0529: }
0530:
0531: /**
0532: * Changes the user attribute a user is associated with.
0533: */
0534: public void doRemoveheading(RunData data, TemplateContext context)
0535: throws Exception {
0536: setup(data, context);
0537: if (!report.isEditable(user)) {
0538: setNoPermissionMessage();
0539: setTarget(data, "reports,ReportList.vm");
0540: return;
0541: }
0542:
0543: int axis = params.getInt("axis", 0); // 0=row; 1=column
0544: int level = params.getInt("heading", -1);
0545: //int type = params.getInt("headingtype", 0);
0546:
0547: if (level >= 0) {
0548: List headings = report.getReportDefinition().getAxis(axis)
0549: .getReportHeadings();
0550: headings.remove(level);
0551: scarabR.setConfirmMessage(L10NKeySet.HeadingRemoved);
0552: } else {
0553: scarabR.setAlertMessage(L10NKeySet.NoHeadingSelected);
0554: }
0555: }
0556:
0557: /**
0558: * Redirects to screen to group the options/users in the selected
0559: * heading.
0560: */
0561: public void doGotoeditgroups(RunData data, TemplateContext context)
0562: throws Exception {
0563: setup(data, context);
0564:
0565: int level = params.getInt("heading", -1);
0566:
0567: // user groups is not implemented
0568: if (level >= 0) {
0569: int axis = params.getInt("axis", 0); // 0=row; 1=column
0570: ReportHeading heading = (ReportHeading) report
0571: .getReportDefinition().getAxis(axis)
0572: .getReportHeadings().get(level);
0573: if (heading.calculateType() == 0) {
0574: setTarget(data, "reports,EditGroups.vm");
0575: } else {
0576: scarabR.setAlertMessage(getLocalizationTool(context)
0577: .get("GroupsAreForOptionsOnly"));
0578: }
0579: } else {
0580: scarabR.setAlertMessage(L10NKeySet.NoHeadingSelected);
0581: }
0582: }
0583:
0584: /**
0585: *
0586: */
0587: public void doAddheading(RunData data, TemplateContext context)
0588: throws Exception {
0589: setup(data, context);
0590: if (!report.isEditable(user)) {
0591: setNoPermissionMessage();
0592: setTarget(data, "reports,ReportList.vm");
0593: return;
0594: }
0595:
0596: int axisIndex = params.getInt("axis", 0); // 0=row; 1=column
0597: ReportAxis axis = report.getReportDefinition().getAxis(
0598: axisIndex);
0599: axis.addReportHeading(new ReportHeading());
0600: params.setString("heading", String.valueOf(axis
0601: .getReportHeadings().size() - 1));
0602: // remove old intake data
0603: Intake intake = getIntakeTool(context);
0604: intake.removeAll();
0605: scarabR.setConfirmMessage(L10NKeySet.HeadingAddedNowAddContent);
0606: }
0607:
0608: public void doAddgroup(RunData data, TemplateContext context)
0609: throws Exception {
0610: setup(data, context);
0611: if (!report.isEditable(user)) {
0612: setNoPermissionMessage();
0613: setTarget(data, "reports,ReportList.vm");
0614: return;
0615: }
0616:
0617: int axis = params.getInt("axis", 0); // 0=row; 1=column
0618: int level = params.getInt("heading", -1);
0619:
0620: String name = params.getString("groupname_new");
0621: if (name != null) {
0622: name = name.trim();
0623: }
0624:
0625: if (name == null || name.length() == 0) {
0626: scarabR.setAlertMessage(L10NKeySet.InvalidGroupName);
0627: } else {
0628: ReportHeading heading = report.getReportDefinition()
0629: .getAxis(axis).getHeading(level);
0630: ReportGroup group = new ReportGroup();
0631: group.setName(name);
0632: // make sure we are not adding a new group with a non-unique name
0633: List groups = heading.getReportGroups();
0634: if (groups != null && groups.contains(group)) {
0635: scarabR.setAlertMessage(L10NKeySet.DuplicateGroupName);
0636: } else {
0637: heading.addReportGroup(group);
0638:
0639: params.remove("groupname_new");
0640: //params.setString("groupname_" + index, group.getName());
0641: scarabR.setConfirmMessage(L10NKeySet.GroupAdded);
0642: }
0643:
0644: /* intake way
0645: UIReportGroup group = new UIReportGroup();
0646: Group intakeGroup = intake.get("UIReportGroup",
0647: group.getQueryKey(), false);
0648: if (intakeGroup != null)
0649: {
0650: intakeGroup.setProperties(group);
0651: if (group.getDisplayValue() != null
0652: && group.getDisplayValue().length() > 0)
0653: {
0654: ReportHeading heading = report.getReportDefinition()
0655: .getAxis(axis).getHeading(level);
0656: ReportGroup rgroup = new ReportGroup();
0657: rgroup.setName(group.getDisplayValue());
0658: heading.addReportGroup(rgroup);
0659: }
0660: }
0661: */
0662: }
0663: }
0664:
0665: public void doDeletegroup(RunData data, TemplateContext context)
0666: throws Exception {
0667: setup(data, context);
0668: if (!report.isEditable(user)) {
0669: setNoPermissionMessage();
0670: setTarget(data, "reports,ReportList.vm");
0671: return;
0672: }
0673:
0674: String[] groupIndices = params.getStrings("selectgroup");
0675: if (groupIndices == null || groupIndices.length == 0) {
0676: scarabR.setAlertMessage(L10NKeySet.NoGroupSelected);
0677: } else {
0678: int axis = params.getInt("axis", 0); // 0=row; 1=column
0679: int level = params.getInt("heading", -1);
0680: List reportGroups = report.getReportDefinition().getAxis(
0681: axis).getHeading(level).getReportGroups();
0682:
0683: for (int j = groupIndices.length - 1; j >= 0; j--) {
0684: reportGroups.remove(Integer.parseInt(groupIndices[j]));
0685: }
0686: scarabR.setConfirmMessage(L10NKeySet.SelectedGroupDeleted);
0687:
0688: /* intake way
0689: for (int i=groups.size()-1; i>=0; i--)
0690: {
0691: ReportGroup rgroup = (ReportGroup)groups.get(i);
0692: UIReportGroup uirg = new UIReportGroup(rgroup.getName());
0693: Group intakeGroup = intake.get("UIReportGroup",
0694: uirg.getQueryKey(), false);
0695: if (intakeGroup != null)
0696: {
0697: intakeGroup.setProperties(uirg);
0698: if (uirg.isSelected())
0699: {
0700: groups.remove(i);
0701: }
0702: }
0703: }
0704: */
0705: }
0706: }
0707:
0708: public void doEditgroupname(RunData data, TemplateContext context)
0709: throws Exception {
0710: setup(data, context);
0711:
0712: if (!report.isEditable(user)) {
0713: setNoPermissionMessage();
0714: setTarget(data, "reports,ReportList.vm");
0715: return;
0716: }
0717:
0718: Object[] keys = params.getKeys();
0719: int axis = params.getInt("axis", 0); // 0=row; 1=column
0720: int level = params.getInt("heading", -1);
0721: List reportGroups = report.getReportDefinition().getAxis(axis)
0722: .getHeading(level).getReportGroups();
0723:
0724: for (int i = 0; i < keys.length; i++) {
0725: String key = keys[i].toString();
0726: if (key.startsWith("groupname_")
0727: && key.indexOf("new") == -1) {
0728: int index = Integer.parseInt(key.substring(key
0729: .indexOf("_") + 1, key.length()));
0730: ReportGroup group = (ReportGroup) reportGroups
0731: .get(index);
0732: String name = params.getString(key, "").trim();
0733: if (name.length() == 0) {
0734: scarabR
0735: .setAlertMessage(L10NKeySet.InvalidGroupName);
0736: } else {
0737: group.setName(name);
0738: }
0739: }
0740: }
0741: scarabR.setConfirmMessage(L10NKeySet.GroupsChanged);
0742: }
0743:
0744: public void doSavegroups(RunData data, TemplateContext context)
0745: throws Exception {
0746: setup(data, context);
0747:
0748: if (!report.isEditable(user)) {
0749: setNoPermissionMessage();
0750: setTarget(data, "reports,ReportList.vm");
0751: return;
0752: }
0753:
0754: int axis = params.getInt("axis", 0); // 0=row; 1=column
0755: int level = params.getInt("heading", -1);
0756: ReportHeading heading = report.getReportDefinition().getAxis(
0757: axis).getHeading(level);
0758: List reportGroups = heading.getReportGroups();
0759: // the form contains data to restore the groups and it is
0760: // easier to start from scratch but grab a copy of the option/users
0761: // before resetting them to null.
0762: List groupedAttributes = heading.retrieveGroupedAttributes();
0763: for (Iterator i = reportGroups.iterator(); i.hasNext();) {
0764: ReportGroup group = (ReportGroup) i.next();
0765: group.reset();
0766: }
0767:
0768: boolean success = true;
0769: if (heading.calculateType() == 0) {
0770: for (Iterator j = groupedAttributes.iterator(); j.hasNext();) {
0771: ReportOptionAttribute reportOption = (ReportOptionAttribute) j
0772: .next();
0773: String name = params.getString("option_"
0774: + reportOption.getOptionId());
0775: if (name == null || name.trim().length() == 0) {
0776: scarabR
0777: .setAlertMessage(L10NKeySet.InvalidGroupName);
0778: success = false;
0779: break;
0780: } else {
0781: for (Iterator i = reportGroups.iterator(); i
0782: .hasNext();) {
0783: ReportGroup group = (ReportGroup) i.next();
0784: if (name.equals(group.getName())) {
0785: group
0786: .addReportOptionAttribute(reportOption);
0787: }
0788: }
0789: }
0790: }
0791: } else {
0792: for (Iterator j = groupedAttributes.iterator(); j.hasNext();) {
0793: ReportUserAttribute reportUser = (ReportUserAttribute) j
0794: .next();
0795: String name = params.getString(new StringBuffer(20)
0796: .append("att_").append(
0797: reportUser.getAttributeId()).append(
0798: "user_").append(reportUser.getUserId())
0799: .toString());
0800: if (name == null || name.trim().length() == 0) {
0801: scarabR
0802: .setAlertMessage(L10NKeySet.InvalidGroupName);
0803: success = false;
0804: break;
0805: } else {
0806: for (Iterator i = reportGroups.iterator(); i
0807: .hasNext();) {
0808: ReportGroup group = (ReportGroup) i.next();
0809: if (name.equals(group.getName())) {
0810: group.addReportUserAttribute(reportUser);
0811: }
0812: }
0813: }
0814: }
0815: }
0816:
0817: if (success) {
0818: setTarget(data, "reports,AxisConfiguration.vm");
0819: }
0820: }
0821:
0822: public void doAdddate(RunData data, TemplateContext context)
0823: throws Exception {
0824: setup(data, context);
0825: if (!report.isEditable(user)) {
0826: setNoPermissionMessage();
0827: setTarget(data, "reports,ReportList.vm");
0828: return;
0829: }
0830:
0831: int axis = params.getInt("axis", 0); // 0=row; 1=column
0832: int level = params.getInt("heading", -1);
0833:
0834: ReportHeading heading = report.getReportDefinition().getAxis(
0835: axis).getHeading(level);
0836: // if level was -1, we have created a new level. So mark the new
0837: // level as the current one.
0838: params.setString("heading", "0");
0839:
0840: List dates = heading.getReportDates();
0841: int index = 1;
0842: if (dates == null) {
0843: // make sure the heading does not contain old option or user data
0844: heading.reset();
0845: } else {
0846: index = dates.size() + 1;
0847: }
0848: Calendar cal = scarabR.getCalendar();
0849: cal.set(Calendar.YEAR, params.getInt("y_" + index));
0850: cal.set(Calendar.MONTH, params.getInt("m_" + index) - 1);
0851: cal.set(Calendar.DAY_OF_MONTH, params.getInt("d_" + index));
0852: cal.set(Calendar.HOUR_OF_DAY, params.getInt("h_" + index));
0853: cal.set(Calendar.MINUTE, 0);
0854: cal.set(Calendar.SECOND, 0);
0855: ReportDate rdate = new ReportDate();
0856: rdate.setTime(cal.getTime().getTime());
0857: heading.addReportDate(rdate);
0858: scarabR.setConfirmMessage(L10NKeySet.DateAdded);
0859: }
0860:
0861: public void doDeletedate(RunData data, TemplateContext context)
0862: throws Exception {
0863: setup(data, context);
0864:
0865: if (!report.isEditable(user)) {
0866: setNoPermissionMessage();
0867: setTarget(data, "reports,ReportList.vm");
0868: return;
0869: }
0870:
0871: String[] dateIndices = params.getStrings("selectdate");
0872: if (dateIndices == null || dateIndices.length == 0) {
0873: scarabR.setAlertMessage(L10NKeySet.NoDateSelected);
0874: } else {
0875: int axis = params.getInt("axis", 0); // 0=row; 1=column
0876: int level = params.getInt("heading", -1);
0877: List reportDates = report.getReportDefinition().getAxis(
0878: axis).getHeading(level).getReportDates();
0879:
0880: for (int j = dateIndices.length - 1; j >= 0; j--) {
0881: reportDates
0882: .remove(Integer.parseInt(dateIndices[j]) - 1);
0883: }
0884: scarabR.setConfirmMessage(L10NKeySet.SelectedDateDeleted);
0885: }
0886: }
0887:
0888: public void doRedirecttocrossmodulelist(RunData data,
0889: TemplateContext context) throws Exception {
0890: setup(data, context);
0891: // x-module/issuetype works off of user's list, so make the report
0892: // list the current user's list.
0893: user.setCurrentMITList(report.getMITList());
0894: setTarget(data, "reports,XModuleList.vm");
0895: }
0896:
0897: public void doConfinedataset(RunData data, TemplateContext context)
0898: throws Exception {
0899: setup(data, context);
0900:
0901: if (!report.isEditable(user)) {
0902: setNoPermissionMessage();
0903: setTarget(data, "reports,ReportList.vm");
0904: return;
0905: }
0906:
0907: if ("fixed".equals(params.getString("def_date"))) {
0908: Calendar cal = scarabR.getCalendar();
0909: cal.set(Calendar.YEAR, params.getInt("def_yr"));
0910: cal.set(Calendar.MONTH, params.getInt("def_month") - 1);
0911: cal.set(Calendar.DAY_OF_MONTH, params.getInt("def_day"));
0912: cal.set(Calendar.HOUR_OF_DAY, params.getInt("def_hr"));
0913: cal.set(Calendar.MINUTE, 0);
0914: cal.set(Calendar.SECOND, 0);
0915: report.setDefaultDate(cal.getTime());
0916: } else {
0917: report.setDefaultDate(null);
0918: }
0919: ScarabLocalizationTool l10n = getLocalizationTool(context);
0920: scarabR.setConfirmMessage(L10NKeySet.ChangesSaved);
0921: setTarget(data, "reports,ConfineDataset.vm");
0922: }
0923:
0924: public void doSwaprowcol(RunData data, TemplateContext context)
0925: throws Exception {
0926: setup(data, context);
0927:
0928: if (!report.isEditable(user)) {
0929: setNoPermissionMessage();
0930: setTarget(data, "reports,ReportList.vm");
0931: return;
0932: }
0933:
0934: ReportDefinition reportDefn = report.getReportDefinition();
0935: List axes = reportDefn.getReportAxisList();
0936: if (axes != null && !axes.isEmpty()) {
0937: if (axes.size() == 1) {
0938: reportDefn.addReportAxis(new ReportAxis());
0939: }
0940: ReportAxis row = (ReportAxis) axes.remove(0);
0941: // add back as column
0942: reportDefn.addReportAxis(row);
0943: }
0944:
0945: // FIXME: do we need a confirmation message? -jon
0946: }
0947:
0948: public void doVerifyreport(RunData data, TemplateContext context)
0949: throws Exception {
0950: setup(data, context);
0951: if (report.removeStaleDefinitions()) {
0952: scarabR
0953: .setInfoMessage(l10n
0954: .get("ReportDefinitionModified"));
0955: }
0956: }
0957:
0958: public void doGeneratereport(RunData data, TemplateContext context)
0959: throws Exception {
0960: setup(data, context);
0961:
0962: if (report.removeStaleDefinitions()) {
0963: scarabR
0964: .setInfoMessage(l10n
0965: .get("ReportDefinitionModified"));
0966: } else if (report.getReportDefinition()
0967: .reportQueryIsExpensive()) {
0968: scarabR.setAlertMessage(l10n.format("ReportIsTooExpensive",
0969: String.valueOf(report.getReportDefinition()
0970: .maximumHeadings())));
0971: } else {
0972: // Determine the report display format, looking first at the
0973: // request parameters.
0974: String format = ScarabUtil.findValue(data,
0975: ExportFormat.KEY_NAME);
0976: if (StringUtils.isEmpty(format)) {
0977: // Next, examine the request for the Intake parameter
0978: // (which we'll allow to override a persisted pref).
0979: Group intakeReport = getIntakeReportGroup(
0980: getIntakeTool(context), report);
0981: // Exports from the "Report output" screen have a null
0982: // Intake Group.
0983: if (intakeReport != null) {
0984: format = (String) intakeReport.get("Format")
0985: .getValue();
0986: }
0987:
0988: if (format == null) {
0989: // Lastly, look at the persisted report format.
0990: format = report.getFormat();
0991: }
0992: }
0993:
0994: if (ExportFormat.EXCEL_FORMAT.equalsIgnoreCase(format)
0995: || ExportFormat.TSV_FORMAT.equalsIgnoreCase(format)) {
0996: // The ReportExport screen has no corresponding template.
0997: data.getParameters().setString(ExportFormat.KEY_NAME,
0998: format);
0999: setTarget(data, "ReportExport.vm");
1000: } else {
1001: setTarget(data, "reports,Report_1.vm");
1002: }
1003:
1004: }
1005: }
1006:
1007: public void doCreatenew(RunData data, TemplateContext context)
1008: throws Exception {
1009: String key = data.getParameters().getString(
1010: ScarabConstants.CURRENT_REPORT);
1011: data.getParameters().remove(ScarabConstants.CURRENT_REPORT);
1012: ScarabUser user = (ScarabUser) data.getUser();
1013: if (key != null && key.length() > 0) {
1014: user.setCurrentReport(key, null);
1015: }
1016: if (user.getCurrentMITList() == null) {
1017: //context.add("report", Boolean.TRUE);
1018: setTarget(data, "reports,XModuleList.vm");
1019: } else {
1020: setTarget(data, "reports,Info.vm");
1021: }
1022: }
1023:
1024: public void doSavereport(RunData data, TemplateContext context)
1025: throws Exception {
1026: setup(data, context);
1027:
1028: if (!report.isSavable((ScarabUser) data.getUser())) {
1029: setNoPermissionMessage();
1030: setTarget(data, "reports,ReportList.vm");
1031: } else if (intake.isAllValid()) {
1032: // make sure report has a name
1033: if (report.getName() == null
1034: || report.getName().trim().length() == 0) {
1035: Group intakeReport = getIntakeReportGroup(intake,
1036: report);
1037: if (intakeReport != null) {
1038: intakeReport.setValidProperties(report);
1039: }
1040: }
1041:
1042: if (report.getName() == null
1043: || report.getName().trim().length() == 0) {
1044: scarabR
1045: .setAlertMessage(L10NKeySet.SavedReportsMustHaveName);
1046: setTarget(data, "reports,Info.vm");
1047: } else {
1048: //don't save extra whitespace as part of name.
1049: String name = report.getName().trim();
1050: report.setName(name);
1051: // make sure name is unique, mysql text queries are
1052: // case-insensitive, otherwise we may need to do this
1053: // differently to avoid similar but not exact matches.
1054: org.tigris.scarab.om.Report savedReport = ReportPeer
1055: .retrieveByName(name);
1056: if (savedReport == null
1057: || savedReport.getQueryKey().equals(
1058: report.getQueryKey())) {
1059: report.save();
1060: scarabR.setConfirmMessage(L10NKeySet.ReportSaved);
1061: } else {
1062: scarabR
1063: .setAlertMessage(L10NKeySet.ReportNameNotUnique);
1064: setTarget(data, "reports,Info.vm");
1065: }
1066: }
1067: } else {
1068: scarabR
1069: .setAlertMessage(L10NKeySet.ErrorPreventedSavingReport);
1070: }
1071: }
1072:
1073: public void doDeletestoredreport(RunData data,
1074: TemplateContext context) throws Exception {
1075: setup(data, context);
1076: ScarabUser user = (ScarabUser) data.getUser();
1077: String[] reportIds = data.getParameters().getStrings(
1078: "report_id");
1079: if (reportIds == null || reportIds.length == 0) {
1080: scarabR.setAlertMessage(l10n.get("MustSelectReport"));
1081: } else {
1082: for (int i = 0; i < reportIds.length; i++) {
1083: String reportId = reportIds[i];
1084: if (reportId != null && reportId.length() > 0) {
1085: Report torqueReport = ReportManager.getInstance(
1086: new NumberKey(reportId), false);
1087: if (new ReportBridge(torqueReport)
1088: .isDeletable(user)) {
1089: torqueReport.setDeleted(true);
1090: torqueReport.save();
1091: } else {
1092: setNoPermissionMessage();
1093: }
1094: }
1095: }
1096: }
1097: }
1098:
1099: private void setNoPermissionMessage() {
1100: if (scarabR == null | l10n == null) {
1101: throw new RuntimeException(
1102: "setup() method not called for action event");
1103: }
1104: scarabR.setAlertMessage(l10n.get(NO_PERMISSION_MESSAGE));
1105: }
1106:
1107: /**
1108: * @param intake The instance of Intake used for lookup.
1109: * @param report A specific report.
1110: * @return The <code>Report</code> group.
1111: */
1112: private Group getIntakeReportGroup(Intake intake,
1113: ReportBridge report) throws Exception {
1114: Group intakeReport = intake.get("Report", report.getQueryKey(),
1115: false);
1116: if (intakeReport == null) {
1117: intakeReport = intake.get("Report", "", false);
1118: }
1119: return intakeReport;
1120: }
1121: }
|