Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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