1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.specimen.abcd206.in;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.io.PrintStream;
|
15
|
import java.net.URI;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.List;
|
20
|
import java.util.Map;
|
21
|
import java.util.Map.Entry;
|
22
|
import java.util.Set;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
28
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
32
|
|
33
|
/**
|
34
|
* Gathers information about the ABCD import and presents them in a suitable way.
|
35
|
* @author pplitzner
|
36
|
* @date Jan 23, 2015
|
37
|
*
|
38
|
*/
|
39
|
public class Abcd206ImportReport {
|
40
|
|
41
|
static private final Logger logger = Logger.getLogger(Abcd206ImportReport.class);
|
42
|
|
43
|
|
44
|
private final List<Taxon> createdTaxa = new ArrayList<Taxon>();
|
45
|
private final Map<Taxon, List<UnitIdSpecimen>> taxonToAssociatedSpecimens = new HashMap<Taxon, List<UnitIdSpecimen>>();
|
46
|
private final Map<UnitIdSpecimen, List<UnitIdSpecimen>> derivateMap = new HashMap<UnitIdSpecimen, List<UnitIdSpecimen>>();
|
47
|
private final List<UnitIdSpecimen> alreadyExistingSpecimens = new ArrayList<UnitIdSpecimen>();
|
48
|
private final List<TaxonNameBase<?, ?>> createdNames = new ArrayList<TaxonNameBase<?,?>>();
|
49
|
private final List<TaxonNode> createdTaxonNodes = new ArrayList<TaxonNode>();
|
50
|
private final List<String> infoMessages = new ArrayList<String>();
|
51
|
|
52
|
public void addTaxon(Taxon taxon){
|
53
|
createdTaxa.add(taxon);
|
54
|
}
|
55
|
|
56
|
public void addName(TaxonNameBase<?, ?> taxonName){
|
57
|
createdNames.add(taxonName);
|
58
|
}
|
59
|
|
60
|
public void addTaxonNode(TaxonNode taxonNode){
|
61
|
createdTaxonNodes.add(taxonNode);
|
62
|
}
|
63
|
|
64
|
public void addDerivate(DerivedUnit parent, Abcd206ImportConfigurator config){
|
65
|
addDerivate(parent, null, config);
|
66
|
}
|
67
|
|
68
|
public void addDerivate(DerivedUnit parent, DerivedUnit child, Abcd206ImportConfigurator config){
|
69
|
UnitIdSpecimen parentUnitIdSpecimen = new UnitIdSpecimen(AbcdImportUtility.getUnitID(parent, config), parent);
|
70
|
List<UnitIdSpecimen> children = derivateMap.get(parentUnitIdSpecimen);
|
71
|
if(children==null){
|
72
|
children = new ArrayList<UnitIdSpecimen>();
|
73
|
}
|
74
|
if(child!=null){
|
75
|
children.add(new UnitIdSpecimen(AbcdImportUtility.getUnitID(child, config), child));
|
76
|
}
|
77
|
derivateMap.put(parentUnitIdSpecimen, children);
|
78
|
}
|
79
|
|
80
|
public void addIndividualAssociation(Taxon taxon, String derivedUnitId, DerivedUnit derivedUnitBase) {
|
81
|
UnitIdSpecimen derivedUnitIdSpecimen = new UnitIdSpecimen(derivedUnitId, derivedUnitBase);
|
82
|
List<UnitIdSpecimen> associatedSpecimens = taxonToAssociatedSpecimens.get(taxon);
|
83
|
if(associatedSpecimens==null){
|
84
|
associatedSpecimens = new ArrayList<UnitIdSpecimen>();
|
85
|
}
|
86
|
associatedSpecimens.add(derivedUnitIdSpecimen);
|
87
|
taxonToAssociatedSpecimens.put(taxon, associatedSpecimens);
|
88
|
}
|
89
|
|
90
|
public void addAlreadyExistingSpecimen(String unitId, DerivedUnit derivedUnit){
|
91
|
alreadyExistingSpecimens.add(new UnitIdSpecimen(unitId, derivedUnit));
|
92
|
}
|
93
|
|
94
|
public void addException(String message, Exception e) {
|
95
|
infoMessages.add(message+"\n"+e.getMessage()+"\n"+e.toString()+"\n"+e.getStackTrace());
|
96
|
}
|
97
|
|
98
|
public void addInfoMessage(String message) {
|
99
|
infoMessages.add(message);
|
100
|
}
|
101
|
|
102
|
public void printReport(URI reportUri) {
|
103
|
PrintStream out;
|
104
|
if(reportUri != null){
|
105
|
try {
|
106
|
out = new PrintStream(new File(reportUri));
|
107
|
} catch (FileNotFoundException e) {
|
108
|
logger.warn("Report file could not be found.");
|
109
|
out = System.out;
|
110
|
}
|
111
|
}
|
112
|
else{
|
113
|
out = System.out;
|
114
|
}
|
115
|
printReport(out);
|
116
|
}
|
117
|
|
118
|
|
119
|
public void printReport(PrintStream out) {
|
120
|
|
121
|
out.println("++++++++Import Report+++++++++");
|
122
|
//all specimens
|
123
|
Set<UnitIdSpecimen> allSpecimens = new HashSet<UnitIdSpecimen>();
|
124
|
Map<SpecimenOrObservationType, Integer> specimenTypeToCount = new HashMap<SpecimenOrObservationType, Integer>();
|
125
|
for (Entry<UnitIdSpecimen, List<UnitIdSpecimen>> entry : derivateMap.entrySet()) {
|
126
|
UnitIdSpecimen parentSpecimen = entry.getKey();
|
127
|
incrementSpecimenTypeCount(specimenTypeToCount, parentSpecimen);
|
128
|
allSpecimens.add(parentSpecimen);
|
129
|
for (UnitIdSpecimen childSpecimen : entry.getValue()) {
|
130
|
incrementSpecimenTypeCount(specimenTypeToCount, childSpecimen);
|
131
|
allSpecimens.add(childSpecimen);
|
132
|
}
|
133
|
}
|
134
|
out.println("Specimens created: "+allSpecimens.size());
|
135
|
for(Entry<SpecimenOrObservationType, Integer> entry:specimenTypeToCount.entrySet()){
|
136
|
SpecimenOrObservationType type = entry.getKey();
|
137
|
out.println(type+": "+entry.getValue());
|
138
|
}
|
139
|
out.println("\n");
|
140
|
|
141
|
//taxon name
|
142
|
out.println("---Created Taxon Names ("+createdNames.size()+")---");
|
143
|
for (TaxonNameBase<?, ?> taxonName : createdNames) {
|
144
|
out.println(taxonName.getTitleCache());
|
145
|
}
|
146
|
out.println("\n");
|
147
|
|
148
|
//taxa
|
149
|
out.println("---Created Taxa ("+createdTaxa.size()+")---");
|
150
|
for (Taxon taxon : createdTaxa) {
|
151
|
out.println(taxon.getTitleCache());
|
152
|
}
|
153
|
out.println("\n");
|
154
|
|
155
|
//taxon nodes
|
156
|
out.println("---Created Taxon Nodes ("+createdTaxonNodes.size()+")---");
|
157
|
for (TaxonNode taxonNode : createdTaxonNodes) {
|
158
|
String nodeString = taxonNode.toString();
|
159
|
if(taxonNode.getTaxon()!=null){
|
160
|
nodeString += " ("+taxonNode.getTaxon().getTitleCache()+")";
|
161
|
}
|
162
|
if(taxonNode.getParent()!=null){
|
163
|
nodeString += " with parent "+taxonNode.getParent();
|
164
|
if(taxonNode.getParent().getTaxon()!=null){
|
165
|
nodeString += " ("+taxonNode.getParent().getTaxon().getTitleCache()+")";
|
166
|
}
|
167
|
}
|
168
|
out.println(nodeString);
|
169
|
}
|
170
|
out.println("\n");
|
171
|
|
172
|
//not imported
|
173
|
out.println("---Already existing specimen (not imported)---");
|
174
|
for(UnitIdSpecimen specimen:alreadyExistingSpecimens){
|
175
|
out.println(formatSpecimen(specimen));
|
176
|
}
|
177
|
out.println("\n");
|
178
|
|
179
|
//taxa with associated specimens
|
180
|
out.println("---Taxa with associated specimens---");
|
181
|
for(Entry<Taxon, List<UnitIdSpecimen>> entry:taxonToAssociatedSpecimens.entrySet()){
|
182
|
Taxon taxon = entry.getKey();
|
183
|
List<UnitIdSpecimen> specimens = entry.getValue();
|
184
|
out.println(taxon.getTitleCache() + " ("+specimens.size()+")");
|
185
|
for (UnitIdSpecimen derivedUnit : specimens) {
|
186
|
out.println("\t- "+formatSpecimen(derivedUnit));
|
187
|
//check for derivatives
|
188
|
List<UnitIdSpecimen> list = derivateMap.get(derivedUnit);
|
189
|
for (UnitIdSpecimen derivate : list) {
|
190
|
out.println("\t\t- "+formatSpecimen(derivate));
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
out.println("\n");
|
195
|
out.println("\n");
|
196
|
//info messages
|
197
|
out.println("---Info messages---");
|
198
|
for(String message:infoMessages){
|
199
|
out.println(message);
|
200
|
out.println("---");
|
201
|
}
|
202
|
if(out!=System.out){
|
203
|
out.close();
|
204
|
}
|
205
|
}
|
206
|
|
207
|
private void incrementSpecimenTypeCount(Map<SpecimenOrObservationType, Integer> specimenTypeToCount,
|
208
|
UnitIdSpecimen specimen) {
|
209
|
SpecimenOrObservationType specimenType = specimen.getSpecimen().getRecordBasis();
|
210
|
Integer count = specimenTypeToCount.get(specimenType);
|
211
|
if(count==null){
|
212
|
count = 1;
|
213
|
}
|
214
|
else{
|
215
|
count++;
|
216
|
}
|
217
|
specimenTypeToCount.put(specimenType, count);
|
218
|
}
|
219
|
|
220
|
private String formatSpecimen(UnitIdSpecimen specimen){
|
221
|
return "("+specimen.getUnitId()+") ["+specimen.getSpecimen().getRecordBasis()+"] "+specimen.getSpecimen().getTitleCache();
|
222
|
}
|
223
|
|
224
|
private class UnitIdSpecimen{
|
225
|
private final String unitId;
|
226
|
private final SpecimenOrObservationBase<?> specimen;
|
227
|
|
228
|
|
229
|
public UnitIdSpecimen(String unitId, SpecimenOrObservationBase<?> specimen) {
|
230
|
super();
|
231
|
this.unitId = unitId;
|
232
|
this.specimen = specimen;
|
233
|
}
|
234
|
public String getUnitId() {
|
235
|
return unitId;
|
236
|
}
|
237
|
public SpecimenOrObservationBase<?> getSpecimen() {
|
238
|
return specimen;
|
239
|
}
|
240
|
@Override
|
241
|
public int hashCode() {
|
242
|
final int prime = 31;
|
243
|
int result = 1;
|
244
|
result = prime * result + ((specimen == null) ? 0 : specimen.hashCode());
|
245
|
result = prime * result + ((unitId == null) ? 0 : unitId.hashCode());
|
246
|
return result;
|
247
|
}
|
248
|
@Override
|
249
|
public boolean equals(Object obj) {
|
250
|
if (this == obj) {
|
251
|
return true;
|
252
|
}
|
253
|
if (obj == null) {
|
254
|
return false;
|
255
|
}
|
256
|
if (getClass() != obj.getClass()) {
|
257
|
return false;
|
258
|
}
|
259
|
UnitIdSpecimen other = (UnitIdSpecimen) obj;
|
260
|
if (specimen == null) {
|
261
|
if (other.specimen != null) {
|
262
|
return false;
|
263
|
}
|
264
|
} else if (!specimen.equals(other.specimen)) {
|
265
|
return false;
|
266
|
}
|
267
|
if (unitId == null) {
|
268
|
if (other.unitId != null) {
|
269
|
return false;
|
270
|
}
|
271
|
} else if (!unitId.equals(other.unitId)) {
|
272
|
return false;
|
273
|
}
|
274
|
return true;
|
275
|
}
|
276
|
|
277
|
}
|
278
|
|
279
|
}
|