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
			e.printStackTrace();
52
		}
53
		return new DefaultHttpHeaders("lsuccess");
54
	}
55
 
56
 
57
	public HttpHeaders create(){
58
		System.out.println(inventoryUpdate);
59
		// need to store all data from object to service
60
		CatalogServiceClient catalogServiceClient;
61
		Client client;
62
		try {
63
			catalogServiceClient = new CatalogServiceClient();
64
			client = catalogServiceClient.getClient();
65
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
66
			String timestamp = inventoryUpdate.getCurrentTimestamp();
67
			Map<String, Long> availability = new HashMap<String, Long>();
68
			List<Update> updates = inventoryUpdate.getUpdates();
69
			for(Update update: updates){
616 chandransh 70
				String brand = update.getBrand().trim();
71
				String model = update.getModel().trim();
72
				String color = update.getColor().trim();
73
				String key = brand + ";" + model + ";" + color;
490 rajveer 74
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 75
				if(availability.containsKey(key)){
76
					quantity = quantity + availability.get(key);
490 rajveer 77
				}
616 chandransh 78
				availability.put(key, quantity);
490 rajveer 79
			}
80
			client.updateInventory(warehouseId, timestamp, availability);
81
 
82
		} catch (Exception e) {
83
			e.printStackTrace();
84
		}
85
 
86
		return new DefaultHttpHeaders("psuccess");
87
 
88
	}
89
 
90
	public int getErrorCode() {
91
		return errorCode;
92
	}
93
 
94
	public String getErrorMessage() {
95
		return errorMessage;
96
	}
97
 
98
	public String getId(){
99
		return id;
100
	}
101
 
102
	public void setId(String id){
103
		this.id = id;
104
	}
105
 
106
	public InventoryUpdate getInventoryUpdate() {
107
		return inventoryUpdate;
108
	}
109
 
110
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
111
		this.inventoryUpdate = inventoryUpdate;
112
	}
113
 
114
	public void setTimestamp(String timestamp) {
115
		this.timestamp = timestamp;
116
	}
117
 
118
	public String getTimestamp() {
119
		return timestamp;
616 chandransh 120
	}	
490 rajveer 121
}