Mercurial > dive4elements > gnv-client
changeset 725:dde7f51dbe1e
Improved exception handling while feeding an artifact.
gnv-artifacts/trunk@757 c6561f87-3c4e-4783-a992-168aeb5c3f6f
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/ChangeLog Wed Mar 10 13:27:14 2010 +0000 @@ -1,3 +1,40 @@ +2010-03-10 Ingo Weinzierl <ingo.weinzierl@intevation.de> + + * doc/conf/products/timeseries/conf_timeseriespoint.xml: Use MinMaxDateState + to handle date input instead of MinMaxState. + + * src/main/java/de/intevation/gnv/artifacts/GNVDefaultArtifact.java: Added + EXCEPTION_NO_INPUT key used for a lookup in resource bundles. + + * src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java, + src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java + src/main/java/de/intevation/gnv/state/OutputStateBase.java + src/main/java/de/intevation/gnv/state/StateBase.java, + src/main/java/de/intevation/gnv/state/MinMaxState.java, + src/main/java/de/intevation/gnv/state/State.java: Return localized + error messages as xml documents if something failed while feeding the + current artifact/state. These error messages are used to be displayed in + the user interface. There are two different error messages at the moment - + for general error messages related to server errors and a message invalid + user input. The difference between these messages is their location in the + xml document. Each message type is created with a separate method in + ArtifactXMLUtilities (createExceptionReport() and createInputException()). + The intention to separate these message types is to display them on + different places in the user interface. + + * src/main/java/de/intevation/gnv/state/MinMaxDateState.java: New state to + handle date input. + + * src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java: Converted + some class methods into static methods and added a new method to create an + xml document which contains an error message for invalid user input. + + * src/main/resources/lang/artifactMessages.properties, + src/main/resources/lang/artifactMessages_de_DE.properties, + src/main/resources/lang/artifactMessages_en.properties, + src/main/resources/lang/artifactMessages_de.properties: Added text for + error messages. + 2010-03-09 Tim Englich <tim.englich@intevation.de> * doc/conf/queries.properties:
--- a/gnv-artifacts/doc/conf/products/timeseries/conf_timeseriespoint.xml Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/doc/conf/products/timeseries/conf_timeseriespoint.xml Wed Mar 10 13:27:14 2010 +0000 @@ -159,7 +159,7 @@ <to state="timeseries_interval"/> </transition> - <state id="timeseries_interval" description="timeseries_interval" state="de.intevation.gnv.state.MinMaxState"> + <state id="timeseries_interval" description="timeseries_interval" state="de.intevation.gnv.state.MinMaxDateState"> <queryID>timeseries_interval</queryID> <dataname>timeinterval</dataname> <data-multiselect>false</data-multiselect> @@ -208,7 +208,7 @@ <inputvalue name="width" type="Integer" value="600"/> <inputvalue name="height" type="Integer" value="400"/> <inputvalue name="bincount" type="Integer" value="7"/> - <inputvalue name="binwidth" type="Integer" value="7"/> + <inputvalue name="binwidth" type="Double" value="7"/> </parameters> <exportModes> <export name="img" description="IMG-Export der Daten" mime-type="image/png" />
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java Wed Mar 10 13:27:14 2010 +0000 @@ -1,6 +1,3 @@ -/** - * - */ package de.intevation.gnv.artifacts; import de.intevation.artifactdatabase.Config; @@ -44,6 +41,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.xml.xpath.XPathConstants; @@ -344,7 +342,11 @@ @Override public Document feed(Document target, CallContext context) { log.debug("GNVArtifactBase.feed"); - Document result = XMLUtils.newDocument(); + RessourceFactory fac = RessourceFactory.getInstance(); + Locale[] locales = fac.getLocales(); + Locale locale = context.getMeta().getPreferredLocale(locales); + Document result = XMLUtils.newDocument(); + try { if (this.current != null) { Collection<InputData> inputData = this.parseInputData( @@ -352,14 +354,18 @@ XPATH_INPUT_DATA); if (!inputData.isEmpty()){ - current.feed(inputData, super.identifier); - result = new ArtifactXMLUtilities().createSuccessReport( - "Feed success", XMLUtils.newDocument()); + result = current.feed(context, inputData, super.identifier); }else{ - String msg = "No Inputdata given. Please select at least one Entry."; + //String msg = "No Inputdata given. Please select at least one Entry."; + String msg = fac.getRessource( + locale, + EXCEPTION_NO_INPUT, + EXCEPTION_NO_INPUT); + log.warn(msg); - result = new ArtifactXMLUtilities().createExceptionReport(msg, - XMLUtils.newDocument()); + result = new ArtifactXMLUtilities().createInputExceptionReport( + msg, + XMLUtils.newDocument()); } } else { String msg = "No State instantiated";
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVDefaultArtifact.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVDefaultArtifact.java Wed Mar 10 13:27:14 2010 +0000 @@ -21,6 +21,8 @@ private static Logger logger = Logger.getLogger(GNVDefaultArtifact.class); + public static final String EXCEPTION_NO_INPUT = "no.input.data"; + public GNVDefaultArtifact() { super();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java Wed Mar 10 13:27:14 2010 +0000 @@ -0,0 +1,132 @@ +package de.intevation.gnv.state; + +import de.intevation.artifactdatabase.XMLUtils; + +import de.intevation.artifacts.CallContext; +import de.intevation.artifacts.CallMeta; + +import de.intevation.gnv.artifacts.ressource.RessourceFactory; + +import de.intevation.gnv.geobackend.base.Result; + +import de.intevation.gnv.geobackend.util.DateUtils; + +import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData; +import de.intevation.gnv.state.describedata.DescribeData; +import de.intevation.gnv.state.describedata.MinMaxDescribeData; + +import de.intevation.gnv.state.exception.StateException; + +import de.intevation.gnv.utils.InputValidator; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) + */ +public class MinMaxDateState extends MinMaxState { + + + public static final String EXCEPTION_DATE_REQUIRED = + "input.is.not.valid.date.required"; + + public static final String EXCEPTION_START_AFTER_END = + "start.date.after.end.date"; + + private static Logger logger = Logger.getLogger(MinMaxDateState.class); + + + public MinMaxDateState() { + super(); + } + + + /** + * @see de.intevation.gnv.state.StateBase#feed(java.util.Collection, + * java.lang.String) + */ + @Override + public Document feed( + CallContext context, + Collection<InputData> inputData, + String uuid) + throws StateException { + RessourceFactory resFactory = RessourceFactory.getInstance(); + Locale[] serverLocales = resFactory.getLocales(); + Locale locale = context.getMeta().getPreferredLocale( + serverLocales); + + if (inputData == null) { + String msg = "No input data given."; + logger.warn(msg); + return feedFailure(msg); + } + + InputValidator iv = new InputValidator(); + Iterator iter = inputData.iterator(); + + Object min = null; + Object max = null; + + while (iter.hasNext()) { + InputData tmp = (InputData) iter.next(); + InputValue meta = inputValues.get(tmp.getName()); + String type = meta.getType(); + String value = tmp.getValue(); + String name = tmp.getName(); + + if (meta == null) { + String msg = "Input data not expected here. Data will be ignored."; + logger.warn(msg); + return feedFailure(msg); + } + + boolean valid = iv.isInputValid(value, type); + if (!valid) { + String msg = resFactory.getRessource( + locale, EXCEPTION_DATE_REQUIRED, EXCEPTION_DATE_REQUIRED); + logger.error(msg); + return feedFailure(msg); + } + + if (name.equals(MINVALUEFIELDNAME)) { + min = value; + } + + if (name.equals(MAXVALUEFIELDNAME)) { + max = value; + } + + if (min != null && max != null) { + if (!iv.isInputValid((String) min, (String) max, type)) { + String msg = resFactory.getRessource( + locale, + EXCEPTION_START_AFTER_END, + EXCEPTION_START_AFTER_END); + logger.error(msg); + return feedFailure(msg); + } + } + } + + DescribeData values = new DefaultMinMaxDescribeData( + dataName, min, max, getID()); + + this.inputData.put(dataName, new DefaultInputData(dataName, values)); + + return feedSuccess(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxState.java Wed Mar 10 13:27:14 2010 +0000 @@ -2,6 +2,7 @@ import de.intevation.artifactdatabase.XMLUtils; +import de.intevation.artifacts.CallContext; import de.intevation.artifacts.CallMeta; import de.intevation.gnv.artifacts.ressource.RessourceFactory; @@ -38,10 +39,6 @@ public class MinMaxState extends StateBase { /** - * The UID of this Class - */ - private static final long serialVersionUID = -3525903792105187408L; - /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(MinMaxState.class); @@ -84,11 +81,18 @@ * java.lang.String) */ @Override - public void feed(Collection<InputData> inputData, String uuid) - throws StateException - { - if (inputData == null) - return; + public Document feed( + CallContext context, + Collection<InputData> inputData, + String uuid) + throws StateException { + RessourceFactory resFactory = RessourceFactory.getInstance(); + + if (inputData == null) { + String msg = "No input data given."; + log.warn(msg); + return feedFailure(msg); + } InputValidator iv = new InputValidator(); Iterator iter = inputData.iterator(); @@ -99,19 +103,21 @@ while (iter.hasNext()) { InputData tmp = (InputData) iter.next(); InputValue meta = inputValues.get(tmp.getName()); + String type = meta.getType(); String value = tmp.getValue(); String name = tmp.getName(); if (meta == null) { - log.warn("Input data not expected here. Data will be ignored."); - return; + String msg = "Input data not expected here. Data will be ignored."; + log.warn(msg); + return feedFailure(msg); } - boolean valid = iv.isInputValid(value, meta.getType()); + boolean valid = iv.isInputValid(value, type); if (!valid) { String msg = "Input is not valid for this state."; log.error(msg); - throw new StateException(msg); + return feedFailure(msg); } if (name.equals(MINVALUEFIELDNAME)) { @@ -121,14 +127,24 @@ if (name.equals(MAXVALUEFIELDNAME)) { max = value; } + + if (min != null && max != null) { + if (!iv.isInputValid((String) min, (String) max, type)) { + String msg = "Input is not valid for this state."; + log.error(msg); + return feedFailure(msg); + } + } } - if (min != null && max != null) { - DescribeData values = new DefaultMinMaxDescribeData( - dataName, min, max, getID()); + - this.inputData.put(dataName, new DefaultInputData(dataName, values)); - } + DescribeData values = new DefaultMinMaxDescribeData( + dataName, min, max, getID()); + + this.inputData.put(dataName, new DefaultInputData(dataName, values)); + + return feedSuccess(); }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java Wed Mar 10 13:27:14 2010 +0000 @@ -1,6 +1,3 @@ -/** - * - */ package de.intevation.gnv.state; import java.io.OutputStream; @@ -36,7 +33,8 @@ import de.intevation.gnv.utils.InputValidator; /** - * @author Tim Englich <tim.englich@intevation.de> + * @author Tim Englich (tim.englich@intevation.de) + * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) * */ public abstract class OutputStateBase @@ -308,10 +306,15 @@ @Override - public void feed(Collection<InputData> inputData, String uuid) + public Document feed( + CallContext context, + Collection<InputData> inputData, + String uuid) throws StateException { putInputData(inputData, uuid); + + return feedSuccess(); } /**
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/State.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/State.java Wed Mar 10 13:27:14 2010 +0000 @@ -33,7 +33,8 @@ String uuid ); - public void feed(Collection<InputData> inputData, String uuid) + public Document feed( + CallContext context, Collection<InputData> inputData, String uuid) throws StateException; public void setParent(State state);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java Wed Mar 10 13:27:14 2010 +0000 @@ -29,6 +29,7 @@ import de.intevation.gnv.state.exception.StateException; +import de.intevation.gnv.utils.ArtifactXMLUtilities; import de.intevation.gnv.utils.InputValidator; import java.util.ArrayList; @@ -39,6 +40,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.xml.xpath.XPathConstants; @@ -79,6 +81,10 @@ public final static String XPATH_STATIC_UI = "art:static"; public final static String XPATH_DYNAMIC_UI = "art:dynamic"; + public static final String EXCEPTION_NO_INPUT = "no.input.data"; + public static final String EXCEPTION_INVALID_INPUT = + "input.is.not.valid"; + /** input value names which should not be rendered from State itself */ public final static String[] BLACKLIST = {"sourceid", "fisname"}; @@ -215,9 +221,17 @@ } - public void feed(Collection<InputData> inputData, String uuid) + public Document feed( + CallContext context, + Collection<InputData> inputData, + String uuid) throws StateException { + RessourceFactory resFactory = RessourceFactory.getInstance(); + Locale[] serverLocales = resFactory.getLocales(); + Locale locale = context.getMeta().getPreferredLocale( + serverLocales); + if (inputData != null) { Iterator<InputData> it = inputData.iterator(); InputValidator iv = new InputValidator(); @@ -237,26 +251,49 @@ tmpItem.setDescription(desc); this.inputData.put(tmpItem.getName(), tmpItem); } else { - String errMsg = "Wrong input for " + tmpItem.getValue() - + " is not an " + inputValue.getType() - + " Value."; - log.warn(errMsg); - throw new StateException(errMsg); + String msg = resFactory.getRessource( + locale, + EXCEPTION_INVALID_INPUT, + EXCEPTION_INVALID_INPUT); + log.warn(msg); + return feedFailure(msg); } } else { - String errMsg = "No Inputvalue given for Inputdata " - + tmpItem.getName(); - log.warn(errMsg + "Value will be ignored"); + String msg = resFactory.getRessource( + locale, + EXCEPTION_INVALID_INPUT, + EXCEPTION_INVALID_INPUT); + log.warn(msg); + return feedFailure(msg); } } + + return feedSuccess(); } else { - log.warn("No Inputdata given"); + String msg = resFactory.getRessource( + locale, + EXCEPTION_NO_INPUT, + EXCEPTION_NO_INPUT); + log.warn(msg); + return feedFailure(msg); } } + protected Document feedSuccess() { + return ArtifactXMLUtilities.createSuccessReport( + "Initialize success", XMLUtils.newDocument()); + } + + + protected Document feedFailure(String msg) { + return ArtifactXMLUtilities.createInputExceptionReport( + msg, XMLUtils.newDocument()); + } + + private String[] getDescriptionForInputData(InputData data, String uuid) { // there is only one element in the list, so take the first Object obj = getDescibeData(uuid).get(0);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/timeseries/TimeSeriesOutputState.java Wed Mar 10 13:27:14 2010 +0000 @@ -257,8 +257,11 @@ } } } catch (NumberFormatException e) { - log.error(e, e); - throw new StateException(e); + log.warn(e, e); + XMLUtils.toStream( + feedFailure("not.a.number"), + outputStream); + return; } try {
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ArtifactXMLUtilities.java Wed Mar 10 13:27:14 2010 +0000 @@ -1,6 +1,3 @@ -/** - * - */ package de.intevation.gnv.utils; import java.io.ByteArrayInputStream; @@ -58,14 +55,14 @@ * @param document * @return */ - public Element createArtifactElement(Document document, String name) { + public static Element createArtifactElement(Document document, String name) { Element node = document.createElementNS( ArtifactNamespaceContext.NAMESPACE_URI, name); node.setPrefix(ArtifactNamespaceContext.NAMESPACE_PREFIX); return node; } - public String writeDocument2String(Document document) { + public static String writeDocument2String(Document document) { try { TransformerFactory transformerFactory = TransformerFactory .newInstance(); @@ -120,23 +117,37 @@ return node; } - public Document createExceptionReport(String message, Document document) { + public static Document createExceptionReport(String message, Document document) { log.debug("ArtifactXMLUtilities.createExceptionReport"); - Element exceptionReportNode = this.createArtifactElement(document, + Element exceptionReportNode = createArtifactElement(document, "exceptionreport"); document.appendChild(exceptionReportNode); - Element exceptionNode = this.createArtifactElement(document, + Element exceptionNode = createArtifactElement(document, "exception"); exceptionNode.setTextContent(message); exceptionReportNode.appendChild(exceptionNode); return document; } - public Document createSuccessReport(String message, Document document) { + public static Document createInputExceptionReport(String msg, Document doc) { + Element exceptionReportNode = createArtifactElement( + doc,"exceptionreport"); + Element exceptionNode = createArtifactElement( + doc,"exception"); + Element inputNode = createArtifactElement( + doc, "input"); + inputNode.setTextContent(msg); + exceptionNode.appendChild(inputNode); + exceptionReportNode.appendChild(exceptionNode); + doc.appendChild(exceptionReportNode); + return doc; + } + + public static Document createSuccessReport(String message, Document document) { log.debug("ArtifactXMLUtilities.creatSuccessReport"); - Element reportNode = this.createArtifactElement(document, "result"); + Element reportNode = createArtifactElement(document, "result"); document.appendChild(reportNode); - Element successNode = this.createArtifactElement(document, "success"); + Element successNode = createArtifactElement(document, "success"); successNode.setTextContent(message); reportNode.appendChild(successNode); return document;
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages.properties Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages.properties Wed Mar 10 13:27:14 2010 +0000 @@ -69,3 +69,9 @@ chart.verticalcrosssection.title.yaxis=Depth [m] chart.horizontalprofile.title.xaxis=Distance [km] chart.horizontalcrosssection.title.xaxis=Latitude [m] + +# error messages +no.input.data= No Entry was chosen. Please select at least one entry. +input.is.not.valid=Invalid input. Please try again. +input.is.not.valid.date.required=Invalid input. A date is required. Please try again. +start.date.after.end.date=Start date lies after end date. Please try again.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_de.properties Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_de.properties Wed Mar 10 13:27:14 2010 +0000 @@ -69,3 +69,9 @@ chart.verticalcrosssection.title.yaxis=Tiefe [m] chart.horizontalprofile.title.xaxis=Distanz [km] chart.horizontalcrosssection.title.xaxis=Ausdehnung [m] + +# error messages +no.input.data=Es wurde kein Eintrag ausgew\u00e4hlt. Bitte w\u00e4hlen sie mindestens einen Eintrag aus. +input.is.not.valid=Ung\u00fcltige Eingabe. Bitte versuchen Sie es erneut. +input.is.not.valid.date.required=Ung\u00fcltige Eingabe. Ein Datum wird ben\u00f6tigt. Bitte versuchen Sie es erneut. +start.date.after.end.date=Anfangsdatum liegt hinter dem Enddatum. Bitte versuchen Sie es erneut.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_de_DE.properties Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_de_DE.properties Wed Mar 10 13:27:14 2010 +0000 @@ -69,3 +69,9 @@ chart.verticalcrosssection.title.yaxis=Tiefe [m] chart.horizontalprofile.title.xaxis=Distanz [km] chart.horizontalcrosssection.title.xaxis=Ausdehnung [m] + +# error messages +no.input.data=Es wurde kein Eintrag ausgew\u00e4hlt. Bitte w\u00e4hlen sie mindestens einen Eintrag aus. +input.is.not.valid=Ung\u00fcltige Eingabe. Bitte versuchen Sie es erneut. +input.is.not.valid.date.required=Ung\u00fcltige Eingabe. Ein Datum wird ben\u00f6tigt. Bitte versuchen Sie es erneut. +start.date.after.end.date=Anfangsdatum liegt nach dem Enddatum. Bitte versuchen Sie es erneut.
--- a/gnv-artifacts/src/main/resources/lang/artifactMessages_en.properties Tue Mar 09 12:54:05 2010 +0000 +++ b/gnv-artifacts/src/main/resources/lang/artifactMessages_en.properties Wed Mar 10 13:27:14 2010 +0000 @@ -70,3 +70,9 @@ chart.verticalcrosssection.title.yaxis=Depth [m] chart.horizontalprofile.title.xaxis=Distance [km] chart.horizontalcrosssection.title.xaxis=Latitude [m] + +# error messages +no.input.data= No Entry was chosen. Please select at least one entry. +input.is.not.valid=Invalid input. Please try again. +input.is.not.valid.date.required=Invalid input. A date is required. Please try again. +start.date.after.end.date=Start date lies after end date. Please try again.