Subversion Repositories SmartDukaan

Rev

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