Subversion Repositories SmartDukaan

Rev

Rev 5215 | Rev 5220 | 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;
5212 mandeep.dh 4
import in.shop2020.model.v1.catalog.InventoryServiceException;
5210 mandeep.dh 5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.catalog.ItemType;
7
import in.shop2020.model.v1.catalog.VendorItemMapping;
490 rajveer 8
import in.shop2020.model.v1.catalog.Warehouse;
4585 rajveer 9
import in.shop2020.support.models.BillingUpdate;
490 rajveer 10
import in.shop2020.support.models.InventoryUpdate;
4987 mandeep.dh 11
import in.shop2020.support.models.PLBDetails;
490 rajveer 12
import in.shop2020.support.models.Update;
3125 rajveer 13
import in.shop2020.thrift.clients.CatalogClient;
4585 rajveer 14
import in.shop2020.thrift.clients.TransactionClient;
3326 chandransh 15
import in.shop2020.utils.ConfigClientKeys;
4990 mandeep.dh 16
import in.shop2020.utils.GmailUtils;
490 rajveer 17
 
4990 mandeep.dh 18
import java.io.File;
19
import java.util.ArrayList;
4987 mandeep.dh 20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
 
5212 mandeep.dh 24
import javax.mail.MessagingException;
25
 
490 rajveer 26
import org.apache.struts2.rest.DefaultHttpHeaders;
27
import org.apache.struts2.rest.HttpHeaders;
5212 mandeep.dh 28
import org.apache.thrift.TException;
3104 chandransh 29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31
 
490 rajveer 32
import com.opensymphony.xwork2.ModelDriven;
33
 
1367 chandransh 34
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 35
 
4987 mandeep.dh 36
    /**
37
     * 
38
     */
39
    private static final String FIELD_DELIMITER = "~#~#~#";
40
 
3104 chandransh 41
    private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
42
 
490 rajveer 43
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
4987 mandeep.dh 44
	private String timestampAndItemIds;
490 rajveer 45
 
46
	private int errorCode = 0;
47
	private String errorMessage;
5210 mandeep.dh 48
	private boolean fullPlbSync = false;
490 rajveer 49
	private String id;
50
 
51
 
52
	public UpdatesController(){
53
 
54
	}
55
 
56
	@Override
1367 chandransh 57
	public InventoryUpdate getModel() {
490 rajveer 58
		return this.inventoryUpdate;
59
	}
60
 
61
	public HttpHeaders show(){
3104 chandransh 62
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 63
		CatalogClient catalogServiceClient;
490 rajveer 64
		Client client;
65
 
66
		try {
3125 rajveer 67
			catalogServiceClient = new CatalogClient();
490 rajveer 68
			client = catalogServiceClient.getClient();
69
			long warehouseId = Long.parseLong(getId());
70
			Warehouse warehouse = client.getWarehouse(warehouseId);
4987 mandeep.dh 71
			timestampAndItemIds = warehouse.getVendorString();
5210 mandeep.dh 72
 
4987 mandeep.dh 73
			List<String> itemKeys = client.getItemKeysToBeProcessed(warehouseId);
5210 mandeep.dh 74
			if (fullPlbSync) {
75
			    itemKeys = new ArrayList<String>();
76
			    List<Item> items = client.getAllItems(true);
77
			    for (Item item : items) {
78
			        if (ItemType.NON_SERIALIZED.equals(item.getType())) {
79
			            continue;
80
			        }
81
 
82
			        List<VendorItemMapping> mappings = client.getVendorItemMappings(item.getId());
83
			        if (mappings != null) {
84
			            for (VendorItemMapping mapping : mappings) {
85
			                if (mapping.getVendorId() == 1) {
86
			                    itemKeys.add(mapping.getItemKey());
87
			                    break;
88
			                }
89
			            }
90
			        }
91
			    }
92
			}
93
 
4987 mandeep.dh 94
			if (!itemKeys.isEmpty()) {
95
			    for (String itemKey : itemKeys) {
96
			        timestampAndItemIds += FIELD_DELIMITER + itemKey;
97
			    }
98
			}
490 rajveer 99
		} catch (Exception e) {
3104 chandransh 100
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 101
		}
102
		return new DefaultHttpHeaders("lsuccess");
103
	}
104
 
3326 chandransh 105
	/**
106
	 * Stores the inventory updates on the history and the production server.
107
	 * @return
108
	 */
490 rajveer 109
	public HttpHeaders create(){
3104 chandransh 110
		logger.info(inventoryUpdate.toString());
490 rajveer 111
		try {
3326 chandransh 112
		    //The snapshot client is used to update the inventory snapshot for production systems.
113
		    CatalogClient snapshotCatalogServiceClient = new CatalogClient();
114
			Client snapshotClient = snapshotCatalogServiceClient.getClient();
115
 
116
	         //The history client is used to update the inventory history on the processing system.
117
            CatalogClient historyCatalogServiceClient = new CatalogClient(
118
                    ConfigClientKeys.inventory_history_service_server_host.toString(),
119
                    ConfigClientKeys.inventory_history_service_server_port.toString());
120
			Client historyClient = historyCatalogServiceClient.getClient();
4585 rajveer 121
 
122
		    //The snapshot client is used to update the inventory snapshot for production systems.
123
		    TransactionClient transactionClient = new TransactionClient();
124
			in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
125
 
490 rajveer 126
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
127
			String timestamp = inventoryUpdate.getCurrentTimestamp();
128
			Map<String, Long> availability = new HashMap<String, Long>();
129
			List<Update> updates = inventoryUpdate.getUpdates();
130
			for(Update update: updates){
1367 chandransh 131
				String key = update.getKey();
490 rajveer 132
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 133
				if(availability.containsKey(key)){
134
					quantity = quantity + availability.get(key);
490 rajveer 135
				}
616 chandransh 136
				availability.put(key, quantity);
490 rajveer 137
			}
4585 rajveer 138
 
3326 chandransh 139
			//Update the snapshot before the history since that is more important.
140
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
141
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
4585 rajveer 142
 
143
			List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
144
			if(billingUpdates!=null){
145
				for(BillingUpdate billingUpdate: billingUpdates){
4765 rajveer 146
					tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());
4585 rajveer 147
				}
148
			}
4987 mandeep.dh 149
 
150
			List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
151
			if (plbDetails != null) {
5212 mandeep.dh 152
			    Map<String, PLBDetails> currentInventory = new HashMap<String, PLBDetails>();
4987 mandeep.dh 153
			    for (PLBDetails plbDetail : plbDetails) {
154
			        try {
5212 mandeep.dh 155
			            if (fullPlbSync) {
156
			                currentInventory.put(plbDetail.getItemKey(), plbDetail);
157
			            }
158
			            else {
159
	                        snapshotClient.resetAvailability(
160
	                                plbDetail.getItemKey(), 1,
161
	                                plbDetail.getQuantity().longValue(), warehouseId);
162
	                        snapshotClient.markMissedInventoryUpdatesAsProcessed(
163
	                                plbDetail.getItemKey(), warehouseId);			                
164
			            }
4987 mandeep.dh 165
                    } catch (Exception e) {
166
                        logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
4990 mandeep.dh 167
                        GmailUtils g = new GmailUtils();
168
                        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 169
                    }
170
			    }
5212 mandeep.dh 171
 
5218 mandeep.dh 172
			    sendMailForPLBMismatches(warehouseId, currentInventory);
4987 mandeep.dh 173
			}
490 rajveer 174
		} catch (Exception e) {
3104 chandransh 175
			logger.error("Unable to update inventory", e);
490 rajveer 176
		}
177
 
178
		return new DefaultHttpHeaders("psuccess");
179
 
180
	}
181
 
5218 mandeep.dh 182
    private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
5212 mandeep.dh 183
            throws InventoryServiceException, TException, MessagingException {
184
        if (fullPlbSync) {
185
            Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
5218 mandeep.dh 186
            Client snapshotClient = new CatalogClient().getClient();
5212 mandeep.dh 187
            List<Item> items = snapshotClient.getAllItems(true);
188
            for (Item item : items) {
189
                if (ItemType.NON_SERIALIZED.equals(item.getType())) {
190
                    continue;
191
                }
192
 
193
                List<VendorItemMapping> mappings = snapshotClient.getVendorItemMappings(item.getId());
194
                if (mappings != null) {
195
                    for (VendorItemMapping mapping : mappings) {
196
                        if (mapping.getVendorId() == 1) {
5215 mandeep.dh 197
                            try {
198
                                if (!item.getItemInventory().getAvailability().get(warehouseId).equals(currentInventory.get(mapping.getItemKey()).getQuantity().longValue())) {
199
                                    mismatches.put(item, currentInventory.get(mapping.getItemKey()));
200
                                }
201
                            } catch (Exception e) {
5212 mandeep.dh 202
                                mismatches.put(item, currentInventory.get(mapping.getItemKey()));
203
                            }
204
                        }
205
                    }
206
                }
207
            }
208
 
209
            String subject = mismatches.size() + " mismatches with PLB sync for warehouse id: " + warehouseId;
210
            StringBuilder body = new StringBuilder("");
211
            for (Item item : mismatches.keySet()) {
5215 mandeep.dh 212
                PLBDetails plbDetails = mismatches.get(item);
213
                if (plbDetails != null) {
214
                    body.append(item.getId() + " " + plbDetails.getItemKey() + " " + item.getItemInventory().getAvailability().get(warehouseId) + " " + plbDetails.getQuantity().longValue() + "\n");
215
                }
216
                else {
217
                    body.append(item.getId() + " " + item.getBrand() + "|" + item.getModelName() + "|" + item.getModelNumber() + "|" + item.getColor() + " " + item.getItemInventory().getAvailability().get(warehouseId) + " 0\n");
218
                }                
5212 mandeep.dh 219
            }
220
            GmailUtils g = new GmailUtils();
221
            g.sendSSLMessage(new String[]{ "mandeep.dhir@shop2020.in" }, subject, body.toString(), "cnc.center@shop2020.in", "5hop2o2o", new ArrayList<File>());
222
        }
223
    }
224
 
490 rajveer 225
	public int getErrorCode() {
226
		return errorCode;
227
	}
228
 
229
	public String getErrorMessage() {
230
		return errorMessage;
231
	}
232
 
233
	public String getId(){
234
		return id;
235
	}
236
 
237
	public void setId(String id){
238
		this.id = id;
239
	}
240
 
241
	public InventoryUpdate getInventoryUpdate() {
242
		return inventoryUpdate;
243
	}
244
 
245
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
246
		this.inventoryUpdate = inventoryUpdate;
247
	}
248
 
4987 mandeep.dh 249
	public void setTimestampAndItemIds(String timestampAndItemIds) {
250
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 251
	}
252
 
4987 mandeep.dh 253
	public String getTimestampAndItemIds() {
254
		return timestampAndItemIds;
5210 mandeep.dh 255
	}
256
 
257
    public boolean isFullPlbSync() {
258
        return fullPlbSync;
259
    }
260
 
261
    public void setFullPlbSync(boolean fullPlbSync) {
262
        this.fullPlbSync = fullPlbSync;
263
    }	
490 rajveer 264
}