smaller changes in ABCD import - continue
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / SpecimenImportBase.java
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
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 org.apache.log4j.Logger;
22
23 import eu.etaxonomy.cdm.api.application.ICdmRepository;
24 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
25 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
26 import eu.etaxonomy.cdm.api.service.pager.Pager;
27 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28 import eu.etaxonomy.cdm.io.common.CdmImportBase;
29 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
30 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Identification;
31 import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
32 import eu.etaxonomy.cdm.model.agent.AgentBase;
33 import eu.etaxonomy.cdm.model.agent.Institution;
34 import eu.etaxonomy.cdm.model.agent.Person;
35 import eu.etaxonomy.cdm.model.agent.Team;
36 import eu.etaxonomy.cdm.model.common.CdmBase;
37 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
38 import eu.etaxonomy.cdm.model.common.LanguageString;
39 import eu.etaxonomy.cdm.model.description.DescriptionBase;
40 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
41 import eu.etaxonomy.cdm.model.description.Feature;
42 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
43 import eu.etaxonomy.cdm.model.description.TaxonDescription;
44 import eu.etaxonomy.cdm.model.name.INonViralName;
45 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
46 import eu.etaxonomy.cdm.model.name.Rank;
47 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
48 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
49 import eu.etaxonomy.cdm.model.name.TaxonName;
50 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
51 import eu.etaxonomy.cdm.model.occurrence.Collection;
52 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
53 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
54 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
55 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
56 import eu.etaxonomy.cdm.model.reference.ISourceable;
57 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
58 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
59 import eu.etaxonomy.cdm.model.reference.Reference;
60 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
61 import eu.etaxonomy.cdm.model.taxon.Classification;
62 import eu.etaxonomy.cdm.model.taxon.Synonym;
63 import eu.etaxonomy.cdm.model.taxon.Taxon;
64 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
65 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
66 import eu.etaxonomy.cdm.model.term.DefinedTerm;
67 import eu.etaxonomy.cdm.persistence.query.MatchMode;
68 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
69 import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
70 import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
71
72
73 /**
74 * @author p.kelbert
75 * @since 20.10.2008
76 */
77 public abstract class SpecimenImportBase<CONFIG extends IImportConfigurator, STATE extends SpecimenImportStateBase>
78 extends CdmImportBase<CONFIG, STATE> {
79
80 private static final long serialVersionUID = 4423065367998125678L;
81 private static final Logger logger = Logger.getLogger(SpecimenImportBase.class);
82
83 protected static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
84
85 private static final String COLON = ":";
86
87 protected Map<String, DefinedTerm> kindOfUnitsMap;
88
89
90 @Override
91 protected abstract void doInvoke(STATE state);
92
93 /**
94 * Handle a single unit
95 * @param state
96 * @param item
97 */
98 protected abstract void handleSingleUnit(STATE state, Object item) ;
99
100
101
102 protected TaxonName getOrCreateTaxonName(String scientificName, Rank rank, boolean preferredFlag, STATE state, int unitIndexInAbcdFile){
103 TaxonName taxonName = null;
104 SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
105
106 //check atomised name data for rank
107 //new name will be created
108 TaxonName atomisedTaxonName = null;
109 if (rank==null && unitIndexInAbcdFile>=0 && ((state.getDataHolder().getAtomisedIdentificationList() != null && !state.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
110 atomisedTaxonName = setTaxonNameByType(state.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile), scientificName, state);
111 if(atomisedTaxonName!=null){
112 rank = atomisedTaxonName.getRank();
113 }
114 }
115 if(config.isReuseExistingTaxaWhenPossible()){
116 TaxonName parsedName = atomisedTaxonName;
117 if(parsedName==null){
118
119 parsedName = parseScientificName(scientificName, state, state.getReport(), rank);
120
121 }
122 atomisedTaxonName = parsedName;
123 if(config.isIgnoreAuthorship() && parsedName!=null){// && preferredFlag){
124 // do not ignore authorship for non-preferred names because they need
125 // to be created for the determination history
126 String nameCache = TaxonName.castAndDeproxy(parsedName).getNameCache();
127 List<TaxonName> names = getNameService().findNamesByNameCache(nameCache, MatchMode.EXACT, null);
128 if (!names.isEmpty()){
129 taxonName = getBestMatchingName(scientificName, new ArrayList<>(names), state);
130 }
131 if (taxonName == null && !names.isEmpty()){
132 taxonName = names.get(0);
133 }
134
135 } else {
136 //search for existing names
137 List<TaxonName> names = getNameService().listByTitleWithRestrictions(TaxonName.class, scientificName, MatchMode.EXACT, null, null, null, null, null);
138 taxonName = getBestMatchingName(scientificName, names, state);
139 //still nothing found -> try with the atomised name full title cache
140 if(taxonName==null && atomisedTaxonName!=null){
141 names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getFullTitleCache(), MatchMode.EXACT, null, null, null, null, null);
142 taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
143 //still nothing found -> try with the atomised name title cache
144 if(taxonName==null){
145 names = getNameService().listByTitleWithRestrictions(TaxonName.class, atomisedTaxonName.getTitleCache(), MatchMode.EXACT, null, null, null, null, null);
146 taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
147 }
148 }
149
150 }
151
152 }
153
154 if(taxonName==null && atomisedTaxonName!=null){
155 taxonName = atomisedTaxonName;
156 state.getReport().addName(taxonName);
157 logger.info("Created new taxon name "+taxonName);
158 if(taxonName.hasProblem()){
159 state.getReport().addInfoMessage(String.format("Created %s with parsing problems", taxonName));
160 }
161 if(!atomisedTaxonName.getTitleCache().equals(scientificName)){
162 state.getReport().addInfoMessage(String.format("Taxon %s was parsed as %s", scientificName, atomisedTaxonName.getTitleCache()));
163 }
164 }
165 else if(taxonName==null){
166 //create new taxon name
167
168 if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP)){
169 taxonName = TaxonNameFactory.NewBotanicalInstance(rank);
170 }else if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN)){
171 taxonName = TaxonNameFactory.NewZoologicalInstance(rank);
172 }else{
173 taxonName = TaxonNameFactory.NewNonViralInstance(rank);
174 }
175 taxonName.setFullTitleCache(scientificName,true);
176 taxonName.setTitleCache(scientificName, true);
177 state.getReport().addName(taxonName);
178 logger.info("Created new taxon name "+taxonName);
179 }
180 save(taxonName, state);
181 return taxonName;
182 }
183
184 protected TaxonName getBestMatchingName(String scientificName, java.util.Collection<TaxonName> names, STATE state){
185 Set<TaxonName> namesWithAcceptedTaxa = new HashSet<>();
186 List<TaxonName> namesWithAcceptedTaxaInClassification = new ArrayList<>();
187 for (TaxonName name : names) {
188 if(!name.getTaxa().isEmpty()){
189 Set<Taxon> taxa = name.getTaxa();
190 for (Taxon taxon:taxa){
191 if (!taxon.getTaxonNodes().isEmpty()){
192 //use only taxa included in a classification
193 for (TaxonNode node:taxon.getTaxonNodes()){
194 if (state.getClassification() != null && node.getClassification().equals(state.getClassification())){
195 namesWithAcceptedTaxaInClassification.add(name);
196 }else {
197 namesWithAcceptedTaxa.add(name);
198 }
199 }
200
201 }
202 }
203
204 }
205 }
206 String message = String.format("More than one taxon name was found for %s, maybe in other classifications!", scientificName);
207 //check for names with accepted taxa in classification
208 if(namesWithAcceptedTaxaInClassification.size()>0){
209 if(namesWithAcceptedTaxaInClassification.size()>1){
210
211 state.getReport().addInfoMessage(message);
212 logger.warn(message);
213 return null;
214 }
215 return namesWithAcceptedTaxaInClassification.iterator().next();
216 }
217 //check for any names with accepted taxa
218 if(namesWithAcceptedTaxa.size()>0){
219 if(namesWithAcceptedTaxa.size()>1){
220
221 state.getReport().addInfoMessage(message);
222 logger.warn(message);
223 return null;
224 }
225 return namesWithAcceptedTaxa.iterator().next();
226 }
227 //no names with accepted taxa found -> check accepted taxa of synonyms
228 List<Taxon> taxaFromSynonyms = new ArrayList<>();
229 for (TaxonName name : names) {
230 Set<TaxonBase> taxonBases = name.getTaxonBases();
231 for (TaxonBase taxonBase : taxonBases) {
232 if(taxonBase.isInstanceOf(Synonym.class)){
233 Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
234 taxaFromSynonyms.add(synonym.getAcceptedTaxon());
235 }
236 }
237 }
238 if(taxaFromSynonyms.size()>0){
239 if(taxaFromSynonyms.size()>1){
240 state.getReport().addInfoMessage(message);
241 logger.warn(message);
242 return null;
243 }
244 return taxaFromSynonyms.iterator().next().getName();
245 }
246 //no accepted and no synonyms -> return one of the names and create a new taxon
247 if (names.isEmpty()){
248 return null;
249 }else{
250 return names.iterator().next();
251 }
252 }
253 /**
254 * Parse automatically the scientific name
255 * @param scientificName the scientific name to parse
256 * @param state the current import state
257 * @param report the import report
258 * @return a parsed name
259 */
260
261 protected TaxonName parseScientificName(String scientificName, STATE state, SpecimenImportReport report, Rank rank) {
262
263 NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
264 TaxonName taxonName = null;
265 boolean problem = false;
266
267 if (logger.isDebugEnabled()){
268 logger.debug("parseScientificName " + state.getDataHolder().getNomenclatureCode().toString());
269 }
270
271 if (state.getDataHolder().getNomenclatureCode().toString().equals("Zoological") || state.getDataHolder().getNomenclatureCode().toString().contains("ICZN")) {
272 taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICZN, rank);
273 if (taxonName.hasProblem()) {
274 problem = true;
275 }
276 }
277 else if (state.getDataHolder().getNomenclatureCode().toString().equals("Botanical") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN") || state.getDataHolder().getNomenclatureCode().toString().contains("ICNAFP")) {
278 taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNAFP, rank);
279 if (taxonName.hasProblem()) {
280 problem = true;
281 }
282 }
283 else if (state.getDataHolder().getNomenclatureCode().toString().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().toString().contains("ICBN")) {
284 taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNB, rank);
285 if (taxonName.hasProblem()) {
286 problem = true;
287 }
288 }
289 else if (state.getDataHolder().getNomenclatureCode().toString().equals("Cultivar") || state.getDataHolder().getNomenclatureCode().toString().contains("ICNCP")) {
290 taxonName = (TaxonName)nvnpi.parseFullName(scientificName, NomenclaturalCode.ICNCP, rank);
291 if (taxonName.hasProblem()) {
292 problem = true;
293 }
294 }
295 if (problem) {
296 String message = String.format("Parsing problems for %s", scientificName);
297 if(taxonName!=null){
298 for (ParserProblem parserProblem : taxonName.getParsingProblems()) {
299 message += "\n\t- "+parserProblem;
300 }
301 }
302 report.addInfoMessage(message);
303 logger.info(message);
304 }
305 return taxonName;
306
307 }
308
309 /**
310 * Create the name without automatic parsing, either because it failed, or because the user deactivated it.
311 * The name is built upon the ABCD fields
312 * @param atomisedMap : the ABCD atomised fields
313 * @param fullName : the full scientific name
314 * @param state
315 * @return the corresponding Botanical or Zoological or... name
316 */
317 protected TaxonName setTaxonNameByType(
318 HashMap<String, String> atomisedMap, String fullName, STATE state) {
319 boolean problem = false;
320 if (logger.isDebugEnabled()){
321 logger.debug("settaxonnamebytype " + state.getDataHolder().getNomenclatureCode().toString());
322 }
323
324 if (state.getDataHolder().getNomenclatureCode().equals("Zoological") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN.getUuid())) {
325 TaxonName taxonName = TaxonNameFactory.NewZoologicalInstance(null);
326 taxonName.setFullTitleCache(fullName, true);
327 taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
328 taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
329 taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap,"SpeciesEpithet")));
330 taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap,"SubspeciesEpithet")));
331
332 if (taxonName.getGenusOrUninomial() != null){
333 taxonName.setRank(Rank.GENUS());
334 }
335
336 if (taxonName.getInfraGenericEpithet() != null){
337 taxonName.setRank(Rank.SUBGENUS());
338 }
339
340 if (taxonName.getSpecificEpithet() != null){
341 taxonName.setRank(Rank.SPECIES());
342 }
343
344 if (taxonName.getInfraSpecificEpithet() != null){
345 taxonName.setRank(Rank.SUBSPECIES());
346 }
347
348 Team team = null;
349 if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
350 team = Team.NewInstance();
351 team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"), true);
352 }
353 else {
354 if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
355 team = Team.NewInstance();
356 team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamAndYear"), true);
357 }
358 }
359 if (team != null) {
360 taxonName.setBasionymAuthorship(team);
361 }
362 else {
363 if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
364 taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"));
365 }
366 else if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
367 taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamAndYear"));
368 }
369 }
370 if (getFromMap(atomisedMap, "CombinationAuthorTeamAndYear") != null) {
371 team = Team.NewInstance();
372 team.setTitleCache(getFromMap(atomisedMap, "CombinationAuthorTeamAndYear"), true);
373 taxonName.setCombinationAuthorship(team);
374 }
375 if (taxonName.hasProblem()) {
376 logger.info("pb ICZN");
377 problem = true;
378 }
379 else {
380 return taxonName;
381 }
382 }
383 else if (state.getDataHolder().getNomenclatureCode().equals("Botanical") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP.getUuid())) {
384 TaxonName taxonName = parseScientificName(fullName, state, state.getReport(), null);
385 if (taxonName != null){
386 return taxonName;
387 }
388 else{
389 taxonName = TaxonNameFactory.NewBotanicalInstance(null);
390 }
391 taxonName.setFullTitleCache(fullName, true);
392 taxonName.setGenusOrUninomial(NB(getFromMap(atomisedMap, "Genus")));
393 taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap, "FirstEpithet")));
394 taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap, "InfraSpeEpithet")));
395 try {
396 taxonName.setRank(Rank.getRankByName(getFromMap(atomisedMap, "Rank")));
397 } catch (Exception e) {
398 if (taxonName.getInfraSpecificEpithet() != null){
399 taxonName.setRank(Rank.SUBSPECIES());
400 }
401 else if (taxonName.getSpecificEpithet() != null){
402 taxonName.setRank(Rank.SPECIES());
403 }
404 else if (taxonName.getInfraGenericEpithet() != null){
405 taxonName.setRank(Rank.SUBGENUS());
406 }
407 else if (taxonName.getGenusOrUninomial() != null){
408 taxonName.setRank(Rank.GENUS());
409 }
410 }
411 Team team = null;
412 if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
413 team = Team.NewInstance();
414 team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"), true);
415 taxonName.setBasionymAuthorship(team);
416 }
417 if (getFromMap(atomisedMap, "AuthorTeam") != null) {
418 team = Team.NewInstance();
419 team.setTitleCache(getFromMap(atomisedMap, "AuthorTeam"), true);
420 taxonName.setCombinationAuthorship(team);
421 }
422 if (team == null) {
423 if (getFromMap(atomisedMap, "AuthorTeamParenthesis") != null) {
424 taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeamParenthesis"));
425 }
426 else if (getFromMap(atomisedMap, "AuthorTeam") != null) {
427 taxonName.setAuthorshipCache(getFromMap(atomisedMap, "AuthorTeam"));
428 }
429 }
430 if (getFromMap(atomisedMap, "CombinationAuthorTeamAndYear") != null) {
431 team = Team.NewInstance();
432 team.setTitleCache(getFromMap(atomisedMap, "CombinationAuthorTeamAndYear"), true);
433 taxonName.setCombinationAuthorship(team);
434 }
435 if (taxonName.hasProblem()) {
436 logger.info("pb ICBN");
437 problem = true;
438 }
439 else {
440 return taxonName;
441 }
442 }
443 else if (state.getDataHolder().getNomenclatureCode().equals("Bacterial") || state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNB.getUuid())) {
444 TaxonName taxonName = TaxonNameFactory.NewBacterialInstance(null);
445 taxonName.setFullTitleCache(fullName, true);
446 taxonName.setGenusOrUninomial(getFromMap(atomisedMap, "Genus"));
447 taxonName.setInfraGenericEpithet(NB(getFromMap(atomisedMap, "SubGenus")));
448 taxonName.setSpecificEpithet(NB(getFromMap(atomisedMap, "Species")));
449 taxonName.setInfraSpecificEpithet(NB(getFromMap(atomisedMap, "SubspeciesEpithet")));
450
451 if (taxonName.getGenusOrUninomial() != null){
452 taxonName.setRank(Rank.GENUS());
453 }
454 else if (taxonName.getInfraGenericEpithet() != null){
455 taxonName.setRank(Rank.SUBGENUS());
456 }
457 else if (taxonName.getSpecificEpithet() != null){
458 taxonName.setRank(Rank.SPECIES());
459 }
460 else if (taxonName.getInfraSpecificEpithet() != null){
461 taxonName.setRank(Rank.SUBSPECIES());
462 }
463
464 if (getFromMap(atomisedMap, "AuthorTeamAndYear") != null) {
465 Team team = Team.NewInstance();
466 team.setTitleCache(getFromMap(atomisedMap, "AuthorTeamAndYear"), true);
467 taxonName.setCombinationAuthorship(team);
468 }
469 if (getFromMap(atomisedMap, "ParentheticalAuthorTeamAndYear") != null) {
470 Team team = Team.NewInstance();
471 team.setTitleCache(getFromMap(atomisedMap, "ParentheticalAuthorTeamAndYear"), true);
472 taxonName.setBasionymAuthorship(team);
473 }
474 if (taxonName.hasProblem()) {
475 logger.info("pb ICNB");
476 problem = true;
477 }
478 else {
479 return taxonName;
480 }
481 }
482 else if (state.getDataHolder().getNomenclatureCode().equals("Cultivar")) {
483 TaxonName taxonName = TaxonNameFactory.NewCultivarInstance(null);
484
485 if (taxonName.hasProblem()) {
486 logger.info("pb ICNCP");
487 problem = true;
488 }
489 else {
490 return taxonName;
491 }
492 return taxonName;
493 }
494
495 if (problem) {
496 logger.info("Problem im setTaxonNameByType ");
497 TaxonName taxonName = TaxonNameFactory.NewNonViralInstance(null);
498 taxonName.setFullTitleCache(fullName, true);
499 return taxonName;
500 }
501 TaxonName tn = TaxonNameFactory.NewNonViralInstance(null);
502 return tn;
503 }
504
505 /**
506 * Get a formated string from a hashmap
507 * @param atomisedMap
508 * @param key
509 * @return
510 */
511 private String getFromMap(HashMap<String, String> atomisedMap, String key) {
512 String value = null;
513 if (atomisedMap.containsKey(key)) {
514 value = atomisedMap.get(key);
515 }
516
517 try {
518 if (value != null && key.matches(".*Year.*")) {
519 value = value.trim();
520 if (value.matches("[a-z A-Z ]*[0-9]{4}$")) {
521 String tmp = value.split("[0-9]{4}$")[0];
522 int year = Integer.parseInt(value.split(tmp)[1]);
523 if (year >= 1752) {
524 value = tmp;
525 }
526 else {
527 value = null;
528 }
529 }
530 else {
531 value = null;
532 }
533 }
534 }
535 catch (Exception e) {
536 value = null;
537 }
538 return value;
539 }
540
541 /**
542 * Very fast and dirty implementation to allow handling of transient objects as described in
543 * https://dev.e-taxonomy.eu/trac/ticket/3726
544 *
545 * Not yet complete.
546 *
547 * @param cdmBase
548 * @param state
549 */
550 protected UUID save(CdmBase cdmBase, SpecimenImportStateBase state) {
551 ICdmRepository cdmRepository = state.getConfig().getCdmAppController();
552 if (cdmRepository == null){
553 cdmRepository = this;
554 }
555
556 if (cdmBase.isInstanceOf(LanguageString.class)){
557 return cdmRepository.getTermService().saveLanguageData(CdmBase.deproxy(cdmBase, LanguageString.class));
558 }else if (cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
559 SpecimenOrObservationBase specimen = CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class);
560
561 return cdmRepository.getOccurrenceService().saveOrUpdate(specimen);
562 }else if (cdmBase.isInstanceOf(Reference.class)){
563 return cdmRepository.getReferenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, Reference.class));
564 }else if (cdmBase.isInstanceOf(Classification.class)){
565 return cdmRepository.getClassificationService().saveOrUpdate(CdmBase.deproxy(cdmBase, Classification.class));
566 }else if (cdmBase.isInstanceOf(AgentBase.class)){
567 return cdmRepository.getAgentService().saveOrUpdate(CdmBase.deproxy(cdmBase, AgentBase.class));
568 }else if (cdmBase.isInstanceOf(Collection.class)){
569 return cdmRepository.getCollectionService().saveOrUpdate(CdmBase.deproxy(cdmBase, Collection.class));
570 }else if (cdmBase.isInstanceOf(DescriptionBase.class)){
571 DescriptionBase description = CdmBase.deproxy(cdmBase, DescriptionBase.class);
572
573 return cdmRepository.getDescriptionService().saveOrUpdate(description);
574 }else if (cdmBase.isInstanceOf(TaxonBase.class)){
575 return cdmRepository.getTaxonService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonBase.class));
576 }else if (cdmBase.isInstanceOf(TaxonName.class)){
577 return cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonName.class));
578 }else if (cdmBase.isInstanceOf(TaxonNode.class)){
579 return cdmRepository.getTaxonNodeService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNode.class));
580 }else{
581 throw new IllegalArgumentException("Class not supported in save method: " + CdmBase.deproxy(cdmBase, CdmBase.class).getClass().getSimpleName());
582 }
583
584 }
585
586
587 protected SpecimenOrObservationBase findExistingSpecimen(String unitId, SpecimenImportStateBase state){
588 ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
589 if(cdmAppController==null){
590 cdmAppController = this;
591 }
592 FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
593 config.setSignificantIdentifier(unitId);
594 List<String> propertyPaths = new ArrayList<>();
595 propertyPaths.add("derivedFrom.*");
596 config.setPropertyPaths(propertyPaths);
597 commitTransaction(state.getTx());
598 state.setTx(startTransaction());
599 try{
600 Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
601 if(!existingSpecimens.getRecords().isEmpty()){
602 if(existingSpecimens.getRecords().size()==1){
603 return existingSpecimens.getRecords().iterator().next();
604 }
605 }
606 }catch(NullPointerException e){
607 logger.error("searching for existing specimen creates NPE: " + config.getSignificantIdentifier());
608 }
609 return null;
610 }
611
612 protected abstract void importAssociatedUnits(STATE state, Object item, DerivedUnitFacade derivedUnitFacade);
613
614 /**
615 * getFacade : get the DerivedUnitFacade based on the recordBasis
616 * @param state
617 *
618 * @return DerivedUnitFacade
619 */
620 protected DerivedUnitFacade getFacade(STATE state) {
621 if (logger.isDebugEnabled()){
622 logger.info("getFacade()");
623 }
624 SpecimenOrObservationType type = null;
625
626 // create specimen
627 if (NB((state.getDataHolder().getRecordBasis())) != null) {
628 if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
629 type = SpecimenOrObservationType.PreservedSpecimen;
630 }
631 if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
632 type = SpecimenOrObservationType.Observation;
633 }
634 if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
635 type = SpecimenOrObservationType.Fossil;
636 }
637 if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
638 type = SpecimenOrObservationType.LivingSpecimen;
639 }
640 if (type == null) {
641 logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
642 type = SpecimenOrObservationType.DerivedUnit;
643 }
644 // TODO fossils?
645 } else {
646 logger.info("The basis of record is null");
647 type = SpecimenOrObservationType.DerivedUnit;
648 }
649 DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
650 return derivedUnitFacade;
651 }
652
653 /**
654 * Look if the Institution does already exist
655 * @param institutionCode: a string with the institutioncode
656 * @param config : the configurator
657 * @return the Institution (existing or new)
658 */
659 protected Institution getInstitution(String institutionCode, STATE state) {
660 SpecimenImportConfiguratorBase config = state.getConfig();
661 Institution institution=null;
662 institution = (Institution)state.institutions.get(institutionCode);
663 if (institution != null){
664 return institution;
665 }
666 List<Institution> institutions;
667 try {
668 institutions = getAgentService().searchInstitutionByCode(institutionCode);
669
670 } catch (Exception e) {
671 institutions = new ArrayList<Institution>();
672 logger.warn(e);
673 }
674 if (institutions.size() > 0 && config.isReuseExistingMetaData()) {
675 for (Institution institut:institutions){
676 try{
677 if (institut.getCode().equalsIgnoreCase(institutionCode)) {
678 institution=institut;
679 break;
680 }
681 }catch(Exception e){logger.warn("no institution code in the db");}
682 }
683 }
684 if (logger.isDebugEnabled()){
685 if(institution !=null) {
686 logger.info("getinstitution " + institution.toString());
687 }
688 }
689 if (institution == null){
690 // create institution
691 institution = Institution.NewInstance();
692 institution.setCode(institutionCode);
693 institution.setTitleCache(institutionCode, true);
694 UUID uuid = save(institution, state);
695 }
696
697
698 state.institutions.put(institutionCode, institution);
699 return institution;
700 }
701
702 /**
703 * Look if the Collection does already exist
704 * @param collectionCode
705 * @param collectionCode: a string
706 * @param config : the configurator
707 * @return the Collection (existing or new)
708 */
709 protected Collection getCollection(Institution institution, String collectionCode, STATE state) {
710 SpecimenImportConfiguratorBase config = state.getConfig();
711 Collection collection = null;
712 List<Collection> collections;
713 collection = (Collection) state.collections.get(collectionCode);
714 if (collection != null){
715 return collection;
716 }
717 try {
718 collections = getCollectionService().searchByCode(collectionCode);
719 } catch (Exception e) {
720 collections = new ArrayList<Collection>();
721 }
722 if (collections.size() > 0 && config.isReuseExistingMetaData()) {
723 for (Collection coll:collections){
724 if (coll.getCode() != null && coll.getInstitute() != null
725 && coll.getCode().equalsIgnoreCase(collectionCode) && coll.getInstitute().equals(institution)) {
726 collection = coll;
727 break;
728 }
729 }
730 }
731
732 if(collection == null){
733 collection =Collection.NewInstance();
734 collection.setCode(collectionCode);
735 collection.setInstitute(institution);
736 collection.setTitleCache(collectionCode);
737 UUID uuid = save(collection, state);
738 }
739
740
741
742 state.collections.put(collectionCode, collection);
743
744 return collection;
745 }
746
747 /**
748 * @param reference
749 * @param citationDetail
750 * @return
751 */
752 //FIXME this method is highly critical, because
753 // * it will have serious performance and memory problems with large databases
754 // (databases may easily have >1 Mio source records)
755 // * it does not make sense to search for existing sources and then clone them
756 // we need to search for existing references instead and use them (if exist)
757 // for our new source.
758 protected IdentifiableSource getIdentifiableSource(Reference reference, String citationDetail) {
759
760 /* List<IdentifiableSource> issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
761
762
763 if (reference != null){
764 try {
765 for (OriginalSourceBase<?> osb: issTmp){
766 if (osb.getCitation() != null && osb.getCitation().getTitleCache().equalsIgnoreCase(reference.getTitleCache())){
767 String osbDetail = osb.getCitationMicroReference();
768 if ((StringUtils.isBlank(osbDetail) && StringUtils.isBlank(citationDetail))
769 || (osbDetail != null && osbDetail.equalsIgnoreCase(citationDetail)) ) {
770 // System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
771 return (IdentifiableSource) osb.clone();
772 }
773 }
774 }
775 } catch (CloneNotSupportedException e) {
776 throw new RuntimeException(e);
777 } catch (Exception e1){
778 e1.printStackTrace();
779 }
780 }
781 */
782 IdentifiableSource sour = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
783 return sour;
784 }
785
786 /**
787 * Add the hierarchy for a Taxon(add higher taxa)
788 * @param classification
789 * @param taxon: a taxon to add as a node
790 * @param state: the ABCD import state
791 */
792 protected void addParentTaxon(Taxon taxon, STATE state, boolean preferredFlag, Classification classification){
793 INonViralName nvname = taxon.getName();
794 Rank rank = nvname.getRank();
795 Taxon genus =null;
796 Taxon subgenus =null;
797 Taxon species = null;
798 Taxon subspecies = null;
799 Taxon parent = null;
800 if(rank!=null){
801 if (rank.isLower(Rank.GENUS() )){
802 String genusOrUninomial = nvname.getGenusOrUninomial();
803 TaxonName taxonName = getOrCreateTaxonName(genusOrUninomial, Rank.GENUS(), preferredFlag, state, -1);
804 genus = getOrCreateTaxonForName(taxonName, state);
805 if (genus == null){
806 logger.debug("The genus should not be null " + taxonName);
807 }
808 if (preferredFlag) {
809 parent = linkParentChildNode(null, genus, classification, state);
810 }
811
812 }
813 if (rank.isLower(Rank.SUBGENUS())){
814 String prefix = nvname.getGenusOrUninomial();
815 String name = nvname.getInfraGenericEpithet();
816 if (name != null){
817 TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SUBGENUS(), preferredFlag, state, -1);
818 subgenus = getOrCreateTaxonForName(taxonName, state);
819 if (preferredFlag) {
820 parent = linkParentChildNode(genus, subgenus, classification, state);
821 } }
822 }
823 if (rank.isLower(Rank.SPECIES())){
824 if (subgenus!=null){
825 String prefix = nvname.getGenusOrUninomial();
826 String name = nvname.getInfraGenericEpithet();
827 String spe = nvname.getSpecificEpithet();
828 if (spe != null){
829 TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name+" "+spe, Rank.SPECIES(), preferredFlag, state, -1);
830 species = getOrCreateTaxonForName(taxonName, state);
831 if (preferredFlag) {
832 parent = linkParentChildNode(subgenus, species, classification, state);
833 }
834 }
835 }
836 else{
837 String prefix = nvname.getGenusOrUninomial();
838 String name = nvname.getSpecificEpithet();
839 if (name != null){
840 TaxonName taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SPECIES(), preferredFlag, state, -1);
841 species = getOrCreateTaxonForName(taxonName, state);
842 if (preferredFlag) {
843 parent = linkParentChildNode(genus, species, classification, state);
844 }
845 }
846 }
847 }
848 if (rank.isLower(Rank.INFRASPECIES())){
849 TaxonName taxonName = getOrCreateTaxonName(nvname.getFullTitleCache(), Rank.SUBSPECIES(), preferredFlag, state, -1);
850 subspecies = getOrCreateTaxonForName(taxonName, state);
851 if (preferredFlag) {
852 parent = linkParentChildNode(species, subspecies, classification, state);
853 }
854 }
855 }
856 if (preferredFlag && parent!=taxon ) {
857 linkParentChildNode(parent, taxon, classification, state);
858 }
859 }
860
861 /**
862 * Link a parent to a child and save it in the current classification
863 * @param parent: the higher Taxon
864 * @param child : the lower (or current) Taxon
865 * return the Taxon from the new created Node
866 * @param classification
867 * @param state
868 */
869 protected Taxon linkParentChildNode(Taxon parent, Taxon child, Classification classification, STATE state) {
870 TaxonNode node =null;
871 if (parent != null) {
872 parent = (Taxon) getTaxonService().find(parent.getUuid());
873 child = (Taxon) getTaxonService().find(child.getUuid());
874 //here we do not have to check if the taxon nodes already exists
875 //this is done by classification.addParentChild()
876 //do not add child node if it already exists
877 if(hasTaxonNodeInClassification(child, classification)){
878 return child;
879 }
880 else{
881 node = classification.addParentChild(parent, child, state.getRef(), "");
882 save(node, state);
883 }
884 }
885 else {
886 if (child == null){
887 logger.debug("The child should not be null!");
888 }
889 child = (Taxon) getTaxonService().find(child.getUuid());
890 //do not add child node if it already exists
891 if(hasTaxonNodeInClassification(child, classification)){
892 return child;
893 }
894 else{
895 node = classification.addChildTaxon(child, state.getRef(), null);
896 save(node, state);
897 }
898 }
899 if(node!=null){
900 state.getReport().addTaxonNode(node);
901 return node.getTaxon();
902 }
903 String message = "Could not create taxon node for " +child;
904 state.getReport().addInfoMessage(message);
905 logger.warn(message);
906 return null;
907 }
908
909 protected Taxon getOrCreateTaxonForName(TaxonName taxonName, STATE state){
910 if (taxonName != null){
911 Set<Taxon> acceptedTaxa = taxonName.getTaxa();
912 if(acceptedTaxa.size()>0){
913 Taxon firstAcceptedTaxon = acceptedTaxa.iterator().next();
914 if(acceptedTaxa.size()>1){
915 String message = "More than one accepted taxon was found for taxon name: "
916 + taxonName.getTitleCache() + "!\n" + firstAcceptedTaxon + "was chosen for "+state.getDerivedUnitBase();
917 state.getReport().addInfoMessage(message);
918 logger.warn(message);
919 }
920 else{
921 return firstAcceptedTaxon;
922 }
923 }
924 else{
925 Set<TaxonBase> taxonAndSynonyms = taxonName.getTaxonBases();
926 for (TaxonBase taxonBase : taxonAndSynonyms) {
927 if(taxonBase.isInstanceOf(Synonym.class)){
928 Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
929 Taxon acceptedTaxonOfSynonym = synonym.getAcceptedTaxon();
930 if(acceptedTaxonOfSynonym == null){
931 String message = "No accepted taxon could be found for taxon name: "
932 + taxonName.getTitleCache()
933 + "!";
934 state.getReport().addInfoMessage(message);
935 logger.warn(message);
936 }
937 else{
938 return acceptedTaxonOfSynonym;
939 }
940 }
941 }
942 }
943 Taxon taxon = Taxon.NewInstance(taxonName, state.getRef());
944 save(taxon, state);
945 state.getReport().addTaxon(taxon);
946 logger.info("Created new taxon "+ taxon);
947 return taxon;
948 }
949 return null;
950
951 }
952
953 private boolean hasTaxonNodeInClassification(Taxon taxon, Classification classification){
954 if(taxon.getTaxonNodes()!=null){
955 for (TaxonNode node : taxon.getTaxonNodes()){
956 if(node.getClassification().equals(classification)){
957 return true;
958 }
959 }
960 }
961 return false;
962 }
963
964 /**
965 * HandleIdentifications : get the scientific names present in the ABCD
966 * document and store link them with the observation/specimen data
967 * @param state: the current ABCD import state
968 * @param derivedUnitFacade : the current derivedunitfacade
969 */
970 protected void handleIdentifications(STATE state, DerivedUnitFacade derivedUnitFacade) {
971 SpecimenImportConfiguratorBase config = state.getConfig();
972
973
974 String scientificName = "";
975 boolean preferredFlag = false;
976
977 if (state.getDataHolder().getNomenclatureCode() == ""){
978 if (config.getNomenclaturalCode() != null){
979 if (config.getNomenclaturalCode() != null){
980 state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
981
982 }
983 }
984 }
985
986 for (int i = 0; i < state.getDataHolder().getIdentificationList().size(); i++) {
987 Identification identification = state.getDataHolder().getIdentificationList().get(i);
988 scientificName = identification.getScientificName().replaceAll(" et ", " & ");
989
990 String preferred = identification.getPreferred();
991 preferredFlag = false;
992 if (preferred != null || state.getDataHolder().getIdentificationList().size()==1){
993 if (preferred.equals("1") || preferred.toLowerCase().indexOf("true") != -1 || state.getDataHolder().getIdentificationList().size()==1) {
994 preferredFlag = true;
995 }
996
997 }
998 if (identification.getCode() != null){
999 if (identification.getCode().indexOf(':') != -1) {
1000 state.getDataHolder().setNomenclatureCode(identification.getCode().split(COLON)[1]);
1001 }
1002 else{
1003 state.getDataHolder().setNomenclatureCode(identification.getCode());
1004 }
1005 }
1006 TaxonName taxonName = getOrCreateTaxonName(scientificName, null, preferredFlag, state, i);
1007 Taxon taxon = getOrCreateTaxonForName(taxonName, state);
1008 addTaxonNode(taxon, state,preferredFlag);
1009 linkDeterminationEvent(state, taxon, preferredFlag, derivedUnitFacade, identification.getIdentifier(), identification.getDate());
1010 }
1011 }
1012
1013 /**
1014 * @param taxon : a taxon to add as a node
1015 * @param state : the ABCD import state
1016 */
1017 protected void addTaxonNode(Taxon taxon, STATE state, boolean preferredFlag) {
1018 SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
1019 logger.info("link taxon to a taxonNode "+taxon.getTitleCache());
1020 //only add nodes if not already existing in current classification or default classification
1021
1022 //check if node exists in current classification
1023 //NOTE: we cannot use hasTaxonNodeInClassification() here because we are first creating it here
1024 if (!existsInClassification(taxon,state.getClassification(), state)){
1025 if(config.isMoveNewTaxaToDefaultClassification()){
1026 //check if node exists in default classification
1027 if (!existsInClassification(taxon, state.getDefaultClassification(true), state)){
1028 addParentTaxon(taxon, state, preferredFlag, state.getDefaultClassification(true));
1029 }
1030 }else{
1031 //add non-existing taxon to current classification
1032 addParentTaxon(taxon, state, preferredFlag, state.getClassification());
1033 }
1034
1035 }
1036 }
1037
1038
1039 private boolean existsInClassification(Taxon taxon, Classification classification, STATE state){
1040 boolean exist = false;
1041 ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
1042 if(cdmAppController==null){
1043 cdmAppController = this;
1044 }
1045 if (classification != null){
1046 if (!taxon.getTaxonNodes().isEmpty()){
1047 for (TaxonNode node:taxon.getTaxonNodes()){
1048 if (node.getClassification().equals(classification)){
1049 return true;
1050 }
1051 }
1052 }
1053 // we do not need this because we already searched for taxa in db in the previous steps
1054 // List<UuidAndTitleCache<TaxonNode>> uuidAndTitleCacheOfAllTaxa = cdmAppController.getClassificationService().getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(classification.getUuid());
1055 // if (uuidAndTitleCacheOfAllTaxa != null){
1056 // for (UuidAndTitleCache p : uuidAndTitleCacheOfAllTaxa){
1057 // try{
1058 // if(p.getTitleCache().equals(taxon.getTitleCache())) {
1059 // exist = true;
1060 // }
1061 // }
1062 // catch(Exception e){
1063 // logger.warn("TaxonNode doesn't seem to have a taxon");
1064 // }
1065 // }
1066 // }
1067 }
1068 return exist;
1069 }
1070
1071 /**
1072 * join DeterminationEvent to the Taxon Object
1073 * @param state : the ABCD import state
1074 * @param taxon: the current Taxon
1075 * @param preferredFlag :if the current name is preferred
1076 * @param derivedFacade : the derived Unit Facade
1077 */
1078 @SuppressWarnings("rawtypes")
1079 protected void linkDeterminationEvent(STATE state, Taxon taxon, boolean preferredFlag, DerivedUnitFacade derivedFacade, String identifierStr, String dateStr) {
1080 SpecimenImportConfiguratorBase config = state.getConfig();
1081 if (logger.isDebugEnabled()){
1082 logger.info("start linkdetermination with taxon:" + taxon.getUuid()+", "+taxon);
1083 }
1084
1085 DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
1086 //determinationEvent.setTaxon(taxon);
1087 determinationEvent.setTaxonName(taxon.getName());
1088 determinationEvent.setPreferredFlag(preferredFlag);
1089
1090
1091 determinationEvent.setIdentifiedUnit(state.getDerivedUnitBase());
1092 if (state.getPersonStore().get(identifierStr) != null){
1093 determinationEvent.setActor((AgentBase)state.getPersonStore().get(identifierStr));
1094 } else if (identifierStr != null){
1095 Person identifier = Person.NewTitledInstance(identifierStr);
1096 determinationEvent.setActor(identifier);
1097 }
1098 if (dateStr != null){
1099 determinationEvent.setTimeperiod(TimePeriodParser.parseString(dateStr));
1100 }
1101 state.getDerivedUnitBase().addDetermination(determinationEvent);
1102
1103 if (logger.isDebugEnabled()){
1104 logger.debug("NB TYPES INFO: "+ state.getDataHolder().getStatusList().size());
1105 }
1106 for (SpecimenTypeDesignationStatus specimenTypeDesignationstatus : state.getDataHolder().getStatusList()) {
1107 if (specimenTypeDesignationstatus != null) {
1108 if (logger.isDebugEnabled()){
1109 logger.debug("specimenTypeDesignationstatus :"+ specimenTypeDesignationstatus);
1110 }
1111
1112 ICdmRepository cdmAppController = config.getCdmAppController();
1113 if(cdmAppController == null){
1114 cdmAppController = this;
1115 }
1116 specimenTypeDesignationstatus = HibernateProxyHelper.deproxy(cdmAppController.getTermService().find(specimenTypeDesignationstatus.getUuid()), SpecimenTypeDesignationStatus.class);
1117 //Designation
1118 TaxonName name = taxon.getName();
1119 SpecimenTypeDesignation designation = SpecimenTypeDesignation.NewInstance();
1120
1121 designation.setTypeStatus(specimenTypeDesignationstatus);
1122 designation.setTypeSpecimen(state.getDerivedUnitBase());
1123 name.addTypeDesignation(designation, true);
1124 }
1125 }
1126 save(state.getDerivedUnitBase(), state);
1127
1128 for (String[] fullReference : state.getDataHolder().getReferenceList()) {
1129
1130
1131 String strReference=fullReference[0];
1132 String citationDetail = fullReference[1];
1133 String citationURL = fullReference[2];
1134 List<Reference> references = getReferenceService().listByTitleWithRestrictions(Reference.class, "strReference", MatchMode.EXACT, null, null, null, null, null);
1135
1136 if (!references.isEmpty()){
1137 Reference reference = null;
1138 for (Reference refe: references) {
1139 if (refe.getTitleCache().equalsIgnoreCase(strReference)) {
1140 reference =refe;
1141 break;
1142 }
1143 }
1144 if (reference ==null){
1145 reference = ReferenceFactory.newGeneric();
1146 reference.setTitleCache(strReference, true);
1147 save(reference, state);
1148 }
1149 determinationEvent.addReference(reference);
1150 }
1151 }
1152 save(state.getDerivedUnitBase(), state);
1153
1154 if (config.isAddIndividualsAssociationsSuchAsSpecimenAndObservations() && preferredFlag) {
1155 //do not add IndividualsAssociation to non-preferred taxa
1156 if (logger.isDebugEnabled()){
1157 logger.debug("isDoCreateIndividualsAssociations");
1158 }
1159
1160 makeIndividualsAssociation(state, taxon, determinationEvent);
1161
1162 save(state.getDerivedUnitBase(), state);
1163 }
1164 }
1165
1166 /**
1167 * create and link each association (specimen, observation..) to the accepted taxon
1168 * @param state : the ABCD import state
1169 * @param taxon: the current Taxon
1170 * @param determinationEvent:the determinationevent
1171 */
1172 protected void makeIndividualsAssociation(STATE state, Taxon taxon, DeterminationEvent determinationEvent) {
1173 SpecimenImportConfiguratorBase<?,?,?> config = state.getConfig();
1174 SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
1175
1176 if (logger.isDebugEnabled()){
1177 logger.info("MAKE INDIVIDUALS ASSOCIATION");
1178 }
1179
1180 TaxonDescription taxonDescription = null;
1181 Set<TaxonDescription> descriptions= taxon.getDescriptions();
1182 if (!descriptions.isEmpty()){ taxonDescription = descriptions.iterator().next();}
1183
1184 // for (TaxonDescription description : descriptions){
1185 // Set<IdentifiableSource> sources = new HashSet<>();
1186 // sources.addAll(description.getTaxon().getSources());
1187 // sources.addAll(description.getSources());
1188 // for (IdentifiableSource source:sources){
1189 // if(state.getRef().equals(source.getCitation())) {
1190 // taxonDescription = description;
1191 // }
1192 // }
1193 // }
1194 // }
1195 if (taxonDescription == null){
1196 taxonDescription = TaxonDescription.NewInstance(taxon, false);
1197 if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1198 taxonDescription.addSource(OriginalSourceType.Import, null, null, state.getRef(), null);
1199 }
1200 state.setDescriptionGroup(taxonDescription);
1201 taxon.addDescription(taxonDescription);
1202 }
1203
1204 //PREPARE REFERENCE QUESTIONS
1205
1206 Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1207
1208 List<IdentifiableSource> issTmp = new ArrayList<>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
1209 List<DescriptionElementSource> issTmp2 = new ArrayList<>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1210
1211 Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
1212 if(issTmp2!=null) {
1213 osbSet.addAll(issTmp2);
1214 }
1215 if(issTmp!=null) {
1216 osbSet.addAll(issTmp);
1217 }
1218
1219
1220 addToSourceMap(sourceMap, osbSet);
1221
1222 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1223 // List<OriginalSourceBase<?>> res = null;
1224 // if(!state.isDescriptionSourcesSet()){
1225 // res = sui.askForSource(sourceMap, "the description group ("+taxon+")",
1226 // "The current reference is "+state.getRef().getTitleCache(),getReferenceService(), state.getDataHolder().docSources);
1227 // state.setDescriptionRefs(res);
1228 // state.setDescriptionSourcesSet(true);
1229 // }
1230 // else{
1231 // res=state.getDescriptionRefs();
1232 // }
1233 // if(res !=null) {
1234 // for (OriginalSourceBase<?> sour:res){
1235 // if(sour.isInstanceOf(IdentifiableSource.class)){
1236 // try {
1237 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1238 // taxonDescription.addSource((IdentifiableSource)sour.clone());
1239 // }
1240 // } catch (CloneNotSupportedException e) {
1241 // logger.warn("no cloning?");
1242 // }
1243 // }else{
1244 // if(sourceNotLinkedToElement(taxonDescription,sour)) {
1245 // taxonDescription.addSource(OriginalSourceType.Import,null, null, sour.getCitation(),sour.getCitationMicroReference());
1246 // }
1247 // }
1248 // }
1249 // }
1250 // }
1251 // else {
1252 if(sourceNotLinkedToElement(taxonDescription,state.getRef(),null)) {
1253 taxonDescription.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1254 }
1255 // }
1256 state.setDescriptionGroup(taxonDescription);
1257
1258 IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
1259 Feature feature = makeFeature(state.getDerivedUnitBase());
1260 indAssociation.setAssociatedSpecimenOrObservation(state.getDerivedUnitBase());
1261 indAssociation.setFeature(feature);
1262 //
1263 // if (((Abcd206ImportConfigurator) state.getConfig()).isInteractWithUser()){
1264 // sourceMap = new HashMap<String, OriginalSourceBase<?>>();
1265 //
1266 // issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
1267 // issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
1268 //
1269 // osbSet = new HashSet<OriginalSourceBase>();
1270 // if(issTmp2!=null) {
1271 // osbSet.addAll(issTmp2);
1272 // }
1273 // if(issTmp!=null) {
1274 // osbSet.addAll(issTmp);
1275 // }
1276 //
1277 //
1278 // addToSourceMap(sourceMap, osbSet);
1279 //
1280 // List<OriginalSourceBase<?>> sources =null;
1281 // if(!state.isAssociationSourcesSet()) {
1282 // sources = sui.askForSource(sourceMap, "descriptive element (association) ",taxon.toString(),
1283 // getReferenceService(),state.getDataHolder().getDocSources());
1284 // state.setAssociationRefs(sources);
1285 // state.setAssociationSourcesSet(true);
1286 // }
1287 // else{
1288 // sources=state.getAssociationRefs();
1289 // }
1290 // if(sources !=null) {
1291 // for (OriginalSourceBase<?> source: sources) {
1292 // if(source !=null) {
1293 // if(source.isInstanceOf(DescriptionElementSource.class)){
1294 // try {
1295 // if(sourceNotLinkedToElement(indAssociation,source)) {
1296 // indAssociation.addSource((DescriptionElementSource)source.clone());
1297 // }
1298 // } catch (CloneNotSupportedException e) {
1299 // logger.warn("clone forbidden?");
1300 // }
1301 // }else{
1302 // if(sourceNotLinkedToElement(indAssociation,source)) {
1303 // indAssociation.addSource(OriginalSourceType.Import,null, null, source.getCitation(),source.getCitationMicroReference());
1304 // }
1305 // try {
1306 // if(sourceNotLinkedToElement(state.getDerivedUnitBase(), source)) {
1307 // state.getDerivedUnitBase().addSource((IdentifiableSource) source.clone());
1308 // }
1309 // } catch (CloneNotSupportedException e) {
1310 // // TODO Auto-generated catch block
1311 // e.printStackTrace();
1312 // }
1313 // }
1314 //
1315 // }
1316 // }
1317 // }
1318 // }else {
1319 if(sourceNotLinkedToElement(indAssociation,state.getRef(),null)) {
1320 indAssociation.addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1321 }
1322 if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
1323 state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1324 }
1325 for (Reference citation : determinationEvent.getReferences()) {
1326 if(sourceNotLinkedToElement(indAssociation,citation,null))
1327 {
1328 indAssociation.addSource(DescriptionElementSource.NewInstance(OriginalSourceType.Import, null, null, citation, null));
1329 }
1330 if(sourceNotLinkedToElement(state.getDerivedUnitBase(), state.getRef(),null)) {
1331 state.getDerivedUnitBase().addSource(OriginalSourceType.Import,null, null, state.getRef(), null);
1332 }
1333 }
1334 // }
1335
1336 taxonDescription.addElement(indAssociation);
1337
1338 save(taxonDescription, state);
1339 save(taxon, state);
1340 state.getReport().addDerivate(state.getDerivedUnitBase(), config);
1341 state.getReport().addIndividualAssociation(taxon, state.getDataHolder().getUnitID(), state.getDerivedUnitBase());
1342 }
1343
1344 /**
1345 * @param derivedUnitBase2
1346 * @param ref2
1347 * @param object
1348 * @return
1349 */
1350 private boolean sourceNotLinkedToElement(DerivedUnit derivedUnitBase2, Reference b, String d) {
1351 Set<IdentifiableSource> linkedSources = derivedUnitBase2.getSources();
1352 for (IdentifiableSource is:linkedSources){
1353 Reference a = is.getCitation();
1354 String c = is.getCitationMicroReference();
1355
1356 boolean refMatch=false;
1357 boolean microMatch=false;
1358
1359 try{
1360 if (a==null && b==null) {
1361 refMatch=true;
1362 }
1363 if (a!=null && b!=null) {
1364 if (a.getTitleCache().equalsIgnoreCase(b.getTitleCache())) {
1365 refMatch=true;
1366 }
1367 }
1368 }catch(Exception e){}
1369
1370
1371 try{
1372 if (c==null && d==null) {
1373 microMatch=true;
1374 }
1375 if(c!=null && d!=null) {
1376 if(c.equalsIgnoreCase(d)) {
1377 microMatch=true;
1378 }
1379 }
1380 }
1381 catch(Exception e){}
1382
1383 if (microMatch && refMatch) {
1384 return false;
1385 }
1386
1387
1388 }
1389 return true;
1390 }
1391
1392 private <T extends OriginalSourceBase<?>> boolean sourceNotLinkedToElement(ISourceable<T> sourcable, Reference reference, String microReference) {
1393 Set<T> linkedSources = sourcable.getSources();
1394 for (T is:linkedSources){
1395 Reference unitReference = is.getCitation();
1396 String unitMicroReference = is.getCitationMicroReference();
1397
1398 boolean refMatch=false;
1399 boolean microMatch=false;
1400
1401 try{
1402 if (unitReference==null && reference==null) {
1403 refMatch=true;
1404 }
1405 if (unitReference!=null && reference!=null) {
1406 if (unitReference.getTitleCache().equalsIgnoreCase(reference.getTitleCache())) {
1407 refMatch=true;
1408 }
1409 }
1410 }catch(Exception e){}
1411
1412 try{
1413 if (unitMicroReference==null && microReference==null) {
1414 microMatch=true;
1415 }
1416 if(unitMicroReference!=null && microReference!=null) {
1417 if(unitMicroReference.equalsIgnoreCase(microReference)) {
1418 microMatch=true;
1419 }
1420 }
1421 }
1422 catch(Exception e){}
1423
1424 if (microMatch && refMatch) {
1425 return false;
1426 }
1427 }
1428 return true;
1429 }
1430
1431 /**
1432 * look for the Feature object (FieldObs, Specimen,...)
1433 * @param unit : a specimen or obersvation base
1434 * @return the corresponding Feature
1435 */
1436 private Feature makeFeature(SpecimenOrObservationBase<?> unit) {
1437 SpecimenOrObservationType type = unit.getRecordBasis();
1438
1439
1440
1441 if (type.isFeatureObservation()){
1442 return Feature.OBSERVATION();
1443 }else if (type.isFeatureSpecimen()){
1444 return Feature.SPECIMEN();
1445 }else if (type == SpecimenOrObservationType.DerivedUnit){
1446 return Feature.OBSERVATION();
1447 // return getFeature("Specimen or observation");
1448 }else{
1449 String message = "Unhandled record basis '%s' for defining individuals association feature type. Use default.";
1450 logger.warn(String.format(message, type.getMessage()));
1451 return Feature.OBSERVATION();
1452 // return getFeature("Specimen or observation");
1453
1454 }
1455 }
1456
1457
1458 /**
1459 * @param sourceMap
1460 * @param osbSet
1461 */
1462 protected void addToSourceMap(Map<String, OriginalSourceBase<?>> sourceMap, Set<OriginalSourceBase> osbSet) {
1463 for( OriginalSourceBase<?> osb:osbSet) {
1464 if(osb.getCitation()!=null && osb.getCitationMicroReference() !=null && !osb.getCitationMicroReference().isEmpty()) {
1465 try{
1466 sourceMap.put(osb.getCitation().getTitleCache()+ "---"+osb.getCitationMicroReference(),osb);
1467 }catch(NullPointerException e){logger.warn("null pointer problem (no ref?) with "+osb);}
1468 } else if(osb.getCitation()!=null){
1469 try{
1470 sourceMap.put(osb.getCitation().getTitleCache(),osb);
1471 }catch(NullPointerException e){logger.warn("null pointer problem (no ref?) with "+osb);}
1472 }
1473 }
1474 }
1475
1476
1477 }