org.xins.server.FunctionResult Maven / Gradle / Ivy
/*
* $Id: FunctionResult.java,v 1.47 2010/11/18 20:35:05 agoubard Exp $
*
* See the COPYRIGHT file for redistribution and use restrictions.
*/
package org.xins.server;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Node;
import org.xins.common.MandatoryArgumentChecker;
import org.xins.common.collections.MapStringUtils;
import org.w3c.dom.Element;
import org.xins.common.xml.DataElementBuilder;
/**
* Result from a function call. Defines an error code, parameters and output
* data section. All are optional.
*
* @version $Revision: 1.47 $ $Date: 2010/11/18 20:35:05 $
* @author Anthony Goubard
* @author Ernst de Haan
*
* @since XINS 1.0.0
*
* @see FunctionRequest
*/
public class FunctionResult {
/**
* The result code. This field is null
if no code was
* returned.
*/
private final String _code;
/**
* The parameters and their values. This field is never null
.
*/
private final Map _parameters;
/**
* The data element builder. This field is lazily initialized, it is
* null
if there is no data element.
*/
private DataElementBuilder _dataElementBuilder;
/**
* Creates a new successful FunctionResult
instance with no
* parameters.
*/
public FunctionResult() {
this(null, null);
}
/**
* Creates a new FunctionResult
instance with no parameters.
*
* @param code
* the error code, can be null
if the result is successful.
*/
public FunctionResult(String code) {
this(code, null);
}
/**
* Creates a new FunctionResult
instance with a specified set
* of parameters.
*
* @param code
* the error code, can be null
if the result is successful.
*
* @param parameters
* the parameters for the result, can be null
if there are
* no parameters.
*/
public FunctionResult(String code, Map parameters) {
_code = code;
if (parameters == null) {
_parameters = new HashMap();
} else {
_parameters = parameters;
}
}
/**
* Returns the result code.
*
* @return
* the result code or null
if no code was returned.
*/
public String getErrorCode() {
return _code;
}
/**
* Checks that the output parameters are set as specified. If a parameter
* is missing or if the value for it is invalid, then an
* InvalidResponseResult
is returned. Otherwise the parameters
* are considered valid, and null
is returned.
*
* The implementation of this method in class {@link FunctionResult}
* always returns null
.
*
* @return
* an {@link InvalidResponseResult} instance if at least one output
* parameter is missing or invalid, or null
otherwise.
*
* @since XINS 2.0.
*/
public InvalidResponseResult checkOutputParameters() {
return null;
}
/**
* Adds an output parameter to the result. The name and the value must
* both be specified.
*
* @param name
* the name of the output parameter, not null
and not an
* empty string.
*
* @param value
* the value of the output parameter, not null
and not an
* empty string.
*
* @throws IllegalArgumentException
* if name == null || "".equals(name)
* || value == null || "".equals(value)
.
*/
protected void param(String name, String value) throws IllegalArgumentException {
// Check preconditions
MandatoryArgumentChecker.check("name", name, "value", value);
if (name.length() < 1) {
throw new IllegalArgumentException("\"\".equals(name)");
} else if (value.length() < 1) {
throw new IllegalArgumentException("\"\".equals(value)");
}
// This will erase any value set before with the same name.
_parameters.put(name, value);
}
/**
* Gets all parameters.
*
* @return
* a {@link Map<String, String>} containing all parameters, never null
;
* the keys will be the names of the parameters
* ({@link String} objects, cannot be null
),
* the values will be the parameter values
* ({@link String} objects as well, cannot be null
).
*/
public Map getParameters() {
return _parameters;
}
/**
* Gets the value of the specified parameter.
*
* @param name
* the parameter element name, cannot be null
.
*
* @return
* string containing the value of the parameter element,
* or null
if the value is not set.
*
* @throws IllegalArgumentException
* if name == null
.
*/
public String getParameter(String name) throws IllegalArgumentException {
// Check preconditions
MandatoryArgumentChecker.check("name", name);
return _parameters.get(name);
}
/**
* Adds a new Element
to the data element.
*
* @param element
* the new element to add to the result, cannot be null
.
*
* @throws IllegalArgumentException
* if element == null
.
*
* @since XINS 1.1.0
* @deprecated Use getDataElementBuilder()
*/
protected void add(Element element) throws IllegalArgumentException {
// Check preconditions
MandatoryArgumentChecker.check("element", element);
Element elementToAdd = (Element) getDataElementBuilder().getDocument().importNode(element, true);
_dataElementBuilder.getDataElement().appendChild(elementToAdd);
}
/**
* Adds a new Element
to the data element.
*
* @param element
* the new element to add to the result, cannot be null
.
*
* @since XINS 3.0.0
*/
protected DataElementBuilder getDataElementBuilder() {
// Lazily initialize _dataElementBuilder
if (_dataElementBuilder == null) {
_dataElementBuilder = new DataElementBuilder();
}
return _dataElementBuilder;
}
/**
* Gets the data element from this result.
*
* @return
* the data element of the result, can be null
.
*/
public Element getDataElement() {
if (_dataElementBuilder == null) {
return null;
} else {
return _dataElementBuilder.getDataElement();
}
}
public String toString() {
String asString = "";
if (_code != null) {
asString += "Error code: " + _code + "; ";
} else {
asString += "Successful result; ";
}
asString += MapStringUtils.toString(_parameters, "no parameters") + "; ";
if (_dataElementBuilder == null) {
asString += "no data section";
} else {
asString += _dataElementBuilder.toString();
}
return asString;
}
}