sascha@372: package de.intevation.flys.artifacts.services; sascha@372: sascha@372: import org.w3c.dom.Document; sascha@372: sascha@372: import org.apache.log4j.Logger; sascha@372: sascha@415: import java.util.Map; sascha@415: import java.util.HashMap; sascha@415: sascha@970: import de.intevation.artifacts.Artifact; sascha@372: import de.intevation.artifacts.CallMeta; sascha@966: import de.intevation.artifacts.GlobalContext; sascha@970: import de.intevation.artifacts.ArtifactDatabase; sascha@970: import de.intevation.artifacts.ArtifactDatabaseException; sascha@372: sascha@372: import de.intevation.artifacts.common.utils.XMLUtils; sascha@970: import de.intevation.artifacts.common.utils.StringUtils; sascha@372: sascha@421: import de.intevation.artifacts.common.ArtifactNamespaceContext; sascha@421: sascha@1015: import de.intevation.flys.artifacts.datacage.Recommendations; sascha@372: sascha@970: import de.intevation.flys.artifacts.FLYSArtifact; sascha@970: felix@4529: felix@4529: /** felix@4529: * Following XPaths are evaluated on the incoming document. felix@4529: * felix@4529: * "/art:meta/art:artifact-id/@value" The UUID of the artifact. Optional. felix@4529: * Used to fill the template enviroment. felix@4529: * "/art:meta/art:user-id/@value" The UUID of the user. Optional. felix@4529: * If given the user specific template is filled. felix@4529: * "/art:meta/art:outs/@value" The list of outs used to recommend for the felix@4529: * various outputs. felix@4529: * "/art:meta/art:parameters/@value" A list of key/value pairs to inject more felix@4529: * filters to the templating. felix@4529: */ sascha@372: public class MetaDataService ingo@1633: extends FLYSService sascha@372: { sascha@372: private static Logger log = Logger.getLogger(MetaDataService.class); sascha@372: sascha@1015: public static final String XPATH_ARTIFACT_ID = "/art:meta/art:artifact-id/@value"; sascha@1015: public static final String XPATH_USER_ID = "/art:meta/art:user-id/@value"; sascha@1015: public static final String XPATH_OUTS = "/art:meta/art:outs/@value"; sascha@1015: public static final String XPATH_PARAMETERS = "/art:meta/art:parameters/@value"; sascha@970: felix@1029: /** The global context key of the artifact database. */ sascha@970: public static final String ARTIFACT_DATA_BASE_KEY = sascha@970: "global.artifact.database"; sascha@969: sascha@372: public MetaDataService() { sascha@372: } sascha@372: sascha@1015: @Override ingo@1633: protected Document doProcess( sascha@1015: Document data, sascha@1015: GlobalContext globalContext, sascha@1015: CallMeta callMeta sascha@1015: ) { sascha@1015: log.debug("MetaDataService.process"); sascha@415: sascha@1015: String artifactId = XMLUtils.xpathString( sascha@1015: data, XPATH_ARTIFACT_ID, ArtifactNamespaceContext.INSTANCE); sascha@415: sascha@1016: if (artifactId != null sascha@1016: && (artifactId = artifactId.trim()).length() == 0) { sascha@1016: artifactId = null; sascha@1016: } sascha@1016: sascha@1015: String userId = XMLUtils.xpathString( sascha@1015: data, XPATH_USER_ID, ArtifactNamespaceContext.INSTANCE); sascha@415: sascha@1016: if (userId != null sascha@1016: && (userId = userId.trim()).length() == 0) { sascha@1016: userId = null; sascha@1016: } sascha@1016: sascha@1015: String outs = XMLUtils.xpathString( sascha@1015: data, XPATH_OUTS, ArtifactNamespaceContext.INSTANCE); sascha@1015: sascha@1015: String parameters = XMLUtils.xpathString( sascha@1015: data, XPATH_PARAMETERS, ArtifactNamespaceContext.INSTANCE); sascha@1015: sascha@1015: return doService( sascha@1015: artifactId, userId, outs, parameters, globalContext); sascha@415: } sascha@415: sascha@1015: protected static Map splitParameters( sascha@974: String parameters, sascha@974: Map data sascha@974: ) { sascha@974: if (parameters != null) { sascha@974: String [] parts = parameters.split("\\s*;\\s*"); sascha@974: for (String part: parts) { sascha@974: String [] kv = part.split("\\s*:\\s*"); sascha@974: if (kv.length < 2 || (kv[0] = kv[0].trim()).length() == 0) { sascha@974: continue; sascha@974: } sascha@974: String [] values = kv[1].split("\\s*,\\s*"); sascha@974: data.put(kv[0], values.length == 1 ? values[0] : values); sascha@974: } sascha@974: } sascha@974: return data; sascha@974: } sascha@974: sascha@1015: protected Document doService( sascha@1015: String artifactId, sascha@1015: String userId, sascha@970: String outsString, sascha@974: String parameters, sascha@970: GlobalContext globalContext sascha@970: ) { sascha@970: Document result = XMLUtils.newDocument(); sascha@970: sascha@974: FLYSArtifact flysArtifact; sascha@970: sascha@975: if (log.isDebugEnabled()) { sascha@1015: log.debug("artifact : " + artifactId); sascha@1015: log.debug("user : " + userId); sascha@1015: log.debug("outs : " + outsString); sascha@1015: log.debug("parameters: " + parameters); sascha@975: } sascha@970: sascha@1015: if (userId != null && !StringUtils.checkUUID(userId)) { sascha@1015: log.warn("'" + userId + "' is not a UUID"); sascha@1015: return result; sascha@1015: } sascha@1015: sascha@1015: if (artifactId != null) { sascha@1015: if (!StringUtils.checkUUID(artifactId)) { sascha@1015: log.warn("'" + artifactId + "' is not a UUID"); sascha@974: return result; sascha@974: } sascha@974: sascha@974: Object dbObject = sascha@974: (ArtifactDatabase)globalContext.get(ARTIFACT_DATA_BASE_KEY); sascha@974: sascha@974: if (!(dbObject instanceof ArtifactDatabase)) { sascha@974: log.error("Cannot find artifact database"); sascha@974: return result; sascha@974: } sascha@974: sascha@974: ArtifactDatabase db = (ArtifactDatabase)dbObject; sascha@974: sascha@974: Artifact artifact; sascha@974: sascha@974: try { sascha@1015: artifact = db.getRawArtifact(artifactId); sascha@974: } sascha@974: catch (ArtifactDatabaseException adbe) { sascha@974: log.warn("fetching artifact failed", adbe); sascha@974: return result; sascha@974: } sascha@974: sascha@974: if (!(artifact instanceof FLYSArtifact)) { sascha@974: log.warn("artifact is not a FLYS artifact."); sascha@974: return result; sascha@974: } sascha@974: sascha@974: flysArtifact = (FLYSArtifact)artifact; sascha@974: } sascha@974: else { sascha@974: flysArtifact = null; sascha@970: } sascha@970: sascha@1015: sascha@1015: Map data = splitParameters( sascha@1015: parameters, new HashMap()); sascha@970: sascha@1016: String [] outs = outsString == null sascha@1016: ? new String [0] sascha@1016: : outsString.split("\\s*,\\s*"); sascha@3076: sascha@1015: Recommendations rec = Recommendations.getInstance(); sascha@1015: rec.recommend( sascha@1015: flysArtifact, userId, outs, data, result); sascha@970: sascha@970: return result; sascha@970: } sascha@372: } sascha@372: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :