Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
package in.shop2020.serving.controllers;
2
 
3
import java.io.BufferedWriter;
4
import java.io.File;
5
import java.io.FileOutputStream;
6
import java.io.FileWriter;
7
import java.io.Writer;
8
import java.util.List;
9
import java.util.ArrayList;
10
import java.util.HashMap;
11
import java.util.Map;
12
 
13
import in.shop2020.model.v1.catalog.InventoryService.Client;
14
import in.shop2020.model.v1.catalog.Warehouse;
15
import in.shop2020.thrift.clients.CatalogServiceClient;
16
 
17
import org.apache.commons.collections.map.HashedMap;
18
import org.apache.struts2.rest.DefaultHttpHeaders;
19
import org.apache.struts2.rest.HttpHeaders;
20
import com.opensymphony.xwork2.ModelDriven;
21
 
22
public class UpdatesController implements ModelDriven<Object>{
23
 
24
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
25
	private String timestamp;
26
 
27
	private int errorCode = 0;
28
	private String errorMessage;
29
 
30
	private String id;
31
 
32
 
33
	public UpdatesController(){
34
 
35
	}
36
 
37
	@Override
38
	public Object getModel() {
39
		return this.inventoryUpdate;
40
	}
41
 
42
	public HttpHeaders show(){
43
		System.out.println("Warehouse Id is:  " + getId());
44
		CatalogServiceClient catalogServiceClient;
45
		Client client;
46
 
47
		try {
48
			catalogServiceClient = new CatalogServiceClient();
49
			client = catalogServiceClient.getClient();
50
			long warehouseId = Long.parseLong(getId());
51
			Warehouse warehouse = client.getWarehouse(warehouseId);
52
			setTimestamp(warehouse.getVendorString());
53
		} catch (Exception e) {
54
			// TODO Auto-generated catch block
55
			e.printStackTrace();
56
		}
57
		return new DefaultHttpHeaders("lsuccess");
58
	}
59
 
60
 
61
	public HttpHeaders create(){
62
//		Writer wr = new BufferedWriter(new FileWriter("inventory-updates.txt"));
63
//		//wr.write(availability.toString());
64
//		wr.write(inventoryUpdate);
65
//		wr.close();
66
 
67
		System.out.println(inventoryUpdate);
68
		// need to store all data from object to service
69
		CatalogServiceClient catalogServiceClient;
70
		Client client;
71
		try {
72
			catalogServiceClient = new CatalogServiceClient();
73
			client = catalogServiceClient.getClient();
74
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
75
			String timestamp = inventoryUpdate.getCurrentTimestamp();
76
			Map<String, Long> availability = new HashMap<String, Long>();
77
			List<Update> updates = inventoryUpdate.getUpdates();
78
			for(Update update: updates){
79
				String skuId = update.getSkuId();
80
				long quantity = (long) Double.parseDouble(update.getQuantity());
81
				if(availability.containsKey(skuId)){
82
					quantity = quantity + availability.get(skuId);
83
				}
84
				availability.put(skuId, quantity);
85
			}
86
 
87
			client.updateInventory(warehouseId, timestamp, availability);
88
 
89
		} catch (Exception e) {
90
			// TODO Auto-generated catch block
91
			e.printStackTrace();
92
		}
93
 
94
		return new DefaultHttpHeaders("psuccess");
95
 
96
	}
97
 
98
	public int getErrorCode() {
99
		return errorCode;
100
	}
101
 
102
	public String getErrorMessage() {
103
		return errorMessage;
104
	}
105
 
106
	public String getId(){
107
		return id;
108
	}
109
 
110
	public void setId(String id){
111
		this.id = id;
112
	}
113
 
114
	public InventoryUpdate getInventoryUpdate() {
115
		return inventoryUpdate;
116
	}
117
 
118
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
119
		this.inventoryUpdate = inventoryUpdate;
120
	}
121
 
122
	public void setTimestamp(String timestamp) {
123
		this.timestamp = timestamp;
124
	}
125
 
126
	public String getTimestamp() {
127
		return timestamp;
128
	}
129
 
130
}