Project

General

Profile

Download (7.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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
package eu.etaxonomy.cdm.io.specimen.abcd206.in;
10

    
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.util.HashSet;
14
import java.util.Set;
15

    
16
import org.apache.http.client.ClientProtocolException;
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.w3c.dom.Element;
19

    
20
import eu.etaxonomy.cdm.api.application.ICdmRepository;
21
import eu.etaxonomy.cdm.common.URI;
22
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
23
import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseQueryServiceWrapper;
24

    
25
/**
26
 * @author pplitzner
27
 * @since 16.06.2015
28
 */
29
public class UnitAssociationParser {
30

    
31
    private static final Logger logger = LogManager.getLogger(UnitAssociationParser.class);
32

    
33
    private final String prefix;
34

    
35
    private final SpecimenImportReport report;
36

    
37
    private final ICdmRepository cdmAppController;
38

    
39
    public UnitAssociationParser(String prefix, SpecimenImportReport report, ICdmRepository cdmAppController) {
40
        this.prefix = prefix;
41
        this.report = report;
42
        this.cdmAppController = cdmAppController;
43
    }
44

    
45
    public UnitAssociationWrapper parse(Element unitAssociation){
46
        if(prefix.equals("abcd:")){
47

    
48
        }
49
        else if(prefix.equals("abcd21:")){
50

    
51
        }
52
        //unit id
53
        String unitId = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"UnitID"));
54
        if(unitId==null){
55
            unitId = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitID"));//abcd 2.0.6
56
        }
57
        //institution code
58
        String institutionCode = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"SourceInstitutionCode"));
59
        if(institutionCode==null){
60
            institutionCode = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitSourceInstitutionCode"));//abcd 2.0.6
61
        }
62
        //institution name
63
        String institutionName = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"SourceName"));
64
        if(institutionName==null){
65
            institutionName = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitSourceName"));//abcd 2.0.6
66
        }
67
        //data access point
68
        URI datasetAccessPoint = AbcdParseUtility.parseFirstUri(unitAssociation.getElementsByTagName(prefix+"DatasetAccessPoint"), report);
69
        if(datasetAccessPoint==null){
70
            datasetAccessPoint = AbcdParseUtility.parseFirstUri(unitAssociation.getElementsByTagName(prefix+"Comment"), report);//abcd 2.0.6
71
        }
72
        //association type
73
        String associationType = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociationType"));
74

    
75
        String unableToLoadMessage = String.format("Unable to load unit %s from %s", unitId, datasetAccessPoint);
76
        if(unitId!=null && datasetAccessPoint!=null){
77
            BioCaseQueryServiceWrapper serviceWrapper = new BioCaseQueryServiceWrapper();
78
            Set<String[]> unitIds = new HashSet<String[]>();
79
            String[] unitIdArray = {unitId};
80
            unitIds.add(unitIdArray);
81
            OccurenceQuery query = new OccurenceQuery(unitIds);
82
            try {
83
                InputStream inputStream = serviceWrapper.query(query, datasetAccessPoint);
84
                if(inputStream!=null){
85
                    UnitAssociationWrapper unitAssociationWrapper = null;
86
                    try {
87
                        unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(inputStream, report);
88
                    } catch (Exception e) {
89
                        String exceptionMessage = "An exception occurred during parsing of associated units!";
90
                        logger.error(exceptionMessage, e);
91
                        report.addException(exceptionMessage, e);
92
                    }
93

    
94
                    if(unitAssociationWrapper!=null){
95
                        unitAssociationWrapper.setAssociationType(associationType);
96
                        unitAssociationWrapper.setAccesPoint(datasetAccessPoint);
97
                        if(unitAssociationWrapper.getAssociatedUnits()!=null && unitAssociationWrapper.getAssociatedUnits().getLength()>1){
98
                            String moreThanOneUnitFoundMessage = String.format("More than one unit was found for unit association to %s", unitId);
99
                            logger.warn(moreThanOneUnitFoundMessage);
100
                            report.addInfoMessage(moreThanOneUnitFoundMessage);
101
                        }
102
                    }
103
                    return unitAssociationWrapper;
104
                }
105
                else{
106
                    logger.error(unableToLoadMessage);
107
                    report.addInfoMessage(unableToLoadMessage);
108
                }
109
            } catch (ClientProtocolException e) {
110
                logger.error(unableToLoadMessage, e);
111
                report.addInfoMessage(unableToLoadMessage);
112
            } catch (IOException e) {
113
                logger.error(unableToLoadMessage, e);
114
                report.addInfoMessage(unableToLoadMessage);
115
            }
116
        }
117
        else{
118
            report.addInfoMessage(unableToLoadMessage);
119
        }
120
        return null;
121
    }
122

    
123
    public UnitAssociationWrapper parseSiblings(String unitID, URI datasetAccessPoint){
124

    
125
        String unableToLoadMessage = String.format("Unable to load unit %s from %s", unitID, datasetAccessPoint);
126
        if(unitID!=null && datasetAccessPoint!=null){
127
            BioCaseQueryServiceWrapper serviceWrapper = new BioCaseQueryServiceWrapper();
128

    
129
            OccurenceQuery query = new OccurenceQuery(unitID);
130

    
131
            try {
132
                InputStream inputStream = serviceWrapper.querySiblings(query, datasetAccessPoint);
133

    
134
                if(inputStream!=null){
135
                    UnitAssociationWrapper unitAssociationWrapper = null;
136
                    try {
137
                        unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(inputStream, report);
138
                    } catch (Exception e) {
139
                        String exceptionMessage = "An exception occurred during parsing of associated units!";
140
                        logger.error(exceptionMessage, e);
141
                        report.addException(exceptionMessage, e);
142
                    }
143

    
144
                return unitAssociationWrapper;
145
                }else{
146
                    logger.error(unableToLoadMessage);
147
                    report.addInfoMessage(unableToLoadMessage);
148
                }
149
            } catch (ClientProtocolException e1) {
150
                // TODO Auto-generated catch block
151
                e1.printStackTrace();
152
            } catch (IOException e1) {
153
                // TODO Auto-generated catch block
154
                e1.printStackTrace();
155
            }
156
        }else{
157
            report.addInfoMessage(unableToLoadMessage);
158
        }
159
        return null;
160
    }
161
}
(14-14/15)