1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.print;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.IOException;
|
14
|
import java.io.PrintWriter;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.jdom.Document;
|
21
|
import org.jdom.Element;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
import org.springframework.web.servlet.ModelAndView;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
27
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
|
30
|
import eu.etaxonomy.cdm.remote.controller.ClassificationController;
|
31
|
import eu.etaxonomy.cdm.remote.controller.ClassificationListController;
|
32
|
import eu.etaxonomy.cdm.remote.controller.FeatureNodeController;
|
33
|
import eu.etaxonomy.cdm.remote.controller.FeatureTreeController;
|
34
|
import eu.etaxonomy.cdm.remote.controller.FeatureTreeListController;
|
35
|
import eu.etaxonomy.cdm.remote.controller.NameController;
|
36
|
import eu.etaxonomy.cdm.remote.controller.TaxonNodePrintAppController;
|
37
|
import eu.etaxonomy.cdm.remote.controller.TaxonPortalController;
|
38
|
import eu.etaxonomy.cdm.remote.controller.dto.PolytomousKeyNodeDtoController;
|
39
|
import eu.etaxonomy.cdm.remote.view.JsonView;
|
40
|
import eu.etaxonomy.cdm.remote.view.JsonView.Type;
|
41
|
import net.sf.json.JsonConfig;
|
42
|
|
43
|
/**
|
44
|
* The local entity factory assumes that an application context is available and
|
45
|
* may be accessed directly without the detour via http.
|
46
|
*
|
47
|
* @author n.hoffmann
|
48
|
* @since Jul 16, 2010
|
49
|
*/
|
50
|
@Component
|
51
|
public class LocalXMLEntityFactory extends XmlEntityFactoryBase {
|
52
|
private static final Logger logger = Logger
|
53
|
.getLogger(LocalXMLEntityFactory.class);
|
54
|
|
55
|
private final JsonView xmlView;
|
56
|
|
57
|
@Autowired
|
58
|
private final ICdmRepository applicationConfiguration;
|
59
|
|
60
|
@Autowired
|
61
|
private JsonConfig jsonConfig;
|
62
|
@Autowired
|
63
|
private JsonConfig jsonConfigPortal;
|
64
|
|
65
|
@Autowired
|
66
|
private ClassificationListController classificationListController;
|
67
|
@Autowired
|
68
|
private ClassificationController classificationController;
|
69
|
@Autowired
|
70
|
private TaxonNodePrintAppController taxonNodePrintAppController;
|
71
|
@Autowired
|
72
|
private NameController nameController;
|
73
|
@Autowired
|
74
|
private FeatureTreeListController featureTreeListController;
|
75
|
|
76
|
@Autowired
|
77
|
private TaxonPortalController taxonPortalController;
|
78
|
|
79
|
@Autowired
|
80
|
private FeatureTreeController featureTreeController;
|
81
|
|
82
|
@Autowired
|
83
|
private FeatureNodeController featureNodeController;
|
84
|
|
85
|
@Autowired
|
86
|
private PolytomousKeyNodeDtoController polytomousKeyNodeDtoController;
|
87
|
|
88
|
private final IProgressMonitor monitor;
|
89
|
|
90
|
/**
|
91
|
*
|
92
|
* @param applicationConfiguration
|
93
|
* @param monitor
|
94
|
*/
|
95
|
protected LocalXMLEntityFactory(
|
96
|
ICdmRepository applicationConfiguration,
|
97
|
IProgressMonitor monitor) {
|
98
|
this.applicationConfiguration = applicationConfiguration;
|
99
|
this.monitor = monitor;
|
100
|
this.xmlView = new JsonView();
|
101
|
xmlView.setType(Type.XML);
|
102
|
initControllers();
|
103
|
initJsonConfigs();
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
*
|
108
|
*/
|
109
|
private void initControllers() {
|
110
|
classificationListController = (ClassificationListController) applicationConfiguration
|
111
|
.getBean("classificationListController");
|
112
|
classificationController = (ClassificationController) applicationConfiguration
|
113
|
.getBean("classificationController");
|
114
|
taxonNodePrintAppController = (TaxonNodePrintAppController) applicationConfiguration
|
115
|
.getBean("taxonNodePrintAppController");
|
116
|
|
117
|
nameController = (NameController) applicationConfiguration
|
118
|
.getBean("nameController");
|
119
|
|
120
|
featureTreeListController = (FeatureTreeListController) applicationConfiguration
|
121
|
.getBean("featureTreeListController");
|
122
|
featureTreeController = (FeatureTreeController) applicationConfiguration
|
123
|
.getBean("featureTreeController");
|
124
|
featureNodeController = (FeatureNodeController) applicationConfiguration
|
125
|
.getBean("featureNodeController");
|
126
|
|
127
|
taxonPortalController = (TaxonPortalController) applicationConfiguration
|
128
|
.getBean("taxonPortalController");
|
129
|
|
130
|
polytomousKeyNodeDtoController = (PolytomousKeyNodeDtoController) applicationConfiguration.getBean("polytomousKeyNodeDtoController");
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
*
|
135
|
*/
|
136
|
private void initJsonConfigs() {
|
137
|
jsonConfig = (JsonConfig) applicationConfiguration.getBean("jsonConfig");
|
138
|
jsonConfigPortal = (JsonConfig) applicationConfiguration
|
139
|
.getBean("jsonConfigPortal");
|
140
|
}
|
141
|
|
142
|
/*
|
143
|
* (non-Javadoc)
|
144
|
*
|
145
|
* @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getClassifications()
|
146
|
*/
|
147
|
@Override
|
148
|
public List<Element> getClassifications() {
|
149
|
xmlView.setJsonConfig(jsonConfig);
|
150
|
Object resultObject = classificationListController.doList(null, null,
|
151
|
null, null, null);
|
152
|
|
153
|
Element result = render(resultObject);
|
154
|
|
155
|
return processElementList(result);
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
public List<Element> getChildNodes(Element treeNode) {
|
160
|
xmlView.setJsonConfig(jsonConfig);
|
161
|
EntityType entityType = XMLHelper.getEntityType(treeNode);
|
162
|
|
163
|
UUID uuid = XMLHelper.getUuid(treeNode);
|
164
|
|
165
|
Object resultObject = null;
|
166
|
try {
|
167
|
if (EntityType.CLASSIFICATION.equals(entityType)) {
|
168
|
resultObject = classificationController.getChildNodes(uuid,
|
169
|
null, null);
|
170
|
} else if (EntityType.TAXON_NODE.equals(entityType)) {
|
171
|
resultObject = taxonNodePrintAppController
|
172
|
.getChildNodes(uuid, null);
|
173
|
}
|
174
|
} catch (IOException e) {
|
175
|
monitor.warning(e.getLocalizedMessage(), e);
|
176
|
logger.error(e);
|
177
|
}
|
178
|
|
179
|
Element result = render(resultObject);
|
180
|
|
181
|
return processElementList(result);
|
182
|
}
|
183
|
|
184
|
@Override
|
185
|
public Element getTaxonNode(UUID taxonNodeUuid) {
|
186
|
xmlView.setJsonConfig(jsonConfig);
|
187
|
Object resultObject = null;
|
188
|
try {
|
189
|
resultObject = taxonNodePrintAppController.doGet(taxonNodeUuid, null, null);
|
190
|
} catch (IOException e) {
|
191
|
monitor.warning(e.getLocalizedMessage(), e);
|
192
|
logger.error(e);
|
193
|
}
|
194
|
Element result = render(resultObject);
|
195
|
|
196
|
return result;
|
197
|
}
|
198
|
|
199
|
/*
|
200
|
* (non-Javadoc)
|
201
|
*
|
202
|
* @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTrees()
|
203
|
*/
|
204
|
@Override
|
205
|
public List<Element> getFeatureTrees() {
|
206
|
xmlView.setJsonConfig(jsonConfig);
|
207
|
Object resultObject = featureTreeListController.doList(0, -1, null, null, null);
|
208
|
|
209
|
Element result = render(resultObject);
|
210
|
|
211
|
return processElementList(result);
|
212
|
}
|
213
|
|
214
|
/*
|
215
|
* (non-Javadoc)
|
216
|
*
|
217
|
* @see
|
218
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureNode(java.util
|
219
|
* .UUID)
|
220
|
*/
|
221
|
@Override
|
222
|
public Element getFeatureNode(UUID uuid) {
|
223
|
xmlView.setJsonConfig(jsonConfig);
|
224
|
Object resultObject = null;
|
225
|
try {
|
226
|
resultObject = featureNodeController.doGet(uuid, null, null);
|
227
|
} catch (IOException e) {
|
228
|
monitor.warning(e.getLocalizedMessage(), e);
|
229
|
logger.error(e);
|
230
|
}
|
231
|
Element result = render(resultObject);
|
232
|
|
233
|
return result;
|
234
|
}
|
235
|
|
236
|
@Override
|
237
|
public Element getFeatureForFeatureNode(UUID uuid) {
|
238
|
xmlView.setJsonConfig(jsonConfig);
|
239
|
Object resultObject = null;
|
240
|
try {
|
241
|
resultObject = featureNodeController.getCdmBaseProperty(uuid,
|
242
|
"feature", null);
|
243
|
} catch (IOException e) {
|
244
|
monitor.warning(e.getLocalizedMessage(), e);
|
245
|
logger.error(e);
|
246
|
}
|
247
|
Element result = render(resultObject);
|
248
|
|
249
|
return result;
|
250
|
}
|
251
|
|
252
|
/*
|
253
|
* (non-Javadoc)
|
254
|
*
|
255
|
* @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTree()
|
256
|
*/
|
257
|
@Override
|
258
|
public Element getFeatureTree(UUID uuid) {
|
259
|
xmlView.setJsonConfig(jsonConfig);
|
260
|
|
261
|
Object resultObject = null;
|
262
|
try {
|
263
|
resultObject = featureTreeController.doGet(uuid, null, null);
|
264
|
} catch (IOException e) {
|
265
|
monitor.warning(e.getLocalizedMessage(), e);
|
266
|
logger.error(e);
|
267
|
}
|
268
|
Element result = render(resultObject);
|
269
|
|
270
|
return result;
|
271
|
}
|
272
|
|
273
|
@Override
|
274
|
public Element getTaxonForTaxonNode(Element taxonNodeElement) {
|
275
|
xmlView.setJsonConfig(jsonConfig);
|
276
|
UUID uuid = XMLHelper.getUuid(taxonNodeElement);
|
277
|
|
278
|
Object resultObject = null;
|
279
|
try {
|
280
|
resultObject = taxonNodePrintAppController.doGetTaxon(uuid);
|
281
|
} catch (IOException e) {
|
282
|
monitor.warning(e.getLocalizedMessage(), e);
|
283
|
logger.error(e);
|
284
|
}
|
285
|
|
286
|
Element result = render(resultObject);
|
287
|
|
288
|
return result;
|
289
|
}
|
290
|
|
291
|
@Override
|
292
|
public Element getAcceptedTaxonElement(Element taxonElement) {
|
293
|
xmlView.setJsonConfig(jsonConfigPortal);
|
294
|
UUID uuid = XMLHelper.getUuid(taxonElement);
|
295
|
|
296
|
Object resultObject = null;
|
297
|
try {
|
298
|
resultObject = HibernateProxyHelper.deproxy(taxonPortalController
|
299
|
.doGet(uuid, null, null));
|
300
|
} catch (IOException e) {
|
301
|
monitor.warning(e.getLocalizedMessage(), e);
|
302
|
logger.error(e);
|
303
|
}
|
304
|
|
305
|
Element result = render(resultObject);
|
306
|
|
307
|
return result;
|
308
|
}
|
309
|
|
310
|
|
311
|
@Override
|
312
|
public List<Element> getSynonymy(Element taxonElement) {
|
313
|
xmlView.setJsonConfig(jsonConfigPortal);
|
314
|
UUID uuid = XMLHelper.getUuid(taxonElement);
|
315
|
|
316
|
ModelAndView resultObject = null;
|
317
|
try {
|
318
|
resultObject = taxonPortalController
|
319
|
.doGetSynonymy(uuid, null, null);
|
320
|
} catch (IOException e) {
|
321
|
monitor.warning(e.getLocalizedMessage(), e);
|
322
|
logger.error(e);
|
323
|
}
|
324
|
|
325
|
Element result = render(resultObject.getModel().values().iterator()
|
326
|
.next());
|
327
|
|
328
|
List<Element> elementList = new ArrayList<Element>();
|
329
|
|
330
|
for (Object child : result.getChildren()) {
|
331
|
if (child instanceof Element) {
|
332
|
Element childElement = (Element) ((Element) child).clone();
|
333
|
|
334
|
childElement.detach();
|
335
|
|
336
|
elementList.add(childElement);
|
337
|
}
|
338
|
}
|
339
|
|
340
|
return elementList;
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public List<Element> getTypeDesignations(Element nameElement) {
|
345
|
xmlView.setJsonConfig(jsonConfig);
|
346
|
|
347
|
UUID uuid = XMLHelper.getUuid(nameElement);
|
348
|
|
349
|
Object resultObject = null;
|
350
|
|
351
|
try {
|
352
|
resultObject = nameController.getCdmBaseProperty(uuid,"typeDesignations", null);
|
353
|
|
354
|
//LORNA: could use service here directly instead of controller with request set to null
|
355
|
//resultObject = nameController.doListNameTypeDesignations(uuid, null, null);
|
356
|
|
357
|
} catch (IOException e) {
|
358
|
monitor.warning(e.getLocalizedMessage(), e);
|
359
|
logger.error(e);
|
360
|
}
|
361
|
Element result = render(resultObject);
|
362
|
|
363
|
return processElementList(result);
|
364
|
}
|
365
|
|
366
|
@Override
|
367
|
public Element getDescriptions(Element taxonElement) {
|
368
|
xmlView.setJsonConfig(jsonConfigPortal);
|
369
|
UUID uuid = XMLHelper.getUuid(taxonElement);
|
370
|
|
371
|
Object resultObject = null;
|
372
|
|
373
|
try {
|
374
|
resultObject = taxonPortalController.doGetDescriptions(uuid, null,
|
375
|
null, null);
|
376
|
} catch (IOException e) {
|
377
|
monitor.warning(e.getLocalizedMessage(), e);
|
378
|
logger.error(e);
|
379
|
}
|
380
|
|
381
|
Element result = render(resultObject);
|
382
|
//Element result = render(resultObject);
|
383
|
|
384
|
return result;
|
385
|
}
|
386
|
|
387
|
|
388
|
@Override
|
389
|
public Element getPolytomousKey(Element taxonElement) {
|
390
|
xmlView.setJsonConfig(jsonConfigPortal);
|
391
|
UUID uuid = XMLHelper.getUuid(taxonElement);
|
392
|
|
393
|
ModelAndView resultObject = null;
|
394
|
try {
|
395
|
//e.g. uuid 02b6579c-2f6d-4df0-b77c-e5d259ddb307 must be the uuid of the polytomous key. Where do we get that
|
396
|
//check web service calls in portal.......
|
397
|
/////resultObject = polytomousKeyNodeDtoController.doLinkedStyle(uuid, null, null);
|
398
|
resultObject = polytomousKeyNodeDtoController.doLinkedStyleByTaxonomicScope(uuid, null, null, null, null);
|
399
|
|
400
|
} catch (IOException e) {
|
401
|
monitor.warning(e.getLocalizedMessage(), e);
|
402
|
logger.error(e);
|
403
|
}
|
404
|
|
405
|
Element result = null;
|
406
|
|
407
|
if (resultObject.getModel().values().iterator().hasNext()) {
|
408
|
|
409
|
result = render(resultObject.getModel().values().iterator().next());
|
410
|
}
|
411
|
|
412
|
return result;
|
413
|
|
414
|
//List<Element> elementList = new ArrayList<Element>();
|
415
|
|
416
|
/*if (result != null){
|
417
|
|
418
|
for (Object child : result.getChildren()) {
|
419
|
if (child instanceof Element) {
|
420
|
Element childElement = (Element) ((Element) child).clone();
|
421
|
childElement.detach();
|
422
|
|
423
|
elementList.add(childElement);
|
424
|
} else {
|
425
|
logger.error("child is not an instance of element");
|
426
|
}
|
427
|
}
|
428
|
}
|
429
|
return elementList;*/
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
*
|
434
|
* @param result
|
435
|
* @return
|
436
|
*/
|
437
|
private Element render(Object result) {
|
438
|
File tmpFile = null;
|
439
|
try {
|
440
|
Document document = new Document();
|
441
|
tmpFile = File.createTempFile("printpublisher", null);
|
442
|
|
443
|
PrintWriter writer = new PrintWriter(tmpFile, "UTF-8");
|
444
|
|
445
|
xmlView.render(HibernateProxyHelper.deproxy(result), writer, null, null, null);
|
446
|
|
447
|
document = builder.build(tmpFile);
|
448
|
|
449
|
Element root = document.getRootElement();
|
450
|
|
451
|
return root;
|
452
|
|
453
|
} catch (IOException e) {
|
454
|
monitor.warning(e.getLocalizedMessage(), e);
|
455
|
throw new RuntimeException(e);
|
456
|
} catch (Exception e) {
|
457
|
monitor.warning(e.getLocalizedMessage(), e);
|
458
|
} finally {
|
459
|
if (tmpFile != null) {
|
460
|
tmpFile.delete();
|
461
|
}
|
462
|
}
|
463
|
|
464
|
return new Element("somethingWentWrong");
|
465
|
}
|
466
|
|
467
|
@Override
|
468
|
public List<Element> getReferences(Element referenceElement) {
|
469
|
// TODO Auto-generated method stub
|
470
|
return null;
|
471
|
}
|
472
|
|
473
|
@Override
|
474
|
public Element getTaxonNodesByName(String taxonName, String classification) {
|
475
|
// TODO Auto-generated method stub
|
476
|
return null;
|
477
|
}
|
478
|
|
479
|
@Override
|
480
|
public List<Element> getMedia(Element taxonElement) {
|
481
|
// TODO Auto-generated method stub
|
482
|
return null;
|
483
|
}
|
484
|
}
|