Subversion Repositories SmartDukaan

Rev

Rev 3125 | Rev 4585 | 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;
3326 chandransh 12
import in.shop2020.utils.ConfigClientKeys;
490 rajveer 13
 
14
import org.apache.struts2.rest.DefaultHttpHeaders;
15
import org.apache.struts2.rest.HttpHeaders;
3104 chandransh 16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18
 
490 rajveer 19
import com.opensymphony.xwork2.ModelDriven;
20
 
1367 chandransh 21
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 22
 
3104 chandransh 23
    private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
24
 
490 rajveer 25
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
26
	private String timestamp;
27
 
28
	private int errorCode = 0;
29
	private String errorMessage;
30
 
31
	private String id;
32
 
33
 
34
	public UpdatesController(){
35
 
36
	}
37
 
38
	@Override
1367 chandransh 39
	public InventoryUpdate getModel() {
490 rajveer 40
		return this.inventoryUpdate;
41
	}
42
 
43
	public HttpHeaders show(){
3104 chandransh 44
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 45
		CatalogClient catalogServiceClient;
490 rajveer 46
		Client client;
47
 
48
		try {
3125 rajveer 49
			catalogServiceClient = new CatalogClient();
490 rajveer 50
			client = catalogServiceClient.getClient();
51
			long warehouseId = Long.parseLong(getId());
52
			Warehouse warehouse = client.getWarehouse(warehouseId);
53
			setTimestamp(warehouse.getVendorString());
54
		} catch (Exception e) {
3104 chandransh 55
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 56
		}
57
		return new DefaultHttpHeaders("lsuccess");
58
	}
59
 
3326 chandransh 60
	/**
61
	 * Stores the inventory updates on the history and the production server.
62
	 * @return
63
	 */
490 rajveer 64
	public HttpHeaders create(){
3104 chandransh 65
		logger.info(inventoryUpdate.toString());
490 rajveer 66
		try {
3326 chandransh 67
		    //The snapshot client is used to update the inventory snapshot for production systems.
68
		    CatalogClient snapshotCatalogServiceClient = new CatalogClient();
69
			Client snapshotClient = snapshotCatalogServiceClient.getClient();
70
 
71
	         //The history client is used to update the inventory history on the processing system.
72
            CatalogClient historyCatalogServiceClient = new CatalogClient(
73
                    ConfigClientKeys.inventory_history_service_server_host.toString(),
74
                    ConfigClientKeys.inventory_history_service_server_port.toString());
75
			Client historyClient = historyCatalogServiceClient.getClient();
76
 
490 rajveer 77
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
78
			String timestamp = inventoryUpdate.getCurrentTimestamp();
79
			Map<String, Long> availability = new HashMap<String, Long>();
80
			List<Update> updates = inventoryUpdate.getUpdates();
81
			for(Update update: updates){
1367 chandransh 82
				String key = update.getKey();
490 rajveer 83
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 84
				if(availability.containsKey(key)){
85
					quantity = quantity + availability.get(key);
490 rajveer 86
				}
616 chandransh 87
				availability.put(key, quantity);
490 rajveer 88
			}
3326 chandransh 89
			//Update the snapshot before the history since that is more important.
90
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
91
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
490 rajveer 92
		} catch (Exception e) {
3104 chandransh 93
			logger.error("Unable to update inventory", e);
490 rajveer 94
		}
95
 
96
		return new DefaultHttpHeaders("psuccess");
97
 
98
	}
99
 
100
	public int getErrorCode() {
101
		return errorCode;
102
	}
103
 
104
	public String getErrorMessage() {
105
		return errorMessage;
106
	}
107
 
108
	public String getId(){
109
		return id;
110
	}
111
 
112
	public void setId(String id){
113
		this.id = id;
114
	}
115
 
116
	public InventoryUpdate getInventoryUpdate() {
117
		return inventoryUpdate;
118
	}
119
 
120
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
121
		this.inventoryUpdate = inventoryUpdate;
122
	}
123
 
124
	public void setTimestamp(String timestamp) {
125
		this.timestamp = timestamp;
126
	}
127
 
128
	public String getTimestamp() {
129
		return timestamp;
616 chandransh 130
	}	
490 rajveer 131
}