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.HashMap;
5
import java.util.Map;
6
 
7
import in.shop2020.model.v1.catalog.InventoryService.Client;
8
import in.shop2020.model.v1.catalog.Warehouse;
9
import in.shop2020.support.models.InventoryUpdate;
10
import in.shop2020.support.models.Update;
11
import in.shop2020.thrift.clients.CatalogServiceClient;
12
 
13
import org.apache.struts2.rest.DefaultHttpHeaders;
14
import org.apache.struts2.rest.HttpHeaders;
15
import com.opensymphony.xwork2.ModelDriven;
16
 
17
public class UpdatesController implements ModelDriven<Object>{
18
 
19
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
20
	private String timestamp;
21
 
22
	private int errorCode = 0;
23
	private String errorMessage;
24
 
25
	private String id;
26
 
27
 
28
	public UpdatesController(){
29
 
30
	}
31
 
32
	@Override
33
	public Object getModel() {
34
		return this.inventoryUpdate;
35
	}
36
 
37
	public HttpHeaders show(){
38
		System.out.println("Warehouse Id is:  " + getId());
39
		CatalogServiceClient catalogServiceClient;
40
		Client client;
41
 
42
		try {
43
			catalogServiceClient = new CatalogServiceClient();
44
			client = catalogServiceClient.getClient();
45
			long warehouseId = Long.parseLong(getId());
46
			Warehouse warehouse = client.getWarehouse(warehouseId);
47
			setTimestamp(warehouse.getVendorString());
48
		} catch (Exception e) {
49
			e.printStackTrace();
50
		}
51
		return new DefaultHttpHeaders("lsuccess");
52
	}
53
 
54
 
55
	public HttpHeaders create(){
56
		System.out.println(inventoryUpdate);
57
		// need to store all data from object to service
58
		CatalogServiceClient catalogServiceClient;
59
		Client client;
60
		try {
61
			catalogServiceClient = new CatalogServiceClient();
62
			client = catalogServiceClient.getClient();
63
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
64
			String timestamp = inventoryUpdate.getCurrentTimestamp();
65
			Map<String, Long> availability = new HashMap<String, Long>();
66
			List<Update> updates = inventoryUpdate.getUpdates();
67
			for(Update update: updates){
961 chandransh 68
				String group = update.getGroup().trim();
616 chandransh 69
				String brand = update.getBrand().trim();
70
				String model = update.getModel().trim();
71
				String color = update.getColor().trim();
961 chandransh 72
				String key = group + ";" + brand + ";" + model + ";" + color;
490 rajveer 73
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 74
				if(availability.containsKey(key)){
75
					quantity = quantity + availability.get(key);
490 rajveer 76
				}
616 chandransh 77
				availability.put(key, quantity);
490 rajveer 78
			}
79
			client.updateInventory(warehouseId, timestamp, availability);
80
 
81
		} catch (Exception e) {
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;
616 chandransh 119
	}	
490 rajveer 120
}