| 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 |
|
|
|
15 |
import org.apache.commons.io.FileUtils;
|
| 5885 |
mandeep.dh |
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;
|
| 5530 |
mandeep.dh |
28 |
import org.apache.struts2.dispatcher.StreamResult;
|
|
|
29 |
import org.slf4j.Logger;
|
|
|
30 |
import org.slf4j.LoggerFactory;
|
|
|
31 |
|
|
|
32 |
import com.opensymphony.xwork2.ActionSupport;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @author mandeep
|
|
|
36 |
*
|
|
|
37 |
*/
|
|
|
38 |
public class FileArchiveController extends ActionSupport {
|
| 5885 |
mandeep.dh |
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
private static final String ARCHIVE_DIR = "/InventoryReports/";
|
|
|
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 |
|
| 5530 |
mandeep.dh |
66 |
private File file;
|
|
|
67 |
private int fileType;
|
|
|
68 |
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
|
|
69 |
|
|
|
70 |
public StreamResult create() {
|
|
|
71 |
try {
|
|
|
72 |
logger.info("Trying to archive file with type: " + fileType);
|
| 5885 |
mandeep.dh |
73 |
File newFile = new File(ARCHIVE_DIR + fileType + "-"
|
|
|
74 |
+ sdf.format(new Date()) + ".xls");
|
|
|
75 |
FileUtils.copyFile(file, newFile);
|
| 5530 |
mandeep.dh |
76 |
|
| 5885 |
mandeep.dh |
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 |
}
|
|
|
82 |
|
| 5530 |
mandeep.dh |
83 |
return new StreamResult(new ByteArrayInputStream(
|
| 5885 |
mandeep.dh |
84 |
("File uploaded successfully at " + sdf.format(new Date()))
|
|
|
85 |
.getBytes()));
|
| 5530 |
mandeep.dh |
86 |
|
|
|
87 |
} catch (IOException e) {
|
|
|
88 |
logger.error("Error archiving file", e);
|
|
|
89 |
return new StreamResult(new ByteArrayInputStream(
|
| 5885 |
mandeep.dh |
90 |
("Could not upload file at " + sdf.format(new Date()))
|
|
|
91 |
.getBytes()));
|
| 5530 |
mandeep.dh |
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
| 5885 |
mandeep.dh |
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();
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
| 5530 |
mandeep.dh |
149 |
public File getFile() {
|
|
|
150 |
return file;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public void setFile(File file) {
|
|
|
154 |
this.file = file;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public int getFileType() {
|
|
|
158 |
return fileType;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public void setFileType(int fileType) {
|
|
|
162 |
this.fileType = fileType;
|
|
|
163 |
}
|
| 5885 |
mandeep.dh |
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 |
}
|
| 5530 |
mandeep.dh |
170 |
}
|