Subversion Repositories SmartDukaan

Rev

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