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 @ 42775
History | View | Annotate | Download (5.57 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.nio.charset.Charset; |
8 |
import java.util.HashMap; |
9 |
import java.util.Map; |
10 |
import java.util.Properties; |
11 |
import java.util.zip.CRC32; |
12 |
import org.apache.commons.io.FileUtils; |
13 |
import org.apache.commons.io.IOUtils; |
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.andami.PluginsLocator; |
16 |
import org.gvsig.andami.PluginsManager; |
17 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
18 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
19 |
import org.gvsig.fmap.dal.feature.FeatureType; |
20 |
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
21 |
import org.gvsig.tools.ToolsLocator; |
22 |
import org.gvsig.tools.dynobject.DynClass; |
23 |
import org.gvsig.tools.dynobject.DynObjectManager; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
public class DefaultFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
28 |
|
29 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
30 |
|
31 |
private File definitionsFolder = null; |
32 |
private final Map<String, DynClass> dynClasses; |
33 |
|
34 |
public DefaultFeatureTypeDefinitionsManager() {
|
35 |
this.dynClasses = new HashMap<>(); |
36 |
} |
37 |
|
38 |
private File getDefinitionsFolder() throws IOException { |
39 |
if (this.definitionsFolder == null) { |
40 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
41 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
42 |
File homeFolder = plugin.getPluginHomeFolder();
|
43 |
File definitionsFolder = new File(homeFolder, "schemas"); |
44 |
if (!definitionsFolder.exists()) {
|
45 |
FileUtils.forceMkdir(definitionsFolder); |
46 |
} |
47 |
this.definitionsFolder = definitionsFolder;
|
48 |
} |
49 |
return this.definitionsFolder; |
50 |
} |
51 |
|
52 |
private long getCRC(FeatureType type) { |
53 |
StringBuilder buffer = new StringBuilder(); |
54 |
for (int i = 0; i < type.size(); i++) { |
55 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
56 |
buffer.append(x.getName()); |
57 |
buffer.append(x.getDataTypeName()); |
58 |
buffer.append(x.getSize()); |
59 |
} |
60 |
CRC32 crc = new CRC32(); |
61 |
byte[] data = buffer.toString().getBytes(); |
62 |
crc.update(data); |
63 |
return crc.getValue();
|
64 |
} |
65 |
|
66 |
private String getKey(FeatureStore store, FeatureType featureType) { |
67 |
return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
68 |
} |
69 |
|
70 |
private File getDefinitionFile(String key) { |
71 |
File folder;
|
72 |
try {
|
73 |
folder = getDefinitionsFolder(); |
74 |
} catch (IOException ex) { |
75 |
return null; |
76 |
} |
77 |
File f = new File(folder, key + ".xml"); |
78 |
if (f.exists()) {
|
79 |
return f;
|
80 |
} |
81 |
Properties prop = new Properties(); |
82 |
FileInputStream fin = null; |
83 |
try {
|
84 |
fin = new FileInputStream(new File(folder, "index.properties")); |
85 |
prop.load(fin); |
86 |
} catch (IOException ex) { |
87 |
return null; |
88 |
} finally {
|
89 |
IOUtils.closeQuietly(fin); |
90 |
} |
91 |
String s = prop.getProperty(key, null); |
92 |
if (s == null) { |
93 |
return null; |
94 |
} |
95 |
f = new File(folder, s); |
96 |
if (f.exists()) {
|
97 |
return f;
|
98 |
} |
99 |
return null; |
100 |
} |
101 |
|
102 |
@Override
|
103 |
public DynClass get(FeatureStore store, FeatureType featureType) {
|
104 |
String key = this.getKey(store, featureType); |
105 |
DynClass dynClass = this.dynClasses.get(key);
|
106 |
if (dynClass != null) { |
107 |
return dynClass;
|
108 |
} |
109 |
File definitionFile = getDefinitionFile(key);
|
110 |
if (definitionFile == null) { |
111 |
return featureType;
|
112 |
} |
113 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
114 |
try {
|
115 |
String xml = FileUtils.readFileToString(definitionFile);
|
116 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
117 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
118 |
for (DynClass aDynClass : dynClasses.values()) {
|
119 |
this.dynClasses.put(aDynClass.getName(), aDynClass);
|
120 |
} |
121 |
} catch (Exception ex) { |
122 |
logger.warn("Can't parse xml definition.", ex);
|
123 |
return null; |
124 |
} |
125 |
|
126 |
dynClass = this.dynClasses.get(key);
|
127 |
if (dynClass != null) { |
128 |
return dynClass;
|
129 |
} |
130 |
return featureType;
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public boolean contains(FeatureStore store, FeatureType featureType) { |
135 |
String key = this.getKey(store, featureType); |
136 |
return this.dynClasses.containsKey(key); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
141 |
try {
|
142 |
String key = this.getKey(store, featureType); |
143 |
File f = new File(getDefinitionsFolder(), key + ".xml"); |
144 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
145 |
String xml = dynObjectManager.exportSimpleDynClassDefinitions(dynClass);
|
146 |
FileUtils.write(f, xml); |
147 |
this.dynClasses.put(key, dynClass);
|
148 |
} catch (IOException ex) { |
149 |
throw new RuntimeException(ex); |
150 |
} |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void remove(FeatureStore store, FeatureType featureType) { |
155 |
String key = this.getKey(store, featureType); |
156 |
this.dynClasses.remove(key);
|
157 |
} |
158 |
|
159 |
public void addModel(File model) { |
160 |
// TODO:
|
161 |
} |
162 |
|
163 |
} |