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 @ 44202
History | View | Annotate | Download (10.9 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.exception.DataException; |
23 |
import org.gvsig.fmap.dal.feature.AbstractFeatureTypeDefinitionsManager; |
24 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
25 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
26 |
import org.gvsig.fmap.dal.feature.FeatureType; |
27 |
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dataTypes.DataTypes; |
30 |
import org.gvsig.tools.dispose.DisposeUtils; |
31 |
import org.gvsig.tools.dynobject.DynClass; |
32 |
import org.gvsig.tools.dynobject.DynClass_v2; |
33 |
import org.gvsig.tools.dynobject.DynField_v2; |
34 |
import org.gvsig.tools.dynobject.DynObjectManager; |
35 |
import org.gvsig.tools.dynobject.Tags; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class DefaultFeatureTypeDefinitionsManager extends AbstractFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
41 |
|
42 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
43 |
|
44 |
private static final String FILE_EXTENSION = "form"; |
45 |
|
46 |
private File definitionsFolder = null; |
47 |
private final Map<String, DynClass> dynClasses; |
48 |
|
49 |
public DefaultFeatureTypeDefinitionsManager() {
|
50 |
this.dynClasses = new HashMap<>(); |
51 |
} |
52 |
|
53 |
protected Map<String, DynClass> getDynClasses() { |
54 |
return this.dynClasses; |
55 |
} |
56 |
|
57 |
protected File getDefinitionsFolder() throws IOException { |
58 |
if (this.definitionsFolder == null) { |
59 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
60 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
61 |
File homeFolder = plugin.getPluginHomeFolder();
|
62 |
File definitionsFolder = new File(homeFolder, "schemas"); |
63 |
if (!definitionsFolder.exists()) {
|
64 |
FileUtils.forceMkdir(definitionsFolder); |
65 |
} |
66 |
this.definitionsFolder = definitionsFolder;
|
67 |
} |
68 |
return this.definitionsFolder; |
69 |
} |
70 |
|
71 |
protected long getCRC(FeatureType type) { |
72 |
StringBuilder buffer = new StringBuilder(); |
73 |
for (int i = 0; i < type.size(); i++) { |
74 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
75 |
if( !x.isComputed() ) {
|
76 |
buffer.append(x.getName()); |
77 |
buffer.append(x.getDataTypeName()); |
78 |
buffer.append(x.getSize()); |
79 |
} |
80 |
} |
81 |
CRC32 crc = new CRC32(); |
82 |
byte[] data = buffer.toString().getBytes(); |
83 |
crc.update(data); |
84 |
return crc.getValue();
|
85 |
} |
86 |
|
87 |
protected String getKey(FeatureStore store, FeatureType featureType) { |
88 |
return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
89 |
} |
90 |
|
91 |
protected String getKey(FeatureStore store, FeatureType featureType, String name) { |
92 |
CRC32 crc = new CRC32(); |
93 |
crc.update(name.getBytes()); |
94 |
crc.getValue(); |
95 |
if( !StringUtils.isAlphanumeric(name) ) {
|
96 |
name = StringUtils.removeAll(name, "[^a-zA-Z0-9_]");
|
97 |
} |
98 |
return store.getName() + name + "_" + Long.toHexString(getCRC(featureType))+Long.toHexString(crc.getValue()) ; |
99 |
} |
100 |
|
101 |
protected File getDefinitionFile(String key) { |
102 |
File folder;
|
103 |
try {
|
104 |
folder = getDefinitionsFolder(); |
105 |
} catch (IOException ex) { |
106 |
return null; |
107 |
} |
108 |
File f = new File(folder, key + "."+FILE_EXTENSION); |
109 |
if (f.exists()) {
|
110 |
return f;
|
111 |
} |
112 |
Properties prop = new Properties(); |
113 |
FileInputStream fin = null; |
114 |
try {
|
115 |
fin = new FileInputStream(new File(folder, "index.properties")); |
116 |
prop.load(fin); |
117 |
} catch (IOException ex) { |
118 |
return null; |
119 |
} finally {
|
120 |
IOUtils.closeQuietly(fin); |
121 |
} |
122 |
String s = prop.getProperty(key, null); |
123 |
if (s == null) { |
124 |
return null; |
125 |
} |
126 |
f = new File(folder, s); |
127 |
if (f.exists()) {
|
128 |
return f;
|
129 |
} |
130 |
f = new File(s); |
131 |
if (f.exists()) {
|
132 |
return f;
|
133 |
} |
134 |
return null; |
135 |
} |
136 |
|
137 |
@Override
|
138 |
public DynClass get(FeatureStore store, FeatureType featureType) {
|
139 |
String key = this.getKey(store, featureType); |
140 |
DynClass dynClass = this.getDynClasses().get(key);
|
141 |
if (dynClass != null) { |
142 |
return dynClass;
|
143 |
} |
144 |
String xml = null; |
145 |
File definitionFile = getDefinitionFile(key);
|
146 |
if (definitionFile == null) { |
147 |
DataServerExplorer explorer = null;
|
148 |
DataResource resource = null;
|
149 |
try {
|
150 |
explorer = store.getExplorer(); |
151 |
resource = explorer.getResource(store, FILE_EXTENSION); |
152 |
if( resource !=null && resource.exists() ) { |
153 |
xml = IOUtils.toString(resource.asInputStream()); |
154 |
} else {
|
155 |
return this.getDynClass(store, featureType); |
156 |
} |
157 |
} catch(Exception ex) { |
158 |
// Do nothing, leave xml to null
|
159 |
} finally {
|
160 |
IOUtils.closeQuietly(resource); |
161 |
DisposeUtils.disposeQuietly(explorer); |
162 |
} |
163 |
} |
164 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
165 |
try {
|
166 |
if( xml!=null ) { |
167 |
xml = FileUtils.readFileToString(definitionFile); |
168 |
xml = xml.replaceAll("[$][{]CWD[}]",definitionFile.getParentFile().getAbsolutePath());
|
169 |
} |
170 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
171 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
172 |
for (DynClass aDynClass : dynClasses.values()) {
|
173 |
this.getDynClasses().put(aDynClass.getName(), aDynClass);
|
174 |
} |
175 |
} catch (Exception ex) { |
176 |
logger.warn("Can't parse xml definition.", ex);
|
177 |
return null; |
178 |
} |
179 |
|
180 |
dynClass = this.getDynClasses().get(key);
|
181 |
if (dynClass != null) { |
182 |
return dynClass;
|
183 |
} |
184 |
return this.getDynClass(store, featureType); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean contains(FeatureStore store, FeatureType featureType) { |
189 |
String key = this.getKey(store, featureType); |
190 |
return this.getDynClasses().containsKey(key); |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
195 |
try {
|
196 |
if( dynClass instanceof FeatureType ) { |
197 |
dynClass = this.getDynClass(store, (FeatureType) dynClass);
|
198 |
} |
199 |
String key = this.getKey(store, featureType); |
200 |
File f = new File(getDefinitionsFolder(), key + "."+FILE_EXTENSION); |
201 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
202 |
String xml = dynObjectManager.exportSimpleDynClassDefinitions(dynClass);
|
203 |
FileUtils.write(f, xml); |
204 |
this.getDynClasses().put(key, dynClass);
|
205 |
} catch (IOException ex) { |
206 |
throw new RuntimeException(ex); |
207 |
} |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void remove(FeatureStore store, FeatureType featureType) { |
212 |
String key = this.getKey(store, featureType); |
213 |
this.getDynClasses().remove(key);
|
214 |
} |
215 |
|
216 |
@Override
|
217 |
public void addModel(File model) { |
218 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
219 |
try {
|
220 |
String xml = FileUtils.readFileToString(model);
|
221 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
222 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
223 |
|
224 |
File folder;
|
225 |
try {
|
226 |
folder = getDefinitionsFolder(); |
227 |
} catch (IOException ex) { |
228 |
return ;
|
229 |
} |
230 |
Properties prop = new Properties(); |
231 |
FileInputStream fin = null; |
232 |
try {
|
233 |
fin = new FileInputStream(new File(folder, "index.properties")); |
234 |
prop.load(fin); |
235 |
} catch (IOException ex) { |
236 |
} finally {
|
237 |
IOUtils.closeQuietly(fin); |
238 |
} |
239 |
for (DynClass aDynClass : dynClasses.values()) {
|
240 |
String className = aDynClass.getName();
|
241 |
prop.setProperty(className, model.getAbsolutePath()); |
242 |
} |
243 |
FileOutputStream fout = null; |
244 |
try {
|
245 |
fout = new FileOutputStream(new File(folder, "index.properties")); |
246 |
prop.store(fout, "");
|
247 |
} catch (IOException ex) { |
248 |
} finally {
|
249 |
IOUtils.closeQuietly(fout); |
250 |
} |
251 |
|
252 |
} catch (Exception ex) { |
253 |
logger.warn("Can't parse xml definition.", ex);
|
254 |
} |
255 |
} |
256 |
|
257 |
private DynClass getDynClass(FeatureStore store, FeatureType featureType) {
|
258 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
259 |
DynClass_v2 definition = (DynClass_v2) manager.createCopy(featureType); |
260 |
|
261 |
DataServerExplorer explorer=null;
|
262 |
if( store!=null ) { |
263 |
try {
|
264 |
if( store.getFeatureTypes().size()==1 ) { |
265 |
definition.setLabel(store.getName()); |
266 |
} |
267 |
explorer = store.getExplorer(); |
268 |
} catch (Exception ex) { |
269 |
} |
270 |
} |
271 |
for (FeatureAttributeDescriptor descriptor : featureType) {
|
272 |
DynField_v2 definitionField = (DynField_v2) definition.getDynField(descriptor.getName()); |
273 |
String profileName = descriptor.getDataProfileName();
|
274 |
if( !StringUtils.isBlank(profileName) ) {
|
275 |
definitionField.setSubtype(profileName); |
276 |
} |
277 |
if( explorer!=null && definitionField.getType()==DataTypes.FILE ) { |
278 |
Tags tags = definitionField.getTags(); |
279 |
if (!tags.has("path")) { |
280 |
try {
|
281 |
File f = explorer.getResourcePath(store, "dat"); |
282 |
if( f!=null ) { |
283 |
tags.set("path", f.getParentFile());
|
284 |
} |
285 |
} catch (DataException ex) {
|
286 |
} |
287 |
} |
288 |
} |
289 |
|
290 |
} |
291 |
return definition;
|
292 |
} |
293 |
} |