| 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;
|
| 6033 |
amar.kumar |
12 |
import java.io.StringReader;
|
| 5530 |
mandeep.dh |
13 |
import java.text.SimpleDateFormat;
|
|
|
14 |
import java.util.Date;
|
|
|
15 |
|
| 6030 |
amar.kumar |
16 |
import javax.xml.parsers.DocumentBuilder;
|
|
|
17 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
18 |
|
| 5530 |
mandeep.dh |
19 |
import org.apache.commons.io.FileUtils;
|
| 5885 |
mandeep.dh |
20 |
import org.apache.http.HttpResponse;
|
|
|
21 |
import org.apache.http.client.HttpClient;
|
|
|
22 |
import org.apache.http.client.methods.HttpPost;
|
|
|
23 |
import org.apache.http.entity.mime.MultipartEntity;
|
|
|
24 |
import org.apache.http.entity.mime.content.FileBody;
|
|
|
25 |
import org.apache.http.entity.mime.content.StringBody;
|
|
|
26 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
27 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
28 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
29 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
30 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
31 |
import org.apache.poi.ss.usermodel.Workbook;
|
| 5530 |
mandeep.dh |
32 |
import org.apache.struts2.dispatcher.StreamResult;
|
|
|
33 |
import org.slf4j.Logger;
|
|
|
34 |
import org.slf4j.LoggerFactory;
|
| 6030 |
amar.kumar |
35 |
import org.w3c.dom.NodeList;
|
| 6033 |
amar.kumar |
36 |
import org.xml.sax.InputSource;
|
| 5530 |
mandeep.dh |
37 |
|
|
|
38 |
import com.opensymphony.xwork2.ActionSupport;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @author mandeep
|
|
|
42 |
*
|
|
|
43 |
*/
|
|
|
44 |
public class FileArchiveController extends ActionSupport {
|
| 5885 |
mandeep.dh |
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
*/
|
|
|
48 |
private static final String ARCHIVE_DIR = "/InventoryReports/";
|
|
|
49 |
private static Logger logger = LoggerFactory.getLogger(FileArchiveController.class);
|
|
|
50 |
|
|
|
51 |
private static enum FILE_TYPES {
|
|
|
52 |
SARVOTTAM_INVENTORY_TSV_FILE(2, 175),
|
| 6030 |
amar.kumar |
53 |
SNCA_INVENTORY_XML_FILE(1, 193);
|
| 5885 |
mandeep.dh |
54 |
|
|
|
55 |
int type;
|
|
|
56 |
long warehouseId;
|
|
|
57 |
|
|
|
58 |
FILE_TYPES(int type, long warehouseId) {
|
|
|
59 |
this.type = type;
|
|
|
60 |
this.warehouseId = warehouseId;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
int getType() {
|
|
|
64 |
return type;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
long getWarehouseId() {
|
|
|
68 |
return warehouseId;
|
|
|
69 |
}
|
|
|
70 |
};
|
|
|
71 |
|
| 5530 |
mandeep.dh |
72 |
private File file;
|
|
|
73 |
private int fileType;
|
|
|
74 |
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
|
|
75 |
|
|
|
76 |
public StreamResult create() {
|
|
|
77 |
try {
|
|
|
78 |
logger.info("Trying to archive file with type: " + fileType);
|
| 5885 |
mandeep.dh |
79 |
File newFile = new File(ARCHIVE_DIR + fileType + "-"
|
|
|
80 |
+ sdf.format(new Date()) + ".xls");
|
|
|
81 |
FileUtils.copyFile(file, newFile);
|
| 5530 |
mandeep.dh |
82 |
|
| 5885 |
mandeep.dh |
83 |
// For Sarvottam, convert to xls
|
|
|
84 |
if (fileType == FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getType()) {
|
|
|
85 |
File file = new File(convertToXls(newFile));
|
|
|
86 |
uploadInventoryFile(file, FILE_TYPES.SARVOTTAM_INVENTORY_TSV_FILE.getWarehouseId());
|
|
|
87 |
}
|
| 6030 |
amar.kumar |
88 |
else if(fileType == FILE_TYPES.SNCA_INVENTORY_XML_FILE.getType()) {
|
|
|
89 |
File file = new File(convertXmlToXls(newFile));
|
|
|
90 |
uploadInventoryFile(file, FILE_TYPES.SNCA_INVENTORY_XML_FILE.getWarehouseId());
|
|
|
91 |
}
|
| 5885 |
mandeep.dh |
92 |
|
| 5530 |
mandeep.dh |
93 |
return new StreamResult(new ByteArrayInputStream(
|
| 5885 |
mandeep.dh |
94 |
("File uploaded successfully at " + sdf.format(new Date()))
|
|
|
95 |
.getBytes()));
|
| 5530 |
mandeep.dh |
96 |
|
|
|
97 |
} catch (IOException e) {
|
|
|
98 |
logger.error("Error archiving file", e);
|
|
|
99 |
return new StreamResult(new ByteArrayInputStream(
|
| 5885 |
mandeep.dh |
100 |
("Could not upload file at " + sdf.format(new Date()))
|
|
|
101 |
.getBytes()));
|
| 5530 |
mandeep.dh |
102 |
}
|
|
|
103 |
}
|
|
|
104 |
|
| 5885 |
mandeep.dh |
105 |
/**
|
|
|
106 |
* @param file
|
|
|
107 |
*/
|
|
|
108 |
private void uploadInventoryFile(File file, long warehouseId) {
|
|
|
109 |
try {
|
|
|
110 |
String inventoryUploadUrl = "http://localhost:8080/inventory/supplier-inventory";
|
|
|
111 |
HttpPost inventoryUploadPost = new HttpPost(inventoryUploadUrl);
|
|
|
112 |
MultipartEntity entity = new MultipartEntity();
|
|
|
113 |
entity.addPart("warehouseId", new StringBody(String.valueOf(warehouseId)));
|
|
|
114 |
entity.addPart("file", new FileBody(file));
|
|
|
115 |
inventoryUploadPost.setEntity(entity);
|
|
|
116 |
|
|
|
117 |
HttpClient client = new DefaultHttpClient();
|
|
|
118 |
HttpResponse response = client.execute(inventoryUploadPost);
|
|
|
119 |
byte[] b = new byte[1000];
|
|
|
120 |
response.getEntity().getContent().read(b);
|
|
|
121 |
System.out.println(new String(b));
|
|
|
122 |
logger.info("Response: " + new String(b));
|
|
|
123 |
} catch (Exception e) {
|
|
|
124 |
logger.error("Could not upload inventory: " + e);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* @param tsvFile
|
|
|
130 |
*/
|
|
|
131 |
private String convertToXls(File tsvFile) {
|
|
|
132 |
try {
|
|
|
133 |
Workbook w = new HSSFWorkbook();
|
|
|
134 |
String convertedFileName = ARCHIVE_DIR + "Converted-"
|
|
|
135 |
+ tsvFile.getName();
|
|
|
136 |
FileOutputStream fileOut = new FileOutputStream(convertedFileName);
|
|
|
137 |
Sheet sheet = w.createSheet();
|
|
|
138 |
BufferedReader br = new BufferedReader(new FileReader(tsvFile));
|
|
|
139 |
String line = br.readLine();
|
|
|
140 |
int i = 0;
|
|
|
141 |
while (line != null) {
|
|
|
142 |
Row row = sheet.createRow(i++);
|
|
|
143 |
int j = 0;
|
|
|
144 |
for (String s : line.split("\t")) {
|
|
|
145 |
Cell cell = row.createCell(j++);
|
|
|
146 |
System.out.println("Writing: " + s);
|
|
|
147 |
cell.setCellValue(s);
|
|
|
148 |
}
|
|
|
149 |
line = br.readLine();
|
|
|
150 |
}
|
|
|
151 |
w.write(fileOut);
|
|
|
152 |
return convertedFileName;
|
|
|
153 |
} catch (Exception e) {
|
|
|
154 |
logger.error("Error converting file", e);
|
|
|
155 |
return tsvFile.getName();
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
| 6030 |
amar.kumar |
159 |
/**
|
|
|
160 |
* @param xmlFile
|
|
|
161 |
*/
|
|
|
162 |
private String convertXmlToXls(File xmlFile) {
|
|
|
163 |
try {
|
| 6033 |
amar.kumar |
164 |
String xmlData = correctMalformedTags(xmlFile);
|
|
|
165 |
|
| 6030 |
amar.kumar |
166 |
Workbook w = new HSSFWorkbook();
|
|
|
167 |
Sheet sheet=w.createSheet("spreadSheet");
|
|
|
168 |
String convertedFileName = ARCHIVE_DIR + "Converted-"
|
|
|
169 |
+ xmlFile.getName();
|
|
|
170 |
FileOutputStream fileOut = new FileOutputStream(convertedFileName);
|
|
|
171 |
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
|
|
|
172 |
DocumentBuilder builder = factory.newDocumentBuilder();
|
| 6033 |
amar.kumar |
173 |
InputSource is = new InputSource(new StringReader(xmlData));
|
|
|
174 |
org.w3c.dom.Document document = builder.parse(is);
|
| 6030 |
amar.kumar |
175 |
|
|
|
176 |
NodeList itemNameList = document.getElementsByTagName("DSPDISPNAME");
|
| 6033 |
amar.kumar |
177 |
NodeList itemQtyList = document.getElementsByTagName("DSPCLQTY");
|
| 6030 |
amar.kumar |
178 |
|
|
|
179 |
for(int i =1; i<=itemNameList.getLength(); i++) {
|
|
|
180 |
Row row = sheet.createRow(i);
|
|
|
181 |
int j = 0;
|
|
|
182 |
Cell cell1 = row.createCell(j++);
|
|
|
183 |
cell1.setCellValue(i);
|
|
|
184 |
Cell cell2 = row.createCell(j++);
|
|
|
185 |
cell2.setCellValue(itemNameList.item(i-1).getFirstChild().getNodeValue());
|
|
|
186 |
Cell cell3 = row.createCell(j++);
|
|
|
187 |
cell3.setCellValue(itemQtyList.item(i-1).getFirstChild().getNodeValue());
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
w.write(fileOut);
|
|
|
191 |
return convertedFileName;
|
|
|
192 |
} catch (Exception e) {
|
|
|
193 |
logger.error("Error converting file", e);
|
|
|
194 |
return xmlFile.getName();
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
| 6033 |
amar.kumar |
198 |
private String correctMalformedTags(File xmlFile) throws IOException {
|
|
|
199 |
String xmlContent = readFileAsString(xmlFile);
|
|
|
200 |
xmlContent = xmlContent.replace("<DSPACCNA\\nE>", "<DSPACCNAME>").
|
|
|
201 |
replace("<DSPACCNA\nE>", "<DSPACCNAME>").replace("DSPACCNAME\\n<", "DSPACCNAME><").
|
|
|
202 |
replace("DSPACCNAME\n<", "DSPACCNAME><").replace("DSPACCNAME\\n <", "DSPACCNAME><").
|
|
|
203 |
replace("DSPACCNAME\n <", "DSPACCNAME><");
|
|
|
204 |
System.out.println(xmlContent);
|
|
|
205 |
return xmlContent;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
private static String readFileAsString(File file)
|
|
|
209 |
throws java.io.IOException{
|
|
|
210 |
StringBuffer fileData = new StringBuffer(1000);
|
|
|
211 |
BufferedReader reader = new BufferedReader(
|
|
|
212 |
new FileReader(file));
|
|
|
213 |
char[] buf = new char[1024];
|
|
|
214 |
int numRead=0;
|
|
|
215 |
while((numRead=reader.read(buf)) != -1){
|
|
|
216 |
String readData = String.valueOf(buf, 0, numRead);
|
|
|
217 |
fileData.append(readData);
|
|
|
218 |
buf = new char[1024];
|
|
|
219 |
}
|
|
|
220 |
reader.close();
|
|
|
221 |
return fileData.toString();
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public File getFile() {
|
| 5530 |
mandeep.dh |
225 |
return file;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
public void setFile(File file) {
|
|
|
229 |
this.file = file;
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
public int getFileType() {
|
|
|
233 |
return fileType;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
public void setFileType(int fileType) {
|
|
|
237 |
this.fileType = fileType;
|
|
|
238 |
}
|
| 5885 |
mandeep.dh |
239 |
|
|
|
240 |
public static void main(String[] args) {
|
| 6030 |
amar.kumar |
241 |
FileArchiveController arch = new FileArchiveController();
|
|
|
242 |
arch.file = new File("/home/amar/Downloads/StockSummary.xml");
|
|
|
243 |
arch.fileType = 1;
|
|
|
244 |
arch.create();
|
|
|
245 |
//new FileArchiveController().uploadInventoryFile(file, 193);
|
| 5885 |
mandeep.dh |
246 |
System.out.println("done");
|
|
|
247 |
}
|
| 5530 |
mandeep.dh |
248 |
}
|