Subversion Repositories SmartDukaan

Rev

Rev 5885 | Rev 6033 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5885 Rev 6030
Line 10... Line 10...
10
import java.io.FileReader;
10
import java.io.FileReader;
11
import java.io.IOException;
11
import java.io.IOException;
12
import java.text.SimpleDateFormat;
12
import java.text.SimpleDateFormat;
13
import java.util.Date;
13
import java.util.Date;
14
 
14
 
-
 
15
import javax.xml.parsers.DocumentBuilder;
-
 
16
import javax.xml.parsers.DocumentBuilderFactory;
-
 
17
 
15
import org.apache.commons.io.FileUtils;
18
import org.apache.commons.io.FileUtils;
16
import org.apache.http.HttpResponse;
19
import org.apache.http.HttpResponse;
17
import org.apache.http.client.HttpClient;
20
import org.apache.http.client.HttpClient;
18
import org.apache.http.client.methods.HttpPost;
21
import org.apache.http.client.methods.HttpPost;
19
import org.apache.http.entity.mime.MultipartEntity;
22
import org.apache.http.entity.mime.MultipartEntity;
Line 26... Line 29...
26
import org.apache.poi.ss.usermodel.Sheet;
29
import org.apache.poi.ss.usermodel.Sheet;
27
import org.apache.poi.ss.usermodel.Workbook;
30
import org.apache.poi.ss.usermodel.Workbook;
28
import org.apache.struts2.dispatcher.StreamResult;
31
import org.apache.struts2.dispatcher.StreamResult;
29
import org.slf4j.Logger;
32
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
33
import org.slf4j.LoggerFactory;
-
 
34
import org.w3c.dom.NodeList;
31
 
35
 
32
import com.opensymphony.xwork2.ActionSupport;
36
import com.opensymphony.xwork2.ActionSupport;
33
 
37
 
34
/**
38
/**
35
 * @author mandeep
39
 * @author mandeep
Line 42... Line 46...
42
    private static final String ARCHIVE_DIR = "/InventoryReports/";
46
    private static final String ARCHIVE_DIR = "/InventoryReports/";
43
    private static Logger logger = LoggerFactory.getLogger(FileArchiveController.class);
47
    private static Logger logger = LoggerFactory.getLogger(FileArchiveController.class);
44
    
48
    
45
    private static enum FILE_TYPES {
49
    private static enum FILE_TYPES {
46
        SARVOTTAM_INVENTORY_TSV_FILE(2, 175),
50
        SARVOTTAM_INVENTORY_TSV_FILE(2, 175),
47
        SNCA_INVENTORY_XLS_FILE(1, 193);
51
        SNCA_INVENTORY_XML_FILE(1, 193);
48
 
52
 
49
        int type;
53
        int type;
50
        long warehouseId;
54
        long warehouseId;
51
 
55
 
52
        FILE_TYPES(int type, long warehouseId) {
56
        FILE_TYPES(int type, long warehouseId) {
Line 77... Line 81...
77
            // For Sarvottam, convert to xls
81
            // For Sarvottam, convert to xls
78
            if (fileType == FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getType()) {
82
            if (fileType == FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getType()) {
79
                File file = new File(convertToXls(newFile));
83
                File file = new File(convertToXls(newFile));
80
                uploadInventoryFile(file, FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getWarehouseId());
84
                uploadInventoryFile(file, FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getWarehouseId());
81
            }
85
            }
-
 
86
            else if(fileType == FILE_TYPES.SNCA_INVENTORY_XML_FILE.getType()) {
-
 
87
            	File file = new File(convertXmlToXls(newFile));
-
 
88
            	uploadInventoryFile(file, FILE_TYPES.SNCA_INVENTORY_XML_FILE.getWarehouseId());
-
 
89
            }
82
 
90
 
83
            return new StreamResult(new ByteArrayInputStream(
91
            return new StreamResult(new ByteArrayInputStream(
84
                    ("File uploaded successfully at " + sdf.format(new Date()))
92
                    ("File uploaded successfully at " + sdf.format(new Date()))
85
                            .getBytes()));
93
                            .getBytes()));
86
 
94
 
Line 144... Line 152...
144
            logger.error("Error converting file", e);
152
            logger.error("Error converting file", e);
145
            return tsvFile.getName();
153
            return tsvFile.getName();
146
        }
154
        }
147
    }
155
    }
148
 
156
 
-
 
157
    /**
-
 
158
    * @param xmlFile
-
 
159
    */
-
 
160
   private String convertXmlToXls(File xmlFile) {
-
 
161
       try {
-
 
162
           Workbook w = new HSSFWorkbook();
-
 
163
           Sheet sheet=w.createSheet("spreadSheet");
-
 
164
           String convertedFileName = ARCHIVE_DIR + "Converted-"
-
 
165
                   + xmlFile.getName();
-
 
166
           FileOutputStream fileOut = new FileOutputStream(convertedFileName);
-
 
167
           DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
-
 
168
           DocumentBuilder builder = factory.newDocumentBuilder();
-
 
169
           org.w3c.dom.Document document = builder.parse(xmlFile);
-
 
170
           
-
 
171
           NodeList itemNameList = document.getElementsByTagName("DSPDISPNAME");
-
 
172
           NodeList itemQtyList = document.getElementsByTagName("DSPCLAMTA");
-
 
173
           
-
 
174
           for(int i =1; i<=itemNameList.getLength(); i++) {
-
 
175
        	   Row row = sheet.createRow(i);
-
 
176
        	   int j = 0;
-
 
177
        	   Cell cell1 = row.createCell(j++);
-
 
178
        	   cell1.setCellValue(i);
-
 
179
        	   Cell cell2 = row.createCell(j++);
-
 
180
        	   cell2.setCellValue(itemNameList.item(i-1).getFirstChild().getNodeValue());
-
 
181
        	   Cell cell3 = row.createCell(j++);
-
 
182
        	   cell3.setCellValue(itemQtyList.item(i-1).getFirstChild().getNodeValue());
-
 
183
           }
-
 
184
           
-
 
185
           w.write(fileOut);
-
 
186
           return convertedFileName;
-
 
187
       } catch (Exception e) {
-
 
188
           logger.error("Error converting file", e);
-
 
189
           return xmlFile.getName();
-
 
190
       }
-
 
191
   }
-
 
192
    
149
    public File getFile() {
193
    public File getFile() {
150
        return file;
194
        return file;
151
    }
195
    }
152
 
196
 
153
    public void setFile(File file) {
197
    public void setFile(File file) {
Line 161... Line 205...
161
    public void setFileType(int fileType) {
205
    public void setFileType(int fileType) {
162
        this.fileType = fileType;
206
        this.fileType = fileType;
163
    }
207
    }
164
 
208
 
165
    public static void main(String[] args) {
209
    public static void main(String[] args) {
-
 
210
    	FileArchiveController arch = new FileArchiveController();
166
        File file = new File("/home/mandeep/2-2012-08-11-14:58:00.xls");
211
        arch.file = new File("/home/amar/Downloads/StockSummary.xml");
-
 
212
        arch.fileType = 1;
-
 
213
        arch.create();
167
        new FileArchiveController().uploadInventoryFile(file, 175);
214
        //new FileArchiveController().uploadInventoryFile(file, 193);
168
        System.out.println("done");
215
        System.out.println("done");
169
    }
216
    }
170
}
217
}