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