Subversion Repositories SmartDukaan

Rev

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