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