comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java @ 335:e964a3d8f7bc

Some Refactoring work done. Moved Transition to State gnv-artifacts/trunk@401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 08 Dec 2009 08:39:03 +0000
parents
children bed9735adf84
comparison
equal deleted inserted replaced
334:e37930705daa 335:e964a3d8f7bc
1 /**
2 *
3 */
4 package de.intevation.gnv.state;
5
6 import java.io.OutputStream;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Locale;
10
11
12 import org.apache.log4j.Logger;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import de.intevation.artifactdatabase.Config;
19 import de.intevation.artifacts.CallMeta;
20 import de.intevation.gnv.artifacts.cache.CacheFactory;
21 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
22 import de.intevation.gnv.geobackend.base.Result;
23 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
24 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
25 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
26 import de.intevation.gnv.state.exception.StateException;
27
28 /**
29 * @author Tim Englich <tim.englich@intevation.de>
30 *
31 */
32 public abstract class OutputStateBase extends StateBase implements
33 OutputState {
34
35 /**
36 * The UID of this Class
37 */
38 private static final long serialVersionUID = -1718732895737303823L;
39
40 /**
41 * the logger, used to log exceptions and additonaly information
42 */
43 private static Logger log = Logger.getLogger(OutputStateBase.class);
44
45 /**
46 * The different Outputmodes which are provided by an OutputState
47 */
48 protected Collection<OutputMode> outputModes = null;
49
50 protected String queryODVID = null;
51
52 /**
53 * Constructor
54 */
55 public OutputStateBase() {
56 super();
57 }
58
59 /**
60 * @see de.intevation.gnv.state.OutputState#getOutputModes()
61 */
62 public Collection<OutputMode> getOutputModes() {
63 log.debug("OutputStateBase.getOutputModes");
64 return this.outputModes;
65 }
66
67 /**
68 * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node)
69 */
70 @Override
71 public void setup(Node configuration) {
72 log.debug("OutputStateBase.setup");
73 super.setup(configuration);
74
75 this.queryODVID = Config.getStringXPath(configuration,"queryID-odv");
76
77 NodeList outputModeList = Config.getNodeSetXPath(configuration,
78 "outputsModes/outputsMode");
79 if (outputModeList != null) {
80 log.debug(outputModeList.getLength() + " were found.");
81 this.outputModes = new ArrayList<OutputMode>(outputModeList
82 .getLength());
83 for (int i = 0; i < outputModeList.getLength(); i++) {
84 Element currentNode = (Element)outputModeList.item(i);
85 String name = currentNode.getAttribute("name");
86 String description =currentNode.getAttribute("description");
87 String mimeType = currentNode.getAttribute("mime-type");
88 NodeList inputValuesList = Config.getNodeSetXPath(currentNode,
89 "parameters/inputvalue");
90 Collection<InputValue> inputParameters = null;
91 if (inputValuesList != null) {
92 inputParameters = new ArrayList<InputValue>(inputValuesList
93 .getLength());
94 for (int j = 0; j < inputValuesList.getLength(); j++) {
95 Element currentInputValuesNode = (Element)inputValuesList.item(j);
96 String inputValueName = currentInputValuesNode.getAttribute("name");
97 String inputValueType = currentInputValuesNode.getAttribute("type");
98 String defaultValue =currentInputValuesNode.getAttribute("value");
99 boolean isMultiselect = false;
100 InputValue inputValue = new DefaultInputValue(
101 inputValueName, inputValueType, defaultValue,
102 isMultiselect);
103 inputParameters.add(inputValue);
104 }
105 }
106
107 OutputMode outputMode = new DefaultOutputMode(name,
108 description, mimeType, inputParameters);
109 log.debug(outputMode.toString());
110 this.outputModes.add(outputMode);
111
112 }
113 }
114 }
115
116 /**
117 * @see de.intevation.gnv.state.StateBase#advance()
118 */
119 @Override
120 public void advance(String uuid, CallMeta callMeta)
121 throws StateException {
122 }
123
124 @Override
125 public void initialize(String uuid, CallMeta callMeta)
126 throws StateException {
127 }
128
129 public void out(Document format, Collection<InputData> inputData,
130 OutputStream outputStream, String uuid, CallMeta callMeta)
131 throws StateException {
132 }
133
134 /**
135 * @see de.intevation.gnv.state.OutputState#out(java.lang.String,
136 * java.util.Collection, java.io.OutputStream)
137 */
138 public void out(String outputMode, Collection<InputData> inputData,
139 OutputStream outputStream) throws StateException {
140 }
141
142 /**
143 * @return
144 */
145 protected Collection<Result> getChartResult(String uuid) {
146 log.debug("OutputStateBase.getChartResult");
147 Collection<Result> result = null;
148 if (CacheFactory.getInstance().isInitialized()) {
149 String key = uuid + super.getID();
150 log.debug("Hash for Queryelements: " + key);
151 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
152 if (value != null) {
153 result = (Collection<Result>) (value.getObjectValue());
154 }else{
155 result = this.getData(this.queryID);
156 if (CacheFactory.getInstance().isInitialized()) {
157 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
158 }
159
160 }
161 }
162 return result;
163 }
164
165 protected Object getChartFromCache(String uuid) {
166 log.debug("Fetch chart [" + uuid + "] from cache");
167 CacheFactory cacheFactory = CacheFactory.getInstance();
168 if (cacheFactory.isInitialized()) {
169 String key = "chart_" + uuid + super.getID();
170 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
171
172 if (object != null) {
173 return object.getObjectValue();
174 }
175 }
176 return null;
177 }
178
179 protected Collection<Result> getODVResult(String uuid) {
180 log.debug("OutputStateBase.getODVResult");
181 // TODO add Caching? I think it's not nessessary
182 Collection<Result> returnValue = null;
183 if (this.queryODVID != null){
184 returnValue = this.getData(this.queryODVID);
185 }else{
186 log.warn("No Query for ODV Data is defined.");
187 }
188 return returnValue;
189 }
190
191 /**
192 * @param returnValue
193 * @return
194 */
195 private Collection<Result> getData(String queryID) {
196 log.debug("OutputStateBase.getData");
197 Collection<Result> returnValue = null;
198 try {
199 String[] filterValues = this.generateFilterValuesFromInputData();
200 try {
201 QueryExecutor queryExecutor = QueryExecutorFactory
202 .getInstance()
203 .getQueryExecutor();
204 returnValue = queryExecutor.executeQuery(queryID,filterValues);
205 } catch (RuntimeException e) {
206 log.error(e, e);
207 }
208 } catch (QueryException e) {
209 log.error(e, e);
210 }
211 return returnValue;
212 }
213
214 protected void removeChartResult(String uuid) {
215 log.debug("OutputStateBase.getChartResult");
216 if (CacheFactory.getInstance().isInitialized()) {
217 String key = uuid + super.getID();
218 log.debug("Hash for Queryelements: " + key);
219 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
220 if (value != null) {
221 CacheFactory.getInstance().getCache().remove(key);
222 }
223 }
224 }
225
226 protected void removeChart(String uuid) {
227 log.debug("OutputStateBase.removeChart from cache");
228
229 CacheFactory cacheFactory = CacheFactory.getInstance();
230 if (cacheFactory.isInitialized()) {
231 String key = "chart_" + uuid + super.getID();
232 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
233 if (object != null)
234 cacheFactory.getCache().remove(key);
235 }
236 }
237
238 protected void purifyChart(Object chart, String uuid) {
239 log.debug("Prufify chart [" + uuid + "]");
240 CacheFactory cacheFactory = CacheFactory.getInstance();
241 if (cacheFactory.isInitialized()) {
242 String key = "chart_" + uuid + getID();
243 cacheFactory.getCache().put(new net.sf.ehcache.Element(key, chart));
244 }
245 }
246
247 /**
248 * @see de.intevation.gnv.state.StateBase#putInputData(java.util.Collection, java.lang.String)
249 */
250 @Override
251 public void putInputData(Collection<InputData> inputData,
252 String uuid)
253 throws StateException {
254 log.debug("OutputStateBase.putInputData");
255 this.removeChartResult(uuid);
256 this.removeChart(uuid);
257 super.putInputData(inputData, uuid);
258 }
259
260 public void out(String outputMode, Collection<InputData> inputData,
261 OutputStream outputStream, String uuid, CallMeta callMeta)
262 throws StateException {
263 }
264
265
266 protected String getMessage(Locale locale, String key, String value) {
267 return RessourceFactory.getInstance().getRessource(
268 locale,
269 key,
270 value
271 );
272 }
273 }

http://dive4elements.wald.intevation.org