ingo@905: package de.intevation.flys.client.server;
ingo@905:
ingo@905: import org.w3c.dom.Document;
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;
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: /**
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:
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:
ingo@905: HttpClient client = new HttpClientImpl(serverUrl, locale);
ingo@905:
ingo@905: try {
ingo@905: return (Artifact) client.create(create, 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 :