Subversion Repositories SmartDukaan

Rev

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