Subversion Repositories SmartDukaan

Rev

Rev 8127 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.crm.util;

import in.shop2020.crm.ActivityType;
import in.shop2020.crm.TicketCategory;
import in.shop2020.crm.TicketPriority;
import in.shop2020.crm.TicketStatus;
import in.shop2020.crm.domain.Activity;
import in.shop2020.crm.domain.SearchFilter;
import in.shop2020.crm.domain.Ticket;
import in.shop2020.crm.handler.ActivityHandler;
import in.shop2020.crm.handler.TicketHandler;
import in.shop2020.model.v1.order.ExtraTransactionProcessingType;
import in.shop2020.model.v1.order.TransactionService.Client;
import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.thrift.clients.TransactionClient;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mandeep
 *
 * This class processes all the orders whose delivery has been delayed due
 * to one or the other issue (delayReason field). We create a ticket per customer
 * in such cases and Outbound team intimates them regarding the delay.
 */
public class DelayedOrderProcessorTask {
    private static Log log = LogFactory.getLog(DelayedOrderProcessorTask.class);
    private static final long   ADMIN_AGENT_ID   = 1;
    private static final long   OUTBOUND_DEFAULT_ASSIGNEE_ID   = 18;

    private TicketHandler      ticketHandler;
    private ActivityHandler    activityHandler;

    public DelayedOrderProcessorTask()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        ticketHandler              = context.getBean(TicketHandler.class);
        activityHandler            = context.getBean(ActivityHandler.class);
    }

    public static void main(String[] args) {
        try {
            DelayedOrderProcessorTask newTask = new DelayedOrderProcessorTask();
            Client client = new TransactionClient().getClient();
            List<Long> transactionIds = client.getTransactionsRequiringExtraProcessing(ExtraTransactionProcessingType.DELAYED_DELIVERY);
            if (transactionIds != null && !transactionIds.isEmpty()) {
                log.info("Fetched " + transactionIds.size() + " transactions");
                for (Long transactionId : transactionIds) {
                    newTask.processDelayedOrder(client.getTransaction(transactionId).getCustomer_id());
                    client.markTransactionAsProcessed(transactionId, ExtraTransactionProcessingType.DELAYED_DELIVERY);
                }
            }
            else {
                log.info("No transactions to process");
            }
        } catch (TTransportException e) {
            log.error("Could not create TransactionService client", e);
        } catch (TException e) {
            log.error("Could not fetch transactions for processing", e);
        } catch (TransactionServiceException e) {
            log.error("Could not lookup transaction", e);
        }
    }

    private void processDelayedOrder(Long customerId) {
        log.info("Processing delayed order for customerId: " + customerId);
        SearchFilter searchFilter = new SearchFilter();
        searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
        searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
        searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
        searchFilter.setTicketCategory(TicketCategory.DELAYED_DELIVERY);
        searchFilter.setCustomerId(customerId);

        // No need to create a ticket if there exists one for the customer!
        if (ticketHandler.getTickets(searchFilter).isEmpty()) {
            Ticket ticket = new Ticket();
            ticket.setCategory(TicketCategory.DELAYED_DELIVERY);
            ticket.setCreatorId(ADMIN_AGENT_ID);
            ticket.setCustomerId(customerId);
            ticket.setDescription("Requires delayed delivery intimation");
            ticket.setPriority(TicketPriority.HIGH);
            ticket.setStatus(TicketStatus.OPEN);
            ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);

            Activity activity = new Activity();
            activity.setCreatorId(ticket.getCreatorId());
            activity.setCustomerId(ticket.getCustomerId());
            activity.setDescription("Creating ticket");
            activity.setTicketCategory(ticket.getCategory());
            activity.setTicketDescription(ticket.getDescription());
            activity.setTicketPriority(ticket.getPriority());
            activity.setTicketStatus(ticket.getStatus());
            activity.setType(ActivityType.OTHER);
            activity.setTicketAssigneeId(ticket.getAssigneeId());

            createTicket(ticket, activity);
        }
    }

    @Transactional
    private void createTicket(Ticket ticket, Activity activity) {
        ticketHandler.insertTicket(ticket);
        activity.setTicketId(ticket.getId());
        activityHandler.insertActivity(activity);
    }
}