Project

General

Profile

Download (6.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.common;
10

    
11
import java.io.Serializable;
12
import java.util.ArrayList;
13
import java.util.List;
14

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

    
17
/**
18
 * @author a.mueller
19
 * @date 24.03.2017
20
 *
21
 */
22
public abstract class IoResultBase implements Serializable{
23

    
24
    private static final long serialVersionUID = -2077936463767046918L;
25

    
26
    private List<IoInfo> errors = new ArrayList<>();
27
    private List<IoInfo> warnings = new ArrayList<>();
28
    private List<IoInfo> exceptions = new ArrayList<>();
29

    
30
    public class IoInfo implements Serializable{
31
        private static final long serialVersionUID = -8077358746590123757L;
32
        String message;
33
        Exception exception;
34
        String codeLocation;
35
        String dataLocation;
36
        private IoInfo(String msg, Exception e){
37
            this.message = msg;
38
            this.exception = e;
39
        }
40
//        private IoInfo(String msg, Exception e, String location){
41
//            this.message = msg;
42
//            this.exception = e;
43
//            this.codeLocation = location;
44
//        }
45
        private IoInfo(String msg, Exception e, String codeLocation, String dataLocation){
46
            this.message = msg;
47
            this.exception = e;
48
            this.codeLocation = codeLocation;
49
            this.dataLocation = dataLocation;
50
        }
51

    
52
        public String getMessage(){
53
            return message;
54
        }
55

    
56
        public Exception getException(){
57
            return exception;
58
        }
59
        public String getCodeLocation(){
60
            return codeLocation;
61
        }
62
        public String getDataLocation(){
63
            return dataLocation;
64
        }
65
    }
66

    
67

    
68

    
69
// ************* GETTERS / SETTERS / ADDERS ***********************/
70

    
71
    public List<IoInfo> getErrors() {return errors;}
72
    public void setErrors(List<IoInfo> ioInfos) {this.errors = ioInfos;}
73
    public void addError(String message) {
74
        addError(message, null, getLocationByException());
75
    }
76
    public void addError(String message, Exception e) {
77
        addError(message, e, null, null);
78
    }
79
    public void addError(String message, int location) {
80
        addError(message, null, getLocationByException(), String.valueOf(location));
81
    }
82
    public void addError(String message, String codeLocation) {
83
        addError(message, null, codeLocation, null);
84
    }
85
    public void addError(String message, Exception e, String codeLocation) {
86
        addError(message, e, codeLocation, null);
87
    }
88
    public void addError(String message, Exception e, String codeLocation, String dataLocation) {
89
        errors.add(new IoInfo(message, e, makeLocation(e, codeLocation), dataLocation));
90
    }
91

    
92

    
93
    public List<IoInfo> getWarnings() {return warnings;}
94
    public void setWarnings(List<IoInfo> warnings) {this.warnings = warnings;}
95
    public void addWarning(String message) {
96
        addWarning(message, getLocationByException(), null);
97
    }
98
    public void addWarning(String message, int location) {
99
        addWarning(message, null, String.valueOf(location));
100
    }
101
    public void addWarning(String message, String codeLocation) {
102
        addWarning(message, codeLocation, null);
103
    }
104
    public void addWarning(String message, String codeLocation, String dataLocation) {
105
        warnings.add(new IoInfo(message, null, codeLocation, dataLocation));
106
    }
107

    
108

    
109

    
110
    public List<IoInfo> getExceptions() {return exceptions;}
111
    public void setExceptions(List<IoInfo> exceptions) {this.exceptions = exceptions;}
112
    public void addException(Exception e) {
113
        addException(e, null, null, null);
114
    }
115
    public void addException(Exception e, String message) {
116
        addException(e, message, null, null);
117
    }
118
    public void addException(Exception e, String message, String codeLocation) {
119
        addException(e, message, codeLocation, null);
120
    }
121
    public void addException(Exception e, String message, String codeLocation, String dataLocation) {
122
        exceptions.add(new IoInfo(message, e, makeLocation(e, codeLocation), dataLocation));
123
        setExceptionState();
124
    }
125

    
126

    
127
    /**
128
     * Computes the location string. If location is not null the location
129
     * parameter is returned. If location is <code>null</code> the stacktrace
130
     * is examined and tried to retrieve the location from there
131
     * @param e
132
     * @param location
133
     * @return
134
     */
135
    private String makeLocation(Throwable e, String location) {
136
        if (location == null && e != null){
137
            StackTraceElement[] stackTrace = e.getStackTrace();
138
            if (stackTrace != null && stackTrace.length > 0){
139
                StackTraceElement el = stackTrace[0];
140
                location = locByStackTraceElement(el);
141
            }
142
        }
143
        return location;
144
    }
145
    private String getLocationByException() {
146
        try {
147
            throw new RuntimeException();
148
        } catch (Exception e) {
149
            StackTraceElement st = e.getStackTrace()[2];
150
            return locByStackTraceElement(st);
151
        }
152
    }
153

    
154
    private String locByStackTraceElement(StackTraceElement st) {
155
        return st.getMethodName() + "(" + st.getClassName()+ ":" + st.getLineNumber() + ")";
156
    }
157

    
158
    protected abstract void setExceptionState();
159

    
160
    /**
161
     * Adds an error and aborts the import.
162
     * @param string
163
     */
164
    public void setAborted(String error) {
165
        this.addError(error);
166
        this.setAborted();
167
    }
168

    
169
    public abstract void setAborted();
170

    
171
    /**
172
     * @return
173
     */
174
    public StringBuffer createReport() {
175
        StringBuffer report = new StringBuffer("");
176
        addErrorReport(report, "Errors", errors);
177
        addErrorReport(report, "Exceptions", exceptions);
178
        addErrorReport(report, "Warnings", warnings);
179
        return report;
180
    }
181

    
182

    
183
    /**
184
     * @param report
185
     * @param label
186
     * @param list
187
     */
188
    private void addErrorReport(StringBuffer report, String label, List<IoInfo> list) {
189
        if (!list.isEmpty()){
190
            report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
191
            for (IoInfo ioInfo : list){
192
                String codeLocation = ioInfo.codeLocation == null ? "" : ( "[" + ioInfo.codeLocation + "]");
193
                String dataLocation = ioInfo.dataLocation == null ? "" : (ioInfo.dataLocation + ": ");
194
                String message = ioInfo.message != null ? ioInfo.message : ioInfo.exception != null ? ioInfo.exception.getMessage() : "";
195

    
196
                message = StringUtils.isBlank(message)? "no message" : message;
197
                Object stacktrace = ioInfo.exception == null? null : ioInfo.exception.getStackTrace();
198
                String available = (stacktrace != null ? " (stacktrace available)" : "");
199
                report.append("\n" + dataLocation + message + available + codeLocation);
200
            }
201
        }
202
    }
203
}
    (1-1/1)