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.app.greece;
|
11
|
|
12
|
import java.util.Collection;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
24
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
25
|
import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
29
|
import eu.etaxonomy.cdm.model.description.Feature;
|
30
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
31
|
import eu.etaxonomy.cdm.model.description.TextData;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
33
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
|
34
|
|
35
|
/**
|
36
|
* @author a.mueller
|
37
|
* @since 18.07.2020
|
38
|
*/
|
39
|
public class GreeceExcludedNoteDeleterActivator {
|
40
|
|
41
|
private static final Logger logger = Logger.getLogger(GreeceExcludedNoteDeleterActivator.class);
|
42
|
|
43
|
static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_greece();
|
44
|
// static final ICdmDataSource cdmDestination = CdmDestinations.cdm_production_greece_checklist();
|
45
|
|
46
|
static boolean testOnly = false;
|
47
|
|
48
|
private void deleteExcludedNote(ICdmDataSource cdmDestination){
|
49
|
CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, DbSchemaValidation.VALIDATE);
|
50
|
TransactionStatus tx = app.startTransaction();
|
51
|
|
52
|
List<TaxonNode> list = app.getTaxonNodeService().list(TaxonNode.class, null, null, null, null);
|
53
|
for (TaxonNode taxonNode : list){
|
54
|
if (!taxonNode.getStatusNote().isEmpty()){
|
55
|
Collection<LanguageString> nodeNotes = taxonNode.getStatusNote().values();
|
56
|
if (nodeNotes.size() > 1){
|
57
|
logger.warn("More than 1 language reprepresentation exists. Taxon not handled. NodeId: " + taxonNode.getId());
|
58
|
}else{
|
59
|
String noteStr = nodeNotes.iterator().next().getText();
|
60
|
if (taxonNode.getStatus() != TaxonNodeStatus.EXCLUDED){
|
61
|
logger.warn("NoteStr exists but status was not 'excluded' but " + taxonNode.getStatus() + "; " + taxonNode.getTaxon().getName().getTitleCache() + "; NodeId: " + taxonNode.getId());
|
62
|
continue;
|
63
|
}
|
64
|
Set<TextData> descrNote = taxonNode.getTaxon().getDescriptionItems(Feature.NOTES(), TextData.class);
|
65
|
if (descrNote.size()>1){
|
66
|
logger.warn("More than 1 language reprepresentation exists. Taxon not handled. Taxon: " + taxonNode.getTaxon().getName().getTitleCache() + "; NodeId: " + taxonNode.getId());
|
67
|
}else if (descrNote.isEmpty()){
|
68
|
logger.warn("No description note found. Taxon: "+ taxonNode.getTaxon().getName().getTitleCache() + "; NodeId: " + taxonNode.getId());
|
69
|
}else{
|
70
|
TextData textNote = descrNote.iterator().next();
|
71
|
Map<Language, LanguageString> multiText = textNote.getMultilanguageText();
|
72
|
if (multiText == null ||multiText.isEmpty()){
|
73
|
logger.warn("MultiText is empty or null. NodeId: " + taxonNode.getId());
|
74
|
}else if (multiText.values().size() > 1){
|
75
|
logger.warn("More than 1 language exists for multitext. Taxon not handled. NodeId: " + taxonNode.getId());
|
76
|
}else{
|
77
|
String singleText = multiText.values().iterator().next().getText();
|
78
|
if (CdmUtils.nullSafeEqual(noteStr, singleText)){
|
79
|
TaxonDescription description = CdmBase.deproxy(textNote.getInDescription(), TaxonDescription.class);
|
80
|
description.removeElement(textNote);
|
81
|
if (description.getElements().isEmpty()){
|
82
|
app.getDescriptionService().deleteDescription(description);
|
83
|
// logger.warn("Removed description. " + taxonNode.getTaxon().getName().getTitleCache() + "; " + taxonNode.getId());
|
84
|
}else{
|
85
|
logger.warn("Removed. " + taxonNode.getTaxon().getName().getTitleCache() + "; " + taxonNode.getId());
|
86
|
}
|
87
|
}else{
|
88
|
logger.warn("NoteStr and textNote exist but differ. NoteStr: " + noteStr + "; textNote: "+ textNote + "; Taxon: " + taxonNode.getTaxon().getName().getTitleCache() + "; TaxonNode id: " + taxonNode.getId());
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
|
96
|
if (testOnly){
|
97
|
tx.setRollbackOnly();
|
98
|
}
|
99
|
app.commitTransaction(tx);
|
100
|
|
101
|
}
|
102
|
|
103
|
public static void main(String[] args) {
|
104
|
GreeceExcludedNoteDeleterActivator me = new GreeceExcludedNoteDeleterActivator();
|
105
|
try {
|
106
|
me.deleteExcludedNote(cdmDestination);
|
107
|
} catch (Exception e) {
|
108
|
e.printStackTrace();
|
109
|
}
|
110
|
System.exit(0);
|
111
|
}
|
112
|
|
113
|
}
|