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