1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.importers;
21
22 import com.fasterxml.jackson.annotation.JsonCreator;
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.fasterxml.jackson.annotation.JsonValue;
25 import com.google.common.collect.Maps;
26
27 import java.io.PrintStream;
28 import java.util.Map;
29 import java.util.Optional;
30
31
32
33
34
35
36 public class ImportLog {
37
38 private int created;
39 private int updated;
40 private int unchanged;
41 private final String logMessage;
42 private final Map<String, String> errors = Maps.newHashMap();
43
44
45
46
47
48
49
50 public ImportLog(String logMessage) {
51 this.logMessage = logMessage;
52 }
53
54 public ImportLog() {
55 this(null);
56 }
57
58 @JsonCreator
59 public ImportLog(
60 @JsonProperty("message") String logMessage,
61 @JsonProperty("created") int created,
62 @JsonProperty("updated") int updated,
63 @JsonProperty("unchanged") int unchanged,
64 @JsonProperty("errors") Map<String, String> errors) {
65 this(logMessage);
66 this.created = created;
67 this.unchanged = unchanged;
68 this.updated = updated;
69 this.errors.putAll(errors);
70 }
71
72
73
74
75 public void addCreated() {
76 created++;
77 }
78
79
80
81
82 public void addUpdated() {
83 updated++;
84 }
85
86
87
88
89 public void addUnchanged() {
90 unchanged++;
91 }
92
93
94
95
96 public int getCreated() {
97 return created;
98 }
99
100
101
102
103 public int getUpdated() {
104 return updated;
105 }
106
107
108
109
110 public int getUnchanged() {
111 return unchanged;
112 }
113
114
115
116
117 public int getErrored() {
118 return errors.size();
119 }
120
121
122
123
124 public Map<String, String> getErrors() {
125 return errors;
126 }
127
128
129
130
131
132
133
134
135 public void addError(String item, String error) {
136 errors.put(item, error);
137 }
138
139
140
141
142
143
144
145 public boolean hasDoneWork() {
146 return created > 0 || updated > 0;
147 }
148
149
150
151
152 public int getChanged() {
153 return created + updated;
154 }
155
156 @JsonValue
157 public Map<String, Object> getData() {
158 Map<String, Object> data = Maps.newHashMap();
159 data.put("created", created);
160 data.put("updated", updated);
161 data.put("unchanged", unchanged);
162 data.put("errors", errors);
163 data.put("message", logMessage);
164 return data;
165 }
166
167 public Optional<String> getLogMessage() {
168 return Optional.of(logMessage);
169 }
170
171 @Override
172 public String toString() {
173 return String.format(
174 "Created: %d, Updated: %d, Unchanged: %d, Errors: %d",
175 created, updated, unchanged, errors.size());
176 }
177 }