Skip to content

Commit

Permalink
Merge pull request #11 from erssebaggala/get_datetime_from_filename
Browse files Browse the repository at this point in the history
Get export date time from filename
  • Loading branch information
erssebaggala committed Jul 26, 2019
2 parents 352bda7 + 8c74106 commit 748918b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
Binary file modified dist/boda-ztexlscmparser.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.bodastage.cm</groupId>
<artifactId>boda-ztexlscmparser</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
Expand All @@ -39,6 +41,7 @@
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
Expand All @@ -51,7 +54,7 @@ public class ZTEXLSCMParser {
*
* Since 1.3.0
*/
final static String VERSION = "1.2.3";
final static String VERSION = "1.2.4";

private static final Logger LOGGER = LoggerFactory.getLogger(ZTEXLSCMParser.class);

Expand Down Expand Up @@ -392,6 +395,14 @@ public String getFileBasename(String filename){
* @throws UnsupportedEncodingException
*/
public void parse() throws XMLStreamException, FileNotFoundException, UnsupportedEncodingException, IOException, InvalidFormatException {

//Get date from file name ...YYYYMMDDHHMMSS.xlsx
Pattern p = Pattern.compile("(\\d+)\\.\\D+$");
Matcher m = p.matcher(dataSource);
if(m.find()){
dateTime = m.group(1);
}

//Extract parameters
if (parserState == ParserStates.EXTRACTING_PARAMETERS) {
processFileOrDirectory();
Expand Down Expand Up @@ -678,6 +689,7 @@ public void parseFile(String fileName ) throws FileNotFoundException, IOExceptio
//String cellValue = sheetRowCell.getStringCellValue();
String cellValue = "";


if(null == sheetRowCell.getCellTypeEnum()){
cellValue = "";
}else {
Expand All @@ -686,7 +698,10 @@ public void parseFile(String fileName ) throws FileNotFoundException, IOExceptio
cellValue = sheetRowCell.getStringCellValue();
break;
case NUMERIC:
cellValue = Double.toString(sheetRowCell.getNumericCellValue());
Double value = sheetRowCell.getNumericCellValue();
Long longValue = value.longValue();
cellValue = new String(longValue.toString());
//cellValue = Double.toString(sheetRowCell.getNumericCellValue());
break;
default:
cellValue = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -55,17 +59,25 @@ public void testGeneralParsing(){
int i = 0;
String st;
while ((st = br.readLine()) != null) {

//Repalce the date with YYYY-MM-DD HH:MI:SS as the parser generates
//as unique datetime whenever it runs
//as unique datetime whenever it runs.
//Don't replace anything in the header
if(i == 0) {
csvResult[i] = st;
i++;
continue;
}
String c [] = st.split(",");
c[1] = "YYYY-MM-DD HH:MI:SS";

csvResult[i] = "";
for(int idx =0; idx < c.length; idx++){
if( idx > 0) csvResult[idx] += ",";
if( idx > 0) csvResult[i] += ",";
csvResult[i] += c[idx];
}
i++;

if(i++ == 2) break; //Stop on 3rd iteration
}

assertTrue(Arrays.equals(expectedResult, csvResult));
Expand All @@ -76,7 +88,58 @@ public void testGeneralParsing(){
Logger.getLogger(ZTEXLSCMParser.class.getName()).log(Level.SEVERE, null, ex);
}


}

public void testDateTimeInFileName(){

ClassLoader classLoader = getClass().getClassLoader();
File inFile = new File(classLoader.getResource("templatedata_20190620132000.xlsx").getFile());
String inputFile = inFile.getAbsolutePath();

String outputFolder = System.getProperty("java.io.tmpdir");

ZTEXLSCMParser parser = new ZTEXLSCMParser();

String[] args = { "-i", inputFile, "-o", outputFolder};

parser.main(args);

String expectedResult [] = {
"FileName,varDateTime,NeType,TemplateType,TemplateVersion,DataType,SomeMO1Param1,SomeMO1Param2,SomeMO1Param3",
"templatedata_20190620132000.xlsx,20190620132000,Multi-mode Controller,Plan,V0123,tech_radio,1,2,3",
"templatedata_20190620132000.xlsx,20190620132000,Multi-mode Controller,Plan,V0123,tech_radio,4,5,6"
};

try{
String csvFile = outputFolder + File.separator + "SomeMO1.csv";

BufferedReader br = new BufferedReader(new FileReader(csvFile));
String csvResult [] = new String[3];

int i = 0;
String st;
while ((st = br.readLine()) != null) {
csvResult[i] = st;
if(i++ == 2) break; //Stop on 3rd iteration

}

assertTrue(Arrays.equals(expectedResult, csvResult));

}catch(FileNotFoundException ex){
Logger.getLogger(ZTEXLSCMParser.class.getName()).log(Level.SEVERE, null, ex);
}catch(IOException ex){
Logger.getLogger(ZTEXLSCMParser.class.getName()).log(Level.SEVERE, null, ex);
}

//AfterTest cleanup. This should be in @AfterEachTest
// Path path = FileSystems.getDefault()
// .getPath(outputFolder + File.separator + "SomeMO1.csv");
// try {
// Files.delete(path);
// } catch (IOException ex) {
// Logger.getLogger(ZTEXLSCMParserTest.class.getName()).log(Level.SEVERE, null, ex);
// }

}
}
Binary file modified src/test/resources/templatedata.xlsx
Binary file not shown.
Binary file not shown.

0 comments on commit 748918b

Please sign in to comment.