001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.validation.attributes;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.geotools.feature.FeatureIterator;
024: import org.geotools.data.FeatureSource;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureIterator;
027: import org.geotools.validation.DefaultIntegrityValidation;
028: import org.geotools.validation.ValidationResults;
029:
030: import com.vividsolutions.jts.geom.Envelope;
031:
032: /**
033: * Ensure every feature has a unique Feature Id specified by uniqueID.
034: *
035: * <p>
036: * Please note that featureIDs are not attributes. Attributes may be checked
037: * with the UniquityValidation class.
038: * </p>
039: *
040: * <p>
041: * The FeatureTypes it checks against are defined by typeNames[]. If a
042: * duplicate ID is detected, an error message returned via a Validation Result
043: * used as a visitor in the validation() method.
044: * </p>
045: *
046: * <p>
047: * Example Use:
048: * </p>
049: * <pre><code>
050: * UniqueFIDIntegrityValidation x = new UniqueFIDIntegrityValidation("uniqueFID_road", "Checks if each feature has a unique ID", new String[] {"road", "river"}, "FID");
051: * x.validate();
052: * </code></pre>
053: *
054: * @author bowens, Refractions Research, Inc.
055: * @author $Author: dmzwiers $ (last modification)
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/attributes/UniqueFIDValidation.java $
057: * @version $Id: UniqueFIDValidation.java 22754 2006-11-16 03:03:20Z jgarnett $
058: */
059: public class UniqueFIDValidation extends DefaultIntegrityValidation {
060: /** Type Ref or "" for all */
061: String typeRef;
062:
063: /**
064: * UniqueFIDIntegrityValidation constructor.
065: *
066: * <p>
067: * An empty constructor placed here for Java Beans
068: * </p>
069: */
070: public UniqueFIDValidation() {
071: }
072:
073: /**
074: * UniqueFIDIntegrityValidation constructor.
075: *
076: * <p>
077: * Initializes allinformation needed to perform the validation.
078: * </p>
079: *
080: * @return DOCUMENT ME!
081: */
082:
083: /*public UniqueFIDValidation(String name, String description, String[] typeNames, String uniqueID) {
084: this.name = name;
085: this.description = description;
086: this.typeNames = typeNames;
087: this.uniqueID = uniqueID;
088: }*/
089:
090: /**
091: * Override getPriority.
092: *
093: * <p>
094: * Sets the priority level of this validation.
095: * </p>
096: *
097: * @return A made up priority for this validation.
098: *
099: * @see org.geotools.validation.Validation#getPriority()
100: */
101: public int getPriority() {
102: return 10;
103: }
104:
105: /**
106: * Override validate.
107: *
108: * <p>
109: * Description ... This is supposed to go off and grab the necesary
110: * features from the database using the envelope with the typeNames. But
111: * it doesn't yet. It just uses the ones passed in through parameter
112: * layers.
113: * </p>
114: *
115: * @param layers a HashMap of key="TypeName" value="FeatureSource"
116: * @param envelope The bounding box of modified features
117: * @param results Storage for the error and warning messages
118: *
119: * @return True if there were no errors. False if there were errors.
120: *
121: * @throws Exception DOCUMENT ME!
122: *
123: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map,
124: * com.vividsolutions.jts.geom.Envelope,
125: * org.geotools.validation.ValidationResults)
126: */
127: public boolean validate(Map layers, Envelope envelope,
128: ValidationResults results) throws Exception {
129: HashMap FIDs = new HashMap(); // FIDs used for lookup to see if any match
130: boolean result = true;
131: Iterator it = layers.values().iterator();
132:
133: //TODO: get the needed layers from the database and use them instead
134: while (it.hasNext()) // for each layer
135: {
136: FeatureSource featureSource = (FeatureSource) it.next();
137: FeatureIterator features = featureSource.getFeatures()
138: .features();
139:
140: try {
141: while (features.hasNext()) // for each feature
142: {
143: Feature feature = features.next();
144: String fid = feature.getID();
145:
146: if (FIDs.containsKey(fid)) // if a FID like this one already exists
147: {
148: results.error(feature, "FID already exists.");
149: result = false;
150: } else {
151: FIDs.put(fid, fid);
152: }
153: }
154: } finally {
155: features.close(); // this is an important line
156: }
157: }
158:
159: return result;
160: }
161:
162: /**
163: * Implementation of getTypeNames.
164: *
165: * @return Array of typeNames, or empty array for all, null for disabled
166: *
167: * @see org.geotools.validation.Validation#getTypeNames()
168: */
169: public String[] getTypeRefs() {
170: if (typeRef == null) {
171: return null; // disabled
172: } else if (typeRef.equals("*")) {
173: return new String[0]; // apply to all
174: } else {
175: return new String[] { typeRef, };
176: }
177: }
178:
179: /**
180: * Access typeRef property.
181: *
182: * @return Returns the typeRef.
183: */
184: public String getTypeRef() {
185: return typeRef;
186: }
187:
188: /**
189: * Set typeRef to indicate type, or for all.
190: *
191: * @param typeRef The typeRef to set.
192: */
193: public void setTypeRef(String typeRef) {
194: this.typeRef = typeRef;
195: }
196: }
|