001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.model;
015:
016: import org.gjt.xpp.XmlPullNode;
017: import org.jetbrains.annotations.NotNull;
018: import org.pentaho.reportdesigner.crm.report.PropertyKeys;
019: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
020: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
021: import org.pentaho.reportdesigner.lib.common.xml.XMLExternalizable;
022: import org.pentaho.reportdesigner.lib.common.xml.XMLUtils;
023: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
024:
025: import java.io.IOException;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028:
029: /**
030: * User: Martin
031: * Date: 17.02.2007
032: * Time: 15:19:26
033: */
034: public class SubReportParameters implements XMLExternalizable {
035: private boolean globalImport;
036: private boolean globalExport;
037: @NotNull
038: private HashMap<String, SubReportParameter> importParameters;
039: @NotNull
040: private HashMap<String, SubReportParameter> exportParameters;
041:
042: public SubReportParameters() {
043: importParameters = new HashMap<String, SubReportParameter>();
044: exportParameters = new HashMap<String, SubReportParameter>();
045: }
046:
047: public SubReportParameters(boolean globalImport, @NotNull
048: ArrayList<SubReportParameter> importParameterList,
049: boolean globalExport, @NotNull
050: ArrayList<SubReportParameter> exportParameterList) {
051: //noinspection ConstantConditions
052: if (importParameterList == null) {
053: throw new IllegalArgumentException(
054: "importParameterList must not be null");
055: }
056: //noinspection ConstantConditions
057: if (exportParameterList == null) {
058: throw new IllegalArgumentException(
059: "exportParameterList must not be null");
060: }
061:
062: this .globalExport = globalExport;
063: this .globalImport = globalImport;
064: importParameters = new HashMap<String, SubReportParameter>();
065: for (SubReportParameter subReportParameter : importParameterList) {
066: importParameters.put(subReportParameter.getKey(),
067: subReportParameter);
068: }
069: exportParameters = new HashMap<String, SubReportParameter>();
070: for (SubReportParameter subReportParameter : exportParameterList) {
071: exportParameters.put(subReportParameter.getKey(),
072: subReportParameter);
073: }
074: }
075:
076: private SubReportParameters(@NotNull
077: HashMap<String, SubReportParameter> importParameters, @NotNull
078: HashMap<String, SubReportParameter> exportParameters,
079: boolean globalImport, boolean globalExport) {
080: //noinspection ConstantConditions
081: if (importParameters == null) {
082: throw new IllegalArgumentException(
083: "importParameters must not be null");
084: }
085: //noinspection ConstantConditions
086: if (exportParameters == null) {
087: throw new IllegalArgumentException(
088: "exportParameters must not be null");
089: }
090: this .exportParameters = exportParameters;
091: this .globalExport = globalExport;
092: this .globalImport = globalImport;
093: this .importParameters = importParameters;
094: }
095:
096: @NotNull
097: public HashMap<String, SubReportParameter> getExportParameters() {
098: return new HashMap<String, SubReportParameter>(exportParameters);
099: }
100:
101: @NotNull
102: public SubReportParameters addExportParameters(@NotNull
103: SubReportParameter parameter) {
104: //noinspection ConstantConditions
105: if (parameter == null) {
106: throw new IllegalArgumentException(
107: "parameter must not be null");
108: }
109: HashMap<String, SubReportParameter> parameters = getImportParameters();
110: parameters.put(parameter.getKey(), parameter);
111: return new SubReportParameters(importParameters, parameters,
112: globalImport, globalExport);
113: }
114:
115: public boolean isGlobalExport() {
116: return globalExport;
117: }
118:
119: @NotNull
120: public SubReportParameters setGlobalExport(boolean globalExport) {
121: return new SubReportParameters(importParameters,
122: exportParameters, globalImport, globalExport);
123: }
124:
125: public boolean isGlobalImport() {
126: return globalImport;
127: }
128:
129: @NotNull
130: public SubReportParameters setGlobalImport(boolean globalImport) {
131: return new SubReportParameters(importParameters,
132: exportParameters, globalImport, globalExport);
133: }
134:
135: @NotNull
136: public HashMap<String, SubReportParameter> getImportParameters() {
137: return new HashMap<String, SubReportParameter>(importParameters);
138: }
139:
140: @NotNull
141: public SubReportParameters addImportParameter(@NotNull
142: SubReportParameter parameter) {
143: //noinspection ConstantConditions
144: if (parameter == null) {
145: throw new IllegalArgumentException(
146: "parameter must not be null");
147: }
148: HashMap<String, SubReportParameter> parameters = getImportParameters();
149: parameters.put(parameter.getKey(), parameter);
150: return new SubReportParameters(parameters, exportParameters,
151: globalImport, globalExport);
152: }
153:
154: public void externalizeObject(@NotNull
155: XMLWriter xmlWriter, @NotNull
156: XMLContext xmlContext) throws IOException {
157: xmlWriter.writeProperty(PropertyKeys.GLOBAL_IMPORT, Boolean
158: .toString(globalImport));
159: xmlWriter.writeProperty(PropertyKeys.GLOBAL_EXPORT, Boolean
160: .toString(globalExport));
161:
162: for (SubReportParameter styleExpression : importParameters
163: .values()) {
164: xmlWriter.startElement(PropertyKeys.IMPORT_PARAMETER);
165: styleExpression.externalizeObject(xmlWriter, xmlContext);
166: xmlWriter.closeElement(PropertyKeys.IMPORT_PARAMETER);
167: }
168:
169: for (SubReportParameter styleExpression : exportParameters
170: .values()) {
171: xmlWriter.startElement(PropertyKeys.EXPORT_PARAMETER);
172: styleExpression.externalizeObject(xmlWriter, xmlContext);
173: xmlWriter.closeElement(PropertyKeys.EXPORT_PARAMETER);
174: }
175: }
176:
177: public void readObject(@NotNull
178: XmlPullNode node, @NotNull
179: XMLContext xmlContext) throws Exception {
180: while (!node.isFinished()) {
181: Object childNodeList = node.readNextChild();
182: if (childNodeList instanceof XmlPullNode) {
183: XmlPullNode child = (XmlPullNode) childNodeList;
184:
185: if (XMLConstants.PROPERTY.equals(child.getRawName())
186: && PropertyKeys.GLOBAL_IMPORT
187: .equals(child
188: .getAttributeValueFromRawName(XMLConstants.NAME))) {
189: globalImport = Boolean.valueOf(
190: XMLUtils.readProperty(
191: PropertyKeys.GLOBAL_IMPORT, child))
192: .booleanValue();
193: } else if (XMLConstants.PROPERTY.equals(child
194: .getRawName())
195: && PropertyKeys.GLOBAL_EXPORT
196: .equals(child
197: .getAttributeValueFromRawName(XMLConstants.NAME))) {
198: globalExport = Boolean.valueOf(
199: XMLUtils.readProperty(
200: PropertyKeys.GLOBAL_EXPORT, child))
201: .booleanValue();
202: } else if (PropertyKeys.IMPORT_PARAMETER.equals(child
203: .getRawName())) {
204: SubReportParameter subReportParameter = new SubReportParameter(
205: "", "");
206: subReportParameter.readObject(child, xmlContext);
207: importParameters.put(subReportParameter.getKey(),
208: subReportParameter);
209: } else if (PropertyKeys.EXPORT_PARAMETER.equals(child
210: .getRawName())) {
211: SubReportParameter subReportParameter = new SubReportParameter(
212: "", "");
213: subReportParameter.readObject(child, xmlContext);
214: exportParameters.put(subReportParameter.getKey(),
215: subReportParameter);
216: }
217: }
218: }
219: }
220: }
|