Subversion Repositories SmartDukaan

Rev

Rev 5237 | Rev 5241 | 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
 
5237 mandeep.dh 46
	private Map<Long, String> hotspotMappings = new HashMap<Long, String>();
47
 
490 rajveer 48
	private int errorCode = 0;
49
	private String errorMessage;
5210 mandeep.dh 50
	private boolean fullPlbSync = false;
490 rajveer 51
	private String id;
52
 
53
 
54
	public UpdatesController(){
55
 
56
	}
57
 
58
	@Override
1367 chandransh 59
	public InventoryUpdate getModel() {
490 rajveer 60
		return this.inventoryUpdate;
61
	}
62
 
63
	public HttpHeaders show(){
3104 chandransh 64
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 65
		CatalogClient catalogServiceClient;
490 rajveer 66
		Client client;
67
 
68
		try {
5240 mandeep.dh 69
			client = new CatalogClient().getClient();
490 rajveer 70
			long warehouseId = Long.parseLong(getId());
71
			Warehouse warehouse = client.getWarehouse(warehouseId);
4987 mandeep.dh 72
			timestampAndItemIds = warehouse.getVendorString();
5210 mandeep.dh 73
 
5240 mandeep.dh 74
			client = new CatalogClient().getClient();
4987 mandeep.dh 75
			List<String> itemKeys = client.getItemKeysToBeProcessed(warehouseId);
5210 mandeep.dh 76
			if (fullPlbSync) {
5237 mandeep.dh 77
			    loadHotspotMappings();
5210 mandeep.dh 78
			    itemKeys = new ArrayList<String>();
5240 mandeep.dh 79
			    client = new CatalogClient().getClient();
5210 mandeep.dh 80
			    List<Item> items = client.getAllItems(true);
81
			    for (Item item : items) {
82
			        if (ItemType.NON_SERIALIZED.equals(item.getType())) {
83
			            continue;
84
			        }
85
 
5237 mandeep.dh 86
			        if (hotspotMappings.containsKey(item.getId())) {
87
			            itemKeys.add(hotspotMappings.get(item.getId()));
5210 mandeep.dh 88
			        }
89
			    }
90
			}
91
 
4987 mandeep.dh 92
			if (!itemKeys.isEmpty()) {
93
			    for (String itemKey : itemKeys) {
94
			        timestampAndItemIds += FIELD_DELIMITER + itemKey;
95
			    }
96
			}
490 rajveer 97
		} catch (Exception e) {
3104 chandransh 98
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 99
		}
100
		return new DefaultHttpHeaders("lsuccess");
101
	}
102
 
3326 chandransh 103
	/**
5237 mandeep.dh 104
     * 
105
     */
106
    private void loadHotspotMappings() {
107
        try {
108
            Client snapshotClient = new CatalogClient().getClient();
109
            for (VendorItemMapping mapping : snapshotClient
110
                    .getAllVendorItemMappings()) {
111
                if (mapping.getVendorId() == 1) {
112
                    hotspotMappings.put(mapping.getItemId(),
113
                            mapping.getItemKey());
114
                }
115
            }
116
        } catch (Exception e) {
117
            logger.error("Could not load vendor item mappings", e);
118
        }
119
    }
120
 
121
    /**
3326 chandransh 122
	 * Stores the inventory updates on the history and the production server.
123
	 * @return
124
	 */
490 rajveer 125
	public HttpHeaders create(){
3104 chandransh 126
		logger.info(inventoryUpdate.toString());
490 rajveer 127
		try {
3326 chandransh 128
		    //The snapshot client is used to update the inventory snapshot for production systems.
129
		    CatalogClient snapshotCatalogServiceClient = new CatalogClient();
130
			Client snapshotClient = snapshotCatalogServiceClient.getClient();
131
 
132
	         //The history client is used to update the inventory history on the processing system.
133
            CatalogClient historyCatalogServiceClient = new CatalogClient(
134
                    ConfigClientKeys.inventory_history_service_server_host.toString(),
135
                    ConfigClientKeys.inventory_history_service_server_port.toString());
136
			Client historyClient = historyCatalogServiceClient.getClient();
4585 rajveer 137
 
138
		    //The snapshot client is used to update the inventory snapshot for production systems.
139
		    TransactionClient transactionClient = new TransactionClient();
140
			in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
141
 
490 rajveer 142
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
143
			String timestamp = inventoryUpdate.getCurrentTimestamp();
144
			Map<String, Long> availability = new HashMap<String, Long>();
145
			List<Update> updates = inventoryUpdate.getUpdates();
5230 mandeep.dh 146
			if (updates != null) {
147
	            for(Update update: updates){
148
	                String key = update.getKey();
149
	                long quantity = (long) Double.parseDouble(update.getQuantity());
150
	                if(availability.containsKey(key)){
151
	                    quantity = quantity + availability.get(key);
152
	                }
153
	                availability.put(key, quantity);
154
	            }			    
490 rajveer 155
			}
4585 rajveer 156
 
3326 chandransh 157
			//Update the snapshot before the history since that is more important.
158
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
159
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
4585 rajveer 160
 
161
			List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
162
			if(billingUpdates!=null){
163
				for(BillingUpdate billingUpdate: billingUpdates){
4765 rajveer 164
					tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());
4585 rajveer 165
				}
166
			}
4987 mandeep.dh 167
 
168
			List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
169
			if (plbDetails != null) {
5212 mandeep.dh 170
			    Map<String, PLBDetails> currentInventory = new HashMap<String, PLBDetails>();
4987 mandeep.dh 171
			    for (PLBDetails plbDetail : plbDetails) {
172
			        try {
5212 mandeep.dh 173
			            if (fullPlbSync) {
174
			                currentInventory.put(plbDetail.getItemKey(), plbDetail);
175
			            }
176
			            else {
177
	                        snapshotClient.resetAvailability(
178
	                                plbDetail.getItemKey(), 1,
179
	                                plbDetail.getQuantity().longValue(), warehouseId);
180
	                        snapshotClient.markMissedInventoryUpdatesAsProcessed(
181
	                                plbDetail.getItemKey(), warehouseId);			                
182
			            }
4987 mandeep.dh 183
                    } catch (Exception e) {
184
                        logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
4990 mandeep.dh 185
                        GmailUtils g = new GmailUtils();
186
                        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 187
                    }
188
			    }
5212 mandeep.dh 189
 
5218 mandeep.dh 190
			    sendMailForPLBMismatches(warehouseId, currentInventory);
4987 mandeep.dh 191
			}
490 rajveer 192
		} catch (Exception e) {
3104 chandransh 193
			logger.error("Unable to update inventory", e);
490 rajveer 194
		}
5220 mandeep.dh 195
 
490 rajveer 196
		return new DefaultHttpHeaders("psuccess");
197
	}
198
 
5218 mandeep.dh 199
    private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
5212 mandeep.dh 200
            throws InventoryServiceException, TException, MessagingException {
201
        if (fullPlbSync) {
5237 mandeep.dh 202
            loadHotspotMappings();
5212 mandeep.dh 203
            Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
5218 mandeep.dh 204
            Client snapshotClient = new CatalogClient().getClient();
5212 mandeep.dh 205
            List<Item> items = snapshotClient.getAllItems(true);
206
            for (Item item : items) {
207
                if (ItemType.NON_SERIALIZED.equals(item.getType())) {
208
                    continue;
209
                }
210
 
5237 mandeep.dh 211
                if (hotspotMappings.containsKey(item.getId())) {
212
                    PLBDetails plbDetails = currentInventory
213
                            .get(hotspotMappings.get(item.getId()));
214
                    Long currentSnapshotAvailibility = item.getItemInventory()
215
                            .getAvailability().get(warehouseId);
216
                    if ((currentSnapshotAvailibility == null || currentSnapshotAvailibility.equals(0)) && 
217
                        (plbDetails == null || plbDetails.getQuantity().equals(0)))
218
                    {
219
                        continue;
220
                    }
5223 mandeep.dh 221
 
5237 mandeep.dh 222
                    if (currentSnapshotAvailibility == null || plbDetails == null ||
223
                        !currentSnapshotAvailibility.equals(plbDetails.getQuantity().longValue()))
224
                    {
225
                        mismatches.put(item, plbDetails);
5212 mandeep.dh 226
                    }
227
                }
228
            }
229
 
230
            String subject = mismatches.size() + " mismatches with PLB sync for warehouse id: " + warehouseId;
5223 mandeep.dh 231
            StringBuilder body = new StringBuilder("Item Id\tBrand\tModel Name\tModel Number\tColor\tSpice Online Retail\tHotspot\n");
5212 mandeep.dh 232
            for (Item item : mismatches.keySet()) {
5223 mandeep.dh 233
                Long currentSnapshotAvailability = item.getItemInventory().getAvailability().get(warehouseId);
234
                String plbQuantity = mismatches.get(item) == null ? "0" : mismatches.get(item).getQuantity().toString();
235
                String currentSnapshotQuantity = currentSnapshotAvailability == null ? "0" : currentSnapshotAvailability.toString();
236
                body.append(item.getId() + "\t" + item.getBrand() + "\t" + item.getModelName() + "\t" + item.getModelNumber() + "\t" + item.getColor() + "\t" + currentSnapshotQuantity + "\t" + plbQuantity + "\n");
5212 mandeep.dh 237
            }
5220 mandeep.dh 238
 
239
            logger.info(body.toString());
5212 mandeep.dh 240
            GmailUtils g = new GmailUtils();
5223 mandeep.dh 241
            g.sendSSLMessage(new String[]{ "mandeep.dhir@shop2020.in" }, subject, body.toString(), "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
5212 mandeep.dh 242
        }
243
    }
244
 
490 rajveer 245
	public int getErrorCode() {
246
		return errorCode;
247
	}
248
 
249
	public String getErrorMessage() {
250
		return errorMessage;
251
	}
252
 
253
	public String getId(){
254
		return id;
255
	}
256
 
257
	public void setId(String id){
258
		this.id = id;
259
	}
260
 
261
	public InventoryUpdate getInventoryUpdate() {
262
		return inventoryUpdate;
263
	}
264
 
265
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
266
		this.inventoryUpdate = inventoryUpdate;
267
	}
268
 
4987 mandeep.dh 269
	public void setTimestampAndItemIds(String timestampAndItemIds) {
270
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 271
	}
272
 
4987 mandeep.dh 273
	public String getTimestampAndItemIds() {
274
		return timestampAndItemIds;
5210 mandeep.dh 275
	}
276
 
277
    public boolean isFullPlbSync() {
278
        return fullPlbSync;
279
    }
280
 
281
    public void setFullPlbSync(boolean fullPlbSync) {
282
        this.fullPlbSync = fullPlbSync;
283
    }	
490 rajveer 284
}