Subversion Repositories SmartDukaan

Rev

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