Subversion Repositories SmartDukaan

Rev

Rev 4990 | Rev 5212 | 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;
5210 mandeep.dh 4
import in.shop2020.model.v1.catalog.Item;
5
import in.shop2020.model.v1.catalog.ItemType;
6
import in.shop2020.model.v1.catalog.VendorItemMapping;
490 rajveer 7
import in.shop2020.model.v1.catalog.Warehouse;
4585 rajveer 8
import in.shop2020.support.models.BillingUpdate;
490 rajveer 9
import in.shop2020.support.models.InventoryUpdate;
4987 mandeep.dh 10
import in.shop2020.support.models.PLBDetails;
490 rajveer 11
import in.shop2020.support.models.Update;
3125 rajveer 12
import in.shop2020.thrift.clients.CatalogClient;
4585 rajveer 13
import in.shop2020.thrift.clients.TransactionClient;
3326 chandransh 14
import in.shop2020.utils.ConfigClientKeys;
4990 mandeep.dh 15
import in.shop2020.utils.GmailUtils;
490 rajveer 16
 
4990 mandeep.dh 17
import java.io.File;
18
import java.util.ArrayList;
4987 mandeep.dh 19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
 
490 rajveer 23
import org.apache.struts2.rest.DefaultHttpHeaders;
24
import org.apache.struts2.rest.HttpHeaders;
3104 chandransh 25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27
 
490 rajveer 28
import com.opensymphony.xwork2.ModelDriven;
29
 
1367 chandransh 30
public class UpdatesController implements ModelDriven<InventoryUpdate>{
490 rajveer 31
 
4987 mandeep.dh 32
    /**
33
     * 
34
     */
35
    private static final String FIELD_DELIMITER = "~#~#~#";
36
 
3104 chandransh 37
    private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
38
 
490 rajveer 39
	private InventoryUpdate inventoryUpdate = new InventoryUpdate();
4987 mandeep.dh 40
	private String timestampAndItemIds;
490 rajveer 41
 
42
	private int errorCode = 0;
43
	private String errorMessage;
5210 mandeep.dh 44
	private boolean fullPlbSync = false;
490 rajveer 45
	private String id;
46
 
47
 
48
	public UpdatesController(){
49
 
50
	}
51
 
52
	@Override
1367 chandransh 53
	public InventoryUpdate getModel() {
490 rajveer 54
		return this.inventoryUpdate;
55
	}
56
 
57
	public HttpHeaders show(){
3104 chandransh 58
		logger.info("Status requested for warehouse id: " + getId());
3125 rajveer 59
		CatalogClient catalogServiceClient;
490 rajveer 60
		Client client;
61
 
62
		try {
3125 rajveer 63
			catalogServiceClient = new CatalogClient();
490 rajveer 64
			client = catalogServiceClient.getClient();
65
			long warehouseId = Long.parseLong(getId());
66
			Warehouse warehouse = client.getWarehouse(warehouseId);
4987 mandeep.dh 67
			timestampAndItemIds = warehouse.getVendorString();
5210 mandeep.dh 68
 
4987 mandeep.dh 69
			List<String> itemKeys = client.getItemKeysToBeProcessed(warehouseId);
5210 mandeep.dh 70
			if (fullPlbSync) {
71
			    itemKeys = new ArrayList<String>();
72
			    List<Item> items = client.getAllItems(true);
73
			    for (Item item : items) {
74
			        if (ItemType.NON_SERIALIZED.equals(item.getType())) {
75
			            continue;
76
			        }
77
 
78
			        List<VendorItemMapping> mappings = client.getVendorItemMappings(item.getId());
79
			        if (mappings != null) {
80
			            for (VendorItemMapping mapping : mappings) {
81
			                if (mapping.getVendorId() == 1) {
82
			                    itemKeys.add(mapping.getItemKey());
83
			                    break;
84
			                }
85
			            }
86
			        }
87
			    }
88
			}
89
 
4987 mandeep.dh 90
			if (!itemKeys.isEmpty()) {
91
			    for (String itemKey : itemKeys) {
92
			        timestampAndItemIds += FIELD_DELIMITER + itemKey;
93
			    }
94
			}
490 rajveer 95
		} catch (Exception e) {
3104 chandransh 96
			logger.error("Error getting the warehouse or setting the timestamp", e);
490 rajveer 97
		}
98
		return new DefaultHttpHeaders("lsuccess");
99
	}
100
 
3326 chandransh 101
	/**
102
	 * Stores the inventory updates on the history and the production server.
103
	 * @return
104
	 */
490 rajveer 105
	public HttpHeaders create(){
3104 chandransh 106
		logger.info(inventoryUpdate.toString());
490 rajveer 107
		try {
3326 chandransh 108
		    //The snapshot client is used to update the inventory snapshot for production systems.
109
		    CatalogClient snapshotCatalogServiceClient = new CatalogClient();
110
			Client snapshotClient = snapshotCatalogServiceClient.getClient();
111
 
112
	         //The history client is used to update the inventory history on the processing system.
113
            CatalogClient historyCatalogServiceClient = new CatalogClient(
114
                    ConfigClientKeys.inventory_history_service_server_host.toString(),
115
                    ConfigClientKeys.inventory_history_service_server_port.toString());
116
			Client historyClient = historyCatalogServiceClient.getClient();
4585 rajveer 117
 
118
		    //The snapshot client is used to update the inventory snapshot for production systems.
119
		    TransactionClient transactionClient = new TransactionClient();
120
			in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
121
 
490 rajveer 122
			Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
123
			String timestamp = inventoryUpdate.getCurrentTimestamp();
124
			Map<String, Long> availability = new HashMap<String, Long>();
125
			List<Update> updates = inventoryUpdate.getUpdates();
126
			for(Update update: updates){
1367 chandransh 127
				String key = update.getKey();
490 rajveer 128
				long quantity = (long) Double.parseDouble(update.getQuantity());
616 chandransh 129
				if(availability.containsKey(key)){
130
					quantity = quantity + availability.get(key);
490 rajveer 131
				}
616 chandransh 132
				availability.put(key, quantity);
490 rajveer 133
			}
4585 rajveer 134
 
3326 chandransh 135
			//Update the snapshot before the history since that is more important.
136
			snapshotClient.updateInventory(warehouseId, timestamp, availability);
137
			historyClient.updateInventoryHistory(warehouseId, timestamp, availability);
4585 rajveer 138
 
139
			List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
140
			if(billingUpdates!=null){
141
				for(BillingUpdate billingUpdate: billingUpdates){
4765 rajveer 142
					tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor());
4585 rajveer 143
				}
144
			}
4987 mandeep.dh 145
 
146
			List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
147
			if (plbDetails != null) {
148
			    for (PLBDetails plbDetail : plbDetails) {
149
			        try {
150
                        snapshotClient.resetAvailability(
151
                                plbDetail.getItemKey(), 1,
4990 mandeep.dh 152
                                plbDetail.getQuantity().longValue(), warehouseId);
4987 mandeep.dh 153
                        snapshotClient.markMissedInventoryUpdatesAsProcessed(
154
                                plbDetail.getItemKey(), warehouseId);
155
                    } catch (Exception e) {
156
                        logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
4990 mandeep.dh 157
                        GmailUtils g = new GmailUtils();
158
                        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 159
                    }
160
			    }
161
			}
490 rajveer 162
		} catch (Exception e) {
3104 chandransh 163
			logger.error("Unable to update inventory", e);
490 rajveer 164
		}
165
 
166
		return new DefaultHttpHeaders("psuccess");
167
 
168
	}
169
 
170
	public int getErrorCode() {
171
		return errorCode;
172
	}
173
 
174
	public String getErrorMessage() {
175
		return errorMessage;
176
	}
177
 
178
	public String getId(){
179
		return id;
180
	}
181
 
182
	public void setId(String id){
183
		this.id = id;
184
	}
185
 
186
	public InventoryUpdate getInventoryUpdate() {
187
		return inventoryUpdate;
188
	}
189
 
190
	public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
191
		this.inventoryUpdate = inventoryUpdate;
192
	}
193
 
4987 mandeep.dh 194
	public void setTimestampAndItemIds(String timestampAndItemIds) {
195
		this.timestampAndItemIds = timestampAndItemIds;
490 rajveer 196
	}
197
 
4987 mandeep.dh 198
	public String getTimestampAndItemIds() {
199
		return timestampAndItemIds;
5210 mandeep.dh 200
	}
201
 
202
    public boolean isFullPlbSync() {
203
        return fullPlbSync;
204
    }
205
 
206
    public void setFullPlbSync(boolean fullPlbSync) {
207
        this.fullPlbSync = fullPlbSync;
208
    }	
490 rajveer 209
}