001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2005, Refractions Research Inc.
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: */
017: package net.refractions.udig.catalog.internal.db2;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URI;
022: import java.net.URL;
023:
024: import net.refractions.udig.catalog.CatalogPlugin;
025: import net.refractions.udig.catalog.IGeoResource;
026: import net.refractions.udig.catalog.IGeoResourceInfo;
027: import net.refractions.udig.catalog.IService;
028: import net.refractions.udig.ui.graphics.Glyph;
029:
030: import org.eclipse.core.runtime.IProgressMonitor;
031: import org.geotools.data.DataSourceException;
032: import org.geotools.data.DataStore;
033: import org.geotools.data.FeatureReader;
034: import org.geotools.data.FeatureSource;
035: import org.geotools.data.FeatureStore;
036: import org.geotools.feature.Feature;
037: import org.geotools.feature.FeatureIterator;
038: import org.geotools.feature.FeatureType;
039: import org.geotools.geometry.jts.ReferencedEnvelope;
040: import org.geotools.resources.CRSUtilities;
041: import org.opengis.referencing.crs.CoordinateReferenceSystem;
042:
043: import com.vividsolutions.jts.geom.Envelope;
044:
045: /**
046: * Resource handle for the DB2 Universal Database.
047: *
048: * @author Justin Deoliveira,Refractions Research Inc.,jdeolive@refractions.net
049: */
050: public class DB2GeoResource extends IGeoResource {
051:
052: /** parent service * */
053: DB2Service parent;
054:
055: /** feature type (table) name * */
056: String name;
057:
058: /** info object * */
059: volatile DB2GeoResourceInfo info;
060:
061: DB2GeoResource(DB2Service parent, String name) {
062: this .parent = parent;
063: this .name = name;
064: }
065:
066: public <T> boolean canResolve(Class<T> adaptee) {
067: if (adaptee == null)
068: return false;
069:
070: return adaptee.isAssignableFrom(IGeoResourceInfo.class)
071: || adaptee.isAssignableFrom(FeatureStore.class)
072: || adaptee.isAssignableFrom(FeatureSource.class)
073: || adaptee.isAssignableFrom(IService.class)
074: || super .canResolve(adaptee);
075: }
076:
077: @Override
078: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
079: throws IOException {
080:
081: if (adaptee == null)
082: return null;
083:
084: if (adaptee.isAssignableFrom(IGeoResourceInfo.class))
085: return adaptee.cast(getInfo(monitor));
086:
087: if (adaptee.isAssignableFrom(FeatureSource.class)) {
088: DataStore ds = parent.getDataStore(monitor);
089: if (ds != null) {
090: FeatureSource fs = ds.getFeatureSource(name);
091: if (fs != null)
092: return adaptee.cast(fs);
093: }
094: }
095:
096: if (adaptee.isAssignableFrom(FeatureStore.class)) {
097: FeatureSource fs = resolve(FeatureSource.class, monitor);
098: if (fs != null && fs instanceof FeatureStore) {
099: return adaptee.cast(fs);
100: }
101: }
102:
103: if (adaptee.isAssignableFrom(IService.class)) {
104: return adaptee.cast(parent);
105: }
106:
107: return super .resolve(adaptee, monitor);
108: }
109:
110: public IService service(IProgressMonitor monitor)
111: throws IOException {
112: return parent;
113: }
114:
115: public Status getStatus() {
116: return parent.getStatus();
117: }
118:
119: public Throwable getMessage() {
120: return parent.getMessage();
121: }
122:
123: public String getName() {
124: return name;
125: }
126:
127: @SuppressWarnings("unqualified-field-access")
128: public URL getIdentifier() {
129: try {
130: return new URL(parent.getIdentifier() + "#" + name); //$NON-NLS-1$
131: } catch (MalformedURLException e) {
132: return parent.getIdentifier();
133: }
134: }
135:
136: @Override
137: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
138: throws IOException {
139: parent.rLock.lock();
140: try {
141: if (info == null) {
142: info = new DB2GeoResourceInfo(monitor, name);
143: }
144: } finally {
145: parent.rLock.unlock();
146: }
147:
148: return info;
149: }
150:
151: class DB2GeoResourceInfo extends IGeoResourceInfo {
152:
153: private FeatureType ft = null;
154:
155: DB2GeoResourceInfo(IProgressMonitor monitor, String nameArg)
156: throws IOException {
157: DataStore ds = parent.getDataStore(monitor);
158: if (ds == null)
159: return;
160:
161: try {
162: this .ft = ds.getSchema(nameArg);
163: } catch (DataSourceException e) {
164: return;
165: }
166:
167: try {
168: FeatureSource fs = resolve(FeatureSource.class, null);
169: if (fs != null) {
170: bounds = (ReferencedEnvelope) fs.getBounds();
171: }
172:
173: if (bounds == null) {
174: CoordinateReferenceSystem crs = fs.getSchema()
175: .getDefaultGeometry().getCoordinateSystem();
176:
177: // try getting an envelope out of the crs
178: org.opengis.spatialschema.geometry.Envelope envelope = CRSUtilities
179: .getEnvelope(crs);
180:
181: if (envelope != null) {
182: bounds = new ReferencedEnvelope(new Envelope(
183: envelope.getLowerCorner()
184: .getOrdinate(0), envelope
185: .getLowerCorner()
186: .getOrdinate(1), envelope
187: .getUpperCorner()
188: .getOrdinate(0), envelope
189: .getUpperCorner()
190: .getOrdinate(1)), crs);
191: } else {
192: // TODO: perhaps access a preference which indicates
193: // wether to do a full table scan
194: // bounds = new ReferencedEnvelope(new Envelope(),crs);
195: // as a last resort do the full scan
196: FeatureIterator r = fs.getFeatures().features();
197: try {
198: Feature f = r.next();
199:
200: bounds = new ReferencedEnvelope(
201: new Envelope(), crs);
202: bounds.init(f.getBounds());
203: for (; r.hasNext();) {
204: f = r.next();
205: bounds.expandToInclude(f.getBounds());
206: }
207: } finally {
208: r.close();
209: }
210: }
211: }
212: } catch (Exception e) {
213: CatalogPlugin.log(e.getLocalizedMessage(), e);
214: }
215:
216: icon = Glyph.icon(ft);
217: keywords = new String[] { "db2", //$NON-NLS-1$
218: ft.getTypeName(), ft.getNamespace().toString() };
219: }
220:
221: public CoordinateReferenceSystem getCRS() {
222: return ft.getDefaultGeometry().getCoordinateSystem();
223: }
224:
225: @SuppressWarnings("unqualified-field-access")
226: public String getName() {
227: return ft.getTypeName();
228: }
229:
230: public URI getSchema() {
231: return ft.getNamespace();
232: }
233:
234: public String getTitle() {
235: return ft.getTypeName();
236: }
237: }
238: }
|