Subversion Repositories SmartDukaan

Rev

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