Author: nmalin
Date: Sat Dec 23 13:42:15 2017 New Revision: 1819147 URL: http://svn.apache.org/viewvc?rev=1819147&view=rev Log: Improved: Import file with data-file, add start-line to escape the header column line (OFBIZ-10108) When you load a CSV file some time with a data-file definition, if it contains some header column you can use the format attribute of you column. Example: ref;date 100;2017-10-10 200;2017-12-11 This definition failed : <data-file name="MyCsvTest" separator-style="delimited" type-code="text" delimiter=";"> <record name="Test"> <field name="ref" type="String" position="1"/> <field name="date" type="CustomTimestamp" format="yyyyy-MM-dd" position="2"/> </record> </data-file> You need to read the date as string and after convert it on your code to escape the first line. To solve it I added a new attribute start-line on data-file element : <data-file name="MyCsvTest" separator-style="delimited" type-code="text" *start-line="1"* delimiter=";"> <record name="Test"> <field name="ref" type="String" position="1"/> <field name="date" type="CustomTimestamp" format="yyyyy-MM-dd" position="2"/> </record> </data-file> Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/dtd/datafiles.xsd ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/dtd/datafiles.xsd URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/dtd/datafiles.xsd?rev=1819147&r1=1819146&r2=1819147&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/datafile/dtd/datafiles.xsd (original) +++ ofbiz/ofbiz-framework/trunk/framework/datafile/dtd/datafiles.xsd Sat Dec 23 13:42:15 2017 @@ -39,6 +39,7 @@ under the License. <xs:attribute name="sender"/> <xs:attribute name="receiver"/> <xs:attribute name="delimiter"/> + <xs:attribute name="start-line"/> <xs:attribute name="text-delimiter"/> <xs:attribute name="record-length"/> <xs:attribute name="separator-style" use="required"> Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java?rev=1819147&r1=1819146&r2=1819147&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java Sat Dec 23 13:42:15 2017 @@ -48,6 +48,9 @@ public class ModelDataFile { /** The length in bytes of a single record, ONLY if it uses fixed length records */ public int recordLength = -1; + /** Start the file read at line */ + public int startLine = 0; + /** The delimiter used in the file, if delimiter separated fields are used */ public char delimiter = '|'; @@ -121,6 +124,14 @@ public class ModelDataFile { this.delimiter = delimiter; } + public int getStartLine() { + return startLine; + } + + public void setStartLine(int startLine) { + this.startLine = startLine; + } + public String getSeparatorStyle() { return separatorStyle; } Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java?rev=1819147&r1=1819146&r2=1819147&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java Sat Dec 23 13:42:15 2017 @@ -83,6 +83,10 @@ public final class ModelDataFileReader { if (tempStr != null && tempStr.length() == 1) { dataFile.delimiter = tempStr.charAt(0); } + tempStr = dataFileElement.getAttribute("start-line"); + if (tempStr != null) { + dataFile.startLine = Integer.valueOf(tempStr); + } tempStr = UtilXml.checkEmpty(dataFileElement.getAttribute("text-delimiter")); if (UtilValidate.isNotEmpty(tempStr)) { Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java?rev=1819147&r1=1819146&r2=1819147&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java Sat Dec 23 13:42:15 2017 @@ -74,6 +74,14 @@ public class RecordIterator { catch (Exception e) { throw new DataFileException("UTF-8 is not supported"); } + //move the cursor to the good start line + try { + for (int i = 0; i < modelDataFile.startLine; i++) { + br.readLine(); + } + } catch (IOException e) { + throw new DataFileException("Impossible to read the buffer"); + } // get the line seeded this.getNextLine(); } |
Free forum by Nabble | Edit this page |