001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, 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.wfs;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import net.refractions.udig.catalog.CatalogPlugin;
026: import net.refractions.udig.catalog.IGeoResource;
027: import net.refractions.udig.catalog.IGeoResourceInfo;
028: import net.refractions.udig.catalog.IResolveChangeEvent;
029: import net.refractions.udig.catalog.IResolveDelta;
030: import net.refractions.udig.catalog.IService;
031: import net.refractions.udig.catalog.internal.CatalogImpl;
032: import net.refractions.udig.catalog.internal.ResolveChangeEvent;
033: import net.refractions.udig.catalog.internal.ResolveDelta;
034: import net.refractions.udig.ui.graphics.Glyph;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037: import org.geotools.data.FeatureSource;
038: import org.geotools.data.FeatureStore;
039: import org.geotools.data.ows.FeatureSetDescription;
040: import org.geotools.data.wfs.WFSDataStore;
041: import org.geotools.feature.FeatureType;
042: import org.geotools.feature.GeometryAttributeType;
043: import org.geotools.geometry.jts.ReferencedEnvelope;
044: import org.geotools.referencing.crs.DefaultGeographicCRS;
045: import org.opengis.referencing.crs.CoordinateReferenceSystem;
046:
047: /**
048: * Access a feature type in a wfs.
049: *
050: * @author David Zwiers, Refractions Research
051: * @since 0.6
052: */
053: public class WFSGeoResourceImpl extends IGeoResource {
054: WFSServiceImpl parent;
055: String typename = null;
056: private URL identifier;
057:
058: private WFSGeoResourceImpl() {/*not for use*/
059: }
060:
061: /**
062: * Construct <code>WFSGeoResourceImpl</code>.
063: *
064: * @param parent
065: * @param typename
066: */
067: public WFSGeoResourceImpl(WFSServiceImpl parent, String typename) {
068: this .parent = parent;
069: this .typename = typename;
070: try {
071: identifier = new URL(parent.getIdentifier().toString()
072: + "#" + typename); //$NON-NLS-1$
073: } catch (MalformedURLException e) {
074: identifier = parent.getIdentifier();
075: }
076: }
077:
078: public URL getIdentifier() {
079: return identifier;
080: }
081:
082: /*
083: * @see net.refractions.udig.catalog.IGeoResource#getStatus()
084: */
085: public Status getStatus() {
086: return parent.getStatus();
087: }
088:
089: /*
090: * @see net.refractions.udig.catalog.IGeoResource#getStatusMessage()
091: */
092: public Throwable getMessage() {
093: return parent.getMessage();
094: }
095:
096: /*
097: * Required adaptions:
098: * <ul>
099: * <li>IGeoResourceInfo.class
100: * <li>IService.class
101: * </ul>
102: * @see net.refractions.udig.catalog.IResolve#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
103: */
104: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
105: throws IOException {
106: if (adaptee == null)
107: return null;
108: // if(adaptee.isAssignableFrom(IService.class))
109: // return adaptee.cast( parent );
110: if (adaptee.isAssignableFrom(WFSDataStore.class))
111: return parent.resolve(adaptee, monitor);
112: if (adaptee.isAssignableFrom(IGeoResource.class))
113: return adaptee.cast(this );
114: if (adaptee.isAssignableFrom(IGeoResourceInfo.class))
115: return adaptee.cast(getInfo(monitor));
116: if (adaptee.isAssignableFrom(FeatureStore.class)) {
117: FeatureSource fs = parent.getDS(monitor).getFeatureSource(
118: typename);
119: if (fs instanceof FeatureStore)
120: return adaptee.cast(fs);
121: if (adaptee.isAssignableFrom(FeatureSource.class))
122: return adaptee.cast(parent.getDS(monitor)
123: .getFeatureSource(typename));
124: }
125: return super .resolve(adaptee, monitor);
126: }
127:
128: public IService service(IProgressMonitor monitor)
129: throws IOException {
130: return parent;
131: }
132:
133: /*
134: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
135: */
136: public <T> boolean canResolve(Class<T> adaptee) {
137: if (adaptee == null)
138: return false;
139: return (adaptee.isAssignableFrom(IGeoResourceInfo.class)
140: || adaptee.isAssignableFrom(FeatureStore.class)
141: || adaptee.isAssignableFrom(FeatureSource.class)
142: || adaptee.isAssignableFrom(WFSDataStore.class) || adaptee
143: .isAssignableFrom(IService.class))
144: || super .canResolve(adaptee);
145: }
146:
147: private volatile IGeoResourceInfo info;
148:
149: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
150: throws IOException {
151: if (info == null && getStatus() != Status.BROKEN) {
152: parent.rLock.lock();
153: try {
154: if (info == null) {
155: info = new IGeoResourceWFSInfo();
156: }
157: } finally {
158: parent.rLock.unlock();
159: }
160: }
161: return info;
162: }
163:
164: class IGeoResourceWFSInfo extends IGeoResourceInfo {
165:
166: CoordinateReferenceSystem crs = null;
167:
168: IGeoResourceWFSInfo() throws IOException {
169: FeatureType ft = parent.getDS(null).getSchema(typename);
170:
171: List<FeatureSetDescription> fts = parent.getDS(null)
172: .getCapabilities().getFeatureTypes();
173: FeatureSetDescription fsd = null;
174: if (fts != null) {
175: Iterator<FeatureSetDescription> i = fts.iterator();
176: while (i.hasNext() && fsd == null) {
177: FeatureSetDescription t = i.next();
178: if (t != null && typename.equals(t.getName()))
179: fsd = t;
180: }
181: }
182:
183: if (fsd == null) {
184: bounds = new ReferencedEnvelope(-180, 180, -90, 90,
185: DefaultGeographicCRS.WGS84);
186: } else {
187: bounds = new ReferencedEnvelope(fsd
188: .getLatLongBoundingBox(),
189: DefaultGeographicCRS.WGS84);
190: description = fsd.getAbstract();
191: title = fsd.getTitle();
192: }
193:
194: GeometryAttributeType defaultGeom = ft.getDefaultGeometry();
195: if (defaultGeom == null) {
196: crs = null;
197: } else {
198: crs = defaultGeom.getCoordinateSystem();
199: }
200:
201: name = typename;
202: schema = ft.getNamespace();
203:
204: keywords = new String[] { "wfs", //$NON-NLS-1$
205: typename, ft.getNamespace().toString() };
206:
207: icon = Glyph.icon(ft);
208: }
209:
210: /*
211: * @see net.refractions.udig.catalog.IGeoResourceInfo#getCRS()
212: */
213: public CoordinateReferenceSystem getCRS() {
214: if (crs != null)
215: return crs;
216: return super.getCRS();
217: }
218: }
219: }
|