Subversion Repositories SmartDukaan

Rev

Rev 3104 | Rev 3326 | 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;
3125 rajveer 11
import in.shop2020.thrift.clients.CatalogClient;
490 rajveer 12
 
13
import org.apache.struts2.rest.DefaultHttpHeaders;
14
import org.apache.struts2.rest.HttpHeaders;
3104 chandransh 15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17
 
490 rajveer 18
import com.opensymphony.xwork2.ModelDriven;
19
 
1367 chandransh 20
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 21
 
3104 chandransh 22
    private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
23
 
490 rajveer 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
1367 chandransh 38
	public InventoryUpdate getModel() {
490 rajveer 39
		return this.inventoryUpdate;
40
	}
41
 
42
	public HttpHeaders show(){
3104 chandransh 43
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 44
		CatalogClient catalogServiceClient;
490 rajveer 45
		Client client;
46
 
47
		try {
3125 rajveer 48
			catalogServiceClient = new CatalogClient();
490 rajveer 49
			client = catalogServiceClient.getClient();
50
			long warehouseId = Long.parseLong(getId());
51
			Warehouse warehouse = client.getWarehouse(warehouseId);
52
			setTimestamp(warehouse.getVendorString());
53
		} catch (Exception e) {
3104 chandransh 54
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 55
		}
56
		return new DefaultHttpHeaders("lsuccess");
57
	}
58
 
59
 
60
	public HttpHeaders create(){
3104 chandransh 61
		logger.info(inventoryUpdate.toString());
490 rajveer 62
		// need to store all data from object to service
3125 rajveer 63
		CatalogClient catalogServiceClient;
490 rajveer 64
		Client client;
65
		try {
3125 rajveer 66
			catalogServiceClient = new CatalogClient();
490 rajveer 67
			client = catalogServiceClient.getClient();
68
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
69
			String timestamp = inventoryUpdate.getCurrentTimestamp();
70
			Map<String, Long> availability = new HashMap<String, Long>();
71
			List<Update> updates = inventoryUpdate.getUpdates();
72
			for(Update update: updates){
1367 chandransh 73
				String key = update.getKey();
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) {
3104 chandransh 83
			logger.error("Unable to update inventory", e);
490 rajveer 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
}