KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > excelconvert > ExcelConverter


1 /*
2  * Created on Sep 23, 2004
3  */

4 package com.openedit.store.excelconvert;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.List JavaDoc;
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 /**
24  * @author Matthew Avery, mavery@einnovation.com
25  */

26 public abstract class ExcelConverter extends CatalogConverter
27 {
28     private static final Log log = LogFactory.getLog(ExcelConverter.class);
29     protected File JavaDoc fieldSourceSpreadsheet;
30     public ExcelConverter()
31     {
32         super();
33     }
34
35     public synchronized boolean convert(Store inStore, List JavaDoc inLog) throws StoreException
36     {
37         inLog.add("Starting converter with " + getClass().getName());
38         //copy over the /upload/inventory.xls file
39
File JavaDoc rootUploadDir = new File JavaDoc( 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 JavaDoc workingfile = new File JavaDoc( inStore.getStoreDirectory(), "/tmp/inventory.xls");
48         workingfile.getParentFile().mkdirs();
49         try
50         {
51             new OutputFiller().fill(rootUploadDir,workingfile);
52         } catch ( Exception JavaDoc ex )
53         {
54             log.error( ex );
55             return false;
56         }
57         //TODO: Mark all items as 0 inventory
58
inLog.add("Starting inventory import. Using this input: " + workingfile.getName());
59         
60         FileInputStream JavaDoc in = null;
61         try
62         {
63             in = new FileInputStream JavaDoc(workingfile);
64             //productDirectory().mkdirs();
65
POIFSFileSystem fs = new POIFSFileSystem(in);
66             HSSFWorkbook wb = new HSSFWorkbook(fs);
67             processWorkbook(inStore, inLog, wb );
68             inStore.clearProducts();
69         }
70         catch ( Exception JavaDoc ex)
71         {
72             throw new StoreException(ex);
73         }
74         finally
75         {
76             try
77             {
78                 in.close();
79             }catch ( Exception JavaDoc ex)
80             {}
81         }
82         
83         File JavaDoc completed = new File JavaDoc( rootUploadDir.getParentFile(),"completed");
84         completed.mkdirs();
85         String JavaDoc format = SimpleDateFormat.getDateTimeInstance().format(new Date JavaDoc());
86         format = format.replace('/','-');
87         format = format.replace(':','-');
88         format = format.replace(' ','-');
89         format = format.replace(',','-');
90         
91         File JavaDoc done = new File JavaDoc( completed,"inventory-" + format + ".xls" );
92         done.delete(); //just in case
93
rootUploadDir.renameTo(done);
94         inLog.add("Completed conversion");
95         return true;
96     }
97     protected String JavaDoc 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 JavaDoc 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 JavaDoc inLog, HSSFWorkbook inWorkbook )
128         throws Exception JavaDoc;
129 }
130
Popular Tags