diff --git a/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/RowProcessor.java b/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/RowProcessor.java index 90ef2f790362..b66d702fa6fb 100644 --- a/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/RowProcessor.java +++ b/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/RowProcessor.java @@ -140,6 +140,39 @@ public final int processRow(Cell[] row, long rowNumber) throws Throwable { protected abstract int processRow(String[] values, long rowNumber) throws Throwable; + public final boolean preCheck(Cell[] row) throws Throwable { + if (row != null && row.length > 0) { + boolean commented = false; + String[] values = new String[row.length]; + for (int i = 0; i < row.length; i++) { + String value = ""; + if (row[i] != null && !commented) { + value = row[i].getContents(); + if (value != null) { + if (useComments && (acceptCommentsIndex == null || acceptCommentsIndex == i)) { + int commentPos = value.indexOf(commentChar); + if (commentPos >= 0) { + value = trimValues ? value.substring(0, commentPos).trim() : value.substring(0, commentPos); + commented = true; + } else { + value = trimValues ? value.trim() : value; + } + } else { + value = trimValues ? value.trim() : value; + } + } + } + values[i] = value; + } + return preCheck(values); + } + return true; + } + + protected boolean preCheck(String[] values) throws Throwable { + return false; // stop + } + protected boolean processHeaderRow(String[] values) throws Throwable { return false; } diff --git a/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/XlsImporterBase.java b/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/XlsImporterBase.java index dd415d022e68..99a27e5d137e 100644 --- a/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/XlsImporterBase.java +++ b/common/src/main/java/org/phoenixctms/ctsms/fileprocessors/xls/XlsImporterBase.java @@ -58,6 +58,12 @@ protected long readRows(XlsImporterContext context, String encoding, RowProcesso rowCount += processor.processRow(row, lineNumber); } lineNumber++; + for (int i = 1; i < sheetRowCount; i++) { + row = sheet.getRow(i); + if (!processor.preCheck(row)) { + break; + } + } for (int i = 1; i < sheetRowCount; i++) { row = sheet.getRow(i); rowCount += processor.processRow(row, lineNumber); diff --git a/core/src/exec/java/org/phoenixctms/ctsms/executable/xls/EcrfFieldRowProcessor.java b/core/src/exec/java/org/phoenixctms/ctsms/executable/xls/EcrfFieldRowProcessor.java index ca6765a3097e..dae8d3c595a9 100644 --- a/core/src/exec/java/org/phoenixctms/ctsms/executable/xls/EcrfFieldRowProcessor.java +++ b/core/src/exec/java/org/phoenixctms/ctsms/executable/xls/EcrfFieldRowProcessor.java @@ -57,6 +57,7 @@ public class EcrfFieldRowProcessor extends RowProcessor { private int jsValueExpressionColumnIndex; private int jsOutputExpressionColumnIndex; private int notifyColumnIndex; + private boolean noRefs; private HashMap>> ecrfFieldMap; private HashMap> ecrfMap; @Autowired @@ -72,6 +73,7 @@ public EcrfFieldRowProcessor() { super(); filterDupes = false; acceptCommentsIndex = 0; + noRefs = true; ecrfFieldMap = new HashMap>>(); ecrfMap = new HashMap>(); } @@ -224,6 +226,7 @@ public void init() throws Throwable { jsValueExpressionColumnIndex = JS_VALUE_EXPRESSION_COLUMN_INDEX; jsOutputExpressionColumnIndex = JS_OUTPUT_EXPRESSION_COLUMN_INDEX; notifyColumnIndex = NOTIFY_COLUMN_INDEX; + noRefs = true; ecrfFieldMap.clear(); ecrfMap.clear(); ((XlsImporter) context.getImporter()).loadInputFields(context); @@ -271,10 +274,12 @@ protected int processRow(String[] values, long rowNumber) throws Throwable { ECRFField ecrfField = null; if (ecrf != null) { try { - if (CommonUtil.isEmptyString(ref)) { + if (noRefs) { ecrfField = eCRFFieldDao.findByEcrfSectionPosition(ecrf.getId(), section, position).iterator().next(); } else { - ecrfField = eCRFFieldDao.findByEcrfRef(ecrf.getId(), ref).iterator().next(); + if (!CommonUtil.isEmptyString(ref)) { + ecrfField = eCRFFieldDao.findByEcrfRef(ecrf.getId(), ref).iterator().next(); + } } } catch (NoSuchElementException e) { } @@ -340,4 +345,10 @@ protected boolean testNotNullRowFields(String[] values, long rowNumber) { } return true; } + + @Override + protected boolean preCheck(String[] values) throws Throwable { + noRefs &= CommonUtil.isEmptyString(getRef(values)); + return noRefs; + } }