ingo@905: package de.intevation.flys.client.server;
ingo@905:
ingo@905: import org.w3c.dom.Document;
bjoern@3865: import org.w3c.dom.Element;
ingo@905:
ingo@1367: import org.apache.log4j.Logger;
ingo@1367:
ingo@905: import java.util.ArrayList;
ingo@905: import java.util.List;
ingo@905: import java.util.Map;
ingo@905:
ingo@905: import de.intevation.artifacts.common.utils.ClientProtocolUtils;
ingo@905: import de.intevation.artifacts.common.utils.CreationFilter;
ingo@905:
ingo@905: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@905: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@905: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
bjoern@3865: import de.intevation.artifacts.httpclient.utils.ArtifactNamespaceContext;
bjoern@3865: import de.intevation.artifacts.httpclient.utils.XMLUtils;
ingo@905:
ingo@905: import de.intevation.flys.client.shared.exceptions.ServerException;
ingo@905: import de.intevation.flys.client.shared.model.Artifact;
ingo@905:
ingo@905: import de.intevation.flys.client.shared.model.Recommendation;
ingo@905:
ingo@905: /**
ingo@905: * @author Ingo Weinzierl
ingo@905: */
ingo@905: public class ArtifactHelper {
ingo@905:
ingo@1367: private static final Logger logger = Logger.getLogger(ArtifactHelper.class);
ingo@1367:
ingo@1367:
ingo@905: /** The error message key that is thrown if an error occured while artifact
ingo@905: * creation.*/
ingo@905: public static final String ERROR_CREATE_ARTIFACT = "error_create_artifact";
ingo@905:
bjoern@3865: /**
bjoern@3865: * Name of the factory to generate a GaugeDischargeCurveArtifact
bjoern@3865: */
bjoern@3865: private static final String GAUGE_DISCHARGE_CURVE_ARTIFACT = "gaugedischargecurve";
bjoern@3865:
ingo@905:
ingo@905: private ArtifactHelper() {
ingo@905: }
ingo@905:
ingo@905:
felix@1318: /**
felix@1318: * @param factory ArtifactFactory to use.
felix@1318: */
ingo@905: public static Artifact createArtifact(
ingo@905: String serverUrl,
ingo@905: String locale,
ingo@905: String factory,
ingo@905: Recommendation recommendation)
ingo@905: throws ServerException
ingo@905: {
ingo@1367: logger.debug("ArtifactHelper.create");
ingo@905:
ingo@905: String uuid;
ingo@905: String ids;
ingo@905: CreationFilter filter;
ingo@905:
ingo@905: if (recommendation != null) {
ingo@905: uuid = recommendation.getMasterArtifact();
ingo@905: ids = recommendation.getIDs();
ingo@905: filter = convertFilter(recommendation.getFilter());
ingo@905: }
ingo@905: else {
ingo@905: uuid = null;
ingo@905: ids = null;
ingo@905: filter = null;
ingo@905: }
ingo@905:
ingo@905: Document create = ClientProtocolUtils.newCreateDocument(
ingo@905: factory, uuid, ids, filter);
ingo@905:
bjoern@3865: return sendCreate(serverUrl, locale, create);
bjoern@3865:
bjoern@3865: }
bjoern@3865:
bjoern@3865: /**
bjoern@3865: * Creates a new GaugeDischargeCurverArtifact
bjoern@3865: *
bjoern@3865: * @param river the name of the river
bjoern@3865: * @param reference the reference id of the gauge (official number)
bjoern@3865: */
bjoern@3865: public static Artifact createGaugeDischargeCurveArtifact(
bjoern@3865: String serverUrl,
bjoern@3865: String locale,
bjoern@3865: String river,
bjoern@3865: Long reference)
bjoern@3865: throws ServerException
bjoern@3865: {
bjoern@3865: Document create = ClientProtocolUtils.newCreateDocument(
bjoern@3865: GAUGE_DISCHARGE_CURVE_ARTIFACT);
bjoern@3865:
bjoern@3865: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
bjoern@3865: create,
bjoern@3865: ArtifactNamespaceContext.NAMESPACE_URI,
bjoern@3865: ArtifactNamespaceContext.NAMESPACE_PREFIX);
bjoern@3865:
bjoern@3865: Element root = create.getDocumentElement();
bjoern@3865:
bjoern@3865: Element eriver = ec.create("river");
bjoern@3865: ec.addAttr(eriver, "name", river);
bjoern@3865:
bjoern@3865: Element egauge = ec.create("gauge");
bjoern@3865: ec.addAttr(egauge, "reference", reference.toString());
bjoern@3865:
bjoern@3865: root.appendChild(eriver);
bjoern@3865: root.appendChild(egauge);
bjoern@3865:
bjoern@3865: return sendCreate(serverUrl, locale, create);
bjoern@3865: }
bjoern@3865:
bjoern@3865: /**
bjoern@3865: * Sends a create document to the artifact server
bjoern@3865: */
bjoern@3865: private static Artifact sendCreate(
bjoern@3865: String serverUrl,
bjoern@3865: String locale,
bjoern@3865: Document doc)
bjoern@3865: throws ServerException
bjoern@3865: {
ingo@905: HttpClient client = new HttpClientImpl(serverUrl, locale);
ingo@905:
ingo@905: try {
bjoern@3865: return (Artifact) client.create(doc, new FLYSArtifactCreator());
ingo@905: }
ingo@905: catch (ConnectionException ce) {
ingo@1367: logger.error(ce, ce);
ingo@905: }
ingo@905:
ingo@905: throw new ServerException(ERROR_CREATE_ARTIFACT);
ingo@905: }
ingo@905:
ingo@905:
felix@1318: /**
felix@1318: * Create CreationFilter from Recommendation.Filter.
felix@1318: */
ingo@905: public static CreationFilter convertFilter(Recommendation.Filter filter) {
ingo@905:
ingo@905: if (filter == null) {
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: CreationFilter cf = new CreationFilter();
ingo@905:
ingo@905: Map> outs = filter.getOuts();
ingo@905:
ingo@905: for (Map.Entry> entry:
ingo@905: outs.entrySet()) {
ingo@905: List rfs = entry.getValue();
ingo@905: List cfs =
ingo@905: new ArrayList(rfs.size());
ingo@905: for (Recommendation.Facet rf: rfs) {
ingo@905: cfs.add(new CreationFilter.Facet(rf.getName(), rf.getIndex()));
ingo@905: }
ingo@905: cf.add(entry.getKey(), cfs);
ingo@905: }
ingo@905:
ingo@905: return cf;
ingo@905: }
ingo@905: }
ingo@905: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :