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.io.Serializable;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.apache.commons.lang3.StringUtils;
|
18
|
|
19
|
import eu.etaxonomy.cdm.common.IoResultBase;
|
20
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
21
|
|
22
|
/**
|
23
|
* @author cmathew
|
24
|
* @since 5 Aug 2015
|
25
|
*
|
26
|
*/
|
27
|
public class ImportResult extends IoResultBase implements Serializable {
|
28
|
private static final long serialVersionUID = -7299667532720042100L;
|
29
|
|
30
|
private List<byte[]> reports = new ArrayList<>();
|
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
|
/**
|
79
|
* @param success
|
80
|
* @param successTaraxacum
|
81
|
*/
|
82
|
public void merge(ImportResult otherResult) {
|
83
|
mergeMap(this.deletedRecords, otherResult.deletedRecords);
|
84
|
mergeMap(this.updatedRecords, otherResult.updatedRecords);
|
85
|
mergeMap(this.newRecords, otherResult.newRecords);
|
86
|
this.reports.addAll(otherResult.reports);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* @param thisMap
|
91
|
* @param otherMap
|
92
|
*/
|
93
|
private void mergeMap(Map<String, Integer> thisMap, Map<String, Integer> otherMap) {
|
94
|
for (String key: otherMap.keySet()){
|
95
|
int existing = thisMap.get(key)== null ? 0 : thisMap.get(key);
|
96
|
thisMap.put(key, existing + thisMap.get(key));
|
97
|
}
|
98
|
}
|
99
|
|
100
|
public List<byte[]> getReports() {
|
101
|
return reports;
|
102
|
}
|
103
|
/**
|
104
|
* @param reports the reports to set
|
105
|
*/
|
106
|
public void setReports(List<byte[]> reports) {
|
107
|
this.reports = reports;
|
108
|
}
|
109
|
|
110
|
public void addReport(byte[] report) {
|
111
|
reports.add(report);
|
112
|
}
|
113
|
|
114
|
|
115
|
public Map<String, Integer> getNewRecords() {
|
116
|
return clone(newRecords);
|
117
|
}
|
118
|
public Integer getNewRecords(Class<? extends CdmBase> clazz) {
|
119
|
return clone(newRecords).get(clazz.getSimpleName());
|
120
|
}
|
121
|
|
122
|
public Map<String, Integer> getUpdatedRecords() {
|
123
|
return clone(updatedRecords);
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* @param updatedRecords2
|
128
|
* @return
|
129
|
*/
|
130
|
private Map<String, Integer> clone(Map<String, Integer> records) {
|
131
|
Map<String, Integer> result = new HashMap<>(records.size());
|
132
|
for (String clazz : records.keySet()){
|
133
|
result.put(clazz, records.get(clazz));
|
134
|
}
|
135
|
return result;
|
136
|
}
|
137
|
|
138
|
public Map<String, Integer> getDeletedRecords() {
|
139
|
return clone(deletedRecords);
|
140
|
}
|
141
|
|
142
|
//new records
|
143
|
public void addNewRecord(String clazz){
|
144
|
addNewRecords(clazz, 1);
|
145
|
}
|
146
|
public void addNewRecords(String clazz, int count){
|
147
|
addRecord(newRecords, clazz, count);
|
148
|
}
|
149
|
public void addNewRecords(Class<? extends CdmBase> clazz, int count){
|
150
|
addRecord(newRecords, clazz.getSimpleName(), count);
|
151
|
}
|
152
|
public void addNewRecord(CdmBase newRecord) {
|
153
|
this.addNewRecord(CdmBase.deproxy(newRecord).getClass().getSimpleName());
|
154
|
}
|
155
|
|
156
|
|
157
|
//updated records
|
158
|
public void addUpdatedRecord(String clazz){
|
159
|
addUpdatedRecords(clazz, 1);
|
160
|
}
|
161
|
public void addUpdatedRecords(String clazz, int count){
|
162
|
addRecord(updatedRecords, clazz, count);
|
163
|
}
|
164
|
public void addUpdatedRecord(CdmBase updatedRecord) {
|
165
|
this.addUpdatedRecord(CdmBase.deproxy(updatedRecord).getClass().getSimpleName());
|
166
|
}
|
167
|
|
168
|
//deleted
|
169
|
public void addDeletedRecord(String clazz){
|
170
|
addDeletedRecords(clazz, 1);
|
171
|
}
|
172
|
public void addDeletedRecords(String clazz, int count){
|
173
|
addRecord(deletedRecords, clazz, count);
|
174
|
}
|
175
|
|
176
|
private void addRecord(Map<String, Integer> records, String clazz, Integer count){
|
177
|
initClassRecord(records, clazz);
|
178
|
records.put(clazz, records.get(clazz) + count);
|
179
|
}
|
180
|
|
181
|
private void initClassRecord(Map<String, Integer> records, String clazz) {
|
182
|
if (records.get(clazz) == null){
|
183
|
records.put(clazz, 0);
|
184
|
}
|
185
|
}
|
186
|
|
187
|
|
188
|
public void addDeletedRecord(CdmBase deletedRecord) {
|
189
|
this.addDeletedRecord(CdmBase.deproxy(deletedRecord).getClass().getSimpleName());
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
*
|
194
|
*/
|
195
|
@Override
|
196
|
public StringBuffer createReport() {
|
197
|
StringBuffer report = super.createReport();
|
198
|
addEditedReport(report, "New records", this.newRecords);
|
199
|
addEditedReport(report, "Updated records", this.updatedRecords);
|
200
|
addEditedReport(report, "Deleted records", this.deletedRecords);
|
201
|
return report;
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* @param report
|
206
|
* @param string
|
207
|
* @param newRecords2
|
208
|
*/
|
209
|
private void addEditedReport(StringBuffer report, String label, Map<String, Integer> records) {
|
210
|
if (!records.isEmpty()){
|
211
|
report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
|
212
|
for (String key : records.keySet()){
|
213
|
report.append("\n" + key + ": " + records.get(key));
|
214
|
}
|
215
|
}
|
216
|
}
|
217
|
|
218
|
}
|