ingo@301: package de.intevation.flys.artifacts.services; ingo@301: ingo@301: import java.math.BigDecimal; sascha@644: import java.util.Iterator; ingo@301: ingo@301: import org.apache.log4j.Logger; ingo@301: ingo@301: import org.w3c.dom.Document; ingo@301: import org.w3c.dom.Element; ingo@301: ingo@301: import de.intevation.artifacts.CallMeta; ingo@301: ingo@301: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@301: import de.intevation.artifacts.common.utils.XMLUtils; ingo@301: ingo@301: import de.intevation.artifactdatabase.DefaultService; ingo@301: ingo@316: import de.intevation.flys.backend.SessionHolder; ingo@301: import de.intevation.flys.model.Annotation; ingo@301: import de.intevation.flys.model.Attribute; ingo@301: import de.intevation.flys.model.Position; ingo@301: import de.intevation.flys.model.Range; ingo@686: import de.intevation.flys.model.Edge; ingo@301: ingo@301: import de.intevation.flys.artifacts.model.AnnotationsFactory; ingo@301: sascha@314: import org.hibernate.Session; ingo@301: sascha@644: import de.intevation.flys.artifacts.cache.CacheFactory; sascha@644: sascha@644: import net.sf.ehcache.Cache; sascha@644: ingo@301: /** ingo@301: * This service provides information about distances of a specified river. ingo@301: * ingo@301: * @author Ingo Weinzierl ingo@301: */ ingo@301: public class DistanceInfoService extends DefaultService { ingo@301: ingo@301: /** The logger used in this service.*/ ingo@301: private static Logger logger = Logger.getLogger(DistanceInfoService.class); ingo@301: sascha@644: public static final String CACHE_NAME = "service-distanceinfo"; sascha@644: sascha@644: public static final String RIVER_XPATH = "/art:river/text()"; sascha@644: ingo@301: ingo@301: /** ingo@301: * The default constructor. ingo@301: */ ingo@301: public DistanceInfoService() { ingo@301: } ingo@301: ingo@301: ingo@301: public Document process( ingo@301: Document data, ingo@301: Object globalContext, ingo@301: CallMeta callMeta) ingo@301: { ingo@301: logger.debug("DistanceInfoService.process"); ingo@301: ingo@301: String river = XMLUtils.xpathString( sascha@644: data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); ingo@301: sascha@644: if (river == null || (river = river.trim()).length() == 0) { ingo@301: logger.warn("No river specified. Cannot return distance info!"); sascha@644: return XMLUtils.newDocument(); ingo@301: } ingo@301: ingo@301: logger.debug("Search distances for river: " + river); ingo@301: sascha@644: Cache cache = CacheFactory.getCache(CACHE_NAME); sascha@644: sascha@644: if (cache == null) { sascha@644: logger.debug("no cache configured for distance info"); sascha@644: return getUncached(river); sascha@644: } sascha@644: sascha@644: net.sf.ehcache.Element element = cache.get(river); sascha@644: sascha@644: if (element != null) { sascha@644: logger.debug("distance info found in cache"); sascha@644: return (Document)element.getValue(); sascha@644: } sascha@644: sascha@644: Document result = getUncached(river); sascha@644: sascha@644: element = new net.sf.ehcache.Element(river, result); sascha@644: sascha@644: logger.debug("store distance info found into cache"); sascha@644: sascha@644: cache.put(element); sascha@644: sascha@644: return result; sascha@644: } sascha@644: sascha@644: protected Document getUncached(String river) { sascha@644: sascha@644: Document result = XMLUtils.newDocument(); sascha@644: sascha@314: Session session = SessionHolder.acquire(); ingo@301: sascha@644: try { sascha@644: Iterator iter = sascha@644: AnnotationsFactory.getAnnotationsIterator(river); ingo@301: ingo@686: Element all = result.createElement("distances"); sascha@314: sascha@644: while (iter.hasNext()) { sascha@644: Annotation a = iter.next(); ingo@686: Element distance = buildDistanceNode(result, a); sascha@644: all.appendChild(distance); ingo@301: } sascha@314: sascha@314: result.appendChild(all); ingo@301: } sascha@314: finally { sascha@314: session.close(); sascha@314: SessionHolder.release(); sascha@314: } ingo@301: ingo@301: return result; ingo@301: } ingo@301: ingo@301: ingo@301: /** ingo@301: * This method build an Element for a distance info. ingo@301: * ingo@301: * @param ec The ElementCreator. ingo@301: * @param anno The Annotation that provides information about the distance. ingo@301: * ingo@301: * @return an Element that contains information about a distance. ingo@301: */ ingo@686: protected static Element buildDistanceNode( ingo@686: Document document, ingo@686: Annotation anno ingo@686: ) { ingo@686: Position pos = anno.getPosition(); ingo@686: Range range = anno.getRange(); ingo@686: Attribute attr = anno.getAttribute(); ingo@686: Edge edge = anno.getEdge(); ingo@686: BigDecimal a = range.getA(); ingo@686: BigDecimal b = range.getB(); ingo@301: ingo@686: Element distance = document.createElement("distance"); ingo@301: ingo@686: distance.setAttribute("description", pos.getValue()); ingo@686: ingo@686: String riverSide = attr.getValue(); ingo@686: ingo@686: if (riverSide != null && riverSide.length() > 0) { ingo@686: distance.setAttribute("riverside", riverSide); ingo@686: } ingo@686: ingo@686: if (a != null) { ingo@686: distance.setAttribute("from", a.toString()); ingo@686: } ingo@686: if (b != null) { ingo@686: distance.setAttribute("to", b.toString()); ingo@686: } ingo@686: if (edge != null) { ingo@686: BigDecimal bottom = edge.getBottom(); ingo@686: BigDecimal top = edge.getTop(); ingo@686: if (bottom != null) { ingo@686: distance.setAttribute("bottom", bottom.toString()); ingo@686: } ingo@686: if (top != null) { ingo@686: distance.setAttribute("top", top.toString()); ingo@686: } ingo@686: } ingo@301: ingo@301: return distance; ingo@301: } ingo@301: } ingo@301: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :