comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/CoordinateSelectionState.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents 292fbcd5e9ac
children 9a828e5a2390
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 /**
2 *
3 */
4 package de.intevation.gnv.state;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import org.apache.log4j.Logger;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16
17 import com.vividsolutions.jts.geom.Point;
18
19 import de.intevation.artifactdatabase.Config;
20 import de.intevation.gnv.geobackend.base.Result;
21 import de.intevation.gnv.state.describedata.DefaultKeyValueDescribeData;
22 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
23 import de.intevation.gnv.state.describedata.NamedArrayList;
24 import de.intevation.gnv.state.describedata.NamedCollection;
25 import de.intevation.gnv.utils.ArtifactXMLUtilities;
26 import de.intevation.gnv.utils.InputValidator;
27 import de.intevation.gnv.utils.WKTUtils;
28 import de.intevation.gnv.utils.exception.ValidationException;
29
30 /**
31 * @author Tim Englich <tim.englich@intevation.de>
32 *
33 */
34 public class CoordinateSelectionState extends StateBase {
35
36 /**
37 * the logger, used to log exceptions and additonaly information
38 */
39 private static Logger log = Logger.getLogger(CoordinateSelectionState.class);
40
41 /**
42 * The UID of this Class
43 */
44 private static final long serialVersionUID = 6318923553625195063L;
45
46 /**
47 * XPATH Expressions for the setup.
48 */
49
50 private final static String MESH_WIDTH_XPATH = "mesh-width";
51 private final static String XLINK_XPATH = "xlink:href";
52 private final static String MESH_LIST_XPATH = "/mesh-widths/mesh";
53 private final static String ID_XPATH = "id";
54 private final static String WIDTH_VALUE_XPATH = "width";
55
56 /**
57 * Holds all given Widths between two MeshPoints for different Meshes.
58 */
59 private HashMap<String, Double> meshWidths = null;
60
61 /**
62 * Constructor
63 */
64 public CoordinateSelectionState() {
65 super();
66 }
67
68 @Override
69 protected List<Object> purifyResult(Collection<Result> result, String uuid) {
70 log.debug("CoordinateSelectionState.purifyResult");
71 List<Object> describeData = new ArrayList<Object>();
72
73 NamedCollection<KeyValueDescibeData> keyValueDescibeData =
74 extractKVP(result, "FEATUREID", "SHAPE");
75
76 describeData.add(keyValueDescibeData);
77 return describeData;
78 }
79
80 /**
81 * @see de.intevation.gnv.state.StateBase#prepareInputData4RegionDBQuery(java.lang.String)
82 */
83 @Override
84 protected String prepareInputData4RegionDBQuery(String value) {
85 log.debug("CoordinateSelectionState.prepareInputData4RegionDBQuery");
86 double distance=0.;
87 String returnValue = null;
88 try {
89 Point center = new InputValidator().getPointValue(value);
90 String meshId = super.inputData.get("meshid").getValue();
91 int segments = 97;
92 if (meshWidths != null){
93 Double distanceValue = this.meshWidths.get(meshId);
94 if (distanceValue != null){
95 log.debug("User "+distanceValue+" as Buffer around given Point");
96 distance = distanceValue.doubleValue();
97 }else{
98 log.warn("No distance is configured for Mesh with id"+ meshId);
99 }
100 }else{
101 log.warn("No MeshWidth configured. Check if this is correct.");
102 }
103 returnValue = center.buffer(distance,segments).toText();
104 } catch (NumberFormatException e) {
105 log.error(e,e);
106 } catch (ValidationException e) {
107 log.error(e,e);
108 }
109 return returnValue;
110
111 }
112
113 /**
114 * @see de.intevation.gnv.state.StateBase#extractKVP(java.util.Collection, java.lang.String, java.lang.String)
115 */
116 @Override
117 protected NamedCollection<KeyValueDescibeData> extractKVP(
118 Collection<Result> result,
119 String keyid,
120 String valueid) {
121 Iterator<Result> rit = result.iterator();
122 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(
123 this.dataName, result.size());
124 keyValueDescibeData.setMultiSelect(this.dataMultiSelect);
125 String prevKey = null;
126 while (rit.hasNext()) {
127 Result resultValue = rit.next();
128 String key = resultValue.getString(keyid);
129 if(prevKey == null || !prevKey.equals(key)){ // TODO: FIXME: We have to do that because the arcsde does not support a distinct Query on Layers
130 String geomString = this.convert2DisplayCoordinate(resultValue.getString(valueid));
131 String value = geomString;
132 if (resultValue.getResultDescriptor().getColumnIndex("VALUE") > 0){
133 value = resultValue.getString("VALUE") + " - "+value;
134 }
135
136
137 keyValueDescibeData.add(new DefaultKeyValueDescribeData(key,value ));
138 }
139 prevKey = key;
140 }
141 return keyValueDescibeData;
142 }
143
144 protected static String convert2DisplayCoordinate(String wkt){
145 return WKTUtils.toText(wkt);
146 }
147
148 /**
149 * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node)
150 */
151 @Override
152 public void setup(Node configuration) {
153 super.setup(configuration);
154 Element widthElement = (Element)Config.getNodeXPath(configuration, MESH_WIDTH_XPATH);
155
156 if (widthElement != null){
157 String fileName = widthElement.getAttribute(XLINK_XPATH);
158 fileName = Config.replaceConfigDir(fileName);
159 Node configurationNode = new ArtifactXMLUtilities().readConfiguration(fileName);
160 NodeList meshNodes = Config.getNodeSetXPath(configurationNode,MESH_LIST_XPATH );
161 if (meshNodes != null){
162 meshWidths = new HashMap<String, Double>(meshNodes.getLength());
163 for (int i = 0; i < meshNodes.getLength(); i++){
164 Element meshNode = (Element)meshNodes.item(i);
165 String id = meshNode.getAttribute(ID_XPATH);
166 Double value = Double.parseDouble(meshNode.getAttribute(WIDTH_VALUE_XPATH));
167 meshWidths.put(id, value);
168 }
169 }
170 }else{
171 log.warn("No Mesh Width defined.");
172 }
173 }
174 }

http://dive4elements.wald.intevation.org