svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / AbstractFeatureProviderLoadedOnDemand.java @ 47669
History | View | Annotate | Download (3.61 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.simplereader; |
25 |
|
26 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider; |
29 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.primitive.Envelope; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
public abstract class AbstractFeatureProviderLoadedOnDemand extends DefaultFeatureProvider { |
36 |
|
37 |
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractFeatureProviderLoadedOnDemand.class); |
38 |
|
39 |
protected boolean loading; |
40 |
protected boolean loaded; |
41 |
protected String geomName; |
42 |
|
43 |
public AbstractFeatureProviderLoadedOnDemand(FeatureType type) {
|
44 |
super(type);
|
45 |
loading = false;
|
46 |
loaded = false;
|
47 |
FeatureAttributeDescriptor geomdesc = type.getDefaultGeometryAttribute(); |
48 |
if( geomdesc!=null ) { |
49 |
geomName = geomdesc.getName(); |
50 |
} |
51 |
} |
52 |
|
53 |
protected void load() { |
54 |
if (loading || loaded || this.isNew()) { |
55 |
return;
|
56 |
} |
57 |
loading = true;
|
58 |
try {
|
59 |
doLoad(); |
60 |
} catch (Exception e) { |
61 |
this.warn("Can't load feature", e); |
62 |
} finally {
|
63 |
loading = false;
|
64 |
loaded = true;
|
65 |
} |
66 |
} |
67 |
|
68 |
protected abstract void doLoad(); |
69 |
|
70 |
@Override
|
71 |
public void set(int i, Object value) { |
72 |
this.load();
|
73 |
super.set(i, value);
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
public void set(String name, Object value) { |
78 |
this.load();
|
79 |
super.set(featureType.getIndex(name), value);
|
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Object get(int i) { |
84 |
this.load();
|
85 |
return super.get(i); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public Object get(String name) { |
90 |
this.load();
|
91 |
return super.get(name); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public Geometry getDefaultGeometry() {
|
96 |
if( geomName == null ) { |
97 |
return null; |
98 |
} |
99 |
return (Geometry) this.get(geomName); |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public Envelope getDefaultEnvelope() {
|
104 |
Geometry geom = this.getDefaultGeometry();
|
105 |
if( geom==null ) { |
106 |
return null; |
107 |
} |
108 |
try {
|
109 |
return geom.getEnvelope();
|
110 |
} catch(Exception ex) { |
111 |
// En ocasiones con geometrias incorrectas getEnvelop falla.
|
112 |
return null; |
113 |
} |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void setOID(Object oid) { |
118 |
this.loaded = false; |
119 |
super.setOID(oid);
|
120 |
} |
121 |
|
122 |
@Override
|
123 |
public FeatureProvider getCopy() {
|
124 |
this.load();
|
125 |
return super.getCopy(); |
126 |
} |
127 |
|
128 |
protected void warn(String message, Throwable t) { |
129 |
} |
130 |
|
131 |
} |