teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5861: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5861: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.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: teichmann@5835: import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; teichmann@5835: import org.dive4elements.artifacts.common.utils.CreationFilter; ingo@905: teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; teichmann@5835: import org.dive4elements.artifacts.httpclient.utils.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.httpclient.utils.XMLUtils; ingo@905: teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException; teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact; ingo@905: teichmann@5835: import org.dive4elements.river.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"; rrenkert@5503: private static final String SQ_RELATION_ARTIFACT = "staticsqrelation"; 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: } rrenkert@5503: rrenkert@5503: rrenkert@5503: public static Artifact createSQRelationArtifact( rrenkert@5503: String serverUrl, rrenkert@5503: String locale, rrenkert@5503: String river, rrenkert@5503: int measurementStation) rrenkert@5503: throws ServerException rrenkert@5503: { rrenkert@5503: Document create = ClientProtocolUtils.newCreateDocument( rrenkert@5503: SQ_RELATION_ARTIFACT); rrenkert@5503: rrenkert@5503: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( rrenkert@5503: create, rrenkert@5503: ArtifactNamespaceContext.NAMESPACE_URI, rrenkert@5503: ArtifactNamespaceContext.NAMESPACE_PREFIX); rrenkert@5503: rrenkert@5503: Element root = create.getDocumentElement(); rrenkert@5503: rrenkert@5503: Element eriver = ec.create("river"); rrenkert@5503: ec.addAttr(eriver, "name", river); rrenkert@5503: rrenkert@5503: Element estation = ec.create("measurement_station"); rrenkert@5503: ec.addAttr(estation, "number", String.valueOf(measurementStation)); rrenkert@5503: rrenkert@5503: root.appendChild(eriver); rrenkert@5503: root.appendChild(estation); rrenkert@5503: rrenkert@5503: return sendCreate(serverUrl, locale, create); rrenkert@5503: } ingo@905: } ingo@905: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :