You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidationRemark.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql;
  18. import com.iciql.TableDefinition.FieldDefinition;
  19. import com.iciql.TableInspector.ColumnInspector;
  20. import com.iciql.util.StringUtils;
  21. /**
  22. * A validation remark is a result of running a model validation. Each remark
  23. * has a level, associated component (schema, table, column, index), and a
  24. * message.
  25. */
  26. public class ValidationRemark {
  27. /**
  28. * The validation message level.
  29. */
  30. public static enum Level {
  31. CONSIDER, WARN, ERROR;
  32. }
  33. private Level level;
  34. private String table;
  35. private String fieldType;
  36. private String fieldName;
  37. private String message;
  38. private ValidationRemark(Level level, String table, String type, String message) {
  39. this.level = level;
  40. this.table = table;
  41. this.fieldType = type;
  42. this.fieldName = "";
  43. this.message = message;
  44. }
  45. private ValidationRemark(Level level, String table, FieldDefinition field, String message) {
  46. this.level = level;
  47. this.table = table;
  48. this.fieldType = field.dataType;
  49. this.fieldName = field.columnName;
  50. this.message = message;
  51. }
  52. private ValidationRemark(Level level, String table, ColumnInspector col, String message) {
  53. this.level = level;
  54. this.table = table;
  55. this.fieldType = col.type;
  56. this.fieldName = col.name;
  57. this.message = message;
  58. }
  59. public static ValidationRemark consider(String table, String type, String message) {
  60. return new ValidationRemark(Level.CONSIDER, table, type, message);
  61. }
  62. public static ValidationRemark consider(String table, ColumnInspector col, String message) {
  63. return new ValidationRemark(Level.CONSIDER, table, col, message);
  64. }
  65. public static ValidationRemark warn(String table, ColumnInspector col, String message) {
  66. return new ValidationRemark(Level.WARN, table, col, message);
  67. }
  68. public static ValidationRemark warn(String table, String type, String message) {
  69. return new ValidationRemark(Level.WARN, table, type, message);
  70. }
  71. public static ValidationRemark error(String table, ColumnInspector col, String message) {
  72. return new ValidationRemark(Level.ERROR, table, col, message);
  73. }
  74. public static ValidationRemark error(String table, String type, String message) {
  75. return new ValidationRemark(Level.ERROR, table, type, message);
  76. }
  77. public static ValidationRemark error(String table, FieldDefinition field, String message) {
  78. return new ValidationRemark(Level.ERROR, table, field, message);
  79. }
  80. public ValidationRemark throwError(boolean throwOnError) {
  81. if (throwOnError && isError()) {
  82. throw new IciqlException(toString());
  83. }
  84. return this;
  85. }
  86. public boolean isError() {
  87. return level.equals(Level.ERROR);
  88. }
  89. public Level getLevel() {
  90. return level;
  91. }
  92. public String toString() {
  93. StringBuilder sb = new StringBuilder();
  94. sb.append(StringUtils.pad(level.name(), 9, " ", true));
  95. sb.append(StringUtils.pad(table, 25, " ", true));
  96. sb.append(StringUtils.pad(fieldName, 20, " ", true));
  97. sb.append(' ');
  98. sb.append(message);
  99. return sb.toString();
  100. }
  101. public String toCSVString() {
  102. StringBuilder sb = new StringBuilder();
  103. sb.append(level.name()).append(',');
  104. sb.append(table).append(',');
  105. sb.append(fieldType).append(',');
  106. sb.append(fieldName).append(',');
  107. sb.append(message);
  108. return sb.toString();
  109. }
  110. }