aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/iciql/ValidationRemark.java
blob: 33320abc4124165cef4bb1eeaa11bb5fd2fe1bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2004-2011 H2 Group.
 * Copyright 2011 James Moger.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iciql;

import com.iciql.TableDefinition.FieldDefinition;
import com.iciql.TableInspector.ColumnInspector;
import com.iciql.util.StringUtils;

/**
 * A validation remark is a result of running a model validation. Each remark
 * has a level, associated component (schema, table, column, index), and a
 * message.
 */
public class ValidationRemark {

	/**
	 * The validation message level.
	 */
	public static enum Level {
		CONSIDER, WARN, ERROR;
	}

	public final Level level;
	public final String table;
	public final String fieldType;
	public final String fieldName;
	public final String message;

	private ValidationRemark(Level level, String table, String type, String message) {
		this.level = level;
		this.table = table;
		this.fieldType = type;
		this.fieldName = "";
		this.message = message;
	}

	private ValidationRemark(Level level, String table, FieldDefinition field, String message) {
		this.level = level;
		this.table = table;
		this.fieldType = field.dataType;
		this.fieldName = field.columnName;
		this.message = message;
	}

	private ValidationRemark(Level level, String table, ColumnInspector col, String message) {
		this.level = level;
		this.table = table;
		this.fieldType = col.type;
		this.fieldName = col.name;
		this.message = message;
	}

	public static ValidationRemark consider(String table, String type, String message) {
		return new ValidationRemark(Level.CONSIDER, table, type, message);
	}

	public static ValidationRemark consider(String table, ColumnInspector col, String message) {
		return new ValidationRemark(Level.CONSIDER, table, col, message);
	}

	public static ValidationRemark warn(String table, ColumnInspector col, String message) {
		return new ValidationRemark(Level.WARN, table, col, message);
	}

	public static ValidationRemark warn(String table, String type, String message) {
		return new ValidationRemark(Level.WARN, table, type, message);
	}

	public static ValidationRemark error(String table, ColumnInspector col, String message) {
		return new ValidationRemark(Level.ERROR, table, col, message);
	}

	public static ValidationRemark error(String table, String type, String message) {
		return new ValidationRemark(Level.ERROR, table, type, message);
	}

	public static ValidationRemark error(String table, FieldDefinition field, String message) {
		return new ValidationRemark(Level.ERROR, table, field, message);
	}

	public ValidationRemark throwError(boolean throwOnError) {
		if (throwOnError && isError()) {
			throw new IciqlException(toString());
		}
		return this;
	}

	public boolean isError() {
		return level.equals(Level.ERROR);
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(StringUtils.pad(level.name(), 9, " ", true));
		sb.append(StringUtils.pad(table, 25, " ", true));
		sb.append(StringUtils.pad(fieldName, 20, " ", true));
		sb.append(' ');
		sb.append(message);
		return sb.toString();
	}

	public String toCSVString() {
		StringBuilder sb = new StringBuilder();
		sb.append(level.name()).append(',');
		sb.append(table).append(',');
		sb.append(fieldType).append(',');
		sb.append(fieldName).append(',');
		sb.append(message);
		return sb.toString();
	}

}