Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4008 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.crm.util;
5
 
6
import in.shop2020.crm.ActivityType;
7
import in.shop2020.crm.TicketCategory;
8
import in.shop2020.crm.TicketPriority;
9
import in.shop2020.crm.TicketStatus;
10
import in.shop2020.crm.domain.Activity;
11
import in.shop2020.crm.domain.SearchFilter;
12
import in.shop2020.crm.domain.Ticket;
13
import in.shop2020.crm.handler.ActivityHandler;
14
import in.shop2020.crm.handler.TicketHandler;
15
import in.shop2020.model.v1.order.ExtraTransactionProcessingType;
16
import in.shop2020.model.v1.order.TransactionService.Client;
17
import in.shop2020.model.v1.order.TransactionServiceException;
18
import in.shop2020.thrift.clients.TransactionClient;
19
 
20
import java.util.ArrayList;
21
import java.util.List;
22
 
23
import org.apache.commons.logging.Log;
24
import org.apache.commons.logging.LogFactory;
25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
5142 anupam.sin 27
import org.springframework.context.ApplicationContext;
28
import org.springframework.context.support.ClassPathXmlApplicationContext;
4008 mandeep.dh 29
import org.springframework.transaction.annotation.Transactional;
30
 
31
/**
32
 * @author mandeep
33
 *
34
 * This class processes all the orders whose delivery has been delayed due
35
 * to one or the other issue (delayReason field). We create a ticket per customer
36
 * in such cases and Outbound team intimates them regarding the delay.
37
 */
5142 anupam.sin 38
public class DelayedOrderProcessorTask {
4008 mandeep.dh 39
    private static Log log = LogFactory.getLog(DelayedOrderProcessorTask.class);
40
    private static final long   ADMIN_AGENT_ID   = 1;
18461 manish.sha 41
    private static final long   OUTBOUND_DEFAULT_ASSIGNEE_ID   = 18;
4008 mandeep.dh 42
 
43
    private TicketHandler      ticketHandler;
44
    private ActivityHandler    activityHandler;
45
 
5142 anupam.sin 46
    public DelayedOrderProcessorTask()
4008 mandeep.dh 47
    {
5142 anupam.sin 48
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
49
        ticketHandler              = context.getBean(TicketHandler.class);
50
        activityHandler            = context.getBean(ActivityHandler.class);
4008 mandeep.dh 51
    }
52
 
5142 anupam.sin 53
    public static void main(String[] args) {
4008 mandeep.dh 54
        try {
5142 anupam.sin 55
            DelayedOrderProcessorTask newTask = new DelayedOrderProcessorTask();
4008 mandeep.dh 56
            Client client = new TransactionClient().getClient();
57
            List<Long> transactionIds = client.getTransactionsRequiringExtraProcessing(ExtraTransactionProcessingType.DELAYED_DELIVERY);
58
            if (transactionIds != null && !transactionIds.isEmpty()) {
59
                log.info("Fetched " + transactionIds.size() + " transactions");
60
                for (Long transactionId : transactionIds) {
5142 anupam.sin 61
                    newTask.processDelayedOrder(client.getTransaction(transactionId).getCustomer_id());
4008 mandeep.dh 62
                    client.markTransactionAsProcessed(transactionId, ExtraTransactionProcessingType.DELAYED_DELIVERY);
63
                }
64
            }
65
            else {
66
                log.info("No transactions to process");
67
            }
68
        } catch (TTransportException e) {
69
            log.error("Could not create TransactionService client", e);
70
        } catch (TException e) {
71
            log.error("Could not fetch transactions for processing", e);
72
        } catch (TransactionServiceException e) {
73
            log.error("Could not lookup transaction", e);
74
        }
75
    }
76
 
77
    private void processDelayedOrder(Long customerId) {
4191 mandeep.dh 78
        log.info("Processing delayed order for customerId: " + customerId);
79
        SearchFilter searchFilter = new SearchFilter();
80
        searchFilter.setTicketStatuses(new ArrayList<TicketStatus>());
81
        searchFilter.getTicketStatuses().add(TicketStatus.OPEN);
82
        searchFilter.getTicketStatuses().add(TicketStatus.REOPEN);
83
        searchFilter.setTicketCategory(TicketCategory.DELAYED_DELIVERY);
84
        searchFilter.setCustomerId(customerId);
4008 mandeep.dh 85
 
4191 mandeep.dh 86
        // No need to create a ticket if there exists one for the customer!
87
        if (ticketHandler.getTickets(searchFilter).isEmpty()) {
88
            Ticket ticket = new Ticket();
89
            ticket.setCategory(TicketCategory.DELAYED_DELIVERY);
90
            ticket.setCreatorId(ADMIN_AGENT_ID);
91
            ticket.setCustomerId(customerId);
92
            ticket.setDescription("Requires delayed delivery intimation");
93
            ticket.setPriority(TicketPriority.HIGH);
94
            ticket.setStatus(TicketStatus.OPEN);
95
            ticket.setAssigneeId(OUTBOUND_DEFAULT_ASSIGNEE_ID);
4008 mandeep.dh 96
 
4191 mandeep.dh 97
            Activity activity = new Activity();
98
            activity.setCreatorId(ticket.getCreatorId());
99
            activity.setCustomerId(ticket.getCustomerId());
100
            activity.setDescription("Creating ticket");
101
            activity.setTicketCategory(ticket.getCategory());
102
            activity.setTicketDescription(ticket.getDescription());
103
            activity.setTicketPriority(ticket.getPriority());
104
            activity.setTicketStatus(ticket.getStatus());
105
            activity.setType(ActivityType.OTHER);
106
            activity.setTicketAssigneeId(ticket.getAssigneeId());
4008 mandeep.dh 107
 
4191 mandeep.dh 108
            createTicket(ticket, activity);
4008 mandeep.dh 109
        }
110
    }
111
 
112
    @Transactional
113
    private void createTicket(Ticket ticket, Activity activity) {
114
        ticketHandler.insertTicket(ticket);
4179 mandeep.dh 115
        activity.setTicketId(ticket.getId());
4008 mandeep.dh 116
        activityHandler.insertActivity(activity);
117
    }
118
}