1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.data.impl;
17
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.List;
23
24 import org.kuali.common.impex.ProducerUtils;
25 import org.kuali.common.impex.data.MpxHeaderData;
26 import org.kuali.common.impex.data.SqlProducer;
27 import org.kuali.common.impex.model.Column;
28 import org.kuali.common.impex.model.DataType;
29
30
31
32
33 public abstract class AbstractSqlProducer implements SqlProducer {
34
35 protected final static String OUTPUT_DATE_FORMAT = "yyyyMMddHHmmss";
36 public static final int DEFAULT_BATCH_ROW_COUNT_LIMIT = 50;
37 public static final int DEFAULT_DATA_SIZE_LIMIT = 50 * 1024;
38
39 int batchRowCountLimit = DEFAULT_BATCH_ROW_COUNT_LIMIT;
40 int batchDataSizeLimit = DEFAULT_DATA_SIZE_LIMIT;
41
42 protected boolean batchLimitReached(int rows, int length) {
43 if (rows > getBatchRowCountLimit()) {
44 return true;
45 } else if (length > getBatchDataSizeLimit()) {
46 return true;
47 }
48
49 return false;
50 }
51
52 protected abstract String getEscapedValue(Column column, String token);
53
54 protected List<DataBean> buildRowData(List<Column> columns, String[] tokens, MpxHeaderData headerData) {
55 List<DataBean> result = new ArrayList<DataBean>();
56
57
58
59 List<Column> sortedColumns = new ArrayList<Column>(columns.size());
60 for (String colName : headerData.getColumnNames()) {
61
62 Column foundColumn = null;
63 for (Column c : columns) {
64 if (c.getName().equals(colName)) {
65 foundColumn = c;
66 }
67 }
68
69 if (foundColumn == null) {
70 throw new RuntimeException("No column definition found for column name from header: " + colName);
71 }
72
73 sortedColumns.add(foundColumn);
74 }
75
76
77 for (int i = 0; i < tokens.length; i++) {
78 result.add(processToken(sortedColumns.get(i), tokens[i]));
79 }
80 return result;
81 }
82
83 public DataBean processToken(Column column, String token) {
84 DataBean result = new DataBean();
85
86 result.setColumn(column);
87
88 if (token == null) {
89 result.setValue(null);
90 result.setDateValue(null);
91 } else if (ProducerUtils.isDateType(column.getType())) {
92 Date parsedDate = getDate(token);
93 result.setValue(null);
94 result.setDateValue(parsedDate);
95 } else if (isDataTypeTextType(column.getType())) {
96 result.setValue(getEscapedValue(column, token));
97 result.setDateValue(null);
98 } else {
99 result.setDateValue(null);
100 result.setValue(token);
101 }
102
103 return result;
104 }
105
106 protected boolean isDataTypeTextType(DataType type) {
107 return type == DataType.STRING || type == DataType.CLOB || type == DataType.CHAR;
108 }
109
110 protected Date getDate(String token) {
111 SimpleDateFormat sdf = new SimpleDateFormat(ParseUtils.MPX_DATE_FORMAT);
112 try {
113 return sdf.parse(token);
114 } catch (ParseException e) {
115 throw new IllegalArgumentException("Cannot parse [" + token + "] using format [" + ParseUtils.MPX_DATE_FORMAT + "]");
116 }
117 }
118
119 @Override
120 public int getBatchRowCountLimit() {
121 return batchRowCountLimit;
122 }
123
124 @Override
125 public int getBatchDataSizeLimit() {
126 return batchDataSizeLimit;
127 }
128
129 @Override
130 public void setBatchDataSizeLimit(int batchDataSizeLimit) {
131 this.batchDataSizeLimit = batchDataSizeLimit;
132 }
133
134 @Override
135 public void setBatchRowCountLimit(int batchRowCountLimit) {
136 this.batchRowCountLimit = batchRowCountLimit;
137 }
138
139 }