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;
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:
027: import com.vividsolutions.jts.geom.Envelope;
028:
029: /**
030: * UniqueFIDIntegrityValidation purpose.
031: * <p>
032: * Description of UniqueFIDIntegrityValidation ...
033: * <p>
034: * Capabilities:
035: * <ul>
036: * </li></li>
037: * </ul>
038: * Example Use:
039: * <pre><code>
040: * UniqueFIDIntegrityValidation x = new UniqueFIDIntegrityValidation(...);
041: * </code></pre>
042: *
043: * @author bowens, Refractions Research, Inc.
044: * @author $Author: sploreg $ (last modification)
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/UniqueFIDIntegrityValidation.java $
046: * @version $Id: UniqueFIDIntegrityValidation.java 22754 2006-11-16 03:03:20Z jgarnett $
047: */
048: public class UniqueFIDIntegrityValidation implements
049: IntegrityValidation {
050:
051: private String name;
052: private String description;
053: private String[] typeNames;
054:
055: //private String uniqueID;
056:
057: /**
058: * UniqueFIDIntegrityValidation constructor.
059: * <p>
060: * Description
061: * </p>
062: *
063: */
064: public UniqueFIDIntegrityValidation() {
065: }
066:
067: /**
068: * UniqueFIDIntegrityValidation constructor.
069: * <p>
070: * Description
071: * </p>
072: * @param name
073: * @param description
074: * @param typeNames
075: */
076: public UniqueFIDIntegrityValidation(String name,
077: String description, String[] typeNames, String uniqueID) {
078: this .name = name;
079: this .description = description;
080: this .typeNames = typeNames;
081: //this.uniqueID = uniqueID;
082: }
083:
084: /**
085: * Override setName.
086: * <p>
087: * Description ...
088: * </p>
089: * @see org.geotools.validation.Validation#setName(java.lang.String)
090: *
091: * @param name
092: */
093: public void setName(String name) {
094: this .name = name;
095: }
096:
097: /**
098: * Override getName.
099: * <p>
100: * Description ...
101: * </p>
102: * @see org.geotools.validation.Validation#getName()
103: *
104: */
105: public String getName() {
106: return name;
107: }
108:
109: /**
110: * Override setDescription.
111: * <p>
112: * Description ...
113: * </p>
114: * @see org.geotools.validation.Validation#setDescription(java.lang.String)
115: *
116: * @param description
117: */
118: public void setDescription(String description) {
119: this .description = description;
120: }
121:
122: /**
123: * Override getDescription.
124: * <p>
125: * Description ...
126: * </p>
127: * @see org.geotools.validation.Validation#getDescription()
128: *
129: */
130: public String getDescription() {
131: return description;
132: }
133:
134: /**
135: * Override getPriority.
136: * <p>
137: * Description ...
138: * </p>
139: * @see org.geotools.validation.Validation#getPriority()
140: *
141: */
142: public int getPriority() {
143: return 10;
144: }
145:
146: /**
147: * Override setTypeNames.
148: * <p>
149: * Description ...
150: * </p>
151: * @see org.geotools.validation.Validation#setTypeNames(java.lang.String[])
152: *
153: * @param names
154: */
155: public void setTypeNames(String[] names) {
156: this .typeNames = names;
157: }
158:
159: /**
160: * Override getTypeNames.
161: * <p>
162: * Description ...
163: * </p>
164: * @see org.geotools.validation.Validation#getTypeNames()
165: *
166: */
167: public String[] getTypeRefs() {
168: return typeNames;
169: }
170:
171: /**
172: * Override validate.
173: * <p>
174: * Description ...
175: * </p>
176: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map, com.vividsolutions.jts.geom.Envelope, org.geotools.validation.ValidationResults)
177: *
178: * @param layers
179: * @param envelope
180: * @param results
181: */
182: public boolean validate(Map layers, Envelope envelope,
183: ValidationResults results) throws Exception {
184:
185: HashMap FIDs = new HashMap();
186: boolean result = true;
187: Iterator it = layers.values().iterator();
188:
189: while (it.hasNext())// for each layer
190: {
191: FeatureSource featureSource = (FeatureSource) it.next();
192: FeatureIterator features = featureSource.getFeatures()
193: .features();
194: try {
195:
196: while (features.hasNext()) // for each feature
197: {
198: Feature feature = features.next();
199: String fid = feature.getID();
200: if (FIDs.containsKey(fid)) // if a FID like this one already exists
201: {
202: results.error(feature, "FID already exists.");
203: result = false;
204: } else
205: FIDs.put(fid, fid);
206: }
207: } finally {
208: features.close(); // this is an important line
209: }
210:
211: }
212:
213: return result;
214: }
215: }
|