Project

General

Profile

Revision cef0a41d

IDcef0a41db1d71972553cc36ee414ac147d8a7eca
Parent c7415eab
Child 3fac85e9

Added by Patrick Plitzner almost 4 years ago

Improved import report

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/Abcd206ImportReport.java
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.io.PrintWriter;
16
import java.io.StringWriter;
17
import java.net.URI;
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.HashSet;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Map.Entry;
24
import java.util.Set;
25

  
26
import org.apache.log4j.Logger;
27

  
28
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
31
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34

  
35
/**
36
 * Gathers information about the ABCD import and presents them in a suitable way.
37
 * @author pplitzner
38
 * @date Jan 23, 2015
39
 *
40
 */
41
public class Abcd206ImportReport extends SpecimenImportReport{
42

  
43
    static private final Logger logger = Logger.getLogger(Abcd206ImportReport.class);
44

  
45

  
46
    private final List<Taxon> createdTaxa = new ArrayList<Taxon>();
47
    private final Map<Taxon, List<UnitIdSpecimen>> taxonToAssociatedSpecimens =  new HashMap<Taxon, List<UnitIdSpecimen>>();
48
    private final Map<UnitIdSpecimen, List<UnitIdSpecimen>> derivateMap = new HashMap<UnitIdSpecimen, List<UnitIdSpecimen>>();
49
    private final List<UnitIdSpecimen> alreadyExistingSpecimens = new ArrayList<UnitIdSpecimen>();
50
    private final List<TaxonNameBase<?, ?>> createdNames = new ArrayList<TaxonNameBase<?,?>>();
51
    private final List<TaxonNode> createdTaxonNodes = new ArrayList<TaxonNode>();
52
    private final List<String> infoMessages = new ArrayList<String>();
53

  
54
    @Override
55
    public void addTaxon(Taxon taxon){
56
        createdTaxa.add(taxon);
57
    }
58

  
59
    @Override
60
    public void addName(TaxonNameBase<?, ?> taxonName){
61
        createdNames.add(taxonName);
62
    }
63

  
64
    @Override
65
    public void addTaxonNode(TaxonNode taxonNode){
66
        createdTaxonNodes.add(taxonNode);
67
    }
68

  
69
    @Override
70
    public void addDerivate(DerivedUnit parent, Abcd206ImportConfigurator config){
71
        addDerivate(parent, null, config);
72
    }
73

  
74
    @Override
75
    public void addDerivate(DerivedUnit parent, DerivedUnit child, Abcd206ImportConfigurator config){
76
        UnitIdSpecimen parentUnitIdSpecimen = new UnitIdSpecimen(SpecimenImportUtility.getUnitID(parent, config), parent);
77
        List<UnitIdSpecimen> children = derivateMap.get(parentUnitIdSpecimen);
78
        if(children==null){
79
            children = new ArrayList<UnitIdSpecimen>();
80
        }
81
        if(child!=null){
82
            children.add(new UnitIdSpecimen(SpecimenImportUtility.getUnitID(child, config), child));
83
        }
84
        derivateMap.put(parentUnitIdSpecimen, children);
85
    }
86

  
87
    @Override
88
    public void addIndividualAssociation(Taxon taxon, String derivedUnitId, DerivedUnit derivedUnitBase) {
89
        UnitIdSpecimen derivedUnitIdSpecimen = new UnitIdSpecimen(derivedUnitId, derivedUnitBase);
90
        List<UnitIdSpecimen> associatedSpecimens = taxonToAssociatedSpecimens.get(taxon);
91
        if(associatedSpecimens==null){
92
            associatedSpecimens = new ArrayList<UnitIdSpecimen>();
93
        }
94
        associatedSpecimens.add(derivedUnitIdSpecimen);
95
        taxonToAssociatedSpecimens.put(taxon, associatedSpecimens);
96
    }
97

  
98
    @Override
99
    public void addAlreadyExistingSpecimen(String unitId, DerivedUnit derivedUnit){
100
        alreadyExistingSpecimens.add(new UnitIdSpecimen(unitId, derivedUnit));
101
    }
102

  
103
    @Override
104
    public void addException(String message, Exception e) {
105
        StringWriter errors = new StringWriter();
106
        e.printStackTrace(new PrintWriter(errors));
107
        infoMessages.add(message+"\n"+e.getMessage()+"\n"+errors.toString());
108
    }
109

  
110
    @Override
111
    public void addInfoMessage(String message) {
112
        infoMessages.add(message);
113
    }
114

  
115
    @Override
116
    public void printReport(URI reportUri) {
117
        PrintStream out;
118
        if(reportUri != null){
119
            try {
120
                out = new PrintStream(new File(reportUri));
121
            } catch (FileNotFoundException e) {
122
                logger.warn("Report file could not be found.");
123
                out = System.out;
124
            }
125
        }
126
        else{
127
            out = System.out;
128
        }
129
        printReport(out);
130
    }
131

  
132

  
133
    @Override
134
    public void printReport(PrintStream out) {
135

  
136
        out.println("++++++++Import Report+++++++++");
137
      //all specimens
138
        Set<UnitIdSpecimen> allSpecimens = new HashSet<UnitIdSpecimen>();
139
        for (Entry<UnitIdSpecimen, List<UnitIdSpecimen>> entry : derivateMap.entrySet()) {
140
            UnitIdSpecimen parentSpecimen = entry.getKey();
141
            allSpecimens.add(parentSpecimen);
142
            for (UnitIdSpecimen childSpecimen : entry.getValue()) {
143
                allSpecimens.add(childSpecimen);
144
            }
145
        }
146
        out.println("Specimens created: "+allSpecimens.size());
147
        Map<SpecimenOrObservationType, Integer> specimenTypeToCount = new HashMap<SpecimenOrObservationType, Integer>();
148
        for (UnitIdSpecimen unitIdSpecimen : allSpecimens) {
149
            incrementSpecimenTypeCount(specimenTypeToCount, unitIdSpecimen);
150
        }
151
        for(Entry<SpecimenOrObservationType, Integer> entry:specimenTypeToCount.entrySet()){
152
            SpecimenOrObservationType type = entry.getKey();
153
            out.println(type+": "+entry.getValue());
154
        }
155
        //not imported
156
        out.println("Skipped/not imported: "+alreadyExistingSpecimens.size());
157
        out.println("\n");
158

  
159
        //taxon name
160
        out.println("---Created Taxon Names ("+createdNames.size()+")---");
161
        for (TaxonNameBase<?, ?> taxonName : createdNames) {
162
            out.println(taxonName.getTitleCache());
163
        }
164
        out.println("\n");
165

  
166
        //taxa
167
        out.println("---Created Taxa ("+createdTaxa.size()+")---");
168
        for (Taxon taxon : createdTaxa) {
169
            out.println(taxon.getTitleCache());
170
        }
171
        out.println("\n");
172

  
173
        //taxon nodes
174
        out.println("---Created Taxon Nodes ("+createdTaxonNodes.size()+")---");
175
        for (TaxonNode taxonNode : createdTaxonNodes) {
176
            String nodeString = taxonNode.toString();
177
            if(taxonNode.getTaxon()!=null){
178
                nodeString += " ("+taxonNode.getTaxon().getTitleCache()+")";
179
            }
180
            if(taxonNode.getParent()!=null){
181
                nodeString += " with parent "+taxonNode.getParent();
182
                if(taxonNode.getParent().getTaxon()!=null){
183
                    nodeString += " ("+taxonNode.getParent().getTaxon().getTitleCache()+")";
184
                }
185
            }
186
            out.println(nodeString);
187
        }
188
        out.println("\n");
189

  
190
        //not imported
191
        out.println("---Already existing specimen/not imported ("+alreadyExistingSpecimens.size()+")---");
192
        for(UnitIdSpecimen specimen:alreadyExistingSpecimens){
193
            out.println(formatSpecimen(specimen));
194
        }
195
        out.println("\n");
196

  
197
        //taxa with associated specimens
198
        out.println("---Taxa with associated specimens ("+taxonToAssociatedSpecimens.size()+")---");
199
        for(Entry<Taxon, List<UnitIdSpecimen>> entry:taxonToAssociatedSpecimens.entrySet()){
200
            Taxon taxon = entry.getKey();
201
            List<UnitIdSpecimen> specimens = entry.getValue();
202
            out.println(taxon.getTitleCache() + " ("+specimens.size()+")");
203
            for (UnitIdSpecimen derivedUnit : specimens) {
204
                out.println("\t- "+formatSpecimen(derivedUnit));
205
                //check for derivatives
206
                List<UnitIdSpecimen> list = derivateMap.get(derivedUnit);
207
                for (UnitIdSpecimen derivate : list) {
208
                    out.println("\t\t- "+formatSpecimen(derivate));
209
                }
210
            }
211
        }
212
        out.println("\n");
213
        out.println("\n");
214
        //info messages
215
        out.println("---Info messages---");
216
        for(String message:infoMessages){
217
            out.println(message);
218
            out.println("---");
219
        }
220
        if(out!=System.out){
221
            out.close();
222
        }
223
    }
224

  
225
    private void incrementSpecimenTypeCount(Map<SpecimenOrObservationType, Integer> specimenTypeToCount,
226
            UnitIdSpecimen specimen) {
227
        SpecimenOrObservationType specimenType = specimen.getSpecimen().getRecordBasis();
228
        Integer count = specimenTypeToCount.get(specimenType);
229
        if(count==null){
230
            count = 1;
231
        }
232
        else{
233
            count++;
234
        }
235
        specimenTypeToCount.put(specimenType, count);
236
    }
237

  
238
    private String formatSpecimen(UnitIdSpecimen specimen){
239
        return "("+specimen.getUnitId()+") ["+specimen.getSpecimen().getRecordBasis()+"] "+specimen.getSpecimen().getTitleCache();
240
    }
241

  
242
    private class UnitIdSpecimen{
243
        private final String unitId;
244
        private final SpecimenOrObservationBase<?> specimen;
245

  
246

  
247
        public UnitIdSpecimen(String unitId, SpecimenOrObservationBase<?> specimen) {
248
            super();
249
            this.unitId = unitId;
250
            this.specimen = specimen;
251
        }
252
        public String getUnitId() {
253
            return unitId;
254
        }
255
        public SpecimenOrObservationBase<?> getSpecimen() {
256
            return specimen;
257
        }
258
        @Override
259
        public int hashCode() {
260
            final int prime = 31;
261
            int result = 1;
262
            result = prime * result + ((specimen == null) ? 0 : specimen.hashCode());
263
            result = prime * result + ((unitId == null) ? 0 : unitId.hashCode());
264
            return result;
265
        }
266
        @Override
267
        public boolean equals(Object obj) {
268
            if (this == obj) {
269
                return true;
270
            }
271
            if (obj == null) {
272
                return false;
273
            }
274
            if (getClass() != obj.getClass()) {
275
                return false;
276
            }
277
            UnitIdSpecimen other = (UnitIdSpecimen) obj;
278
            if (specimen == null) {
279
                if (other.specimen != null) {
280
                    return false;
281
                }
282
            } else if (!specimen.equals(other.specimen)) {
283
                return false;
284
            }
285
            if (unitId == null) {
286
                if (other.unitId != null) {
287
                    return false;
288
                }
289
            } else if (!unitId.equals(other.unitId)) {
290
                return false;
291
            }
292
            return true;
293
        }
294

  
295
    }
296

  
297
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/abcd206/in/SpecimenImportReport.java
141 141
            SpecimenOrObservationType type = entry.getKey();
142 142
            out.println(type+": "+entry.getValue());
143 143
        }
144
        //not imported
145
        out.println("Skipped/not imported: "+alreadyExistingSpecimens.size());
144 146
        out.println("\n");
145 147

  
146 148
        //taxon name
......
175 177
        out.println("\n");
176 178

  
177 179
        //not imported
178
        out.println("---Already existing specimen (not imported)---");
180
        out.println("---Already existing specimen/not imported ("+alreadyExistingSpecimens.size()+")---");
179 181
        for(UnitIdSpecimen specimen:alreadyExistingSpecimens){
180 182
            out.println(formatSpecimen(specimen));
181 183
        }
182 184
        out.println("\n");
183 185

  
184 186
        //taxa with associated specimens
185
        out.println("---Taxa with associated specimens---");
187
        out.println("---Taxa with associated specimens ("+taxonToAssociatedSpecimens.size()+")---");
186 188
        for(Entry<Taxon, List<UnitIdSpecimen>> entry:taxonToAssociatedSpecimens.entrySet()){
187 189
            Taxon taxon = entry.getKey();
188 190
            List<UnitIdSpecimen> specimens = entry.getValue();

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)