Rev 4751 | Rev 5845 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.crm.Activity;import in.shop2020.crm.ActivityType;import in.shop2020.crm.SearchFilter;import in.shop2020.crm.Ticket;import in.shop2020.crm.TicketCategory;import in.shop2020.crm.TicketPriority;import in.shop2020.crm.TicketStatus;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.OrderStatus;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.model.v1.user.Address;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.payments.Attribute;import in.shop2020.payments.Constants;import in.shop2020.payments.Payment;import in.shop2020.payments.PaymentException;import in.shop2020.payments.PaymentService.Client;import in.shop2020.serving.auth.CRMAuthorizingRealm;import in.shop2020.serving.model.ShipmentUpdate;import in.shop2020.serving.services.BlueDartTrackingService;import in.shop2020.serving.services.AramexTrackingService;import in.shop2020.thrift.clients.CRMClient;import in.shop2020.thrift.clients.PaymentClient;import in.shop2020.thrift.clients.TransactionClient;import in.shop2020.thrift.clients.UserClient;import in.shop2020.utils.ModelUtils;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.List;import java.util.Set;import java.util.concurrent.Callable;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import net.htmlparser.jericho.Source;import org.apache.log4j.Logger;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;/*** @author vikas**/@SuppressWarnings("serial")public class UserOrderInfoController extends BaseController {private static Logger log = Logger.getLogger(Class.class);private static BlueDartTrackingService blueDartTrackingService = new BlueDartTrackingService();private static AramexTrackingService aramexTrackingService = new AramexTrackingService();private long orderId;private Order order;private List<Payment> payments;private List<ShipmentUpdate> shipmentUpdates = new ArrayList<ShipmentUpdate>();private Long codTicketId;private List<Address> addresses;private Set<OrderStatus> setOfcancellableStates;private String cancellationInitiator;private String cancelReason;private String body;public UserOrderInfoController() {super();setOfcancellableStates = new HashSet<OrderStatus>();setOfcancellableStates.add(OrderStatus.SUBMITTED_FOR_PROCESSING);setOfcancellableStates.add(OrderStatus.INVENTORY_LOW);setOfcancellableStates.add(OrderStatus.LOW_INV_PO_RAISED);setOfcancellableStates.add(OrderStatus.LOW_INV_REVERSAL_IN_PROCESS);setOfcancellableStates.add(OrderStatus.LOW_INV_NOT_AVAILABLE_AT_HOTSPOT);setOfcancellableStates.add(OrderStatus.ACCEPTED);setOfcancellableStates.add(OrderStatus.BILLED);}public String index() {try {PaymentClient paymentServiceClient = new PaymentClient();TransactionClient transactionServiceClient = new TransactionClient();order = transactionServiceClient.getClient().getOrder(orderId);payments = paymentServiceClient.getClient().getPaymentForTxnId(order.getTransactionId());// Spawning a thread to capture shipment updates from Bluedart// This is done to ensure that response from Crm web app is sent// within given time limits. Also, we wont be affected in the cases// where bluedart site is down or slowExecutors.newSingleThreadExecutor().invokeAll(Collections.singletonList(new Callable<Boolean>() {public Boolean call() throws Exception {if (order.getLogistics_provider_id() == 1)shipmentUpdates = blueDartTrackingService.getUpdates(order.getAirwaybill_no());else if (order.getLogistics_provider_id() == 2) {shipmentUpdates = aramexTrackingService.getUpdates(order.getAirwaybill_no());}else {log.error("Error : providerId = " + order.getLogistics_provider_id() + "for orderId : " + order.getId());}return true;}}), 5, TimeUnit.SECONDS);if (order.isCod() && OrderStatus.COD_VERIFICATION_PENDING.equals(order.getStatus())) {populateCODTicketId(order.getCustomer_id());}if (canEditOrderAddress()) {userContextServiceClient = new UserClient().getClient();addresses = userContextServiceClient.getAllAddressesForUser(order.getCustomer_id());}} catch (TTransportException e) {log.error("Unable to create thrift Client", e);} catch (TransactionServiceException e) {addActionError("Invalid order id or no order selected.");} catch (TException e) {log.error("Unable to get thrift Client", e);} catch (PaymentException e) {log.error("Unable to get payments for transctionId : " + order.getTransactionId(), e);} catch (InterruptedException e) {log.error("Thread was interrupted", e);} catch (UserContextException e) {log.error("Unable to get addresses for user : " + order.getCustomer_id(), e);}return INDEX;}private boolean canEditOrderAddress() {return false;}private void populateCODTicketId(long customerId) {try {SearchFilter searchFilter = new SearchFilter();searchFilter.setTicketCategory(TicketCategory.COD_VERIFICATION);searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());searchFilter.getTicketStatuses().add(TicketStatus.OPEN);searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);searchFilter.setCustomerId(customerId);crmServiceClient = new CRMClient().getClient();List<Ticket> tickets = crmServiceClient.getTickets(searchFilter);if (tickets != null && !tickets.isEmpty()) {codTicketId = tickets.get(0).getId();}} catch (TException e) {log.error("Error fetching tickets for customerId: " + customerId, e);}}public String markOrderForCancellation() {try{TransactionClient transactionServiceClient = new TransactionClient();log.info("URL = " + request.getRequestURI());log.info("Initiator = " + request.getParameter("cancellationInitiator"));log.info("orderId = " + request.getParameter("orderId"));if (cancellationInitiator.equals("CUSTOMER")) {order = transactionServiceClient.getClient().getOrder(orderId);transactionServiceClient.getClient().markOrderCancellationRequestReceived(orderId);long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();Ticket ticket = new Ticket();String plainTextbody = "";if(body!=null && !body.isEmpty()){plainTextbody = new Source(body).getTextExtractor().toString();}ticket.setDescription("Creating ticket for Cancellation Request Received, Reason : " + cancelReason + " : " + plainTextbody);ticket.setCreatorId(creatorId);ticket.setStatus(TicketStatus.OPEN);ticket.setPriority(TicketPriority.HIGH);ticket.setCategory(TicketCategory.ORDER_CANCELLATION);ticket.setOrderId(orderId);Activity activity = new Activity();activity.setDescription("Creating Ticket");activity.setType(ActivityType.OTHER);activity.setTicketPriority(TicketPriority.HIGH);activity.setTicketStatus(TicketStatus.OPEN);activity.setCreatorId(creatorId);activity.setTicketDescription("Creating ticket for Cancellation Request Received, Reason : " + cancelReason + " : " + plainTextbody);activity.setTicketCategory(TicketCategory.ORDER_CANCELLATION);ticket.setCustomerId(order.getCustomer_id());activity.setCustomerId(order.getCustomer_id());ticket.setCustomerName(order.getCustomer_name());activity.setCustomerName(order.getCustomer_name());ticket.setCustomerEmailId(order.getCustomer_email());activity.setCustomerEmailId(order.getCustomer_email());ticket.setCustomerMobileNumber(order.getCustomer_mobilenumber());activity.setCustomerMobileNumber(order.getCustomer_mobilenumber());crmServiceClient = new CRMClient().getClient();crmServiceClient.insertTicket(ticket, activity);}else if (cancellationInitiator.equals("INTERNAL")) {String plainTextbody = "";if(body!=null && !body.isEmpty()){plainTextbody = new Source(body).getTextExtractor().toString();}transactionServiceClient.getClient().refundOrder(orderId, currentAgentEmailId, cancelReason + " : " + plainTextbody);long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();Ticket ticket = new Ticket();ticket.setDescription("Creating ticket for Order Cancellation due to Low inventory");ticket.setCreatorId(creatorId);ticket.setStatus(TicketStatus.CLOSED);ticket.setPriority(TicketPriority.MEDIUM);ticket.setCategory(TicketCategory.ORDER_CANCELLATION);ticket.setOrderId(orderId);Activity activity = new Activity();activity.setDescription("Creating Ticket");activity.setType(ActivityType.OTHER);activity.setTicketPriority(TicketPriority.MEDIUM);activity.setTicketStatus(TicketStatus.CLOSED);activity.setCreatorId(creatorId);activity.setTicketCategory(TicketCategory.ORDER_CANCELLATION);activity.setTicketDescription("Creating ticket for Order Cancellation due to Low inventory");ticket.setCustomerId(order.getCustomer_id());activity.setCustomerId(order.getCustomer_id());ticket.setCustomerName(order.getCustomer_name());activity.setCustomerName(order.getCustomer_name());ticket.setCustomerEmailId(order.getCustomer_email());activity.setCustomerEmailId(order.getCustomer_email());ticket.setCustomerMobileNumber(order.getCustomer_mobilenumber());activity.setCustomerMobileNumber(order.getCustomer_mobilenumber());crmServiceClient = new CRMClient().getClient();crmServiceClient.insertTicket(ticket, activity);}} catch(Exception e) {log.error("Could not mark order for Cancellation, OrderId : " + orderId, e);}return index();}public boolean canOrderBeCancelled() {if (setOfcancellableStates.contains(order.getStatus())) {return true;}return false;}public String getPaymentGateway(Payment payment) {String gatewayName = "";try {Client paymentServiceClient = new PaymentClient().getClient();gatewayName = paymentServiceClient.getPaymentGateway(payment.getGatewayId()).getName();} catch (TTransportException e) {} catch (PaymentException e) {} catch (TException e) {}return gatewayName;}public String getProductName(LineItem lineItem) {String name = ModelUtils.extractProductNameFromLineItem(lineItem);if (lineItem.getColor() != null && !lineItem.getColor().isEmpty()) {name += "(" + lineItem.getColor() + ")";}return name;}public int convertDouble(double value) {return (int)value;}public String getAddress(Order order) {return ModelUtils.extractAddressFromOrder(order);}public String getPaymentMethod(List<Attribute> paymentAttributes) {String paymentMethod = null;if (paymentAttributes == null || paymentAttributes.isEmpty()) {return "N/A";}for (Attribute a : paymentAttributes) {if ("payMethod".equals(a.getName())) {paymentMethod = Constants.PAYMENT_METHOD.get(a.getValue());break;}}return paymentMethod != null ? paymentMethod : "N/A";}public void setOrderId(String orderId) {try {this.orderId = Long.parseLong(orderId);} catch (NumberFormatException e) {log.error(e);}}public List<Payment> getPayments() {return payments;}public List<ShipmentUpdate> getShipmentUpdates() {return shipmentUpdates;}public void setShipmentUpdates(List<ShipmentUpdate> shipmentUpdates) {this.shipmentUpdates = shipmentUpdates;}public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}public Long getCodTicketId() {return codTicketId;}public void setCodTicketId(Long codTicketId) {this.codTicketId = codTicketId;}public List<Address> getAddresses() {return addresses;}public void setAddresses(List<Address> addresses) {this.addresses = addresses;}public String getOrderStatusDescription(Order order) {String status = order.getStatus().getDescription();if (order.getStatus() == OrderStatus.DELIVERY_SUCCESS) {status = "Completed";}return status;}public String getCancellationInitiator() {return cancellationInitiator;}public void setCancellationInitiator(String cancellationInitiator) {this.cancellationInitiator = cancellationInitiator;}public String getCancelReason() {return cancelReason;}public void setCancelReason(String cancelReason) {this.cancelReason = cancelReason;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}}