Project

General

Profile

Download (13.8 KB) Statistics
| Branch: | Tag: | Revision:
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.logging.log4j.LogManager;
20
import org.apache.logging.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.ICdmRepository;
28
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
31
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
32
import eu.etaxonomy.cdm.remote.controller.ClassificationController;
33
import eu.etaxonomy.cdm.remote.controller.ClassificationListController;
34
import eu.etaxonomy.cdm.remote.controller.NameController;
35
import eu.etaxonomy.cdm.remote.controller.TaxonNodePrintAppController;
36
import eu.etaxonomy.cdm.remote.controller.TaxonPortalController;
37
import eu.etaxonomy.cdm.remote.controller.TermNodeController;
38
import eu.etaxonomy.cdm.remote.controller.TermTreeController;
39
import eu.etaxonomy.cdm.remote.controller.TermTreeListController;
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
 * @since Jul 16, 2010
51
 */
52
@Component
53
public class LocalXMLEntityFactory extends XmlEntityFactoryBase {
54

    
55
    private static final Logger logger = LogManager.getLogger(LocalXMLEntityFactory.class);
56

    
57
    private final JsonView xmlView;
58

    
59
    @Autowired
60
    private final ICdmRepository repository;
61

    
62
    @Autowired
63
    private JsonConfig jsonConfig;
64
    @Autowired
65
    private JsonConfig jsonConfigPortal;
66

    
67
    @Autowired
68
    private ClassificationListController classificationListController;
69
    @Autowired
70
    private ClassificationController classificationController;
71
    @Autowired
72
    private TaxonNodePrintAppController taxonNodePrintAppController;
73
    @Autowired
74
    private NameController nameController;
75
    @Autowired
76
    private TermTreeListController featureTreeListController;
77

    
78
    @Autowired
79
    private TaxonPortalController taxonPortalController;
80

    
81
    @Autowired
82
    private TermTreeController featureTreeController;
83

    
84
    @Autowired
85
    private TermNodeController termNodeController;
86

    
87
    @Autowired
88
    private PolytomousKeyNodeDtoController polytomousKeyNodeDtoController;
89

    
90
    private final IProgressMonitor monitor;
91

    
92
    protected LocalXMLEntityFactory(
93
            ICdmRepository applicationConfiguration,
94
            IProgressMonitor monitor) {
95
        this.repository = applicationConfiguration;
96
        this.monitor = monitor;
97
        this.xmlView = new JsonView();
98
        xmlView.setType(Type.XML);
99
        initControllers();
100
        initJsonConfigs();
101
    }
102

    
103
    private void initControllers() {
104
    	classificationListController = (ClassificationListController) repository
105
    			.getBean("classificationListController");
106
    	classificationController = (ClassificationController) repository
107
    			.getBean("classificationController");
108
    	taxonNodePrintAppController = (TaxonNodePrintAppController) repository
109
    			.getBean("taxonNodePrintAppController");
110

    
111
    	nameController = (NameController) repository
112
    			.getBean("nameController");
113

    
114
    	featureTreeListController = (TermTreeListController) repository
115
    			.getBean("featureTreeListController");
116
    	featureTreeController = (TermTreeController) repository
117
    			.getBean("featureTreeController");
118
    	termNodeController = (TermNodeController) repository
119
    			.getBean("featureNodeController");
120

    
121
    	taxonPortalController = (TaxonPortalController) repository
122
    			.getBean("taxonPortalController");
123

    
124
    	polytomousKeyNodeDtoController = (PolytomousKeyNodeDtoController) repository.getBean("polytomousKeyNodeDtoController");
125
    }
126

    
127
    private void initJsonConfigs() {
128
        jsonConfig = (JsonConfig) repository.getBean("jsonConfig");
129
        jsonConfigPortal = (JsonConfig) repository
130
                .getBean("jsonConfigPortal");
131
    }
132

    
133
    @Override
134
    public List<Element> getClassifications() {
135
        xmlView.setJsonConfig(jsonConfig);
136
        Object resultObject = classificationListController.doList(null, null,
137
                null, null, null);
138

    
139
        Element result = render(resultObject);
140

    
141
        return processElementList(result);
142
    }
143

    
144
    @Override
145
    public List<Element> getChildNodes(Element treeNode) {
146
        xmlView.setJsonConfig(jsonConfig);
147
        EntityType entityType = XMLHelper.getEntityType(treeNode);
148

    
149
        UUID uuid = XMLHelper.getUuid(treeNode);
150

    
151
        boolean includeUnpublished = DaoBase.NO_UNPUBLISHED;  //for now we do not allow any remote service to publish unpublished data
152

    
153
        Object resultObject = null;
154
        try {
155
            if (EntityType.CLASSIFICATION.equals(entityType)) {
156
                resultObject = classificationController.getChildNodes(uuid, null,
157
                        null, null, null);
158
            } else if (EntityType.TAXON_NODE.equals(entityType)) {
159
                resultObject = taxonNodePrintAppController
160
                        .getChildNodes(uuid, includeUnpublished, null);
161
            }
162
        } catch (IOException e) {
163
            monitor.warning(e.getLocalizedMessage(), e);
164
            logger.error(e);
165
        }
166

    
167
        Element result = render(resultObject);
168

    
169
        return processElementList(result);
170
    }
171

    
172
    @Override
173
    public Element getTaxonNode(UUID taxonNodeUuid) {
174
        xmlView.setJsonConfig(jsonConfig);
175
        Object resultObject = null;
176
        try {
177
            resultObject = taxonNodePrintAppController.doGet(taxonNodeUuid, null, null);
178
        } catch (IOException e) {
179
            monitor.warning(e.getLocalizedMessage(), e);
180
            logger.error(e);
181
        }
182
        Element result = render(resultObject);
183

    
184
        return result;
185
    }
186

    
187
    @Override
188
    public List<Element> getFeatureTrees() {
189
        xmlView.setJsonConfig(jsonConfig);
190
        Object resultObject = featureTreeListController.doList(0, -1, null, null, null);
191

    
192
        Element result = render(resultObject);
193

    
194
        return processElementList(result);
195
    }
196

    
197
    @Override
198
    public Element getTermNode(UUID uuid) {
199
        xmlView.setJsonConfig(jsonConfig);
200
        Object resultObject = null;
201
        try {
202
            resultObject = termNodeController.doGet(uuid, null, null);
203
        } catch (IOException e) {
204
            monitor.warning(e.getLocalizedMessage(), e);
205
            logger.error(e);
206
        }
207
        Element result = render(resultObject);
208

    
209
        return result;
210
    }
211

    
212
    @Override
213
    public Element getFeatureForTermNode(UUID uuid) {
214
        xmlView.setJsonConfig(jsonConfig);
215
        Object resultObject = null;
216
        try {
217
            resultObject = termNodeController.getCdmBaseProperty(uuid,
218
                    "feature", null);
219
        } catch (IOException e) {
220
            monitor.warning(e.getLocalizedMessage(), e);
221
            logger.error(e);
222
        }
223
        Element result = render(resultObject);
224

    
225
        return result;
226
    }
227

    
228
    @Override
229
    public Element getFeatureTree(UUID uuid) {
230
        xmlView.setJsonConfig(jsonConfig);
231

    
232
        Object resultObject = null;
233
        try {
234
            resultObject = featureTreeController.doGet(uuid, null, null);
235
        } catch (IOException e) {
236
            monitor.warning(e.getLocalizedMessage(), e);
237
            logger.error(e);
238
        }
239
        Element result = render(resultObject);
240

    
241
        return result;
242
    }
243

    
244
    @Override
245
    public Element getTaxonForTaxonNode(Element taxonNodeElement) {
246
        xmlView.setJsonConfig(jsonConfig);
247
        UUID uuid = XMLHelper.getUuid(taxonNodeElement);
248

    
249
        Object resultObject = null;
250
        try {
251
            resultObject = taxonNodePrintAppController.doGetTaxon(uuid);
252
        } catch (IOException e) {
253
            monitor.warning(e.getLocalizedMessage(), e);
254
            logger.error(e);
255
        }
256

    
257
        Element result = render(resultObject);
258

    
259
        return result;
260
    }
261

    
262
    @Override
263
    public Element getAcceptedTaxonElement(Element taxonElement) {
264
        xmlView.setJsonConfig(jsonConfigPortal);
265
        UUID uuid = XMLHelper.getUuid(taxonElement);
266

    
267
        Object resultObject = null;
268
        try {
269
            resultObject = HibernateProxyHelper.deproxy(taxonPortalController
270
                    .doGet(uuid, null, null));
271
        } catch (IOException e) {
272
            monitor.warning(e.getLocalizedMessage(), e);
273
            logger.error(e);
274
        }
275

    
276
        Element result = render(resultObject);
277

    
278
        return result;
279
    }
280

    
281

    
282
    @Override
283
    public List<Element> getSynonymy(Element taxonElement) {
284
        xmlView.setJsonConfig(jsonConfigPortal);
285
        UUID uuid = XMLHelper.getUuid(taxonElement);
286

    
287
        ModelAndView resultObject = null;
288
        try {
289
            resultObject = taxonPortalController
290
                    .doGetSynonymy(uuid, null, null, null);
291
        } catch (IOException e) {
292
            monitor.warning(e.getLocalizedMessage(), e);
293
            logger.error(e);
294
        }
295

    
296
        Element result = render(resultObject.getModel().values().iterator()
297
                .next());
298

    
299
        List<Element> elementList = new ArrayList<Element>();
300

    
301
        for (Object child : result.getChildren()) {
302
            if (child instanceof Element) {
303
                Element childElement = (Element) ((Element) child).clone();
304

    
305
                childElement.detach();
306

    
307
                elementList.add(childElement);
308
            }
309
        }
310

    
311
        return elementList;
312
    }
313

    
314
    @Override
315
    public List<Element> getTypeDesignations(Element nameElement) {
316
        xmlView.setJsonConfig(jsonConfig);
317

    
318
        UUID uuid = XMLHelper.getUuid(nameElement);
319

    
320
        Object resultObject = null;
321

    
322
        try {
323
            resultObject = nameController.getCdmBaseProperty(uuid,"typeDesignations", null);
324

    
325
    		//LORNA: could use service here directly instead of controller with request set to null
326
            //resultObject = nameController.doListNameTypeDesignations(uuid, null, null);
327

    
328
        } catch (IOException e) {
329
            monitor.warning(e.getLocalizedMessage(), e);
330
            logger.error(e);
331
        }
332
        Element result = render(resultObject);
333

    
334
        return processElementList(result);
335
    }
336

    
337
    @Override
338
    public Element getDescriptions(Element taxonElement) {
339
        xmlView.setJsonConfig(jsonConfigPortal);
340
        UUID uuid = XMLHelper.getUuid(taxonElement);
341

    
342
		Object resultObject = null;
343

    
344
		try {
345
			resultObject = taxonPortalController.doGetDescriptions(uuid, null, null,
346
					null, null);
347
		} catch (IOException e) {
348
			monitor.warning(e.getLocalizedMessage(), e);
349
			logger.error(e);
350
		}
351

    
352
		Element result = render(resultObject);
353
        //Element result = render(resultObject);
354

    
355
        return result;
356
    }
357

    
358

    
359
    @Override
360
    public Element getPolytomousKey(Element taxonElement) {
361
    	xmlView.setJsonConfig(jsonConfigPortal);
362
    	UUID uuid = XMLHelper.getUuid(taxonElement);
363

    
364
    	ModelAndView resultObject = null;
365
    	try {
366
    		//e.g. uuid 02b6579c-2f6d-4df0-b77c-e5d259ddb307 must be the uuid of the polytomous key. Where do we get that
367
    		//check web service calls in portal.......
368
    		/////resultObject = polytomousKeyNodeDtoController.doLinkedStyle(uuid, null, null);
369
    		resultObject = polytomousKeyNodeDtoController.doLinkedStyleByTaxonomicScope(uuid, null, null, null, null);
370

    
371
    	} catch (IOException e) {
372
    		monitor.warning(e.getLocalizedMessage(), e);
373
    		logger.error(e);
374
    	}
375

    
376
    	Element result = null;
377

    
378
    	if (resultObject.getModel().values().iterator().hasNext()) {
379

    
380
    		result = render(resultObject.getModel().values().iterator().next());
381
    	}
382

    
383
    	return result;
384

    
385
    	//List<Element> elementList = new ArrayList<Element>();
386

    
387
    	/*if (result != null){
388

    
389
    		for (Object child : result.getChildren()) {
390
    			if (child instanceof Element) {
391
    				Element childElement = (Element) ((Element) child).clone();
392
    				childElement.detach();
393

    
394
    				elementList.add(childElement);
395
    			} else {
396
    				logger.error("child is not an instance of element");
397
    			}
398
    		}
399
    	}
400
    	return elementList;*/
401
    }
402

    
403
    /**
404
     *
405
     * @param result
406
     * @return
407
     */
408
    private Element render(Object result) {
409
        File tmpFile = null;
410
        try {
411
            Document document = new Document();
412
            tmpFile = File.createTempFile("printpublisher", null);
413

    
414
            PrintWriter writer = new PrintWriter(tmpFile, "UTF-8");
415

    
416
            xmlView.render(HibernateProxyHelper.deproxy(result), writer, null, null, null);
417

    
418
            document = builder.build(tmpFile);
419

    
420
            Element root = document.getRootElement();
421

    
422
            return root;
423

    
424
        } catch (IOException e) {
425
            monitor.warning(e.getLocalizedMessage(), e);
426
            throw new RuntimeException(e);
427
        } catch (Exception e) {
428
            monitor.warning(e.getLocalizedMessage(), e);
429
        } finally {
430
            if (tmpFile != null) {
431
                tmpFile.delete();
432
            }
433
        }
434

    
435
        return new Element("somethingWentWrong");
436
    }
437

    
438
    @Override
439
    public List<Element> getReferences(Element referenceElement) {
440
        // TODO Auto-generated method stub
441
        return null;
442
    }
443

    
444
	@Override
445
	public Element getTaxonNodesByName(String taxonName, String classification) {
446
		// TODO Auto-generated method stub
447
		return null;
448
	}
449

    
450
	@Override
451
	public List<Element> getMedia(Element taxonElement) {
452
		// TODO Auto-generated method stub
453
		return null;
454
	}
455
}
(3-3/10)