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.io.specimen;
|
11
|
|
12
|
import java.awt.Dimension;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import javax.swing.JOptionPane;
|
22
|
import javax.swing.JScrollPane;
|
23
|
import javax.swing.JTextArea;
|
24
|
|
25
|
import org.apache.commons.lang.StringUtils;
|
26
|
import org.apache.log4j.Logger;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
29
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
30
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
31
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
32
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
33
|
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
|
34
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
35
|
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenSynthesysExcelImportConfigurator;
|
36
|
import eu.etaxonomy.cdm.io.taxonx2013.TaxonXImportConfigurator;
|
37
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
38
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
39
|
import eu.etaxonomy.cdm.model.location.Country;
|
40
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
41
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
42
|
|
43
|
/**
|
44
|
* @author p.kelbert
|
45
|
* @created 20.10.2008
|
46
|
*/
|
47
|
public class UnitsGatheringArea {
|
48
|
private static final Logger logger = Logger.getLogger(UnitsGatheringArea.class);
|
49
|
|
50
|
private static final boolean DEBUG = false;
|
51
|
private final ArrayList<DefinedTermBase> areas = new ArrayList<DefinedTermBase>();
|
52
|
private boolean useTDWGarea = false;
|
53
|
|
54
|
private DefinedTermBase<?> wbc;
|
55
|
|
56
|
|
57
|
public UnitsGatheringArea(){
|
58
|
//
|
59
|
}
|
60
|
|
61
|
public void setParams(String isoCountry, String country, ImportConfiguratorBase<?, ?> config, ITermService termService,
|
62
|
IOccurrenceService occurrenceService){
|
63
|
|
64
|
this.setCountry(isoCountry, country, config, termService, occurrenceService);
|
65
|
}
|
66
|
|
67
|
/*
|
68
|
* Constructor
|
69
|
* Set a list of NamedAreas
|
70
|
*/
|
71
|
public void setAreas(Map<String, String> namedAreaList, ImportConfiguratorBase<?, ?> config, ITermService termService, IVocabularyService vocabularyService){
|
72
|
this.setAreaNames(namedAreaList, config, termService, vocabularyService);
|
73
|
}
|
74
|
|
75
|
|
76
|
/*
|
77
|
* Return the current list of NamedAreas
|
78
|
*/
|
79
|
public ArrayList<DefinedTermBase> getAreas(){
|
80
|
return this.areas;
|
81
|
}
|
82
|
|
83
|
/*
|
84
|
* Set the list of NamedAreas
|
85
|
* @param namedAreas
|
86
|
*/
|
87
|
@SuppressWarnings("rawtypes")
|
88
|
public void setAreaNames(Map<String, String> namedAreaList, ImportConfiguratorBase<?, ?> config, ITermService termService, IVocabularyService vocabularyService){
|
89
|
List<NamedArea> termsList = termService.list(NamedArea.class,0,0,null,null);
|
90
|
termsList.addAll(termService.list(Country.class, 0, 0, null, null));
|
91
|
|
92
|
if (DEBUG) {
|
93
|
logger.info(termService.list(NamedArea.class, 0, 0, null, null));
|
94
|
}
|
95
|
|
96
|
HashSet<String> areaToAdd= new HashSet<String>();
|
97
|
HashSet<UUID> areaSet = new HashSet<UUID>();
|
98
|
|
99
|
HashMap<String, UUID> matchingTermsToUuid = new HashMap<String, UUID>();
|
100
|
for (java.util.Map.Entry<String, String> entry : namedAreaList.entrySet()){
|
101
|
String namedAreaStr = entry.getKey();
|
102
|
String namedAreaClass = entry.getValue();
|
103
|
UUID areaUUID = null;
|
104
|
areaUUID = getNamedAreaDecision(namedAreaStr,config);
|
105
|
//first, check if there is an exact match
|
106
|
List<DefinedTermBase> exactMatchingTerms = termService.findByTitle(DefinedTermBase.class, namedAreaStr, MatchMode.EXACT, null, null, null, null, null).getRecords();
|
107
|
if(!exactMatchingTerms.isEmpty()){
|
108
|
//check for continents
|
109
|
List<DefinedTermBase> exactMatchingContinentTerms = new ArrayList<DefinedTermBase>();
|
110
|
if(namedAreaClass!=null && namedAreaClass.equalsIgnoreCase("continent")){
|
111
|
TermVocabulary continentVocabulary = vocabularyService.load(NamedArea.uuidContinentVocabulary);
|
112
|
Set terms = continentVocabulary.getTerms();
|
113
|
for (Object object : terms) {
|
114
|
if(object instanceof DefinedTermBase && exactMatchingTerms.contains(object)){
|
115
|
exactMatchingContinentTerms.add(HibernateProxyHelper.deproxy(object, DefinedTermBase.class));
|
116
|
}
|
117
|
}
|
118
|
if(exactMatchingContinentTerms.size()==1){
|
119
|
areaUUID = exactMatchingContinentTerms.iterator().next().getUuid();
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
if (areaUUID == null && config.isInteractWithUser()){
|
124
|
Pager<DefinedTermBase> matchingTerms = termService.findByTitle(DefinedTermBase.class, namedAreaStr, MatchMode.ANYWHERE, null, null, null, null, null);
|
125
|
String packagePrefix = "eu.etaxonomy.cdm.model.location.";
|
126
|
for (DefinedTermBase matchingTerm : matchingTerms.getRecords()) {
|
127
|
String termLabel = matchingTerm.getTitleCache();
|
128
|
if(matchingTerm.getClass().toString().contains(packagePrefix)){
|
129
|
termLabel += " ("+matchingTerm.getClass().toString().split(packagePrefix)[1] + ")";
|
130
|
}
|
131
|
matchingTermsToUuid.put(termLabel, matchingTerm.getUuid());
|
132
|
}
|
133
|
areaUUID = askForArea(namedAreaStr, matchingTermsToUuid, "area");
|
134
|
}
|
135
|
if (DEBUG) {
|
136
|
logger.info("selected area: "+areaUUID);
|
137
|
}
|
138
|
if (areaUUID == null){
|
139
|
areaToAdd.add(namedAreaStr);
|
140
|
} else {
|
141
|
areaSet.add(areaUUID);
|
142
|
addNamedAreaDecision(namedAreaStr,areaUUID, config);
|
143
|
}
|
144
|
|
145
|
}
|
146
|
for (String areaStr:areaToAdd){
|
147
|
NamedArea ar = NamedArea.NewInstance();
|
148
|
ar.setTitleCache(areaStr, true);
|
149
|
termService.saveOrUpdate(ar);
|
150
|
this.areas.add(ar);
|
151
|
addNamedAreaDecision(areaStr,ar.getUuid(), config);
|
152
|
}
|
153
|
if (!areaSet.isEmpty()){
|
154
|
List<DefinedTermBase> ldtb = termService.find(areaSet);
|
155
|
if (!ldtb.isEmpty()) {
|
156
|
this.areas.addAll(ldtb);
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
|
161
|
private UUID askForArea(String namedAreaStr, HashMap<String, UUID> matchingTerms, String areaType){
|
162
|
// matchingTerms.put("Nothing matches, create a new area",null);
|
163
|
|
164
|
//FIXME names with same label will not make it to the map
|
165
|
JTextArea textArea = new JTextArea("Several CDM-areas could match the current '"+namedAreaStr+"'");
|
166
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
167
|
textArea.setLineWrap(true);
|
168
|
textArea.setWrapStyleWord(true);
|
169
|
scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
|
170
|
String s=null;
|
171
|
List<String> list = new ArrayList<String>(matchingTerms.keySet());
|
172
|
list.add("Nothing matches, create a new area");
|
173
|
|
174
|
if (list.size() <= 1){
|
175
|
return null;
|
176
|
}
|
177
|
while (s == null) {
|
178
|
s= (String)JOptionPane.showInputDialog(
|
179
|
null,
|
180
|
scrollPane,
|
181
|
"Select the right " + areaType + " from the list",
|
182
|
JOptionPane.QUESTION_MESSAGE,
|
183
|
null,
|
184
|
list.toArray(),
|
185
|
null);
|
186
|
}
|
187
|
|
188
|
return matchingTerms.get(s);
|
189
|
}
|
190
|
|
191
|
/*
|
192
|
* Set the current Country
|
193
|
* Search in the DB if the isoCode is known
|
194
|
* If not, search if the country name is in the DB
|
195
|
* If not, create a new Label with the Level Country
|
196
|
* @param iso: the country iso code
|
197
|
* @param fullName: the country's full name
|
198
|
* @param app: the CDM application controller
|
199
|
*/
|
200
|
public void setCountry(String iso, String fullName, ImportConfiguratorBase<?, ?> config, ITermService termService,
|
201
|
IOccurrenceService occurrenceService){
|
202
|
|
203
|
|
204
|
if (!StringUtils.isEmpty(iso)){
|
205
|
wbc = occurrenceService.getCountryByIso(iso);
|
206
|
}
|
207
|
if (wbc == null){
|
208
|
if (!StringUtils.isEmpty(fullName)){
|
209
|
|
210
|
|
211
|
// logger.info("matchingterms: "+matchingTerms.keySet().toString());
|
212
|
UUID areaUUID = null;
|
213
|
//TODO Critical, should be a country decision
|
214
|
areaUUID = getNamedAreaDecision(fullName,config);
|
215
|
|
216
|
if (areaUUID == null){
|
217
|
List<UUID> countryUuids = new ArrayList<UUID>();
|
218
|
HashMap<String, UUID> matchingTerms = new HashMap<String, UUID>();
|
219
|
|
220
|
List<Country> countryList = termService.list(Country.class, 0, 0, null, null);
|
221
|
for (NamedArea na:countryList){
|
222
|
if (na.getTitleCache().equalsIgnoreCase(fullName)) {
|
223
|
countryUuids.add(na.getUuid());
|
224
|
}
|
225
|
if (na.getTitleCache().toLowerCase().indexOf(fullName.toLowerCase()) != -1) {
|
226
|
matchingTerms.put(na.getTitleCache()+" ("+na.getTermType().getMessage() + ")",na.getUuid());
|
227
|
}
|
228
|
}
|
229
|
if (countryUuids.isEmpty()){
|
230
|
List<NamedArea> namedAreaList = termService.list(NamedArea.class,0,0,null,null);
|
231
|
|
232
|
for (NamedArea na:namedAreaList){
|
233
|
if (! na.getClass().isAssignableFrom(Country.class) && na.getTitleCache().toLowerCase().indexOf(fullName.toLowerCase()) != -1) {
|
234
|
matchingTerms.put(na.getTitleCache()+" ("+na.getType().getLabel() + ")",na.getUuid());
|
235
|
}
|
236
|
}
|
237
|
}
|
238
|
if (countryUuids.size() == 1){
|
239
|
areaUUID = countryUuids.get(0);
|
240
|
}else{
|
241
|
if ((matchingTerms.keySet().size()>0) && config.isInteractWithUser()){
|
242
|
areaUUID = askForArea(fullName, matchingTerms, "country");
|
243
|
logger.info("selected area: "+areaUUID);
|
244
|
}else{
|
245
|
logger.warn("Non interaction not yet implemented correctly");
|
246
|
}
|
247
|
}
|
248
|
|
249
|
}
|
250
|
if (areaUUID == null){
|
251
|
NamedArea ar = NamedArea.NewInstance();
|
252
|
//FIXME add vocabulary
|
253
|
logger.warn("Vocabulary not yet set for new country");
|
254
|
ar.setTitleCache(fullName, true);
|
255
|
termService.saveOrUpdate(ar);
|
256
|
wbc = ar;
|
257
|
} else {
|
258
|
wbc = termService.find(areaUUID);
|
259
|
}
|
260
|
addNamedAreaDecision(fullName,wbc.getUuid(),config);
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* @param fullName
|
267
|
* @param uuid
|
268
|
*/
|
269
|
private void addNamedAreaDecision(String fullName, UUID uuid,ImportConfiguratorBase<?, ?> config) {
|
270
|
if (config.getClass().equals(SpecimenSynthesysExcelImportConfigurator.class)) {
|
271
|
((SpecimenSynthesysExcelImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
272
|
}
|
273
|
if (config.getClass().equals(Abcd206ImportConfigurator.class)) {
|
274
|
((Abcd206ImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
275
|
}
|
276
|
if (config.getClass().equals(TaxonXImportConfigurator.class)) {
|
277
|
((TaxonXImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
278
|
}
|
279
|
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* @param fullName
|
284
|
* @return
|
285
|
*/
|
286
|
private UUID getNamedAreaDecision(String fullName, ImportConfiguratorBase<?, ?> config) {
|
287
|
UUID areaUUID = null;
|
288
|
if (config.getClass().equals(SpecimenSynthesysExcelImportConfigurator.class)) {
|
289
|
areaUUID = ((SpecimenSynthesysExcelImportConfigurator) config).getNamedAreaDecision(fullName);
|
290
|
}
|
291
|
if (config.getClass().equals(Abcd206ImportConfigurator.class)) {
|
292
|
areaUUID = ((Abcd206ImportConfigurator) config).getNamedAreaDecision(fullName);
|
293
|
}
|
294
|
if (config.getClass().equals(TaxonXImportConfigurator.class)) {
|
295
|
areaUUID = ((TaxonXImportConfigurator) config).getNamedAreaDecision(fullName);
|
296
|
}
|
297
|
return areaUUID;
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* @param useTDWGarea2
|
302
|
*/
|
303
|
public void useTDWGareas(boolean useTDWGarea) {
|
304
|
this.useTDWGarea=useTDWGarea;
|
305
|
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* @return
|
310
|
*/
|
311
|
public DefinedTermBase<?> getCountry() {
|
312
|
return wbc;
|
313
|
}
|
314
|
|
315
|
|
316
|
|
317
|
}
|