ingo@2683: package de.intevation.flys.artifacts;
ingo@2683: 
ingo@2683: import java.util.List;
ingo@2683: 
ingo@2683: import org.w3c.dom.Document;
ingo@2683: 
ingo@2683: import org.apache.log4j.Logger;
ingo@2683: 
ingo@2683: import com.vividsolutions.jts.geom.Envelope;
ingo@2683: 
ingo@2683: import de.intevation.artifacts.ArtifactFactory;
ingo@2683: import de.intevation.artifacts.CallMeta;
ingo@2683: 
ingo@2683: import de.intevation.artifactdatabase.state.DefaultOutput;
ingo@2683: import de.intevation.artifactdatabase.state.Facet;
ingo@2683: import de.intevation.artifactdatabase.state.State;
ingo@2683: 
ingo@2683: import de.intevation.flys.model.River;
ingo@2683: import de.intevation.flys.model.HydrBoundaryPoly;
ingo@2683: 
ingo@2683: import de.intevation.flys.artifacts.model.FacetTypes;
ingo@2683: import de.intevation.flys.artifacts.model.RiverFactory;
ingo@2683: import de.intevation.flys.utils.FLYSUtils;
ingo@3301: import de.intevation.flys.utils.GeometryUtils;
ingo@2683: 
ingo@2683: 
ingo@2683: public class WMSHydrBoundaryPolyArtifact extends WMSDBArtifact {
ingo@2683: 
ingo@2683:     public static final String NAME = "hydr_boundary_poly";
ingo@2683: 
ingo@2683: 
ingo@2683:     private static final Logger logger =
ingo@2683:         Logger.getLogger(WMSHydrBoundaryPolyArtifact.class);
ingo@2683: 
ingo@2683: 
ingo@2683:     @Override
ingo@2683:     public void setup(
ingo@2683:         String          identifier,
ingo@2683:         ArtifactFactory factory,
ingo@2683:         Object          context,
ingo@2683:         CallMeta        callMeta,
ingo@2683:         Document        data)
ingo@2683:     {
ingo@2683:         logger.debug("WMSHydrBoundaryArtifact.setup");
ingo@2683: 
ingo@2683:         super.setup(identifier, factory, context, callMeta, data);
ingo@2683:     }
ingo@2683: 
ingo@2683: 
ingo@2683:     @Override
ingo@2683:     public String getName() {
ingo@2683:         return NAME;
ingo@2683:     }
ingo@2683: 
ingo@2683: 
ingo@2683:     @Override
ingo@2683:     public State getCurrentState(Object cc) {
ingo@2683:         State s = new HydrBoundaryPolyState(this);
ingo@2683: 
ingo@2683:         List<Facet> fs = facets.get(getCurrentStateId());
ingo@2683: 
ingo@2683:         DefaultOutput o = new DefaultOutput(
ingo@2683:             "floodmap",
ingo@2683:             "floodmap",
ingo@2683:             "image/png",
ingo@2683:             fs,
ingo@2683:             "map");
ingo@2683: 
ingo@2683:         s.getOutputs().add(o);
ingo@2683: 
ingo@2683:         return s;
ingo@2683:     }
ingo@2683: 
ingo@2683: 
ingo@2683:     public static class HydrBoundaryPolyState extends WMSDBState implements FacetTypes
ingo@2683:     {
ingo@2683:         private static final Logger logger =
ingo@2683:             Logger.getLogger(HydrBoundaryPolyState.class);
ingo@2683: 
ingo@2683:         protected int riverId;
ingo@2683: 
ingo@2683:         public HydrBoundaryPolyState(WMSDBArtifact artifact) {
ingo@2683:             super(artifact);
ingo@2683:             riverId = 0;
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getFacetType() {
ingo@2683:             return FLOODMAP_HYDR_BOUNDARY_POLY;
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getUrl() {
ingo@2683:             return FLYSUtils.getUserWMSUrl(artifact.identifier());
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getSrid() {
ingo@2683:             River river = RiverFactory.getRiver(getRiverId());
ingo@2683:             return FLYSUtils.getRiverSrid(river.getName());
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@3918:         protected Envelope getExtent(boolean reproject) {
ingo@2683:             List<HydrBoundaryPoly> boundaries = HydrBoundaryPoly.getHydrBoundaries(
ingo@2683:                 getRiverId(), getName());
ingo@2683: 
ingo@2683:             Envelope max = null;
ingo@2683: 
ingo@2683:             for (HydrBoundaryPoly b: boundaries) {
ingo@2683:                 Envelope env = b.getGeom().getEnvelopeInternal();
ingo@2683: 
ingo@2683:                 if (max == null) {
ingo@2683:                     max = env;
ingo@2683:                     continue;
ingo@2683:                 }
ingo@2683: 
ingo@2683:                 max.expandToInclude(env);
ingo@2683:             }
ingo@2683: 
ingo@3918:             return max != null && reproject
ingo@3301:                 ? GeometryUtils.transform(max, getSrid())
ingo@3301:                 : max;
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getFilter() {
ingo@2683:             return "river_id=" + String.valueOf(getRiverId())
ingo@2683:                 + " AND name='" + getName() + "'";
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getDataString() {
ingo@2683:             String srid = getSrid();
ingo@2683: 
ingo@2683:             if (FLYSUtils.isUsingOracle()) {
ingo@2683:                 return "geom FROM hydr_boundaries USING SRID " + srid;
ingo@2683:             }
ingo@2683:             else {
ingo@2683:                 return "geom FROM hydr_boundaries USING UNIQUE id USING SRID " + srid;
ingo@2683:             }
ingo@2683:         }
ingo@2683: 
ingo@2683:         @Override
ingo@2683:         protected String getGeometryType() {
ingo@2683:             return "POLYGON";
ingo@2683:         }
ingo@2683:     } // end of HydrBoundaryState
ingo@2683: }
ingo@2683: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :