Project

General

Profile

Download (6.34 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 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
package eu.etaxonomy.cdm.io.common;
10

    
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.apache.commons.lang3.StringUtils;
17

    
18
import eu.etaxonomy.cdm.common.IoResultBase;
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20

    
21
/**
22
 * @author cmathew
23
 * @since 5 Aug 2015
24
 */
25
public class ImportResult extends IoResultBase {
26

    
27
    private static final long serialVersionUID = -7299667532720042100L;
28

    
29
    private List<byte[]> reports = new ArrayList<>();
30
    //map with simple class name and count
31
    private Map<String, Integer> newRecords = new HashMap<>();
32
    private Map<String, Integer> updatedRecords = new HashMap<>();
33
    private Map<String, Integer> deletedRecords = new HashMap<>();
34

    
35
    private ImportResultState state;
36

    
37
// **************************** FACTORY ****************************************/
38

    
39
    public static ImportResult NewInstance(){
40
        return new ImportResult();
41
    }
42

    
43
    public static ImportResult NewNoDataInstance(){
44
        ImportResult result = new ImportResult();
45
        result.state = ImportResultState.SUCCESS_BUT_NO_DATA;
46
        return result;
47
    }
48

    
49
// *********************** CONSTRUCTOR *****************************************/
50

    
51
    public ImportResult() {
52
        state = ImportResultState.SUCCESS;
53
    }
54

    
55
    public enum ImportResultState{
56
        SUCCESS_BUT_NO_DATA,   //Only if NO data at all is exported, if only 1 class is exported use SUCCESS
57
        SUCCESS,               //All configured data exported, no warning, no errors
58
        SUCCESS_WITH_WARNING,   //All data exported but with some warnings
59
        FINISHED_WITH_ERROR,    //Probably all data exported but with errors
60
        INCOMPLETE_WITH_ERROR,  //Run to the end, but in the middle there might be "larger" amounts of data missing, e.g. some parts did not run to the end
61
        CANCELED,              //Export canceled by the user
62
        ABORTED,                //An handled exception occurred that lead to abort the export
63
        ;
64
    }
65

    
66
    @Override
67
    protected void setExceptionState() {
68
        state = ImportResultState.INCOMPLETE_WITH_ERROR;
69
    }
70

    
71
    @Override
72
    public void setAborted() {this.state = ImportResultState.ABORTED;}
73

    
74
    public boolean isSuccess(){
75
        return state == ImportResultState.SUCCESS || state == ImportResultState.SUCCESS_WITH_WARNING;
76
    }
77

    
78
    public void merge(ImportResult otherResult) {
79
        mergeMap(this.deletedRecords, otherResult.deletedRecords);
80
        mergeMap(this.updatedRecords, otherResult.updatedRecords);
81
        mergeMap(this.newRecords, otherResult.newRecords);
82
        this.reports.addAll(otherResult.reports);
83
    }
84

    
85
    private void mergeMap(Map<String, Integer> thisMap, Map<String, Integer> otherMap) {
86
        for (String key: otherMap.keySet()){
87
            int existing = thisMap.get(key)== null ? 0 : thisMap.get(key);
88
            thisMap.put(key, existing + thisMap.get(key));
89
        }
90
    }
91

    
92
    public List<byte[]> getReports() {
93
        return reports;
94
    }
95
    public void setReports(List<byte[]> reports) {
96
        this.reports = reports;
97
    }
98

    
99
    public void addReport(byte[] report) {
100
        reports.add(report);
101
    }
102

    
103

    
104
    public Map<String, Integer> getNewRecords() {
105
        return clone(newRecords);
106
    }
107
    public Integer getNewRecords(Class<? extends CdmBase> clazz) {
108
        return clone(newRecords).get(clazz.getSimpleName());
109
    }
110

    
111
    public Map<String, Integer> getUpdatedRecords() {
112
        return clone(updatedRecords);
113
    }
114

    
115
    private Map<String, Integer> clone(Map<String, Integer> records) {
116
        Map<String, Integer> result = new HashMap<>(records.size());
117
        for (String clazz : records.keySet()){
118
            result.put(clazz, records.get(clazz));
119
        }
120
        return result;
121
    }
122

    
123
    public Map<String, Integer> getDeletedRecords() {
124
        return clone(deletedRecords);
125
    }
126

    
127
    //new records
128
    public void addNewRecord(String clazz){
129
        addNewRecords(clazz, 1);
130
    }
131
    public void addNewRecords(String clazz, int count){
132
        addRecord(newRecords, clazz, count);
133
    }
134
    public void addNewRecords(Class<? extends CdmBase> clazz, int count){
135
        addRecord(newRecords, clazz.getSimpleName(), count);
136
    }
137
    public void addNewRecord(CdmBase newRecord) {
138
        this.addNewRecord(CdmBase.deproxy(newRecord).getClass().getSimpleName());
139
    }
140

    
141
    //updated records
142
    public void addUpdatedRecord(String clazz){
143
        addUpdatedRecords(clazz, 1);
144
    }
145
    public void addUpdatedRecords(String clazz, int count){
146
        addRecord(updatedRecords, clazz, count);
147
    }
148
    public void addUpdatedRecord(CdmBase updatedRecord) {
149
        this.addUpdatedRecord(CdmBase.deproxy(updatedRecord).getClass().getSimpleName());
150
    }
151

    
152
    //deleted
153
    public void addDeletedRecord(String clazz){
154
        addDeletedRecords(clazz, 1);
155
    }
156
    public void addDeletedRecords(String clazz, int count){
157
        addRecord(deletedRecords, clazz, count);
158
    }
159

    
160
    private void addRecord(Map<String, Integer> records, String clazz, Integer count){
161
        initClassRecord(records, clazz);
162
        records.put(clazz, records.get(clazz) + count);
163
    }
164

    
165
    private void initClassRecord(Map<String, Integer> records, String clazz) {
166
        if (records.get(clazz) == null){
167
            records.put(clazz, 0);
168
        }
169
    }
170

    
171
    public void addDeletedRecord(CdmBase deletedRecord) {
172
        this.addDeletedRecord(CdmBase.deproxy(deletedRecord).getClass().getSimpleName());
173
    }
174

    
175
    @Override
176
    public StringBuffer createReport() {
177
        StringBuffer report = super.createReport();
178
        addEditedReport(report, "New records", this.newRecords);
179
        addEditedReport(report, "Updated records", this.updatedRecords);
180
        addEditedReport(report, "Deleted records", this.deletedRecords);
181
        return report;
182
    }
183

    
184
    private void addEditedReport(StringBuffer report, String label, Map<String, Integer> records) {
185
        if (!records.isEmpty()){
186
            report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
187
            for (String key : records.keySet()){
188
                report.append("\n" + key + ": " + records.get(key));
189
            }
190
        }
191
    }
192
}
(42-42/65)