Subversion Repositories SmartDukaan

Rev

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

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