svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / DefaultFeatureTypeDefinitionsManager.java @ 44189
History | View | Annotate | Download (9.3 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileOutputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.nio.charset.Charset; |
9 |
import java.util.HashMap; |
10 |
import java.util.Map; |
11 |
import java.util.Properties; |
12 |
import java.util.zip.CRC32; |
13 |
|
14 |
import org.apache.commons.io.FileUtils; |
15 |
import org.apache.commons.io.IOUtils; |
16 |
import org.apache.commons.lang3.StringUtils; |
17 |
import org.gvsig.andami.PluginServices; |
18 |
import org.gvsig.andami.PluginsLocator; |
19 |
import org.gvsig.andami.PluginsManager; |
20 |
import org.gvsig.fmap.dal.DataServerExplorer; |
21 |
import org.gvsig.fmap.dal.DataServerExplorer.DataResource; |
22 |
import org.gvsig.fmap.dal.feature.AbstractFeatureTypeDefinitionsManager; |
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
26 |
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.dispose.DisposeUtils; |
29 |
import org.gvsig.tools.dynobject.DynClass; |
30 |
import org.gvsig.tools.dynobject.DynObjectManager; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
public class DefaultFeatureTypeDefinitionsManager extends AbstractFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
35 |
|
36 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
37 |
|
38 |
private File definitionsFolder = null; |
39 |
private final Map<String, DynClass> dynClasses; |
40 |
|
41 |
public DefaultFeatureTypeDefinitionsManager() {
|
42 |
this.dynClasses = new HashMap<>(); |
43 |
} |
44 |
|
45 |
protected Map<String, DynClass> getDynClasses() { |
46 |
return this.dynClasses; |
47 |
} |
48 |
|
49 |
protected File getDefinitionsFolder() throws IOException { |
50 |
if (this.definitionsFolder == null) { |
51 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
52 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
53 |
File homeFolder = plugin.getPluginHomeFolder();
|
54 |
File definitionsFolder = new File(homeFolder, "schemas"); |
55 |
if (!definitionsFolder.exists()) {
|
56 |
FileUtils.forceMkdir(definitionsFolder); |
57 |
} |
58 |
this.definitionsFolder = definitionsFolder;
|
59 |
} |
60 |
return this.definitionsFolder; |
61 |
} |
62 |
|
63 |
protected long getCRC(FeatureType type) { |
64 |
StringBuilder buffer = new StringBuilder(); |
65 |
for (int i = 0; i < type.size(); i++) { |
66 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
67 |
buffer.append(x.getName()); |
68 |
buffer.append(x.getDataTypeName()); |
69 |
buffer.append(x.getSize()); |
70 |
} |
71 |
CRC32 crc = new CRC32(); |
72 |
byte[] data = buffer.toString().getBytes(); |
73 |
crc.update(data); |
74 |
return crc.getValue();
|
75 |
} |
76 |
|
77 |
protected String getKey(FeatureStore store, FeatureType featureType) { |
78 |
return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
79 |
} |
80 |
|
81 |
protected String getKey(FeatureStore store, FeatureType featureType, String name) { |
82 |
CRC32 crc = new CRC32(); |
83 |
crc.update(name.getBytes()); |
84 |
crc.getValue(); |
85 |
if( !StringUtils.isAlphanumeric(name) ) {
|
86 |
name = StringUtils.removeAll(name, "[^a-zA-Z0-9_]");
|
87 |
} |
88 |
return store.getName() + name + "_" + Long.toHexString(getCRC(featureType))+Long.toHexString(crc.getValue()) ; |
89 |
} |
90 |
|
91 |
protected File getDefinitionFile(String key) { |
92 |
File folder;
|
93 |
try {
|
94 |
folder = getDefinitionsFolder(); |
95 |
} catch (IOException ex) { |
96 |
return null; |
97 |
} |
98 |
File f = new File(folder, key + ".ftd"); |
99 |
if (f.exists()) {
|
100 |
return f;
|
101 |
} |
102 |
Properties prop = new Properties(); |
103 |
FileInputStream fin = null; |
104 |
try {
|
105 |
fin = new FileInputStream(new File(folder, "index.properties")); |
106 |
prop.load(fin); |
107 |
} catch (IOException ex) { |
108 |
return null; |
109 |
} finally {
|
110 |
IOUtils.closeQuietly(fin); |
111 |
} |
112 |
String s = prop.getProperty(key, null); |
113 |
if (s == null) { |
114 |
return null; |
115 |
} |
116 |
f = new File(folder, s); |
117 |
if (f.exists()) {
|
118 |
return f;
|
119 |
} |
120 |
f = new File(s); |
121 |
if (f.exists()) {
|
122 |
return f;
|
123 |
} |
124 |
return null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public DynClass get(FeatureStore store, FeatureType featureType) {
|
129 |
String key = this.getKey(store, featureType); |
130 |
DynClass dynClass = this.getDynClasses().get(key);
|
131 |
if (dynClass != null) { |
132 |
return dynClass;
|
133 |
} |
134 |
File definitionFile = getDefinitionFile(key);
|
135 |
if (definitionFile == null) { |
136 |
DataServerExplorer explorer = null;
|
137 |
try {
|
138 |
explorer = store.getExplorer(); |
139 |
DataResource resource = explorer.getResource(store, key+".ftd");
|
140 |
if( resource !=null && resource.getURL()!=null ) { |
141 |
// FIXME; use resource.asInputStream() instead resource.getURL()
|
142 |
definitionFile = new File(resource.getURL().toURI()); |
143 |
} |
144 |
} catch(Exception ex) { |
145 |
// Do nothing, leave definitionFile to null
|
146 |
} finally {
|
147 |
DisposeUtils.disposeQuietly(explorer); |
148 |
} |
149 |
if( definitionFile == null || !definitionFile.exists()) { |
150 |
return this.getDynClass(featureType); |
151 |
} |
152 |
} |
153 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
154 |
try {
|
155 |
String xml = FileUtils.readFileToString(definitionFile);
|
156 |
xml = xml.replaceAll("[$][{]CWD[}]",definitionFile.getParentFile().getAbsolutePath());
|
157 |
|
158 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
159 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
160 |
for (DynClass aDynClass : dynClasses.values()) {
|
161 |
this.getDynClasses().put(aDynClass.getName(), aDynClass);
|
162 |
} |
163 |
} catch (Exception ex) { |
164 |
logger.warn("Can't parse xml definition.", ex);
|
165 |
return null; |
166 |
} |
167 |
|
168 |
dynClass = this.getDynClasses().get(key);
|
169 |
if (dynClass != null) { |
170 |
return dynClass;
|
171 |
} |
172 |
return this.getDynClass(featureType); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public boolean contains(FeatureStore store, FeatureType featureType) { |
177 |
String key = this.getKey(store, featureType); |
178 |
return this.getDynClasses().containsKey(key); |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
183 |
try {
|
184 |
if( dynClass instanceof FeatureType ) { |
185 |
dynClass = this.getDynClass((FeatureType) dynClass);
|
186 |
} |
187 |
String key = this.getKey(store, featureType); |
188 |
File f = new File(getDefinitionsFolder(), key + ".ftd"); |
189 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
190 |
String xml = dynObjectManager.exportSimpleDynClassDefinitions(dynClass);
|
191 |
FileUtils.write(f, xml); |
192 |
this.getDynClasses().put(key, dynClass);
|
193 |
} catch (IOException ex) { |
194 |
throw new RuntimeException(ex); |
195 |
} |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public void remove(FeatureStore store, FeatureType featureType) { |
200 |
String key = this.getKey(store, featureType); |
201 |
this.getDynClasses().remove(key);
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public void addModel(File model) { |
206 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
207 |
try {
|
208 |
String xml = FileUtils.readFileToString(model);
|
209 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
210 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
211 |
|
212 |
File folder;
|
213 |
try {
|
214 |
folder = getDefinitionsFolder(); |
215 |
} catch (IOException ex) { |
216 |
return ;
|
217 |
} |
218 |
Properties prop = new Properties(); |
219 |
FileInputStream fin = null; |
220 |
try {
|
221 |
fin = new FileInputStream(new File(folder, "index.properties")); |
222 |
prop.load(fin); |
223 |
} catch (IOException ex) { |
224 |
} finally {
|
225 |
IOUtils.closeQuietly(fin); |
226 |
} |
227 |
for (DynClass aDynClass : dynClasses.values()) {
|
228 |
String className = aDynClass.getName();
|
229 |
prop.setProperty(className, model.getAbsolutePath()); |
230 |
} |
231 |
FileOutputStream fout = null; |
232 |
try {
|
233 |
fout = new FileOutputStream(new File(folder, "index.properties")); |
234 |
prop.store(fout, "");
|
235 |
} catch (IOException ex) { |
236 |
} finally {
|
237 |
IOUtils.closeQuietly(fout); |
238 |
} |
239 |
|
240 |
} catch (Exception ex) { |
241 |
logger.warn("Can't parse xml definition.", ex);
|
242 |
} |
243 |
} |
244 |
|
245 |
private DynClass getDynClass(FeatureType featureType) {
|
246 |
return featureType;
|
247 |
// DynObjectManager manager = ToolsLocator.getDynObjectManager();
|
248 |
// DynClass x = manager.createCopy(featureType);
|
249 |
// // FIXME: Aqui falta a?adir el arreglo del subtype
|
250 |
// return x;
|
251 |
} |
252 |
} |