Project

General

Profile

Download (4.81 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.io.common;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13

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

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

    
23
    private List<IoInfo> errors = new ArrayList<>();
24
    private List<IoInfo> warnings = new ArrayList<>();
25
    private List<IoInfo> exceptions = new ArrayList<>();
26

    
27
    public class IoInfo{
28
        String message;
29
        Exception exception;
30
        String location;
31
        private IoInfo(String msg, Exception e){
32
            this.message = msg;
33
            this.exception = e;
34
        }
35
        private IoInfo(String msg, Exception e, String location){
36
            this.message = msg;
37
            this.exception = e;
38
            this.location = location;
39
        }
40

    
41
        public String getMessage(){
42
            return message;
43
        }
44

    
45
        public Exception getException(){
46
            return exception;
47
        }
48
    }
49

    
50
// ************* GETTERS / SETTERS / ADDERS ***********************/
51

    
52
    public List<IoInfo> getErrors() {return errors;}
53
    public void setErrors(List<IoInfo> ioInfos) {this.errors = ioInfos;}
54
    public void addError(String error) {
55
        errors.add(new IoInfo(error, null));
56
    }
57
    public void addError(String error, Exception e) {
58
        errors.add(new IoInfo(error, e));
59
    }
60
    public void addError(String message, int location) {
61
        errors.add(new IoInfo(message, null, String.valueOf(location)));
62
    }
63

    
64
    public List<IoInfo> getWarnings() {return warnings;}
65
    public void setWarnings(List<IoInfo> warnings) {this.warnings = warnings;}
66
    public void addWarning(String warning) {
67
//       warnings.add(warning.getBytes(StandardCharsets.UTF_8));
68
        warnings.add(new IoInfo(warning, null));
69
    }
70
    public void addWarning(String message, int location) {
71
        warnings.add(new IoInfo(message, null, String.valueOf(location)));
72
    }
73
    public void addWarning(String message, String location) {
74
        warnings.add(new IoInfo(message, null, location));
75
    }
76

    
77
    public List<IoInfo> getExceptions() {return exceptions;}
78
    public void setExceptions(List<IoInfo> exceptions) {this.exceptions = exceptions;}
79
    public void addException(Exception e) {
80
        exceptions.add(new IoInfo(null, e));
81
        setExceptionState();
82
    }
83
    public void addException(Exception e, String message) {
84
        exceptions.add(new IoInfo(message, e));
85
        setExceptionState();
86
    }
87
    public void addException(Exception e, String message, String location) {
88
        exceptions.add(new IoInfo(message, e, location));
89
        setExceptionState();
90
    }
91

    
92
    protected abstract void setExceptionState();
93

    
94
    /**
95
     * Adds an error and aborts the import.
96
     * @param string
97
     */
98
    public void setAborted(String error) {
99
        this.addError(error);
100
        this.setAborted();
101
    }
102

    
103
    public abstract void setAborted();
104

    
105
    /**
106
     * @return
107
     */
108
    public StringBuffer createReport() {
109
        StringBuffer report = new StringBuffer("");
110
        addErrorReport(report, "Errors", errors);
111
        addErrorReport(report, "Exceptions", exceptions);
112
        addErrorReport(report, "Warnings", warnings);
113
        return report;
114
    }
115
//    /**
116
//     * @param report
117
//     * @param string
118
//     * @param warnings2
119
//     */
120
//    private void addWarnings(StringBuffer report, String label, List<String> list) {
121
//        if (!list.isEmpty()){
122
//            report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
123
//            for (String warning : list){
124
//                String str = String.valueOf(warning);
125
//                report.append("\n" + str);
126
//            }
127
//        }
128
//    }
129

    
130
    /**
131
     * @param report
132
     * @param label
133
     * @param list
134
     */
135
    private void addErrorReport(StringBuffer report, String label, List<IoInfo> list) {
136
        if (!list.isEmpty()){
137
            report.append("\n\n" + label + ":\n" + StringUtils.leftPad("", label.length()+1, "="));
138
            for (IoInfo ioInfo : list){
139
                String location = ioInfo.location == null ? "" : (ioInfo.location + ": ");
140
                String message = ioInfo.message != null ? ioInfo.message : ioInfo.exception != null ? ioInfo.exception.getMessage() : "";
141
                message = StringUtils.isBlank(message)? "no message" : message;
142
                Object stacktrace = ioInfo.exception == null? null : ioInfo.exception.getStackTrace();
143
                String available = (stacktrace != null ? " (stacktrace available)" : "");
144
                report.append("\n" + location + message + available);
145
            }
146
        }
147
    }
148
}
(50-50/71)