svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.metadata.basic / org.gvsig.metadata.basic.lib / org.gvsig.metadata.lib.basic.impl / src / main / java / org / gvsig / metadata / lib / basic / impl / AbstractMetadataManager.java @ 40608
History | View | Annotate | Download (8.61 KB)
1 | 40608 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
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 2
|
||
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 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2011 Geographic Information research group: http://www.geoinfo.uji.es
|
||
26 | * Departamento de Lenguajes y Sistemas Inform�ticos (LSI)
|
||
27 | * Universitat Jaume I
|
||
28 | */
|
||
29 | package org.gvsig.metadata.lib.basic.impl; |
||
30 | |||
31 | import java.io.File; |
||
32 | import java.io.IOException; |
||
33 | import java.io.InputStream; |
||
34 | import java.util.ArrayList; |
||
35 | import java.util.HashMap; |
||
36 | import java.util.Iterator; |
||
37 | import java.util.Map; |
||
38 | |||
39 | import org.slf4j.Logger; |
||
40 | import org.slf4j.LoggerFactory; |
||
41 | import org.xmlpull.v1.XmlPullParserException; |
||
42 | |||
43 | import org.gvsig.metadata.Metadata; |
||
44 | import org.gvsig.metadata.MetadataContainer; |
||
45 | import org.gvsig.metadata.MetadataManager; |
||
46 | import org.gvsig.metadata.exceptions.AddMetadataDefinitionException; |
||
47 | import org.gvsig.metadata.exceptions.DuplicateMetadataDefinitionException; |
||
48 | import org.gvsig.metadata.exceptions.InvalidMetadataNamespaceException; |
||
49 | import org.gvsig.metadata.exceptions.MetadataException; |
||
50 | import org.gvsig.tools.ToolsLocator; |
||
51 | import org.gvsig.tools.dynobject.DynClass; |
||
52 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
53 | import org.gvsig.tools.dynobject.DynStruct; |
||
54 | import org.gvsig.tools.dynobject.impl.DefaultDynClassName; |
||
55 | import org.gvsig.tools.persistence.impl.exception.CantFindDefinitionInStreamException; |
||
56 | |||
57 | |||
58 | /**
|
||
59 | * Abstract metadata manager used in both metadata basic and full modules.
|
||
60 | */
|
||
61 | public abstract class AbstractMetadataManager implements MetadataManager { |
||
62 | |||
63 | private final Logger LOG = LoggerFactory |
||
64 | .getLogger(AbstractMetadataManager.class); |
||
65 | |||
66 | private Map metadataItems; |
||
67 | |||
68 | protected DynObjectManager dynManager;
|
||
69 | |||
70 | protected File metadataHomeFile = null; |
||
71 | |||
72 | private boolean doSavingValidation; |
||
73 | |||
74 | private boolean doExportingValidation; |
||
75 | |||
76 | public AbstractMetadataManager() {
|
||
77 | dynManager = ToolsLocator.getDynObjectManager(); |
||
78 | } |
||
79 | |||
80 | protected void initDefinitions() { |
||
81 | this.metadataItems = new HashMap(); |
||
82 | Iterator definitionsIter = ToolsLocator.getDynObjectManager().iterator(
|
||
83 | MetadataManager.METADATA_NAMESPACE); |
||
84 | DynStruct definition; |
||
85 | while (definitionsIter.hasNext()) {
|
||
86 | definition = (DynStruct) definitionsIter.next(); |
||
87 | if (!this.getMetadataItems().containsKey(definition)) { |
||
88 | Metadata metadata = createMetadata(definition); |
||
89 | this.getMetadataItems().put(definition, metadata);
|
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public DynStruct addDefinition(String name, String description) |
||
95 | throws MetadataException {
|
||
96 | |||
97 | DynClass definition = dynManager.createDynClass(METADATA_NAMESPACE, |
||
98 | name, description); |
||
99 | |||
100 | addDefinition(definition); |
||
101 | return definition;
|
||
102 | } |
||
103 | |||
104 | public DynStruct addDefinition(String metadataDefinitionName, |
||
105 | InputStream stream, ClassLoader loader) throws MetadataException { |
||
106 | Map x;
|
||
107 | try {
|
||
108 | x = dynManager.importDynClassDefinitions(stream, loader, |
||
109 | METADATA_NAMESPACE); |
||
110 | } catch (XmlPullParserException e) {
|
||
111 | throw new AddMetadataDefinitionException(e, metadataDefinitionName); |
||
112 | } catch (IOException e) { |
||
113 | throw new AddMetadataDefinitionException(e, metadataDefinitionName); |
||
114 | } |
||
115 | DynClass definition = (DynClass) x.get(metadataDefinitionName); |
||
116 | if (definition == null) { |
||
117 | throw new CantFindDefinitionInStreamException( |
||
118 | metadataDefinitionName, getKeys(x)); |
||
119 | } |
||
120 | addDefinition(definition); |
||
121 | return definition;
|
||
122 | } |
||
123 | |||
124 | private String getKeys(Map theMap) { |
||
125 | return new ArrayList(theMap.keySet()).toString(); |
||
126 | } |
||
127 | |||
128 | public DynStruct getDefinition(Metadata metadata) throws MetadataException { |
||
129 | return dynManager.get(METADATA_NAMESPACE, metadata.getMetadataName());
|
||
130 | } |
||
131 | |||
132 | public DynStruct getDefinition(String metadataName) { |
||
133 | if (metadataName == null) |
||
134 | return null; |
||
135 | if (metadataName.contains(":")) { |
||
136 | int index = metadataName.lastIndexOf(":"); |
||
137 | return dynManager.get(metadataName.substring(0, index), |
||
138 | metadataName.substring(index + 1, metadataName.length()));
|
||
139 | } |
||
140 | return dynManager.get(METADATA_NAMESPACE, metadataName);
|
||
141 | } |
||
142 | |||
143 | public void addDefinition(DynClass definition) throws MetadataException { |
||
144 | if (definition == null) { |
||
145 | return;
|
||
146 | } |
||
147 | if ((definition.getNamespace() == null) |
||
148 | || (definition.getNamespace().equals(""))) {
|
||
149 | definition.setNamespace(METADATA_NAMESPACE); |
||
150 | } |
||
151 | |||
152 | if (!METADATA_NAMESPACE.equalsIgnoreCase(definition.getNamespace())) {
|
||
153 | throw new InvalidMetadataNamespaceException(definition.getName(), |
||
154 | definition.getNamespace()); |
||
155 | } |
||
156 | if (dynManager.get(definition.getNamespace(), definition.getName()) != null) { |
||
157 | throw new DuplicateMetadataDefinitionException(definition.getName()); |
||
158 | } |
||
159 | dynManager.add(definition); |
||
160 | |||
161 | LOG.trace("Registered metadata definition {}.",
|
||
162 | definition.getFullName()); |
||
163 | |||
164 | } |
||
165 | |||
166 | public void removeDefinition(DynStruct dynStruct) { |
||
167 | if (dynStruct == null) |
||
168 | return;
|
||
169 | this.metadataItems.remove(dynStruct);
|
||
170 | dynManager.remove(dynStruct); |
||
171 | } |
||
172 | |||
173 | public MetadataContainer createMetadataContainer(String name) { |
||
174 | DynClass dynClass = dynManager.get(METADATA_NAMESPACE, name); |
||
175 | if (dynClass == null) { |
||
176 | throw new IllegalArgumentException( |
||
177 | "Can't locate class '"
|
||
178 | + new DefaultDynClassName(METADATA_NAMESPACE, name)
|
||
179 | .getFullName() + "'.");
|
||
180 | } |
||
181 | |||
182 | return createMetadataContainer(dynClass);
|
||
183 | // return new BasicMetadataContainer(dynClass);
|
||
184 | } |
||
185 | |||
186 | protected abstract MetadataContainer createMetadataContainer( |
||
187 | DynClass dynClass); |
||
188 | |||
189 | public void storeMetadata(Metadata metadata) throws MetadataException { |
||
190 | // In SimpleMetadataManager do nothing
|
||
191 | } |
||
192 | |||
193 | public void loadMetadata(Metadata metadata) throws MetadataException { |
||
194 | // In SimpleMetadataManager do nothing
|
||
195 | } |
||
196 | |||
197 | private Map getMetadataItems() { |
||
198 | if (this.metadataItems == null) { |
||
199 | initDefinitions(); |
||
200 | } |
||
201 | return this.metadataItems; |
||
202 | } |
||
203 | |||
204 | public Metadata getMetadata(DynStruct definition)
|
||
205 | throws InvalidMetadataNamespaceException {
|
||
206 | if (this.getMetadataItems().containsKey(definition)) { |
||
207 | return (Metadata) this.getMetadataItems().get(definition); |
||
208 | } |
||
209 | |||
210 | if (definition == null) { |
||
211 | return null; |
||
212 | } |
||
213 | |||
214 | if ((definition.getNamespace() == null) |
||
215 | || (!definition.getNamespace().equals(METADATA_NAMESPACE))) { |
||
216 | throw new InvalidMetadataNamespaceException(definition); |
||
217 | } |
||
218 | |||
219 | Metadata metadata = createMetadata(definition); |
||
220 | |||
221 | this.getMetadataItems().put(definition, metadata);
|
||
222 | |||
223 | LOG.trace("Registered metadata definition {}.",
|
||
224 | definition.getFullName()); |
||
225 | |||
226 | return metadata;
|
||
227 | } |
||
228 | |||
229 | protected abstract Metadata createMetadata(DynStruct definition); |
||
230 | |||
231 | public File getMetadataHomeRepository() { |
||
232 | if (this.metadataHomeFile == null) { |
||
233 | String path = System.getProperty("user.home"); |
||
234 | path += File.separator + ".org.gvsig.metadata"; |
||
235 | this.metadataHomeFile = new File(path); |
||
236 | } |
||
237 | if (!this.metadataHomeFile.exists()) { |
||
238 | this.metadataHomeFile.mkdirs();
|
||
239 | } |
||
240 | return this.metadataHomeFile; |
||
241 | } |
||
242 | |||
243 | public File getRepositoryFile(String relativePath) { |
||
244 | if ((relativePath == null) || (relativePath.equals(""))) |
||
245 | return this.metadataHomeFile; |
||
246 | return new File(getMetadataHomeRepository().toString() + File.separator |
||
247 | + relativePath); |
||
248 | } |
||
249 | |||
250 | public void storeDefinitions() { |
||
251 | Iterator it = ToolsLocator.getDynObjectManager().iterator(
|
||
252 | METADATA_NAMESPACE); |
||
253 | } |
||
254 | |||
255 | public Iterator getDefinitions() { |
||
256 | // initDefinitions();
|
||
257 | return getMetadataItems().values().iterator();
|
||
258 | } |
||
259 | |||
260 | public void setMetadataHomeRepository(File metadataHomeFile) { |
||
261 | if (metadataHomeFile == null) { |
||
262 | return;
|
||
263 | } |
||
264 | |||
265 | this.metadataHomeFile = metadataHomeFile;
|
||
266 | if (!this.metadataHomeFile.exists()) |
||
267 | this.metadataHomeFile.mkdirs();
|
||
268 | } |
||
269 | |||
270 | public Metadata getMetadata(String metadataID) |
||
271 | throws InvalidMetadataNamespaceException {
|
||
272 | return this.getMetadata(getDefinition(metadataID)); |
||
273 | } |
||
274 | |||
275 | public void setValidationBeforeSaving(boolean doValidation) { |
||
276 | this.doSavingValidation = doValidation;
|
||
277 | } |
||
278 | |||
279 | public boolean getValidationBeforeSaving() { |
||
280 | return this.doSavingValidation; |
||
281 | } |
||
282 | |||
283 | public void setValidationBeforeExporting(boolean doValidation) { |
||
284 | this.doExportingValidation = doValidation;
|
||
285 | } |
||
286 | |||
287 | public boolean getValidationBeforeExporting() { |
||
288 | return this.doExportingValidation; |
||
289 | } |
||
290 | } |