sascha@372: package de.intevation.flys.artifacts.services; sascha@372: sascha@372: import java.io.InputStream; sascha@372: import java.io.IOException; sascha@372: sascha@372: import java.sql.Connection; sascha@372: import java.sql.SQLException; 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@372: import de.intevation.artifacts.CallMeta; sascha@372: import de.intevation.artifacts.ServiceFactory; sascha@372: sascha@372: import de.intevation.artifactdatabase.DefaultService; sascha@372: sascha@372: import de.intevation.artifacts.common.utils.XMLUtils; sascha@372: sascha@421: import de.intevation.artifacts.common.ArtifactNamespaceContext; sascha@421: sascha@372: import de.intevation.flys.artifacts.services.meta.Builder; sascha@372: sascha@372: import de.intevation.flys.backend.SessionHolder; sascha@372: sascha@372: import org.hibernate.Session; sascha@372: sascha@372: import org.hibernate.jdbc.Work; sascha@372: sascha@372: public class MetaDataService sascha@372: extends DefaultService sascha@372: { sascha@372: private static Logger log = Logger.getLogger(MetaDataService.class); sascha@372: sascha@421: public static final String XPATH_RIVER = "/art:river/text()"; sascha@415: sascha@372: public static final String META_DATA_TEMPLATE = "/metadata/template.xml"; sascha@372: sascha@372: protected Builder builder; sascha@372: sascha@372: public MetaDataService() { sascha@372: } sascha@372: sascha@415: protected static Map extractParameters(Document data) { sascha@415: HashMap parameters = new HashMap(); sascha@415: sascha@421: String river = XMLUtils.xpathString( sascha@421: data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE); sascha@415: sascha@415: if (river == null || (river = river.trim()).length() == 0) { sascha@415: river = "%"; // matches all rivers sascha@415: } sascha@415: sascha@415: parameters.put("river", river); sascha@415: sascha@415: return parameters; sascha@415: } sascha@415: sascha@372: @Override sascha@372: public Document process( sascha@372: Document data, sascha@372: Object globalContext, sascha@372: CallMeta callMeta sascha@372: ) { sascha@372: log.debug("MetaDataService.process"); sascha@372: sascha@372: final Document result = XMLUtils.newDocument(); sascha@372: sascha@372: if (builder == null) { sascha@372: log.error("MetaDataService is not setup properly."); sascha@372: return result; sascha@372: } sascha@372: sascha@415: final Map parameters = extractParameters(data); sascha@415: sascha@372: Session session = SessionHolder.acquire(); sascha@372: try { sascha@372: session.doWork(new Work() { sascha@372: @Override sascha@372: public void execute(Connection connection) sascha@372: throws SQLException sascha@372: { sascha@372: log.debug("MetaDataService.execute"); sascha@415: builder.build(connection, result, parameters); sascha@372: } sascha@372: }); sascha@372: } sascha@372: finally { sascha@372: session.close(); sascha@372: SessionHolder.release(); sascha@372: } sascha@372: sascha@372: return result; sascha@372: } sascha@372: sascha@372: @Override sascha@372: public void setup(ServiceFactory factory, Object globalContext) { sascha@372: log.debug("MetaDataService.setup"); sascha@372: sascha@372: InputStream in = getClass().getResourceAsStream(META_DATA_TEMPLATE); sascha@372: sascha@372: if (in == null) { sascha@372: log.error("cannot get template resource"); sascha@372: return; sascha@372: } sascha@372: sascha@372: try { sascha@372: Document template = XMLUtils.parseDocument(in); sascha@372: if (template == null) { sascha@372: log.error("cannot parse meta data template"); sascha@372: } sascha@372: else { sascha@372: builder = new Builder(template); sascha@372: } sascha@372: } sascha@372: finally { sascha@372: try { sascha@372: in.close(); sascha@372: } sascha@372: catch (IOException ioe) { sascha@372: log.error(ioe); sascha@372: } sascha@372: } sascha@372: } sascha@372: } sascha@372: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :