Subversion Repositories SmartDukaan

Rev

Rev 5221 | Rev 5230 | 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();
133
			for(Update update: updates){
1367 chandransh 134
				String key = update.getKey();
490 rajveer 135
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 136
				if(availability.containsKey(key)){
137
					quantity = quantity + availability.get(key);
490 rajveer 138
				}
616 chandransh 139
				availability.put(key, quantity);
490 rajveer 140
			}
4585 rajveer 141
 
3326 chandransh 142
			//Update the snapshot before the history since that is more important.
143
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
144
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
4585 rajveer 145
 
146
			List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
147
			if(billingUpdates!=null){
148
				for(BillingUpdate billingUpdate: billingUpdates){
4765 rajveer 149
					tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());
4585 rajveer 150
				}
151
			}
4987 mandeep.dh 152
 
153
			List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
154
			if (plbDetails != null) {
5212 mandeep.dh 155
			    Map<String, PLBDetails> currentInventory = new HashMap<String, PLBDetails>();
4987 mandeep.dh 156
			    for (PLBDetails plbDetail : plbDetails) {
157
			        try {
5212 mandeep.dh 158
			            if (fullPlbSync) {
159
			                currentInventory.put(plbDetail.getItemKey(), plbDetail);
160
			            }
161
			            else {
162
	                        snapshotClient.resetAvailability(
163
	                                plbDetail.getItemKey(), 1,
164
	                                plbDetail.getQuantity().longValue(), warehouseId);
165
	                        snapshotClient.markMissedInventoryUpdatesAsProcessed(
166
	                                plbDetail.getItemKey(), warehouseId);			                
167
			            }
4987 mandeep.dh 168
                    } catch (Exception e) {
169
                        logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
4990 mandeep.dh 170
                        GmailUtils g = new GmailUtils();
171
                        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 172
                    }
173
			    }
5212 mandeep.dh 174
 
5218 mandeep.dh 175
			    sendMailForPLBMismatches(warehouseId, currentInventory);
4987 mandeep.dh 176
			}
490 rajveer 177
		} catch (Exception e) {
3104 chandransh 178
			logger.error("Unable to update inventory", e);
490 rajveer 179
		}
5220 mandeep.dh 180
 
490 rajveer 181
		return new DefaultHttpHeaders("psuccess");
182
	}
183
 
5218 mandeep.dh 184
    private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
5212 mandeep.dh 185
            throws InventoryServiceException, TException, MessagingException {
186
        if (fullPlbSync) {
187
            Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
5218 mandeep.dh 188
            Client snapshotClient = new CatalogClient().getClient();
5212 mandeep.dh 189
            List<Item> items = snapshotClient.getAllItems(true);
190
            for (Item item : items) {
191
                if (ItemType.NON_SERIALIZED.equals(item.getType())) {
192
                    continue;
193
                }
194
 
195
                List<VendorItemMapping> mappings = snapshotClient.getVendorItemMappings(item.getId());
196
                if (mappings != null) {
197
                    for (VendorItemMapping mapping : mappings) {
198
                        if (mapping.getVendorId() == 1) {
5223 mandeep.dh 199
                            PLBDetails plbDetails = currentInventory.get(mapping.getItemKey());
200
                            Long currentSnapshotAvailibility = item.getItemInventory().getAvailability().get(warehouseId);
201
                            if ((currentSnapshotAvailibility == null || currentSnapshotAvailibility.equals(0)) &&
202
                                (plbDetails == null || plbDetails.getQuantity().equals(0)))
203
                            {
204
                                continue;
5212 mandeep.dh 205
                            }
5223 mandeep.dh 206
 
207
                            if (currentSnapshotAvailibility == null || plbDetails == null ||
208
                                !currentSnapshotAvailibility.equals(plbDetails.getQuantity().longValue()))
209
                            {
210
                                mismatches.put(item, plbDetails);
211
                            }
5212 mandeep.dh 212
                        }
213
                    }
214
                }
215
            }
216
 
217
            String subject = mismatches.size() + " mismatches with PLB sync for warehouse id: " + warehouseId;
5223 mandeep.dh 218
            StringBuilder body = new StringBuilder("Item Id\tBrand\tModel Name\tModel Number\tColor\tSpice Online Retail\tHotspot\n");
5212 mandeep.dh 219
            for (Item item : mismatches.keySet()) {
5223 mandeep.dh 220
                Long currentSnapshotAvailability = item.getItemInventory().getAvailability().get(warehouseId);
221
                String plbQuantity = mismatches.get(item) == null ? "0" : mismatches.get(item).getQuantity().toString();
222
                String currentSnapshotQuantity = currentSnapshotAvailability == null ? "0" : currentSnapshotAvailability.toString();
223
                body.append(item.getId() + "\t" + item.getBrand() + "\t" + item.getModelName() + "\t" + item.getModelNumber() + "\t" + item.getColor() + "\t" + currentSnapshotQuantity + "\t" + plbQuantity + "\n");
5212 mandeep.dh 224
            }
5220 mandeep.dh 225
 
226
            logger.info(body.toString());
5212 mandeep.dh 227
            GmailUtils g = new GmailUtils();
5223 mandeep.dh 228
            g.sendSSLMessage(new String[]{ "mandeep.dhir@shop2020.in" }, subject, body.toString(), "cnc.center@shop2020.in", "5h0p2o2o", new ArrayList<File>());
5212 mandeep.dh 229
        }
230
    }
231
 
490 rajveer 232
	public int getErrorCode() {
233
		return errorCode;
234
	}
235
 
236
	public String getErrorMessage() {
237
		return errorMessage;
238
	}
239
 
240
	public String getId(){
241
		return id;
242
	}
243
 
244
	public void setId(String id){
245
		this.id = id;
246
	}
247
 
248
	public InventoryUpdate getInventoryUpdate() {
249
		return inventoryUpdate;
250
	}
251
 
252
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
253
		this.inventoryUpdate = inventoryUpdate;
254
	}
255
 
4987 mandeep.dh 256
	public void setTimestampAndItemIds(String timestampAndItemIds) {
257
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 258
	}
259
 
4987 mandeep.dh 260
	public String getTimestampAndItemIds() {
261
		return timestampAndItemIds;
5210 mandeep.dh 262
	}
263
 
264
    public boolean isFullPlbSync() {
265
        return fullPlbSync;
266
    }
267
 
268
    public void setFullPlbSync(boolean fullPlbSync) {
269
        this.fullPlbSync = fullPlbSync;
270
    }	
490 rajveer 271
}