ingo@301: package de.intevation.flys.artifacts.services;
ingo@301:
ingo@301: import java.math.BigDecimal;
ingo@301: import java.util.List;
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: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
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@301:
ingo@301: import de.intevation.flys.artifacts.model.AnnotationsFactory;
ingo@301:
sascha@314: import org.hibernate.Session;
ingo@301:
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:
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: Document result = XMLUtils.newDocument();
ingo@301:
ingo@301: String river = XMLUtils.xpathString(
ingo@301: data, "/art:river/text()", ArtifactNamespaceContext.INSTANCE);
ingo@301:
ingo@301: if (river == null || river.trim().length() == 0) {
ingo@301: logger.warn("No river specified. Cannot return distance info!");
ingo@301: return result;
ingo@301: }
ingo@301:
ingo@301: logger.debug("Search distances for river: " + river);
ingo@301:
ingo@301: ElementCreator ec = new ElementCreator(
ingo@301: result,
ingo@301: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@301: ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@301:
sascha@314: Session session = SessionHolder.acquire();
sascha@314: try {
sascha@314: List annotations = AnnotationsFactory.getAnnotations(river);
ingo@301:
sascha@314: if (annotations == null || annotations.size() == 0) {
sascha@314: logger.warn("No information found for the specified river!");
sascha@314: return result;
sascha@314: }
ingo@301:
sascha@314: Element all = ec.create("distances");
sascha@314:
sascha@314: for (Annotation a: annotations) {
sascha@314: Element distance = buildDistanceNode(ec, a);
sascha@314:
sascha@314: if (distance != null) {
sascha@314: all.appendChild(distance);
sascha@314: }
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@301: protected Element buildDistanceNode(ElementCreator ec, Annotation anno) {
ingo@301: Position pos = anno.getPosition();
ingo@301: Range range = anno.getRange();
ingo@301: Attribute attr = anno.getAttribute();
ingo@301:
ingo@301: BigDecimal a = range.getA();
ingo@301: BigDecimal b = range.getB();
ingo@301:
ingo@301: Element distance = ec.create("distance");
ingo@301: ec.addAttr(distance, "description", pos.getValue(), true);
ingo@301: ec.addAttr(distance, "from", a != null ? a.toString() : "", true);
ingo@301: ec.addAttr(distance, "to", b != null ? b.toString() : "", true);
ingo@301: ec.addAttr(distance, "riverside", attr.getValue(), true);
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 :