/source/java/ee/webmedia/alfresco/dora/ImportUtil.java

https://bitbucket.org/smitdevel/delta · Java · 295 lines · 196 code · 44 blank · 55 comment · 31 complexity · f3757c8257733997faf77b5945f6412e MD5 · raw file

  1. package ee.webmedia.alfresco.dora;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.nio.charset.Charset;
  9. import java.sql.Timestamp;
  10. import java.text.DateFormat;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Arrays;
  14. import java.util.Date;
  15. import com.csvreader.CsvReader;
  16. import com.csvreader.CsvWriter;
  17. public class ImportUtil {
  18. private static final String TEXT_NULL = "NULL";
  19. private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
  20. private static final DateFormat DATE_TIME_SHORT_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm");
  21. private static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  22. private static final DateFormat TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  23. public static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
  24. public static final Charset CHARSET_ISO = Charset.forName("ISO-8859-4");
  25. static {
  26. DATE_FORMAT.setLenient(false);
  27. }
  28. public static void checkMandatory(CsvReader reader, int... cols) throws ImportValidationException, IOException {
  29. for (int col : cols) {
  30. if (reader.get(col - 1).isEmpty()) {
  31. error("%s must be valued", reader, col);
  32. }
  33. }
  34. }
  35. public static void checkInteger(CsvReader reader, int... cols) throws ImportValidationException, IOException {
  36. for (int col : cols) {
  37. try {
  38. String value = reader.get(col - 1);
  39. if (!value.isEmpty()) {
  40. Integer.parseInt(value);
  41. }
  42. } catch (NumberFormatException e) {
  43. error("%s must be integer", reader, col);
  44. }
  45. }
  46. }
  47. public static void checkDate(CsvReader reader, int... cols) throws ImportValidationException, IOException {
  48. for (int col : cols) {
  49. try {
  50. String value = reader.get(col - 1);
  51. if (!value.isEmpty()) {
  52. DATE_FORMAT.parse(value);
  53. }
  54. } catch (ParseException e) {
  55. error("%s date must be in format dd.mm.yyyy", reader, col);
  56. }
  57. }
  58. }
  59. public static void checkAnyOf(CsvReader reader, String[] choices, int... cols) throws ImportValidationException, IOException {
  60. for (int col : cols) {
  61. if (Arrays.binarySearch(choices, reader.get(col - 1)) < 0) {
  62. error("Unknown %s", reader, col);
  63. }
  64. }
  65. }
  66. private static void error(String errorMsg, CsvReader reader, int col) throws ImportValidationException, IOException {
  67. throw new ImportValidationException(errorMsg, reader, col - 1);
  68. }
  69. public static String getString(CsvReader reader, int col) throws IOException {
  70. String value = reader.get(col - 1);
  71. return value.isEmpty() || value.equals(TEXT_NULL) ? null : value.trim();
  72. }
  73. public static Integer getInteger(CsvReader reader, int col) throws IOException {
  74. String value = reader.get(col - 1);
  75. return value.isEmpty() || value.equals(TEXT_NULL) ? null : Integer.valueOf(value);
  76. }
  77. public static java.sql.Date getSqlDate(CsvReader reader, int col) throws IOException {
  78. final Date date = getDate(reader.get(col - 1));
  79. return date == null ? null : new java.sql.Date(date.getTime());
  80. }
  81. public static java.sql.Date getSqlDateTS(CsvReader reader, int col) throws IOException {
  82. final Date date = getTimestamp(reader.get(col - 1));
  83. return date == null ? null : new java.sql.Date(date.getTime());
  84. }
  85. public static Date getDate(CsvReader reader, int col) throws IOException {
  86. return getDate(reader.get(col - 1));
  87. }
  88. public static Date getDate(String date) {
  89. return parseDate(date, DATE_FORMAT);
  90. }
  91. public static Date getDateTime(CsvReader reader, int col) throws IOException {
  92. return getDateTime(reader.get(col - 1));
  93. }
  94. public static Date getDateTime(String dateTime) {
  95. return parseDate(dateTime, DATE_TIME_FORMAT);
  96. }
  97. public static Date getDateTimeShort(String dateTime) {
  98. return parseDate(dateTime, DATE_TIME_SHORT_FORMAT);
  99. }
  100. public static Timestamp getTimestamp(CsvReader reader, int col) throws IOException {
  101. return getTimestamp(reader.get(col - 1));
  102. }
  103. public static Timestamp getTimestamp(String timestamp) {
  104. Date dateTime = parseDate(timestamp, TIMESTAMP_FORMAT);
  105. return dateTime != null ? new Timestamp(dateTime.getTime()) : null;
  106. }
  107. public static String formatDate(Date date) {
  108. return date != null ? DATE_FORMAT.format(date) : "";
  109. }
  110. public static String formatDateTime(Date date) {
  111. return date != null ? DATE_TIME_FORMAT.format(date) : "";
  112. }
  113. private static Date parseDate(String datetime, DateFormat dateFormat) {
  114. try {
  115. return datetime == null || datetime.isEmpty() || TEXT_NULL.equals(datetime) ? null : dateFormat.parse(datetime);
  116. } catch (ParseException e) {
  117. throw new RuntimeException(e);
  118. }
  119. }
  120. /**
  121. * Logs general structure import error.
  122. *
  123. * @param errorsFile The errors log file.
  124. * @param error The error message to be logged.
  125. */
  126. public static void structError(File errorsFile, String error) {
  127. writeError("Structure import failed ", errorsFile, error);
  128. }
  129. /**
  130. * Logs general structure import exception.
  131. *
  132. * @param errorsFile The errors log file.
  133. * @param error The caught exception to be logged.
  134. */
  135. public static void structError(File errorsFile, Exception error) {
  136. if (error instanceof ImportValidationException) {
  137. structError(errorsFile, error.getMessage());
  138. } else {
  139. structError(errorsFile, error.toString());
  140. }
  141. }
  142. /**
  143. * Logs general document import error.
  144. *
  145. * @param errorsFile The errors log file.
  146. * @param error The error message to be logged.
  147. */
  148. public static void docsError(File errorsFile, String error) {
  149. writeError("Document import failed ", errorsFile, error);
  150. }
  151. /**
  152. * Logs general document import exception.
  153. *
  154. * @param errorsFile The errors log file.
  155. * @param error The caught exception to be logged.
  156. */
  157. public static void docsError(File errorsFile, Exception error) {
  158. if (error instanceof ImportValidationException) {
  159. docsError(errorsFile, error.getMessage());
  160. } else {
  161. docsError(errorsFile, error.toString());
  162. }
  163. }
  164. /**
  165. * Logs work-flow import exception.
  166. *
  167. * @param errorsFile The errors log file.
  168. * @param csvFile The CSV which caused the error.
  169. * @param error The caught exception to be logged.
  170. */
  171. public static void workflowError(File errorsFile, File csvFile, Exception error) {
  172. if (error instanceof ImportValidationException) {
  173. writeError(csvFile.getName(), errorsFile, error.getMessage());
  174. } else {
  175. writeError(csvFile.getName(), errorsFile, error.toString());
  176. }
  177. }
  178. /**
  179. * Logs general work-flow import error.
  180. *
  181. * @param errorsFile The errors log file.
  182. * @param error The error message to be logged.
  183. */
  184. public static void workflowError(File errorsFile, String error) {
  185. writeError("Workflow import failed ", errorsFile, error);
  186. }
  187. /**
  188. * Logs general work-flow import exception.
  189. *
  190. * @param errorsFile The errors log file.
  191. * @param error The caught exception to be logged.
  192. */
  193. public static void workflowError(File errorsFile, Exception error) {
  194. if (error instanceof ImportValidationException) {
  195. workflowError(errorsFile, error.getMessage());
  196. } else {
  197. workflowError(errorsFile, error.toString());
  198. }
  199. }
  200. /**
  201. * Logs document import exception.
  202. *
  203. * @param errorsFile The errors log file.
  204. * @param docXmlFile The document XML which caused the error.
  205. * @param error The caught exception to be logged.
  206. */
  207. public static void docsError(File errorsFile, File docXmlFile, Exception error) {
  208. if (error instanceof ImportValidationException) {
  209. writeError(docXmlFile.getName(), errorsFile, error.getMessage());
  210. } else {
  211. writeError(docXmlFile.getName(), errorsFile, error.toString());
  212. }
  213. }
  214. private static void writeError(String prefix, File errorsFile, String error) {
  215. CsvWriter logWriter = null;
  216. try {
  217. logWriter = createLogWriter(errorsFile, true);
  218. logWriter.write(prefix);
  219. logWriter.write(error);
  220. logWriter.endRecord();
  221. } catch (IOException e) {
  222. throw new RuntimeException(e);
  223. } finally {
  224. if (logWriter != null) {
  225. logWriter.close();
  226. }
  227. }
  228. }
  229. /**
  230. * Deletes the provided log file when it exists.
  231. *
  232. * @param logFile The log file to be deleted.
  233. */
  234. public static void dropLogFile(File logFile) {
  235. if (logFile.exists()) {
  236. logFile.delete();
  237. }
  238. }
  239. public static CsvReader createDataReader(File logFile) throws FileNotFoundException {
  240. CsvReader reader = new CsvReader(new BufferedInputStream(new FileInputStream(logFile)), ';', CHARSET_UTF8);
  241. reader.setTrimWhitespace(true);
  242. reader.setSkipEmptyRecords(true);
  243. return reader;
  244. }
  245. public static CsvReader createLogReader(File logFile) throws FileNotFoundException {
  246. return new CsvReader(new BufferedInputStream(new FileInputStream(logFile)), ';', CHARSET_UTF8);
  247. }
  248. public static CsvWriter createLogWriter(File logFile, boolean append) throws FileNotFoundException {
  249. return new CsvWriter(new FileOutputStream(logFile, append), ';', CHARSET_UTF8);
  250. }
  251. }