001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.validation;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import net.refractions.udig.core.enums.Priority;
022: import net.refractions.udig.issues.FeatureIssue;
023: import net.refractions.udig.project.ILayer;
024: import net.refractions.udig.project.IMap;
025: import net.refractions.udig.project.internal.Layer;
026: import net.refractions.udig.project.ui.ApplicationGIS;
027: import net.refractions.udig.validation.internal.Messages;
028:
029: import org.geotools.feature.Feature;
030: import org.geotools.validation.Validation;
031: import org.geotools.validation.ValidationResults;
032:
033: /**
034: * A generic version of the validation results class which returns mostly everything you would want
035: * to know about the validation results.
036: * <p>
037: * </p>
038: *
039: * @since 1.0.1
040: */
041: public class GenericValidationResults implements ValidationResults,
042: Iterable<Feature> {
043: public ArrayList<Validation> validationList;
044: public ArrayList<Feature> failedFeatures;
045: public ArrayList<Feature> warningFeatures;
046: public ArrayList<String> failureMessages;
047: public ArrayList<String> warningMessages;
048: public ArrayList<FeatureIssue> issues;
049:
050: /**
051: * GenericValidationResults constructor.
052: * <p>
053: * Description
054: * </p>
055: *
056: */
057: public GenericValidationResults() {
058: validationList = new ArrayList<Validation>();
059: failedFeatures = new ArrayList<Feature>();
060: warningFeatures = new ArrayList<Feature>();
061: failureMessages = new ArrayList<String>();
062: warningMessages = new ArrayList<String>();
063: issues = new ArrayList<FeatureIssue>();
064:
065: }
066:
067: /**
068: * Override setValidation.
069: * <p>
070: * Description ...
071: * </p>
072: * @see org.geotools.validation.ValidationResults#setValidation(org.geotools.validation.Validation)
073: *
074: * @param validation
075: */
076: public void setValidation(Validation validation) {
077: if (!validationList.contains(validation)) { //only add each validation once
078: validationList.add(validation);
079: }
080: }
081:
082: /**
083: * Override error.
084: * <p>
085: * Description ...
086: * </p>
087: * @see org.geotools.validation.ValidationResults#error(org.geotools.feature.Feature, java.lang.String)
088: *
089: * @param feature
090: * @param message
091: */
092: public void error(Feature feature, String message) {
093: //add the error to our list of failed features + failure messages
094: if (message == null)
095: message = ""; //$NON-NLS-1$
096: failedFeatures.add(feature);
097: failureMessages.add(feature.getID() + ": " + message); //$NON-NLS-1$
098: //find the layer of the current feature
099: IMap activeMap = ApplicationGIS.getActiveMap();
100: List<ILayer> layers = new ArrayList<ILayer>();
101: if (activeMap != null) {
102: layers.addAll(activeMap.getMapLayers());
103: }
104: Layer layer = null;
105: for (Iterator i = layers.listIterator(); i.hasNext();) {
106: Layer this Layer = (Layer) i.next();
107: if (feature.getFeatureType().getTypeName().equals(
108: this Layer.getName())) {
109: layer = this Layer;
110: break;
111: }
112: }
113: //add the error to the issues list
114: FeatureIssue issue = new FeatureIssue(Priority.HIGH, message,
115: layer, feature,
116: Messages.GenericValidationResults_validationError);
117: issues.add(issue);
118: }
119:
120: /**
121: * Override warning.
122: * <p>
123: * Description ...
124: * </p>
125: * @see org.geotools.validation.ValidationResults#warning(org.geotools.feature.Feature, java.lang.String)
126: *
127: * @param feature
128: * @param message
129: */
130: public void warning(Feature feature, String message) {
131: //add the warning to our list of warned features + warning messages
132: warningFeatures.add(feature);
133: warningMessages.add(feature.getID() + ": " + message); //$NON-NLS-1$
134: //find the layer of the current feature
135: List<ILayer> layers = ApplicationGIS.getActiveMap()
136: .getMapLayers();
137: ILayer layer = null;
138: for (Iterator i = layers.listIterator(); i.hasNext();) {
139: layer = (ILayer) i.next();
140: if (feature.getFeatureType().getTypeName().equals(
141: layer.getName())) {
142: break;
143: }
144: }
145: //add the error to the issues list
146: FeatureIssue issue = new FeatureIssue(Priority.WARNING,
147: message, layer, feature,
148: Messages.GenericValidationResults_validationWarning);
149: issues.add(issue);
150: }
151:
152: /**
153: * returns the failed features from validation
154: */
155: public Iterator<Feature> iterator() {
156: return failedFeatures.iterator();
157: }
158:
159: }
|