ingo@1870: package de.intevation.flys.artifacts;
ingo@1870: 
ingo@1870: import java.util.List;
ingo@1870: 
ingo@1870: import org.w3c.dom.Document;
ingo@1870: 
ingo@1870: import org.apache.log4j.Logger;
ingo@1870: 
ingo@1870: import com.vividsolutions.jts.geom.Envelope;
ingo@1870: 
ingo@1870: import de.intevation.artifacts.ArtifactFactory;
ingo@1870: import de.intevation.artifacts.CallMeta;
ingo@1870: 
ingo@1870: import de.intevation.artifactdatabase.state.DefaultOutput;
ingo@1870: import de.intevation.artifactdatabase.state.Facet;
ingo@1870: import de.intevation.artifactdatabase.state.State;
ingo@1870: 
ingo@1870: import de.intevation.flys.model.River;
ingo@1870: import de.intevation.flys.model.Line;
ingo@1870: 
ingo@1870: import de.intevation.flys.artifacts.model.FacetTypes;
ingo@1870: import de.intevation.flys.artifacts.model.RiverFactory;
ingo@1870: import de.intevation.flys.utils.FLYSUtils;
ingo@3301: import de.intevation.flys.utils.GeometryUtils;
ingo@1870: 
ingo@1870: 
ingo@1870: public class WMSLineArtifact extends WMSDBArtifact {
ingo@1870: 
ingo@1870:     public static final String NAME = "lines";
ingo@1870: 
ingo@1870: 
ingo@1870:     private static final Logger logger =
ingo@1870:         Logger.getLogger(WMSLineArtifact.class);
ingo@1870: 
ingo@1870: 
ingo@1870:     @Override
ingo@1870:     public void setup(
ingo@1870:         String          identifier,
ingo@1870:         ArtifactFactory factory,
ingo@1870:         Object          context,
ingo@1870:         CallMeta        callMeta,
ingo@1870:         Document        data)
ingo@1870:     {
ingo@1870:         logger.debug("WMSLineArtifact.setup");
ingo@1870: 
ingo@1870:         super.setup(identifier, factory, context, callMeta, data);
ingo@1870:     }
ingo@1870: 
ingo@1870: 
ingo@1870:     @Override
ingo@1870:     public String getName() {
ingo@1870:         return NAME;
ingo@1870:     }
ingo@1870: 
ingo@1870: 
ingo@1870:     @Override
ingo@1870:     public State getCurrentState(Object cc) {
ingo@1870:         State s = new LineState(this);
ingo@1870: 
ingo@1870:         List<Facet> fs = facets.get(getCurrentStateId());
ingo@1870: 
ingo@1870:         DefaultOutput o = new DefaultOutput(
ingo@1870:             "floodmap",
ingo@1870:             "floodmap",
ingo@1870:             "image/png",
ingo@1870:             fs,
ingo@1870:             "map");
ingo@1870: 
ingo@1870:         s.getOutputs().add(o);
ingo@1870: 
ingo@1870:         return s;
ingo@1870:     }
ingo@1870: 
ingo@1870: 
ingo@1870:     public static class LineState extends WMSDBState implements FacetTypes
ingo@1870:     {
ingo@1870:         private static final Logger logger =
ingo@1870:             Logger.getLogger(LineState.class);
ingo@1870: 
ingo@1870:         protected int riverId;
ingo@1870: 
ingo@1870:         public LineState(WMSDBArtifact artifact) {
ingo@1870:             super(artifact);
ingo@1870:             riverId = 0;
ingo@1870:         }
ingo@1870: 
ingo@1870:         public int getRiverId() {
ingo@1870:             if (riverId == 0) {
ingo@2683:                 String   ids   = artifact.getDataAsString("ids");
ingo@2683:                 String[] parts = ids.split(";");
ingo@1870: 
ingo@1870:                 try {
sascha@3405:                     riverId = Integer.parseInt(parts[0]);
ingo@1870:                 }
ingo@1870:                 catch (NumberFormatException nfe) {
ingo@2683:                     logger.error("Cannot parse river id from '" + parts[0] + "'");
ingo@1870:                 }
ingo@1870:             }
ingo@1870: 
ingo@1870:             return riverId;
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getFacetType() {
ingo@1870:             return FLOODMAP_LINES;
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getUrl() {
ingo@1870:             return FLYSUtils.getUserWMSUrl(artifact.identifier());
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getSrid() {
ingo@1870:             River river = RiverFactory.getRiver(getRiverId());
ingo@1870:             return FLYSUtils.getRiverSrid(river.getName());
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected Envelope getExtent() {
ingo@2683:             List<Line> lines = Line.getLines(getRiverId(), getName());
ingo@1870: 
ingo@1870:             Envelope max = null;
ingo@1870: 
ingo@1870:             for (Line l: lines) {
ingo@1870:                 Envelope env = l.getGeom().getEnvelopeInternal();
ingo@1870: 
ingo@1870:                 if (max == null) {
ingo@1870:                     max = env;
ingo@1870:                     continue;
ingo@1870:                 }
ingo@1870: 
ingo@1870:                 max.expandToInclude(env);
ingo@1870:             }
ingo@1870: 
ingo@3301:             return max != null
ingo@3301:                 ? GeometryUtils.transform(max, getSrid())
ingo@3301:                 : max;
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getFilter() {
ingo@2683:             return "river_id=" + String.valueOf(getRiverId()) +
ingo@2683:                 " AND name='" + getName() + "'";
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getDataString() {
ingo@1870:             String srid = getSrid();
ingo@1870: 
ingo@1870:             if (FLYSUtils.isUsingOracle()) {
ingo@1870:                 return "geom FROM lines USING SRID " + srid;
ingo@1870:             }
ingo@1870:             else {
ingo@1870:                 return "geom FROM lines USING UNIQUE id USING SRID " + srid;
ingo@1870:             }
ingo@1870:         }
ingo@1870: 
ingo@1870:         @Override
ingo@1870:         protected String getGeometryType() {
ingo@1870:             return "LINE";
ingo@1870:         }
ingo@1870:     } // end of WMSKmState
ingo@1870: }
ingo@1870: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :