001: /*
002: * Created on Jun 28, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.vfny.geoserver.action.validation;
008:
009: import com.vividsolutions.jts.geom.Envelope;
010: import org.geotools.data.DataStore;
011: import org.geotools.data.FeatureReader;
012: import org.geotools.data.FeatureSource;
013: import org.geotools.data.Repository;
014: import org.geotools.feature.FeatureCollection;
015: import org.geotools.validation.ValidationProcessor;
016: import org.geotools.validation.ValidationResults;
017: import org.geotools.validation.Validator;
018: import org.vfny.geoserver.config.DataConfig;
019: import org.vfny.geoserver.config.DataStoreConfig;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.logging.Logger;
027: import javax.servlet.ServletContext;
028: import javax.servlet.http.HttpServletRequest;
029:
030: /**
031: * @author rgould
032: * @author bowens
033: *
034: * TODO To change the template for this generated type comment go to
035: * Window - Preferences - Java - Code Style - Code Templates
036: */
037: public class ValidationRunnable implements Runnable {
038: /** Standard logging instance for class */
039: private static final Logger LOGGER = org.geotools.util.logging.Logging
040: .getLogger("org.vfny.geoserver.responses");
041: private Map testSuites;
042: private Map plugins;
043: private DataConfig dataConfig;
044: private ServletContext context;
045: private HttpServletRequest request;
046: Validator validator;
047: ValidationProcessor gv;
048: public TestValidationResults results; // I get filled up with goodies
049: Repository repository;
050: public final static String KEY = "validationTestDoItThread.key";
051:
052: //public ValidationRunnable(Map ts, Map plugins, DataConfig dataConfig, ServletContext context, HttpServletRequest request) throws Exception
053: public ValidationRunnable(HttpServletRequest request) {
054: // this.testSuites = ts; // what tests we are actually running
055: // this.plugins = plugins; // every possible plugin
056: // this.dataConfig = dataConfig;
057: // this.context = context;
058: this .request = request;
059:
060: /*
061: LOGGER.finer("testSuites.size() = " + testSuites.size());
062: LOGGER.finer("plugins.size() = " + plugins.size());
063: LOGGER.finer("" + (TestSuiteDTO) testSuites.values().toArray()[0]);
064:
065: //DataConfig dataConfig = (DataConfig) getDataConfig();
066: Map dataStoreConfigs = dataConfig.getDataStores();
067: DefaultRepository dataRepository = new DefaultRepository();
068: Iterator it = dataStoreConfigs.keySet().iterator();
069:
070: // get all the data stores and build up our dataRepository
071: while (it.hasNext())
072: {
073: String dsKey = it.next().toString();
074: DataStoreConfig dsc = (DataStoreConfig) dataStoreConfigs.get(dsKey);
075: DataStore ds = null;
076: try {
077: ds = dsc.findDataStore(context);
078: dataRepository.register(dsKey, ds);
079: }
080: catch (Exception e) {
081: e.printStackTrace();
082: }
083: }
084:
085: validator = new Validator(dataRepository, gv);
086: */
087: }
088:
089: public void setup(TestValidationResults vr, Repository repo,
090: Map plugins, Map testSuites) throws Exception {
091: gv = new ValidationProcessor();
092: gv.load(plugins, testSuites);
093: results = vr;
094: repository = repo;
095: validator = new Validator(repository, gv);
096: }
097:
098: /* (non-Javadoc)
099: * @see java.lang.Runnable#run()
100: */
101: public void run() {
102: //GeoValidator gv = new GeoValidator(testSuites,plugins);
103: //Map dataStores = dataConfig.getDataStores();
104: //TestValidationResults vr = runTransactions(dataStores,gv,context);
105: request.getSession().setAttribute(
106: TestValidationResults.CURRENTLY_SELECTED_KEY, results);
107:
108: Map dataStores = repository.getFeatureSources();
109: Iterator it = dataStores.entrySet().iterator();
110:
111: // Go through each data store and run the featureValidation test on
112: // each feature type
113: while (it.hasNext()) {
114: Map.Entry entry = (Map.Entry) it.next();
115: String typeRef = (String) entry.getKey();
116: FeatureSource featureSource = (FeatureSource) entry
117: .getValue();
118: String dataStoreId = typeRef.split(":")[0];
119:
120: try {
121: LOGGER.finer(dataStoreId + ": feature validation, "
122: + featureSource);
123:
124: FeatureCollection features = featureSource
125: .getFeatures();
126: validator.featureValidation(dataStoreId, features,
127: results);
128: } catch (Exception e1) {
129: e1.printStackTrace();
130: }
131: }
132:
133: /** ------------------------------------------------------------------ */
134:
135: /** run INTEGRITY validations */
136:
137: // this is stupid
138: Envelope env = new Envelope(Integer.MIN_VALUE,
139: Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
140:
141: // a map of typeref -> DataSource
142: try {
143: Map featureSources = repository.getFeatureSources();
144: LOGGER.finer("integrity tests entry for "
145: + featureSources.size() + " dataSources.");
146: validator.integrityValidation(featureSources, env, results);
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150:
151: results.run = true;
152: }
153:
154: private TestValidationResults runTransactions(Map dsm,
155: ValidationProcessor v, ServletContext sc) {
156: if ((dsm == null) || (dsm.size() == 0)) {
157: System.out.println("No Datastores were defined.");
158:
159: return null;
160: }
161:
162: if (v == null) {
163: System.err
164: .println("An error occured: Cannot run without a ValidationProcessor.");
165:
166: return null;
167: }
168:
169: TestValidationResults vr = new TestValidationResults();
170: Iterator i = dsm.keySet().iterator();
171:
172: //TODO: we only get one datastore here when we may need more than that
173: // do another pass through it and get those typesNames
174: List dataStores = new ArrayList();
175: try {
176: while (i.hasNext()) {
177: Map sources = new HashMap();
178: String key = i.next().toString();
179: DataStoreConfig dsc = (DataStoreConfig) dsm.get(key);
180:
181: try {
182: DataStore ds = dsc.findDataStore(sc);
183: dataStores.add(ds);
184: String[] ss = ds.getTypeNames();
185:
186: for (int j = 0; j < ss.length; j++) {
187: FeatureSource fs = ds.getFeatureSource(ss[j]);
188: sources.put(dsc.getId() + ":" + ss[j], fs);
189:
190: //v.runFeatureTests(dsc.getId(),fs.getSchema(),
191: // fs.getFeatures().collection(), (ValidationResults) vr);
192: System.out.println("Feature Test Results for "
193: + key + ":" + ss[j]);
194: System.out.println(vr.toString());
195: }
196: } catch (Exception e) {
197: e.printStackTrace();
198: }
199:
200: Envelope env = new Envelope(Integer.MIN_VALUE,
201: Integer.MIN_VALUE, Integer.MAX_VALUE,
202: Integer.MAX_VALUE);
203:
204: try {
205: v.runIntegrityTests(sources.keySet(), sources, env,
206: (ValidationResults) vr);
207: System.out
208: .println("Feature Integrety Test Results");
209: System.out.println(vr.toString());
210: } catch (Exception e) {
211: e.printStackTrace();
212: }
213: }
214: } finally {
215: for (Iterator it = dataStores.iterator(); it.hasNext();) {
216: DataStore ds = (DataStore) it.next();
217: ds.dispose();
218: }
219: }
220:
221: return vr;
222: }
223: }
|