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.UUID;
|
18
|
|
19
|
import javax.swing.JOptionPane;
|
20
|
import javax.swing.JScrollPane;
|
21
|
import javax.swing.JTextArea;
|
22
|
|
23
|
import org.apache.commons.lang.StringUtils;
|
24
|
import org.apache.log4j.Logger;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
27
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
28
|
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
|
29
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
30
|
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenSynthesysExcelImportConfigurator;
|
31
|
import eu.etaxonomy.cdm.io.taxonx2013.TaxonXImportConfigurator;
|
32
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
33
|
import eu.etaxonomy.cdm.model.location.Country;
|
34
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
35
|
|
36
|
/**
|
37
|
* @author p.kelbert
|
38
|
* @created 20.10.2008
|
39
|
*/
|
40
|
public class UnitsGatheringArea {
|
41
|
private static final Logger logger = Logger.getLogger(UnitsGatheringArea.class);
|
42
|
|
43
|
private static final boolean DEBUG = false;
|
44
|
private final ArrayList<DefinedTermBase> areas = new ArrayList<DefinedTermBase>();
|
45
|
private boolean useTDWGarea = false;
|
46
|
|
47
|
private DefinedTermBase<?> wbc;
|
48
|
|
49
|
|
50
|
public UnitsGatheringArea(){
|
51
|
//
|
52
|
}
|
53
|
|
54
|
public void setParams(String isoCountry, String country, ImportConfiguratorBase<?, ?> config, ITermService termService,
|
55
|
IOccurrenceService occurrenceService){
|
56
|
|
57
|
this.setCountry(isoCountry, country, config, termService, occurrenceService);
|
58
|
}
|
59
|
|
60
|
/*
|
61
|
* Constructor
|
62
|
* Set a list of NamedAreas
|
63
|
*/
|
64
|
public void setAreas(List<String> namedAreaList, ImportConfiguratorBase<?, ?> config, ITermService termService ){
|
65
|
this.setAreaNames(namedAreaList, config, termService);
|
66
|
}
|
67
|
|
68
|
|
69
|
/*
|
70
|
* Return the current list of NamedAreas
|
71
|
*/
|
72
|
public ArrayList<DefinedTermBase> getAreas(){
|
73
|
return this.areas;
|
74
|
}
|
75
|
|
76
|
/*
|
77
|
* Set the list of NamedAreas
|
78
|
* @param namedAreas
|
79
|
*/
|
80
|
@SuppressWarnings("rawtypes")
|
81
|
public void setAreaNames(List<String> namedAreas, ImportConfiguratorBase<?, ?> config, ITermService termService){
|
82
|
List<DefinedTermBase> termsList = termService.list(NamedArea.class,0,0,null,null);
|
83
|
termsList.addAll(termService.list(Country.class, 0, 0, null, null));
|
84
|
|
85
|
if (DEBUG) {
|
86
|
logger.info(termService.list(NamedArea.class, 0, 0, null, null));
|
87
|
}
|
88
|
|
89
|
HashSet<String> areaToAdd= new HashSet<String>();
|
90
|
HashSet<UUID> areaSet = new HashSet<UUID>();
|
91
|
|
92
|
HashMap<String, UUID> matchingTerms = new HashMap<String, UUID>();
|
93
|
for (String namedAreaStr : namedAreas){
|
94
|
for (DefinedTermBase na:termsList){
|
95
|
if (na.getTitleCache().toLowerCase().indexOf(namedAreaStr.toLowerCase()) != -1) {
|
96
|
if (na.getClass().toString().indexOf("eu.etaxonomy.cdm.model.location.") != -1) {
|
97
|
matchingTerms.put(na.getTitleCache()+" ("+na.getClass().toString().split("eu.etaxonomy.cdm.model.location.")[1]+")",na.getUuid());
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
// logger.info("matchingterms: "+matchingTerms.keySet().toString());
|
102
|
UUID areaUUID = null;
|
103
|
areaUUID = getNamedAreaDecision(namedAreaStr,config);
|
104
|
|
105
|
if (areaUUID == null && config.isInteractWithUser()){
|
106
|
areaUUID = askForArea(namedAreaStr, matchingTerms);
|
107
|
}
|
108
|
if (DEBUG) {
|
109
|
logger.info("selected area: "+areaUUID);
|
110
|
}
|
111
|
if (areaUUID == null){
|
112
|
areaToAdd.add(namedAreaStr);
|
113
|
} else {
|
114
|
areaSet.add(areaUUID);
|
115
|
addNamedAreaDecision(namedAreaStr,areaUUID, config);
|
116
|
}
|
117
|
|
118
|
}
|
119
|
for (String areaStr:areaToAdd){
|
120
|
NamedArea ar = NamedArea.NewInstance();
|
121
|
ar.setTitleCache(areaStr, true);
|
122
|
termService.saveOrUpdate(ar);
|
123
|
this.areas.add(ar);
|
124
|
addNamedAreaDecision(areaStr,ar.getUuid(), config);
|
125
|
}
|
126
|
if (!areaSet.isEmpty()){
|
127
|
List<DefinedTermBase> ldtb = termService.find(areaSet);
|
128
|
if (!ldtb.isEmpty()) {
|
129
|
this.areas.addAll(ldtb);
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
|
134
|
private UUID askForArea(String namedAreaStr, HashMap<String, UUID> matchingTerms){
|
135
|
matchingTerms.put("Nothing matches, create a new area",null);
|
136
|
|
137
|
JTextArea textArea = new JTextArea("Several CDM-areas could match the current '"+namedAreaStr+"'");
|
138
|
JScrollPane scrollPane = new JScrollPane(textArea);
|
139
|
textArea.setLineWrap(true);
|
140
|
textArea.setWrapStyleWord(true);
|
141
|
scrollPane.setPreferredSize( new Dimension( 700, 50 ) );
|
142
|
String s=null;
|
143
|
while (s == null) {
|
144
|
s= (String)JOptionPane.showInputDialog(
|
145
|
null,
|
146
|
scrollPane,
|
147
|
"Select the right one from the list",
|
148
|
JOptionPane.QUESTION_MESSAGE,
|
149
|
null,
|
150
|
matchingTerms.keySet().toArray(),
|
151
|
null);
|
152
|
}
|
153
|
|
154
|
return matchingTerms.get(s);
|
155
|
}
|
156
|
|
157
|
/*
|
158
|
* Set the current Country
|
159
|
* Search in the DB if the isoCode is known
|
160
|
* If not, search if the country name is in the DB
|
161
|
* If not, create a new Label with the Level Country
|
162
|
* @param iso: the country iso code
|
163
|
* @param fullName: the country's full name
|
164
|
* @param app: the CDM application controller
|
165
|
*/
|
166
|
@SuppressWarnings("rawtypes")
|
167
|
public void setCountry(String iso, String fullName, ImportConfiguratorBase<?, ?> config, ITermService termService,
|
168
|
IOccurrenceService occurrenceService){
|
169
|
List<DefinedTermBase> termsList = termService.list(NamedArea.class,0,0,null,null);
|
170
|
termsList.addAll(termService.list(Country.class, 0, 0, null, null));
|
171
|
|
172
|
HashMap<String, UUID> matchingTerms = new HashMap<String, UUID>();
|
173
|
|
174
|
if (!StringUtils.isEmpty(iso)){
|
175
|
wbc = occurrenceService.getCountryByIso(iso);
|
176
|
}
|
177
|
if (wbc == null){
|
178
|
if (!StringUtils.isEmpty(fullName)){
|
179
|
|
180
|
for (DefinedTermBase<?> na:termsList){
|
181
|
if (na.getTitleCache().toLowerCase().indexOf(fullName.toLowerCase()) != -1) {
|
182
|
if (na.getClass().toString().indexOf("eu.etaxonomy.cdm.model.location.") != -1) {
|
183
|
matchingTerms.put(na.getTitleCache()+" ("+na.getClass().toString().split("eu.etaxonomy.cdm.model.location.")[1]+")",na.getUuid());
|
184
|
}
|
185
|
}
|
186
|
}
|
187
|
// logger.info("matchingterms: "+matchingTerms.keySet().toString());
|
188
|
UUID areaUUID = null;
|
189
|
areaUUID = getNamedAreaDecision(fullName,config);
|
190
|
|
191
|
if ((areaUUID == null) && (matchingTerms.keySet().size()>0) && config.isInteractWithUser()){
|
192
|
areaUUID = askForArea(fullName, matchingTerms);
|
193
|
logger.info("selected area: "+areaUUID);
|
194
|
}
|
195
|
if (areaUUID == null){
|
196
|
NamedArea ar = NamedArea.NewInstance();
|
197
|
ar.setTitleCache(fullName, true);
|
198
|
termService.saveOrUpdate(ar);
|
199
|
wbc = ar;
|
200
|
} else {
|
201
|
wbc = termService.find(areaUUID);
|
202
|
}
|
203
|
addNamedAreaDecision(fullName,wbc.getUuid(),config);
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* @param fullName
|
210
|
* @param uuid
|
211
|
*/
|
212
|
private void addNamedAreaDecision(String fullName, UUID uuid,ImportConfiguratorBase<?, ?> config) {
|
213
|
if (config.getClass().equals(SpecimenSynthesysExcelImportConfigurator.class)) {
|
214
|
((SpecimenSynthesysExcelImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
215
|
}
|
216
|
if (config.getClass().equals(Abcd206ImportConfigurator.class)) {
|
217
|
((Abcd206ImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
218
|
}
|
219
|
if (config.getClass().equals(TaxonXImportConfigurator.class)) {
|
220
|
((TaxonXImportConfigurator) config).putNamedAreaDecision(fullName, uuid);
|
221
|
}
|
222
|
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* @param fullName
|
227
|
* @return
|
228
|
*/
|
229
|
private UUID getNamedAreaDecision(String fullName, ImportConfiguratorBase<?, ?> config) {
|
230
|
UUID areaUUID = null;
|
231
|
// System.out.println("getNamedAreaDecision "+config);
|
232
|
if (config.getClass().equals(SpecimenSynthesysExcelImportConfigurator.class)) {
|
233
|
areaUUID = ((SpecimenSynthesysExcelImportConfigurator) config).getNamedAreaDecision(fullName);
|
234
|
}
|
235
|
if (config.getClass().equals(Abcd206ImportConfigurator.class)) {
|
236
|
areaUUID = ((Abcd206ImportConfigurator) config).getNamedAreaDecision(fullName);
|
237
|
}
|
238
|
if (config.getClass().equals(TaxonXImportConfigurator.class)) {
|
239
|
areaUUID = ((TaxonXImportConfigurator) config).getNamedAreaDecision(fullName);
|
240
|
}
|
241
|
return areaUUID;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @param useTDWGarea2
|
246
|
*/
|
247
|
public void useTDWGareas(boolean useTDWGarea) {
|
248
|
this.useTDWGarea=useTDWGarea;
|
249
|
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* @return
|
254
|
*/
|
255
|
public DefinedTermBase<?> getCountry() {
|
256
|
return wbc;
|
257
|
}
|
258
|
|
259
|
// /**
|
260
|
// * @param config
|
261
|
// */
|
262
|
// public void setConfig(SpecimenSynthesysExcelImportConfigurator config, IOccurrenceService occurrenceService, ITermService termService) {
|
263
|
// this.config=config;
|
264
|
// this.termService = termService;
|
265
|
// this.occurrenceService = occurrenceService;
|
266
|
//
|
267
|
// }
|
268
|
//
|
269
|
// /**
|
270
|
// * @param config2
|
271
|
// */
|
272
|
// public void setConfig(Abcd206ImportConfigurator config, IOccurrenceService occurrenceService, ITermService termService) {
|
273
|
// this.config=config;
|
274
|
// this.termService = termService;
|
275
|
// this.occurrenceService = occurrenceService;
|
276
|
//
|
277
|
// }
|
278
|
|
279
|
// /**
|
280
|
// * @param config2
|
281
|
// */
|
282
|
// public void setConfig(TaxonXImportConfigurator config, IOccurrenceService occurrenceService, ITermService termService) {
|
283
|
// this.config=config;
|
284
|
// this.termService = termService;
|
285
|
// this.occurrenceService = occurrenceService;
|
286
|
//
|
287
|
// }
|
288
|
|
289
|
}
|