Subversion Repositories SmartDukaan

Rev

Rev 5223 | Rev 5231 | 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
 
197
                List<VendorItemMapping> mappings = snapshotClient.getVendorItemMappings(item.getId());
198
                if (mappings != null) {
199
                    for (VendorItemMapping mapping : mappings) {
200
                        if (mapping.getVendorId() == 1) {
5223 mandeep.dh 201
                            PLBDetails plbDetails = currentInventory.get(mapping.getItemKey());
202
                            Long currentSnapshotAvailibility = item.getItemInventory().getAvailability().get(warehouseId);
203
                            if ((currentSnapshotAvailibility == null || currentSnapshotAvailibility.equals(0)) &&
204
                                (plbDetails == null || plbDetails.getQuantity().equals(0)))
205
                            {
206
                                continue;
5212 mandeep.dh 207
                            }
5223 mandeep.dh 208
 
209
                            if (currentSnapshotAvailibility == null || plbDetails == null ||
210
                                !currentSnapshotAvailibility.equals(plbDetails.getQuantity().longValue()))
211
                            {
212
                                mismatches.put(item, plbDetails);
213
                            }
5212 mandeep.dh 214
                        }
215
                    }
216
                }
217
            }
218
 
219
            String subject = mismatches.size() + " mismatches with PLB sync for warehouse id: " + warehouseId;
5223 mandeep.dh 220
            StringBuilder body = new StringBuilder("Item Id\tBrand\tModel Name\tModel Number\tColor\tSpice Online Retail\tHotspot\n");
5212 mandeep.dh 221
            for (Item item : mismatches.keySet()) {
5223 mandeep.dh 222
                Long currentSnapshotAvailability = item.getItemInventory().getAvailability().get(warehouseId);
223
                String plbQuantity = mismatches.get(item) == null ? "0" : mismatches.get(item).getQuantity().toString();
224
                String currentSnapshotQuantity = currentSnapshotAvailability == null ? "0" : currentSnapshotAvailability.toString();
225
                body.append(item.getId() + "\t" + item.getBrand() + "\t" + item.getModelName() + "\t" + item.getModelNumber() + "\t" + item.getColor() + "\t" + currentSnapshotQuantity + "\t" + plbQuantity + "\n");
5212 mandeep.dh 226
            }
5220 mandeep.dh 227
 
228
            logger.info(body.toString());
5212 mandeep.dh 229
            GmailUtils g = new GmailUtils();
5223 mandeep.dh 230
            g.sendSSLMessage(new String[]{ "mandeep.dhir@shop2020.in" }, subject, body.toString(), "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
5212 mandeep.dh 231
        }
232
    }
233
 
490 rajveer 234
	public int getErrorCode() {
235
		return errorCode;
236
	}
237
 
238
	public String getErrorMessage() {
239
		return errorMessage;
240
	}
241
 
242
	public String getId(){
243
		return id;
244
	}
245
 
246
	public void setId(String id){
247
		this.id = id;
248
	}
249
 
250
	public InventoryUpdate getInventoryUpdate() {
251
		return inventoryUpdate;
252
	}
253
 
254
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
255
		this.inventoryUpdate = inventoryUpdate;
256
	}
257
 
4987 mandeep.dh 258
	public void setTimestampAndItemIds(String timestampAndItemIds) {
259
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 260
	}
261
 
4987 mandeep.dh 262
	public String getTimestampAndItemIds() {
263
		return timestampAndItemIds;
5210 mandeep.dh 264
	}
265
 
266
    public boolean isFullPlbSync() {
267
        return fullPlbSync;
268
    }
269
 
270
    public void setFullPlbSync(boolean fullPlbSync) {
271
        this.fullPlbSync = fullPlbSync;
272
    }	
490 rajveer 273
}