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