1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2013 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
|
package eu.etaxonomy.cdm.ext.occurrence.bioCase;
|
11
|
|
12
|
import org.jdom.Document;
|
13
|
import org.jdom.Element;
|
14
|
import org.jdom.Namespace;
|
15
|
|
16
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
17
|
|
18
|
/**
|
19
|
* Generates an XML query according to the BioCASe protocol.
|
20
|
* @author pplitzner
|
21
|
* @date 13.09.2013
|
22
|
*
|
23
|
*/
|
24
|
public class BioCaseQueryGenerator {
|
25
|
/*
|
26
|
<?xml version='1.0' encoding='UTF-8'?>
|
27
|
<request xmlns='http://www.biocase.org/schemas/protocol/1.3'>
|
28
|
<header><type>search</type></header>
|
29
|
<search>
|
30
|
<requestFormat>http://www.tdwg.org/schemas/abcd/2.06</requestFormat>
|
31
|
<responseFormat start='0' limit='10'>http://www.tdwg.org/schemas/abcd/2.06</responseFormat>
|
32
|
<filter>
|
33
|
<like path='/DataSets/DataSet/Units/Unit/Identifications/Identification/Result/TaxonIdentified/ScientificName/FullScientificNameString'>A*</like>
|
34
|
</filter>
|
35
|
<count>false</count>
|
36
|
</search>
|
37
|
</request>
|
38
|
|
39
|
|
40
|
<?xml version='1.0' encoding='UTF-8'?>
|
41
|
<request xmlns='http://www.biocase.org/schemas/protocol/1.3'>
|
42
|
<header><type>search</type></header>
|
43
|
<search>
|
44
|
<requestFormat>http://www.tdwg.org/schemas/abcd/2.06</requestFormat>
|
45
|
<responseFormat start='0' limit='10'>http://www.tdwg.org/schemas/abcd/2.06</responseFormat>
|
46
|
<filter>
|
47
|
<equals path='/DataSets/DataSet/Units/Unit/UnitID'>B -W 11422 -01 0</equals>
|
48
|
</filter>
|
49
|
<count>false</count>
|
50
|
</search>
|
51
|
</request>
|
52
|
*/
|
53
|
|
54
|
private static final String FALSE = "false";
|
55
|
private static final String PATH = "path";
|
56
|
private static final String LIMIT = "limit";
|
57
|
private static final String START = "start";
|
58
|
private static final String ABCD_SCHEMA_2_0 = "http://www.tdwg.org/schemas/abcd/2.06";
|
59
|
private static final String COUNT = "count";
|
60
|
private static final String LIKE = "like";
|
61
|
private static final String EQUALS = "equals";
|
62
|
private static final String AND = "and";
|
63
|
private static final String FILTER = "filter";
|
64
|
private static final String RESPONSE_FORMAT = "responseFormat";
|
65
|
private static final String REQUEST_FORMAT = "requestFormat";
|
66
|
private static final String SEARCH = "search";
|
67
|
private static final String TYPE = "type";
|
68
|
private static final String HEADER = "header";
|
69
|
private static final String REQUEST = "request";
|
70
|
private static final String NAMESPACE = "http://www.biocase.org/schemas/protocol/1.3";
|
71
|
private static final String UNIT_PATH = "/DataSets/DataSet/Units/Unit";
|
72
|
private static final String UNIT_ID_PATH_ABCD_2_0 = UNIT_PATH + "/UnitID";
|
73
|
private static final String TAXON_NAME_PATH_ABCD_2_0 = UNIT_PATH + "/Identifications/Identification/Result/TaxonIdentified/ScientificName/FullScientificNameString";
|
74
|
private static final String LOCALITY_PATH_ABCD_2_0 = UNIT_PATH + "/Gathering/LocalityText";
|
75
|
private static final String HERBARIUM_PATH_ABCD_2_0 = UNIT_PATH + "/SourceID";
|
76
|
private static final String COUNTRY_PATH_ABCD_2_0 = UNIT_PATH + "/Gathering/Country";
|
77
|
private static final String COLLECTOR_NUMBER_PATH_ABCD_2_0 = UNIT_PATH + "/CollectorsFieldNumber";
|
78
|
private static final String COLLECTOR_PATH_ABCD_2_0 = UNIT_PATH + "/Gathering/Agents/GatheringAgent";
|
79
|
private static final String ACCESSION_NUMBER_PATH_ABCD_2_0 = UNIT_PATH + "/SpecimenUnit/Accessions/AccessionNumber";
|
80
|
|
81
|
/**
|
82
|
* Generates an XML query according to the BioCASe protocol.
|
83
|
* @param query the {@link OccurenceQuery} to transform to XML
|
84
|
* @return the query XML {@link Document} according BioCASe protocol
|
85
|
*/
|
86
|
public static Document generateXMLQuery(OccurenceQuery query){
|
87
|
Document document = new Document();
|
88
|
Element elRequest = new Element(REQUEST, Namespace.getNamespace(NAMESPACE));
|
89
|
Element elHeader = new Element(HEADER);
|
90
|
Element elType = new Element(TYPE);
|
91
|
Element elSearch = new Element(SEARCH);
|
92
|
Element elRequestFormat = new Element(REQUEST_FORMAT);
|
93
|
Element elResponseFormat = new Element(RESPONSE_FORMAT);
|
94
|
Element elFilter = new Element(FILTER);
|
95
|
Element elAnd = new Element(AND);
|
96
|
Element elCount = new Element(COUNT);
|
97
|
|
98
|
document.setRootElement(elRequest);
|
99
|
elRequest.addContent(elHeader);
|
100
|
elHeader.addContent(elType);
|
101
|
elType.addContent(SEARCH);
|
102
|
|
103
|
elRequest.addContent(elSearch);
|
104
|
elSearch.addContent(elRequestFormat);
|
105
|
elRequestFormat.addContent(ABCD_SCHEMA_2_0);
|
106
|
|
107
|
elSearch.addContent(elResponseFormat);
|
108
|
elResponseFormat.setAttribute(START, "0");
|
109
|
elResponseFormat.setAttribute(LIMIT, "100");
|
110
|
elResponseFormat.addContent(ABCD_SCHEMA_2_0);
|
111
|
|
112
|
elSearch.addContent(elFilter);
|
113
|
elFilter.addContent(elAnd);
|
114
|
|
115
|
if(query.unitId!=null && !query.unitId.trim().isEmpty()){
|
116
|
addEqualsFilter(elAnd, query.unitId, UNIT_ID_PATH_ABCD_2_0);
|
117
|
}
|
118
|
if(query.accessionNumber!=null && !query.accessionNumber.trim().isEmpty()){
|
119
|
addLikeFilter(elAnd, query.accessionNumber, ACCESSION_NUMBER_PATH_ABCD_2_0);
|
120
|
}
|
121
|
if(query.collector!=null && !query.collector.trim().isEmpty()){
|
122
|
addLikeFilter(elAnd, query.collector, COLLECTOR_PATH_ABCD_2_0);
|
123
|
}
|
124
|
if(query.collectorsNumber!=null && !query.collectorsNumber.trim().isEmpty()){
|
125
|
addLikeFilter(elAnd, query.collectorsNumber, COLLECTOR_NUMBER_PATH_ABCD_2_0);
|
126
|
}
|
127
|
if(query.country!=null && !query.country.trim().isEmpty()){
|
128
|
addLikeFilter(elAnd, query.country, COUNTRY_PATH_ABCD_2_0);
|
129
|
}
|
130
|
//TODO: implement
|
131
|
// if(query.date!=null){
|
132
|
// addFilter(elFilter, query.date);
|
133
|
// }
|
134
|
if(query.herbarium!=null && !query.herbarium.trim().isEmpty()){
|
135
|
addLikeFilter(elAnd, query.herbarium, HERBARIUM_PATH_ABCD_2_0);
|
136
|
}
|
137
|
if(query.locality!=null && !query.locality.trim().isEmpty()){
|
138
|
addLikeFilter(elAnd, query.locality, LOCALITY_PATH_ABCD_2_0);
|
139
|
}
|
140
|
if(query.taxonName!=null && !query.taxonName.trim().isEmpty()){
|
141
|
addLikeFilter(elAnd, query.taxonName, TAXON_NAME_PATH_ABCD_2_0);
|
142
|
}
|
143
|
|
144
|
elSearch.addContent(elCount);
|
145
|
elCount.addContent(FALSE);
|
146
|
|
147
|
return document;
|
148
|
}
|
149
|
|
150
|
private static void addLikeFilter(Element filterElement, String taxonName, String path){
|
151
|
Element elLike = new Element(LIKE);
|
152
|
filterElement.addContent(elLike);
|
153
|
elLike.setAttribute(PATH, path);
|
154
|
elLike.addContent(taxonName);
|
155
|
}
|
156
|
|
157
|
private static void addEqualsFilter(Element filterElement, String taxonName, String path){
|
158
|
Element elEquals = new Element(EQUALS);
|
159
|
filterElement.addContent(elEquals);
|
160
|
elEquals.setAttribute(PATH, path);
|
161
|
elEquals.addContent(taxonName);
|
162
|
}
|
163
|
}
|