Subversion Repositories SmartDukaan

Rev

Rev 1023 | Rev 3104 | Go to most recent revision | Details | Compare with Previous | 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
 
1367 chandransh 17
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 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
1367 chandransh 33
	public InventoryUpdate getModel() {
490 rajveer 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){
1367 chandransh 68
				String key = update.getKey();
490 rajveer 69
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 70
				if(availability.containsKey(key)){
71
					quantity = quantity + availability.get(key);
490 rajveer 72
				}
616 chandransh 73
				availability.put(key, quantity);
490 rajveer 74
			}
75
			client.updateInventory(warehouseId, timestamp, availability);
76
 
77
		} catch (Exception e) {
78
			e.printStackTrace();
79
		}
80
 
81
		return new DefaultHttpHeaders("psuccess");
82
 
83
	}
84
 
85
	public int getErrorCode() {
86
		return errorCode;
87
	}
88
 
89
	public String getErrorMessage() {
90
		return errorMessage;
91
	}
92
 
93
	public String getId(){
94
		return id;
95
	}
96
 
97
	public void setId(String id){
98
		this.id = id;
99
	}
100
 
101
	public InventoryUpdate getInventoryUpdate() {
102
		return inventoryUpdate;
103
	}
104
 
105
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
106
		this.inventoryUpdate = inventoryUpdate;
107
	}
108
 
109
	public void setTimestamp(String timestamp) {
110
		this.timestamp = timestamp;
111
	}
112
 
113
	public String getTimestamp() {
114
		return timestamp;
616 chandransh 115
	}	
490 rajveer 116
}