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.wp6.diptera;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
22
|
import eu.etaxonomy.cdm.api.service.INameService;
|
23
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
24
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
25
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
26
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
27
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
28
|
import eu.etaxonomy.cdm.model.common.Language;
|
29
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
31
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
32
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
33
|
import eu.etaxonomy.cdm.model.description.Feature;
|
34
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
35
|
import eu.etaxonomy.cdm.model.description.TextData;
|
36
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
|
40
|
/**
|
41
|
* @author a.mueller
|
42
|
* @created 01.10.2009
|
43
|
* @version 1.0
|
44
|
*/
|
45
|
public class DipteraPostImportUpdater {
|
46
|
private static final Logger logger = Logger.getLogger(DipteraPostImportUpdater.class);
|
47
|
|
48
|
static final ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();
|
49
|
|
50
|
/**
|
51
|
* This method updateds the citation text by deleting <code>OriginalName</code> tags and
|
52
|
* adding the original name to the source either as a link to an existing taxon name
|
53
|
* or as a string. The later becomes true if there is not exactly one matching name
|
54
|
* @param dataSource
|
55
|
* @return
|
56
|
*/
|
57
|
public ImportResult updateCitations(ICdmDataSource dataSource) {
|
58
|
|
59
|
logger.warn("start updating citations");
|
60
|
ImportResult result = new ImportResult();
|
61
|
try{
|
62
|
CdmApplicationController cdmApp = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
|
63
|
Set<DescriptionElementBase> citationsToSave = new HashSet<DescriptionElementBase>();
|
64
|
TransactionStatus tx = cdmApp.startTransaction();
|
65
|
|
66
|
logger.warn("start updating citations ... application context started");
|
67
|
int modCount = 100;
|
68
|
int page = 0;
|
69
|
int count = cdmApp.getTaxonService().count(Taxon.class);
|
70
|
List<Taxon> taxonList = cdmApp.getTaxonService().list(Taxon.class, 100000, page, null, null);
|
71
|
List<TaxonNameBase> nameList = cdmApp.getNameService().list(null, 100000, page, null, null);
|
72
|
Map<String, TaxonNameBase> nameMap = new HashMap<String, TaxonNameBase>();
|
73
|
Map<String, TaxonNameBase> nameDuplicateMap = new HashMap<String, TaxonNameBase>();
|
74
|
fillNameMaps(nameList, nameMap, nameDuplicateMap);
|
75
|
|
76
|
int i = 0;
|
77
|
|
78
|
for (Taxon taxon : taxonList){
|
79
|
if ((i++ % modCount) == 0){ logger.warn("taxa handled: " + (i-1));}
|
80
|
|
81
|
Set<TextData> citations = getCitations(taxon);
|
82
|
for (TextData citation : citations){
|
83
|
Language language = Language.DEFAULT();
|
84
|
String text = citation.getText(language);
|
85
|
String originalNameString = parseOriginalNameString(text);
|
86
|
String newText = parseNewText(text);
|
87
|
citation.removeText(language);
|
88
|
citation.putText(language, newText);
|
89
|
TaxonNameBase<?,?> scientificName = getScientificName(originalNameString, nameMap, nameDuplicateMap);
|
90
|
|
91
|
Set<DescriptionElementSource> sources = citation.getSources();
|
92
|
if (sources.size() > 1){
|
93
|
logger.warn("There are more then 1 sources for a description");
|
94
|
}else if (sources.size() == 0){
|
95
|
DescriptionElementSource source = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
|
96
|
citation.addSource(source);
|
97
|
sources = citation.getSources();
|
98
|
}
|
99
|
for (DescriptionElementSource source : sources){
|
100
|
if (scientificName != null){
|
101
|
source.setNameUsedInSource(scientificName);
|
102
|
}else{
|
103
|
source.setOriginalNameString(originalNameString);
|
104
|
}
|
105
|
}
|
106
|
|
107
|
citationsToSave.add(citation);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
cdmApp.getDescriptionService().saveDescriptionElement(citationsToSave);
|
112
|
//commit
|
113
|
cdmApp.commitTransaction(tx);
|
114
|
logger.warn("Citations updated!");
|
115
|
result.setSuccess(true);
|
116
|
return result;
|
117
|
} catch (Exception e) {
|
118
|
e.printStackTrace();
|
119
|
logger.error("ERROR in citation update");
|
120
|
result.setSuccess(false);
|
121
|
result.addReport(e.getMessage().getBytes());
|
122
|
return result;
|
123
|
}
|
124
|
|
125
|
}
|
126
|
|
127
|
public ImportResult updateCollections(ICdmDataSource dataSource){
|
128
|
DipteraCollectionImport collectionImport = new DipteraCollectionImport();
|
129
|
return collectionImport.invoke(dataSource);
|
130
|
}
|
131
|
|
132
|
|
133
|
private void fillNameMaps(List<TaxonNameBase> nameList, Map<String, TaxonNameBase> nameMap, Map<String, TaxonNameBase> duplicateMap) {
|
134
|
for (TaxonNameBase name : nameList){
|
135
|
NonViralName nvn = name.deproxy(name, NonViralName.class);
|
136
|
String nameCache = nvn.getNameCache();
|
137
|
if (nameMap.containsKey(nameCache)){
|
138
|
duplicateMap.put(nameCache, nvn);
|
139
|
}else{
|
140
|
nameMap.put(nameCache, nvn);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
|
146
|
private TaxonNameBase getScientificName(String originalNameString, Map<String, TaxonNameBase> nameMap, Map<String, TaxonNameBase> nameDuplicateMap) {
|
147
|
originalNameString = originalNameString.trim();
|
148
|
TaxonNameBase result = nameMap.get(originalNameString);
|
149
|
if (nameDuplicateMap.containsKey(originalNameString)){
|
150
|
result = null;
|
151
|
}
|
152
|
return result;
|
153
|
}
|
154
|
|
155
|
private TaxonNameBase getScientificName(String originalNameString, INameService nameService) {
|
156
|
Pager<TaxonNameBase> names = nameService.findByName(null, originalNameString, null, null, null, null, null, null);
|
157
|
if (names.getCount() != 1){
|
158
|
return null;
|
159
|
}else{
|
160
|
return names.getRecords().get(0);
|
161
|
}
|
162
|
}
|
163
|
|
164
|
private String parseOriginalNameString(String text) {
|
165
|
String originalName = "<OriginalName>";
|
166
|
int start = text.indexOf(originalName);
|
167
|
int end = text.indexOf("</OriginalName>");
|
168
|
if (start >-1 ){
|
169
|
text = text.substring(start + originalName.length(), end);
|
170
|
}
|
171
|
text = text.trim();
|
172
|
return text;
|
173
|
}
|
174
|
|
175
|
private String parseNewText(String text) {
|
176
|
int start = text.indexOf("</OriginalName>");
|
177
|
text = text.substring(start + "</OriginalName>".length());
|
178
|
text = text.trim();
|
179
|
if (text.startsWith(":")){
|
180
|
text = text.substring(1);
|
181
|
}
|
182
|
text = text.trim();
|
183
|
return text;
|
184
|
}
|
185
|
|
186
|
private Set<TextData> getCitations(Taxon taxon) {
|
187
|
Set<TextData> result = new HashSet<TextData>();
|
188
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
189
|
for (DescriptionBase description : descriptions){
|
190
|
Set<DescriptionElementBase> elements = description.getElements();
|
191
|
for (DescriptionElementBase element : elements){
|
192
|
Feature feature = element.getFeature();
|
193
|
if (feature.equals(Feature.CITATION())){
|
194
|
if (! element.isInstanceOf(TextData.class)){
|
195
|
logger.warn("Citation is not of class TextData but " + element.getClass().getSimpleName());
|
196
|
}else{
|
197
|
TextData textData = element.deproxy(element, TextData.class);
|
198
|
result.add(textData);
|
199
|
}
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
return result;
|
204
|
}
|
205
|
|
206
|
|
207
|
|
208
|
|
209
|
/**
|
210
|
* @param args
|
211
|
*/
|
212
|
public static void main(String[] args) {
|
213
|
DipteraPostImportUpdater updater = new DipteraPostImportUpdater();
|
214
|
try {
|
215
|
updater.updateCitations(cdmDestination);
|
216
|
} catch (Exception e) {
|
217
|
e.printStackTrace();
|
218
|
logger.error("ERROR in feature tree update");
|
219
|
}
|
220
|
}
|
221
|
|
222
|
}
|