Subversion Repositories SmartDukaan

Rev

Rev 4765 | Rev 4990 | 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 in.shop2020.model.v1.catalog.InventoryService.Client;
4
import in.shop2020.model.v1.catalog.Warehouse;
4585 rajveer 5
import in.shop2020.support.models.BillingUpdate;
490 rajveer 6
import in.shop2020.support.models.InventoryUpdate;
4987 mandeep.dh 7
import in.shop2020.support.models.PLBDetails;
490 rajveer 8
import in.shop2020.support.models.Update;
3125 rajveer 9
import in.shop2020.thrift.clients.CatalogClient;
4585 rajveer 10
import in.shop2020.thrift.clients.TransactionClient;
3326 chandransh 11
import in.shop2020.utils.ConfigClientKeys;
490 rajveer 12
 
4987 mandeep.dh 13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
 
490 rajveer 17
import org.apache.struts2.rest.DefaultHttpHeaders;
18
import org.apache.struts2.rest.HttpHeaders;
3104 chandransh 19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21
 
490 rajveer 22
import com.opensymphony.xwork2.ModelDriven;
23
 
1367 chandransh 24
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 25
 
4987 mandeep.dh 26
    /**
27
     * 
28
     */
29
    private static final String FIELD_DELIMITER = "~#~#~#";
30
 
3104 chandransh 31
    private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
32
 
490 rajveer 33
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
4987 mandeep.dh 34
	private String timestampAndItemIds;
490 rajveer 35
 
36
	private int errorCode = 0;
37
	private String errorMessage;
38
 
39
	private String id;
40
 
41
 
42
	public UpdatesController(){
43
 
44
	}
45
 
46
	@Override
1367 chandransh 47
	public InventoryUpdate getModel() {
490 rajveer 48
		return this.inventoryUpdate;
49
	}
50
 
51
	public HttpHeaders show(){
3104 chandransh 52
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 53
		CatalogClient catalogServiceClient;
490 rajveer 54
		Client client;
55
 
56
		try {
3125 rajveer 57
			catalogServiceClient = new CatalogClient();
490 rajveer 58
			client = catalogServiceClient.getClient();
59
			long warehouseId = Long.parseLong(getId());
60
			Warehouse warehouse = client.getWarehouse(warehouseId);
4987 mandeep.dh 61
			timestampAndItemIds = warehouse.getVendorString();
62
 
63
			List<String> itemKeys = client.getItemKeysToBeProcessed(warehouseId);
64
			if (!itemKeys.isEmpty()) {
65
			    for (String itemKey : itemKeys) {
66
			        timestampAndItemIds += FIELD_DELIMITER + itemKey;
67
			    }
68
			}
490 rajveer 69
		} catch (Exception e) {
3104 chandransh 70
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 71
		}
72
		return new DefaultHttpHeaders("lsuccess");
73
	}
74
 
3326 chandransh 75
	/**
76
	 * Stores the inventory updates on the history and the production server.
77
	 * @return
78
	 */
490 rajveer 79
	public HttpHeaders create(){
3104 chandransh 80
		logger.info(inventoryUpdate.toString());
490 rajveer 81
		try {
3326 chandransh 82
		    //The snapshot client is used to update the inventory snapshot for production systems.
83
		    CatalogClient snapshotCatalogServiceClient = new CatalogClient();
84
			Client snapshotClient = snapshotCatalogServiceClient.getClient();
85
 
86
	         //The history client is used to update the inventory history on the processing system.
87
            CatalogClient historyCatalogServiceClient = new CatalogClient(
88
                    ConfigClientKeys.inventory_history_service_server_host.toString(),
89
                    ConfigClientKeys.inventory_history_service_server_port.toString());
90
			Client historyClient = historyCatalogServiceClient.getClient();
4585 rajveer 91
 
92
		    //The snapshot client is used to update the inventory snapshot for production systems.
93
		    TransactionClient transactionClient = new TransactionClient();
94
			in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
95
 
490 rajveer 96
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
97
			String timestamp = inventoryUpdate.getCurrentTimestamp();
98
			Map<String, Long> availability = new HashMap<String, Long>();
99
			List<Update> updates = inventoryUpdate.getUpdates();
100
			for(Update update: updates){
1367 chandransh 101
				String key = update.getKey();
490 rajveer 102
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 103
				if(availability.containsKey(key)){
104
					quantity = quantity + availability.get(key);
490 rajveer 105
				}
616 chandransh 106
				availability.put(key, quantity);
490 rajveer 107
			}
4585 rajveer 108
 
3326 chandransh 109
			//Update the snapshot before the history since that is more important.
110
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
111
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
4585 rajveer 112
 
113
			List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
114
			if(billingUpdates!=null){
115
				for(BillingUpdate billingUpdate: billingUpdates){
4765 rajveer 116
					tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());
4585 rajveer 117
				}
118
			}
4987 mandeep.dh 119
 
120
			List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
121
			if (plbDetails != null) {
122
			    for (PLBDetails plbDetail : plbDetails) {
123
			        try {
124
                        snapshotClient.resetAvailability(
125
                                plbDetail.getItemKey(), 1,
126
                                plbDetail.getQuantity(), warehouseId);
127
                        snapshotClient.markMissedInventoryUpdatesAsProcessed(
128
                                plbDetail.getItemKey(), warehouseId);
129
                    } catch (Exception e) {
130
                        logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
131
                    }
132
			    }
133
			}
490 rajveer 134
		} catch (Exception e) {
3104 chandransh 135
			logger.error("Unable to update inventory", e);
490 rajveer 136
		}
137
 
138
		return new DefaultHttpHeaders("psuccess");
139
 
140
	}
141
 
142
	public int getErrorCode() {
143
		return errorCode;
144
	}
145
 
146
	public String getErrorMessage() {
147
		return errorMessage;
148
	}
149
 
150
	public String getId(){
151
		return id;
152
	}
153
 
154
	public void setId(String id){
155
		this.id = id;
156
	}
157
 
158
	public InventoryUpdate getInventoryUpdate() {
159
		return inventoryUpdate;
160
	}
161
 
162
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
163
		this.inventoryUpdate = inventoryUpdate;
164
	}
165
 
4987 mandeep.dh 166
	public void setTimestampAndItemIds(String timestampAndItemIds) {
167
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 168
	}
169
 
4987 mandeep.dh 170
	public String getTimestampAndItemIds() {
171
		return timestampAndItemIds;
616 chandransh 172
	}	
490 rajveer 173
}