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