Subversion Repositories SmartDukaan

Rev

Rev 5885 | Rev 6033 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5530 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.support.controllers;
5
 
5885 mandeep.dh 6
import java.io.BufferedReader;
5530 mandeep.dh 7
import java.io.ByteArrayInputStream;
8
import java.io.File;
5885 mandeep.dh 9
import java.io.FileOutputStream;
10
import java.io.FileReader;
5530 mandeep.dh 11
import java.io.IOException;
12
import java.text.SimpleDateFormat;
13
import java.util.Date;
14
 
6030 amar.kumar 15
import javax.xml.parsers.DocumentBuilder;
16
import javax.xml.parsers.DocumentBuilderFactory;
17
 
5530 mandeep.dh 18
import org.apache.commons.io.FileUtils;
5885 mandeep.dh 19
import org.apache.http.HttpResponse;
20
import org.apache.http.client.HttpClient;
21
import org.apache.http.client.methods.HttpPost;
22
import org.apache.http.entity.mime.MultipartEntity;
23
import org.apache.http.entity.mime.content.FileBody;
24
import org.apache.http.entity.mime.content.StringBody;
25
import org.apache.http.impl.client.DefaultHttpClient;
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
5530 mandeep.dh 31
import org.apache.struts2.dispatcher.StreamResult;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
6030 amar.kumar 34
import org.w3c.dom.NodeList;
5530 mandeep.dh 35
 
36
import com.opensymphony.xwork2.ActionSupport;
37
 
38
/**
39
 * @author mandeep
40
 * 
41
 */
42
public class FileArchiveController extends ActionSupport {
5885 mandeep.dh 43
    /**
44
     * 
45
     */
46
    private static final String ARCHIVE_DIR = "/InventoryReports/";
47
    private static Logger logger = LoggerFactory.getLogger(FileArchiveController.class);
48
 
49
    private static enum FILE_TYPES {
50
        SARVOTTAM_INVENTORY_TSV_FILE(2, 175),
6030 amar.kumar 51
        SNCA_INVENTORY_XML_FILE(1, 193);
5885 mandeep.dh 52
 
53
        int type;
54
        long warehouseId;
55
 
56
        FILE_TYPES(int type, long warehouseId) {
57
            this.type = type;
58
            this.warehouseId = warehouseId;
59
        }
60
 
61
        int getType() {
62
            return type;
63
        }
64
 
65
        long getWarehouseId() {
66
            return warehouseId;
67
        }
68
    };
69
 
5530 mandeep.dh 70
    private File file;
71
    private int fileType;
72
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
73
 
74
    public StreamResult create() {
75
        try {
76
            logger.info("Trying to archive file with type: " + fileType);
5885 mandeep.dh 77
            File newFile = new File(ARCHIVE_DIR + fileType + "-"
78
                    + sdf.format(new Date()) + ".xls");
79
            FileUtils.copyFile(file, newFile);
5530 mandeep.dh 80
 
5885 mandeep.dh 81
            // For Sarvottam, convert to xls
82
            if (fileType == FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getType()) {
83
                File file = new File(convertToXls(newFile));
84
                uploadInventoryFile(file, FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getWarehouseId());
85
            }
6030 amar.kumar 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
            }
5885 mandeep.dh 90
 
5530 mandeep.dh 91
            return new StreamResult(new ByteArrayInputStream(
5885 mandeep.dh 92
                    ("File uploaded successfully at " + sdf.format(new Date()))
93
                            .getBytes()));
5530 mandeep.dh 94
 
95
        } catch (IOException e) {
96
            logger.error("Error archiving file", e);
97
            return new StreamResult(new ByteArrayInputStream(
5885 mandeep.dh 98
                    ("Could not upload file at " + sdf.format(new Date()))
99
                            .getBytes()));
5530 mandeep.dh 100
        }
101
    }
102
 
5885 mandeep.dh 103
    /**
104
     * @param file
105
     */
106
    private void uploadInventoryFile(File file, long warehouseId) {
107
        try {
108
            String inventoryUploadUrl = "http://localhost:8080/inventory/supplier-inventory";
109
            HttpPost inventoryUploadPost = new HttpPost(inventoryUploadUrl);
110
            MultipartEntity entity = new MultipartEntity();
111
            entity.addPart("warehouseId", new StringBody(String.valueOf(warehouseId)));
112
            entity.addPart("file", new FileBody(file));
113
            inventoryUploadPost.setEntity(entity);
114
 
115
            HttpClient client = new DefaultHttpClient();
116
            HttpResponse response = client.execute(inventoryUploadPost);
117
            byte[] b = new byte[1000];
118
            response.getEntity().getContent().read(b);
119
            System.out.println(new String(b));
120
            logger.info("Response: " + new String(b));
121
        } catch (Exception e) {
122
            logger.error("Could not upload inventory: " + e);
123
        }
124
    }
125
 
126
    /**
127
     * @param tsvFile
128
     */
129
    private String convertToXls(File tsvFile) {
130
        try {
131
            Workbook w = new HSSFWorkbook();
132
            String convertedFileName = ARCHIVE_DIR + "Converted-"
133
                    + tsvFile.getName();
134
            FileOutputStream fileOut = new FileOutputStream(convertedFileName);
135
            Sheet sheet = w.createSheet();
136
            BufferedReader br = new BufferedReader(new FileReader(tsvFile));
137
            String line = br.readLine();
138
            int i = 0;
139
            while (line != null) {
140
                Row row = sheet.createRow(i++);
141
                int j = 0;
142
                for (String s : line.split("\t")) {
143
                    Cell cell = row.createCell(j++);
144
                    System.out.println("Writing: " + s);
145
                    cell.setCellValue(s);
146
                }
147
                line = br.readLine();
148
            }
149
            w.write(fileOut);
150
            return convertedFileName;
151
        } catch (Exception e) {
152
            logger.error("Error converting file", e);
153
            return tsvFile.getName();
154
        }
155
    }
156
 
6030 amar.kumar 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
 
5530 mandeep.dh 193
    public File getFile() {
194
        return file;
195
    }
196
 
197
    public void setFile(File file) {
198
        this.file = file;
199
    }
200
 
201
    public int getFileType() {
202
        return fileType;
203
    }
204
 
205
    public void setFileType(int fileType) {
206
        this.fileType = fileType;
207
    }
5885 mandeep.dh 208
 
209
    public static void main(String[] args) {
6030 amar.kumar 210
    	FileArchiveController arch = new FileArchiveController();
211
        arch.file = new File("/home/amar/Downloads/StockSummary.xml");
212
        arch.fileType = 1;
213
        arch.create();
214
        //new FileArchiveController().uploadInventoryFile(file, 193);
5885 mandeep.dh 215
        System.out.println("done");
216
    }
5530 mandeep.dh 217
}