View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
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   * Class that serves as a manifest for an import batch,
33   * detailing how many items were created and updated,
34   * and how many failed.
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       * Constructor.
47       *
48       * @param logMessage a log message
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       * Increment the creation count.
74       */
75      public void addCreated() {
76          created++;
77      }
78  
79      /**
80       * Increment the update count.
81       */
82      public void addUpdated() {
83          updated++;
84      }
85  
86      /**
87       * Increment the unchanged count.
88       */
89      public void addUnchanged() {
90          unchanged++;
91      }
92  
93      /**
94       * @return returns the number of created items
95       */
96      public int getCreated() {
97          return created;
98      }
99  
100     /**
101      * @return returns the number of updated items
102      */
103     public int getUpdated() {
104         return updated;
105     }
106 
107     /**
108      * @return returns the number of unchanged items
109      */
110     public int getUnchanged() {
111         return unchanged;
112     }
113 
114     /**
115      * @return the number of errored item imports
116      */
117     public int getErrored() {
118         return errors.size();
119     }
120 
121     /**
122      * @return the import errors
123      */
124     public Map<String, String> getErrors() {
125         return errors;
126     }
127 
128     /**
129      * Indicate that importing the item with the given id
130      * failed with the given error.
131      *
132      * @param item  The item
133      * @param error The error that occurred
134      */
135     public void addError(String item, String error) {
136         errors.put(item, error);
137     }
138 
139     /**
140      * Indicated whether the import succeeded at all,
141      * in terms of items created/updated.
142      *
143      * @return returns whether the import succeeded
144      */
145     public boolean hasDoneWork() {
146         return created > 0 || updated > 0;
147     }
148 
149     /**
150      * @return returns the number of items that were either created or updated.
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 }