001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.property;
017:
018: import java.io.File;
019: import java.io.FileReader;
020: import java.io.IOException;
021: import java.io.LineNumberReader;
022:
023: import org.geotools.data.AbstractFeatureLocking;
024: import org.geotools.data.DataStore;
025: import org.geotools.data.FeatureEvent;
026: import org.geotools.data.FeatureListener;
027: import org.geotools.data.Query;
028: import org.geotools.data.Transaction;
029: import org.geotools.feature.FeatureType;
030: import org.opengis.filter.Filter;
031:
032: import com.vividsolutions.jts.geom.Envelope;
033:
034: public class PropertyFeatureSource extends AbstractFeatureLocking {
035: String typeName;
036: FeatureType featureType;
037: PropertyDataStore store;
038:
039: long cacheTimestamp = 0;
040: Envelope cacheBounds = null;
041: int cacheCount = -1;
042:
043: PropertyFeatureSource(PropertyDataStore propertyDataStore,
044: String typeName) throws IOException {
045: this .store = propertyDataStore;
046: this .typeName = typeName;
047: this .featureType = store.getSchema(typeName);
048: store.listenerManager.addFeatureListener(this ,
049: new FeatureListener() {
050: public void changed(FeatureEvent featureEvent) {
051: if (cacheBounds != null) {
052: if (featureEvent.getEventType() == FeatureEvent.FEATURES_ADDED) {
053: cacheBounds
054: .expandToInclude(featureEvent
055: .getBounds());
056: } else {
057: cacheBounds = null;
058: }
059: }
060: cacheCount = -1;
061: }
062: });
063: }
064:
065: public DataStore getDataStore() {
066: return store;
067: }
068:
069: public void addFeatureListener(FeatureListener listener) {
070: store.listenerManager.addFeatureListener(this , listener);
071: }
072:
073: public void removeFeatureListener(FeatureListener listener) {
074: store.listenerManager.removeFeatureListener(this , listener);
075: }
076:
077: public FeatureType getSchema() {
078: return featureType;
079: }
080:
081: public int getCount(Query query) {
082: if (Filter.INCLUDE == query.getFilter()
083: && getTransaction() == Transaction.AUTO_COMMIT) {
084: File file = new File(store.directory, typeName
085: + ".properties");
086: if (cacheCount != -1
087: && file.lastModified() == cacheTimestamp) {
088: return cacheCount;
089: }
090: cacheCount = countFile(file);
091: cacheTimestamp = file.lastModified();
092: return cacheCount;
093: }
094: return -1;
095: }
096:
097: private int countFile(File file) {
098: try {
099: LineNumberReader reader = new LineNumberReader(
100: new FileReader(file));
101: while (reader.readLine() != null)
102: ;
103: return reader.getLineNumber() - 1;
104: } catch (IOException e) {
105: return -1;
106: }
107: }
108:
109: public Envelope getBounds() {
110: File file = new File(store.directory, typeName + ".properties");
111: if (cacheBounds != null
112: && file.lastModified() == cacheTimestamp) {
113: // we have the cache
114: return cacheBounds;
115: }
116: try {
117: // calculate and store in cache
118: cacheBounds = getFeatures().getBounds();
119: cacheTimestamp = file.lastModified();
120: return cacheBounds;
121: } catch (IOException e) {
122: }
123: // bounds are unavailable!
124: return null;
125: }
126:
127: }
|