Subversion Repositories SmartDukaan

Rev

Rev 9855 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9449 manish.sha 1
package in.shop2020.inventory.controllers;
2
 
12290 manish.sha 3
 
9449 manish.sha 4
import in.shop2020.logistics.DeliveryType;
5
import in.shop2020.logistics.LogisticsService;
9622 manish.sha 6
import in.shop2020.model.v1.catalog.CatalogService;
9449 manish.sha 7
import in.shop2020.model.v1.catalog.CatalogServiceException;
8
import in.shop2020.model.v1.catalog.Item;
9622 manish.sha 9
import in.shop2020.model.v1.catalog.ProductFeedSubmit;
9855 manish.sha 10
import in.shop2020.logistics.ItemText;
9449 manish.sha 11
import in.shop2020.thrift.clients.CatalogClient;
12
import in.shop2020.thrift.clients.LogisticsClient;
13
 
9855 manish.sha 14
import java.io.BufferedInputStream;
15
import java.io.File;
16
import java.io.FileInputStream;
17
import java.io.FileNotFoundException;
18
import java.io.FileOutputStream;
9449 manish.sha 19
import java.io.IOException;
9855 manish.sha 20
import java.io.InputStream;
21
import java.util.ArrayList;
9449 manish.sha 22
import java.util.List;
23
 
9855 manish.sha 24
import javax.servlet.ServletOutputStream;
25
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletResponse;
27
import javax.servlet.http.HttpSession;
28
 
29
import org.apache.poi.hssf.usermodel.HSSFRow;
30
import org.apache.poi.hssf.usermodel.HSSFSheet;
31
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
9449 manish.sha 32
import org.apache.thrift.TException;
33
import org.apache.thrift.transport.TTransportException;
34
 
12290 manish.sha 35
import com.google.api.services.content.handler.ContentFeedsHandler;
36
import com.google.api.services.content.model.Product;
9449 manish.sha 37
 
12290 manish.sha 38
 
9449 manish.sha 39
public class ProductFeedsController extends BaseController{
40
 
41
	private String cataLogItemId;
42
	private String sentFeedResult;
9622 manish.sha 43
	private String catalogItemIdDelete;
44
	private String deleteFeedResult;
45
	private String stockLinkFeed;
46
	private boolean stockLinkedFeed = false;
9449 manish.sha 47
	public String index() {
48
		return "index";
49
	}
50
 
9855 manish.sha 51
	public void downloadAllProductFeeds() throws IOException{
52
		File file = new File("/tmp/Product-Feeds.xls");
53
		HSSFWorkbook hwb=new HSSFWorkbook();
54
		HSSFSheet sheet =  hwb.createSheet("Product Feeds");
55
		HSSFRow rowhead=   sheet.createRow(0);
56
		rowhead.createCell(0).setCellValue("Catalog Item Id");
57
		rowhead.createCell(1).setCellValue("Product Name");
58
		rowhead.createCell(2).setCellValue("Stock Linked");
59
 
60
		CatalogClient catalogClient = null;
61
		try {
62
			catalogClient = new CatalogClient();
63
		} catch (Exception e1) {
64
			e1.printStackTrace();
65
		}
66
 
67
		List<ProductFeedSubmit> feedSubmits = new ArrayList<ProductFeedSubmit>();
68
 
69
		try{
70
			feedSubmits = catalogClient.getClient().getAllProductFeedSubmit();
71
		} catch(Exception e){
72
			e.printStackTrace();
73
		}
74
 
75
		int i = 1;
76
		try{
77
			for(ProductFeedSubmit feedSubmit : feedSubmits){
78
				HSSFRow rowToAdd = sheet.createRow(i);
79
				rowToAdd.createCell(0).setCellValue(feedSubmit.getCatalogItemId());
80
				Item item = catalogClient.getClient().getItemsByCatalogId(feedSubmit.getCatalogItemId()).get(0);
81
				rowToAdd.createCell(1).setCellValue(item.getBrand() + " "+ item.getModelName() + " " + item.getModelNumber());
82
				rowToAdd.createCell(2).setCellValue(feedSubmit.isStockLinkedFeed());
83
				i++;
84
			}
85
		} catch(Exception e){
86
			e.printStackTrace();
87
		}
88
 
89
		FileOutputStream fileOut = null;
90
		try {
91
			fileOut = new FileOutputStream(file);
92
		} catch (FileNotFoundException e) {
93
			// TODO Auto-generated catch block
94
			e.printStackTrace();
95
		}
96
		try {
97
			hwb.write(fileOut);
98
		} catch (IOException e) {
99
			// TODO Auto-generated catch block
100
			e.printStackTrace();
101
		}
102
		try {
103
			fileOut.close();
104
		} catch (IOException e) {
105
			// TODO Auto-generated catch block
106
			e.printStackTrace();
107
		}
108
		byte[] buffer = new byte[(int)file.length()];
109
		InputStream input = null;
110
		try {
111
			int totalBytesRead = 0;
112
			input = new BufferedInputStream(new FileInputStream(file));
113
			while(totalBytesRead < buffer.length){
114
				int bytesRemaining = buffer.length - totalBytesRead;
115
				int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining); 
116
				if (bytesRead > 0){
117
					totalBytesRead = totalBytesRead + bytesRead;
118
				}
119
			}
120
		}
121
		finally {
122
			input.close();
123
			file.delete();
124
		}
125
 
126
		response.setHeader("Content-Disposition", "attachment; filename=\"Product-Feeds.xls\"");
127
		response.setContentType("application/octet-stream");
128
		ServletOutputStream sos;
129
		try {
130
			sos = response.getOutputStream();
131
			sos.write(buffer);
132
			sos.flush();
133
		} catch (IOException e) {
134
			System.out.println("Unable to stream the manifest file");
135
		}   
136
	}
137
 
9449 manish.sha 138
	public String sendProductFeeds(){
139
		//boolean sendFeedsResult = false;
9622 manish.sha 140
		processStockLinkFeed();
9449 manish.sha 141
		Product insertedProduct = null;
9622 manish.sha 142
		CatalogClient catalogClient = null;
143
		try {
144
			catalogClient = new CatalogClient();
145
		} catch (TTransportException e1) {
146
			e1.printStackTrace();
147
		}
148
 
9449 manish.sha 149
		if(cataLogItemId!=null && !("").equalsIgnoreCase(cataLogItemId)){
150
			List<Long> itemIdList = getEntityLogisticsEstimation(Long.parseLong(cataLogItemId));
12290 manish.sha 151
			ContentFeedsHandler sample = new ContentFeedsHandler();
9622 manish.sha 152
 
153
			if(catalogClient!= null){
154
				if(itemIdList!=null && itemIdList.size()>0){
155
					try {
156
						Item item = catalogClient.getClient().getItem(itemIdList.get(0));
157
						insertedProduct = sample.sendProductFeed(item);
158
					} catch (IOException e) {
159
						e.printStackTrace();
160
					} catch (CatalogServiceException e) {
161
						e.printStackTrace();
162
					} catch (TException e) {
163
						e.printStackTrace();
164
					}
9449 manish.sha 165
				}
9622 manish.sha 166
				else{
167
					sentFeedResult = "Error: There is No Product avaliable for given Catalog Item Id.";
168
					return "output";
169
				}
9449 manish.sha 170
			}
171
		}
172
		if(insertedProduct!=null){
12290 manish.sha 173
			if(insertedProduct.getDescription()!=null && insertedProduct.getDescription().indexOf("Error:")>-1){
174
				sentFeedResult = insertedProduct.getDescription();
9449 manish.sha 175
				return "output";
176
			}
9622 manish.sha 177
 
9449 manish.sha 178
			sentFeedResult = "Product Feeds are sent Successfully and Product Details are Given Below:\nProduct Id- "
12290 manish.sha 179
				+insertedProduct.getId()+" Product Title- "+insertedProduct.getTitle();
9622 manish.sha 180
 
181
			if(catalogClient!=null){
182
				ProductFeedSubmit feedSubmit = null;
183
				try {
184
					feedSubmit= catalogClient.getClient().getProductFeedSubmit(Long.parseLong(cataLogItemId));
185
					if(feedSubmit!=null && feedSubmit.getCatalogItemId() > 0l){
186
						System.out.println("Comes into Update ");
187
						feedSubmit.setCatalogItemId(Long.parseLong(cataLogItemId));
188
						feedSubmit.setStockLinkedFeed(stockLinkedFeed);
189
						boolean resultUpdate = catalogClient.getClient().updateProductFeedSubmit(feedSubmit);
190
						if(resultUpdate){
191
							sentFeedResult = sentFeedResult + "\n And Updated to Our System Successfully ";
192
						}
193
					}
194
					else{
195
						System.out.println("Comes into Add ");
196
						feedSubmit = new ProductFeedSubmit();
197
						feedSubmit.setCatalogItemId(Long.parseLong(cataLogItemId));
198
						feedSubmit.setStockLinkedFeed(stockLinkedFeed);
199
						boolean resultUpdate = catalogClient.getClient().addProductFeedSubmit(feedSubmit);
200
						if(resultUpdate){
201
							sentFeedResult = sentFeedResult + "\n And Added to Our System Successfully ";
202
						}
203
					}
204
				} catch (CatalogServiceException e) {
205
					e.printStackTrace();
206
				} catch (TException e) {
207
					e.printStackTrace();
208
				}
209
			}
9449 manish.sha 210
			return "output";
211
		}
212
		else{
213
			sentFeedResult = "Some Error occured while sending feeds. Please contact concerned Team";
214
			return "output";
215
		}
216
		/*if(sendFeedsResult){
217
			successmsg = "Product Feeds are sent Successfully";
218
			return index();
219
		}
220
		else{
221
			errorMsg = "Some Error occured while sending feeds. Please contact concerned Team";
222
			return index();
223
		}*/
224
	}
225
 
9622 manish.sha 226
	public String deleteProductFeeds(){
227
		CatalogClient catalogClient = null;
228
		try {
229
			catalogClient = new CatalogClient();
230
		} catch (TTransportException e1) {
231
			e1.printStackTrace();
232
		}
233
		if(catalogClient!=null){
234
			ProductFeedSubmit feedSubmit = null;
235
			try {
236
				feedSubmit= catalogClient.getClient().getProductFeedSubmit(Long.parseLong(catalogItemIdDelete));
237
				if(feedSubmit!=null && feedSubmit.getCatalogItemId() > 0l){
12290 manish.sha 238
 
239
					ContentFeedsHandler sample = new ContentFeedsHandler();
240
					boolean deleteFeedResultBool = sample.deleteProductFeed(Long.parseLong(catalogItemIdDelete));
9622 manish.sha 241
					if(deleteFeedResultBool){
242
						deleteFeedResult = "Product Feeds are deleted Successfully For Product Id "+ catalogItemIdDelete;
243
						boolean deleteResult = catalogClient.getClient().deleteProductFeedSubmit(Long.parseLong(catalogItemIdDelete));
244
						if(deleteResult){
245
							deleteFeedResult = deleteFeedResult + "\n And Deleted from our System Successfully";
246
						}
247
					}
248
					else{
249
						deleteFeedResult = "Error Occurred: Product Feeds Deletion Unsuccessful";
250
					}
251
				}
252
			} catch (CatalogServiceException e) {
253
				e.printStackTrace();
254
			} catch (TException e) {
255
				e.printStackTrace();
256
			}
257
		}	
258
 
259
		return "output";
260
	}
261
 
9449 manish.sha 262
	public List<Long> getEntityLogisticsEstimation(long productId){
9855 manish.sha 263
    	List<ItemText> items=null;
264
    	List<Long> itemIds = new ArrayList<Long>();
9449 manish.sha 265
    	try {
266
    		LogisticsClient cl = new LogisticsClient();
267
            LogisticsService.Client client = cl.getClient();
268
            items = client.getEntityLogisticsEstimation(productId, "110001", DeliveryType.PREPAID );
9855 manish.sha 269
            for(ItemText text : items)
270
            	itemIds.add(text.getItemId());
9449 manish.sha 271
        } catch (Exception e1) {
272
            System.out.println("Unable to get items by catalog item id"+e1.getMessage());
273
        }
9855 manish.sha 274
        return itemIds;
9449 manish.sha 275
    }
276
 
9622 manish.sha 277
	public void processStockLinkFeed(){
278
		if(stockLinkFeed!=null && ("on").equalsIgnoreCase(stockLinkFeed)){
279
			stockLinkedFeed = true;
280
		}
281
	}
282
 
283
	public String getCatalogItemIdDelete() {
284
		return catalogItemIdDelete;
285
	}
286
 
287
	public void setCatalogItemIdDelete(String catalogItemIdDelete) {
288
		this.catalogItemIdDelete = catalogItemIdDelete;
289
	}
290
 
291
	public String getDeleteFeedResult() {
292
		return deleteFeedResult;
293
	}
294
 
295
	public void setDeleteFeedResult(String deleteFeedResult) {
296
		this.deleteFeedResult = deleteFeedResult;
297
	}
298
 
9449 manish.sha 299
	public String getCataLogItemId() {
300
		return cataLogItemId;
301
	}
302
 
303
	public void setCataLogItemId(String cataLogItemId) {
304
		this.cataLogItemId = cataLogItemId;
305
	}
306
 
307
	public String getSentFeedResult() {
308
		return sentFeedResult;
309
	}
310
 
311
	public void setSentFeedResult(String sentFeedResult) {
312
		this.sentFeedResult = sentFeedResult;
313
	}
9622 manish.sha 314
 
315
	public String getStockLinkFeed() {
316
		return stockLinkFeed;
317
	}
318
 
319
	public void setStockLinkFeed(String stockLinkFeed) {
320
		this.stockLinkFeed = stockLinkFeed;
321
	}
9449 manish.sha 322
}