teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * 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; teichmann@6450: import java.util.concurrent.Semaphore; 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: felix@6043: /** Private logging instance. */ teichmann@8203: private static final Logger log = 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: felix@6043: /** Name of the factory to generate a GaugeDischargeCurveArtifact. */ bjoern@3865: private static final String GAUGE_DISCHARGE_CURVE_ARTIFACT = "gaugedischargecurve"; felix@6043: felix@6043: /** Name of the factory to generate a MainvaluesArtifact. */ felix@6043: private static final String MAINVALUE_ARTIFACT_FACTORY = "mainvalue"; felix@6043: rrenkert@5503: private static final String SQ_RELATION_ARTIFACT = "staticsqrelation"; ingo@905: teichmann@6450: // To prevent pile up of create artifact calls only permit a limited teichmann@6450: // number of parallel creates. teichmann@6450: public static final int MAX_CREATE = 5; teichmann@6450: teichmann@6450: private static final Semaphore CREATE_SEMAPHORE = new Semaphore(MAX_CREATE); teichmann@6450: 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: { teichmann@8203: log.debug("ArtifactHelper.create"); ingo@905: ingo@905: String uuid; ingo@905: String ids; ingo@905: CreationFilter filter; aheinecke@6136: String targetOut; ingo@905: ingo@905: if (recommendation != null) { aheinecke@6136: uuid = recommendation.getMasterArtifact(); aheinecke@6136: ids = recommendation.getIDs(); aheinecke@6136: filter = convertFilter(recommendation.getFilter()); aheinecke@6136: targetOut = recommendation.getTargetOut(); ingo@905: } ingo@905: else { aheinecke@6136: uuid = null; aheinecke@6136: ids = null; aheinecke@6136: filter = null; aheinecke@6136: targetOut = null; ingo@905: } ingo@905: ingo@905: Document create = ClientProtocolUtils.newCreateDocument( aheinecke@6136: factory, uuid, ids, filter, targetOut); ingo@905: bjoern@3865: return sendCreate(serverUrl, locale, create); bjoern@3865: } bjoern@3865: bjoern@3865: /** felix@6044: * Creates a new MainvaluesArtifact. felix@6044: * felix@6044: * @param river the name of the river felix@6044: */ felix@6044: public static Artifact createMainvalueArtifact( felix@6044: String serverUrl, felix@6044: String locale, felix@6044: String river, felix@6044: Long gaugeRef) felix@6044: throws ServerException felix@6044: { felix@6044: Document create = ClientProtocolUtils.newCreateDocument( felix@6044: MAINVALUE_ARTIFACT_FACTORY); felix@6044: felix@6044: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( felix@6044: create, felix@6044: ArtifactNamespaceContext.NAMESPACE_URI, felix@6044: ArtifactNamespaceContext.NAMESPACE_PREFIX); felix@6044: felix@6044: Element root = create.getDocumentElement(); felix@6044: felix@6044: Element eriver = ec.create("river"); felix@6044: ec.addAttr(eriver, "name", river); felix@6044: felix@6044: Element egauge = ec.create("gauge"); felix@6044: ec.addAttr(egauge, "reference", gaugeRef.toString()); felix@6044: felix@6044: root.appendChild(eriver); felix@6044: root.appendChild(egauge); felix@6044: felix@6044: return sendCreate(serverUrl, locale, create); felix@6044: } felix@6044: /** 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: /** felix@6043: * 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: try { teichmann@6450: CREATE_SEMAPHORE.acquire(); ingo@905: } teichmann@6450: catch (InterruptedException ie) { teichmann@6450: throw new ServerException(ERROR_CREATE_ARTIFACT); ingo@905: } teichmann@6450: try { teichmann@6450: HttpClient client = new HttpClientImpl(serverUrl, locale); ingo@905: teichmann@6450: try { teichmann@6450: return (Artifact) client.create(doc, new FLYSArtifactCreator()); teichmann@6450: } teichmann@6450: catch (ConnectionException ce) { teichmann@8203: log.error(ce, ce); teichmann@6450: } teichmann@6450: teichmann@6450: throw new ServerException(ERROR_CREATE_ARTIFACT); teichmann@6450: } teichmann@6450: finally { teichmann@6450: CREATE_SEMAPHORE.release(); teichmann@6450: } 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 :