1 4 package com.openedit.store.excelconvert; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 import java.util.List ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.apache.poi.hssf.usermodel.HSSFCell; 15 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 16 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 17 18 import com.openedit.store.CatalogConverter; 19 import com.openedit.store.Store; 20 import com.openedit.store.StoreException; 21 import com.openedit.util.OutputFiller; 22 23 26 public abstract class ExcelConverter extends CatalogConverter 27 { 28 private static final Log log = LogFactory.getLog(ExcelConverter.class); 29 protected File fieldSourceSpreadsheet; 30 public ExcelConverter() 31 { 32 super(); 33 } 34 35 public synchronized boolean convert(Store inStore, List inLog) throws StoreException 36 { 37 inLog.add("Starting converter with " + getClass().getName()); 38 File rootUploadDir = new File ( inStore.getStoreDirectory(), "/upload/inventory.xls"); 40 41 if ( !rootUploadDir.exists() ) 42 { 43 log.error("No input found"); 44 inLog.add("No input found"); 45 return false; 46 } 47 File workingfile = new File ( inStore.getStoreDirectory(), "/tmp/inventory.xls"); 48 workingfile.getParentFile().mkdirs(); 49 try 50 { 51 new OutputFiller().fill(rootUploadDir,workingfile); 52 } catch ( Exception ex ) 53 { 54 log.error( ex ); 55 return false; 56 } 57 inLog.add("Starting inventory import. Using this input: " + workingfile.getName()); 59 60 FileInputStream in = null; 61 try 62 { 63 in = new FileInputStream (workingfile); 64 POIFSFileSystem fs = new POIFSFileSystem(in); 66 HSSFWorkbook wb = new HSSFWorkbook(fs); 67 processWorkbook(inStore, inLog, wb ); 68 inStore.clearProducts(); 69 } 70 catch ( Exception ex) 71 { 72 throw new StoreException(ex); 73 } 74 finally 75 { 76 try 77 { 78 in.close(); 79 }catch ( Exception ex) 80 {} 81 } 82 83 File completed = new File ( rootUploadDir.getParentFile(),"completed"); 84 completed.mkdirs(); 85 String format = SimpleDateFormat.getDateTimeInstance().format(new Date ()); 86 format = format.replace('/','-'); 87 format = format.replace(':','-'); 88 format = format.replace(' ','-'); 89 format = format.replace(',','-'); 90 91 File done = new File ( completed,"inventory-" + format + ".xls" ); 92 done.delete(); rootUploadDir.renameTo(done); 94 inLog.add("Completed conversion"); 95 return true; 96 } 97 protected String toString(HSSFCell inCell) 98 { 99 if ( inCell == null) 100 { 101 return null; 102 } 103 if ( inCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) 104 { 105 double d = inCell.getNumericCellValue(); 106 double rem = d % 1; 107 if ( rem == 0) 108 { 109 return String.valueOf((int)d); 110 } 111 return String.valueOf(d); 112 } 113 else if ( inCell.getCellType() == HSSFCell.CELL_TYPE_BLANK ) 114 { 115 return null; 116 } 117 else 118 { 119 String val = inCell.getStringCellValue(); 120 if ( val != null) 121 { 122 val = val.trim(); 123 } 124 return val; 125 } 126 } 127 protected abstract void processWorkbook(Store inStore, List inLog, HSSFWorkbook inWorkbook ) 128 throws Exception ; 129 } 130 | Popular Tags |