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.net.URI;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.http.client.ClientProtocolException;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.w3c.dom.Element;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
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
|
*/
|
30
|
public class UnitAssociationParser {
|
31
|
|
32
|
private static final Logger logger = Logger.getLogger(UnitAssociationParser.class);
|
33
|
|
34
|
private final String prefix;
|
35
|
|
36
|
private final SpecimenImportReport report;
|
37
|
|
38
|
private final ICdmRepository cdmAppController;
|
39
|
|
40
|
public UnitAssociationParser(String prefix, SpecimenImportReport report, ICdmRepository cdmAppController) {
|
41
|
this.prefix = prefix;
|
42
|
this.report = report;
|
43
|
this.cdmAppController = cdmAppController;
|
44
|
}
|
45
|
|
46
|
public UnitAssociationWrapper parse(Element unitAssociation){
|
47
|
if(prefix.equals("abcd:")){
|
48
|
|
49
|
}
|
50
|
else if(prefix.equals("abcd21:")){
|
51
|
|
52
|
}
|
53
|
//unit id
|
54
|
String unitId = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"UnitID"));
|
55
|
if(unitId==null){
|
56
|
unitId = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitID"));//abcd 2.0.6
|
57
|
}
|
58
|
//institution code
|
59
|
String institutionCode = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"SourceInstitutionCode"));
|
60
|
if(institutionCode==null){
|
61
|
institutionCode = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitSourceInstitutionCode"));//abcd 2.0.6
|
62
|
}
|
63
|
//institution name
|
64
|
String institutionName = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"SourceName"));
|
65
|
if(institutionName==null){
|
66
|
institutionName = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociatedUnitSourceName"));//abcd 2.0.6
|
67
|
}
|
68
|
//data access point
|
69
|
URI datasetAccessPoint = AbcdParseUtility.parseFirstUri(unitAssociation.getElementsByTagName(prefix+"DatasetAccessPoint"), report);
|
70
|
if(datasetAccessPoint==null){
|
71
|
datasetAccessPoint = AbcdParseUtility.parseFirstUri(unitAssociation.getElementsByTagName(prefix+"Comment"), report);//abcd 2.0.6
|
72
|
}
|
73
|
//association type
|
74
|
String associationType = AbcdParseUtility.parseFirstTextContent(unitAssociation.getElementsByTagName(prefix+"AssociationType"));
|
75
|
|
76
|
String unableToLoadMessage = String.format("Unable to load unit %s from %s", unitId, datasetAccessPoint);
|
77
|
if(unitId!=null && datasetAccessPoint!=null){
|
78
|
BioCaseQueryServiceWrapper serviceWrapper = new BioCaseQueryServiceWrapper();
|
79
|
Set<String[]> unitIds = new HashSet<String[]>();
|
80
|
String[] unitIdArray = {unitId};
|
81
|
unitIds.add(unitIdArray);
|
82
|
OccurenceQuery query = new OccurenceQuery(unitIds);
|
83
|
try {
|
84
|
InputStream inputStream = serviceWrapper.query(query, datasetAccessPoint);
|
85
|
if(inputStream!=null){
|
86
|
UnitAssociationWrapper unitAssociationWrapper = null;
|
87
|
try {
|
88
|
unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(inputStream, report);
|
89
|
} catch (Exception e) {
|
90
|
String exceptionMessage = "An exception occurred during parsing of associated units!";
|
91
|
logger.error(exceptionMessage, e);
|
92
|
report.addException(exceptionMessage, e);
|
93
|
}
|
94
|
|
95
|
if(unitAssociationWrapper!=null){
|
96
|
unitAssociationWrapper.setAssociationType(associationType);
|
97
|
unitAssociationWrapper.setAccesPoint(datasetAccessPoint);
|
98
|
if(unitAssociationWrapper.getAssociatedUnits()!=null && unitAssociationWrapper.getAssociatedUnits().getLength()>1){
|
99
|
String moreThanOneUnitFoundMessage = String.format("More than one unit was found for unit association to %s", unitId);
|
100
|
logger.warn(moreThanOneUnitFoundMessage);
|
101
|
report.addInfoMessage(moreThanOneUnitFoundMessage);
|
102
|
}
|
103
|
}
|
104
|
return unitAssociationWrapper;
|
105
|
}
|
106
|
else{
|
107
|
logger.error(unableToLoadMessage);
|
108
|
report.addInfoMessage(unableToLoadMessage);
|
109
|
}
|
110
|
} catch (ClientProtocolException e) {
|
111
|
logger.error(unableToLoadMessage, e);
|
112
|
report.addInfoMessage(unableToLoadMessage);
|
113
|
} catch (IOException e) {
|
114
|
logger.error(unableToLoadMessage, e);
|
115
|
report.addInfoMessage(unableToLoadMessage);
|
116
|
}
|
117
|
}
|
118
|
else{
|
119
|
report.addInfoMessage(unableToLoadMessage);
|
120
|
}
|
121
|
return null;
|
122
|
}
|
123
|
|
124
|
public UnitAssociationWrapper parseSiblings(String unitID, URI datasetAccessPoint){
|
125
|
|
126
|
String unableToLoadMessage = String.format("Unable to load unit %s from %s", unitID, datasetAccessPoint);
|
127
|
if(unitID!=null && datasetAccessPoint!=null){
|
128
|
BioCaseQueryServiceWrapper serviceWrapper = new BioCaseQueryServiceWrapper();
|
129
|
|
130
|
|
131
|
OccurenceQuery query = new OccurenceQuery(unitID);
|
132
|
|
133
|
|
134
|
InputStream inputStream;
|
135
|
try {
|
136
|
inputStream = serviceWrapper.querySiblings(query, datasetAccessPoint);
|
137
|
|
138
|
if(inputStream!=null){
|
139
|
UnitAssociationWrapper unitAssociationWrapper = null;
|
140
|
try {
|
141
|
unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(inputStream, report);
|
142
|
|
143
|
} catch (Exception e) {
|
144
|
String exceptionMessage = "An exception occurred during parsing of associated units!";
|
145
|
logger.error(exceptionMessage, e);
|
146
|
report.addException(exceptionMessage, e);
|
147
|
}
|
148
|
|
149
|
|
150
|
return unitAssociationWrapper;
|
151
|
}
|
152
|
else{
|
153
|
logger.error(unableToLoadMessage);
|
154
|
report.addInfoMessage(unableToLoadMessage);
|
155
|
}
|
156
|
} catch (ClientProtocolException e1) {
|
157
|
// TODO Auto-generated catch block
|
158
|
e1.printStackTrace();
|
159
|
} catch (IOException e1) {
|
160
|
// TODO Auto-generated catch block
|
161
|
e1.printStackTrace();
|
162
|
}
|
163
|
|
164
|
}
|
165
|
else{
|
166
|
report.addInfoMessage(unableToLoadMessage);
|
167
|
}
|
168
|
return null;
|
169
|
}
|
170
|
|
171
|
}
|