Subversion Repositories SmartDukaan

Rev

Rev 11950 | 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.Ticket;
import in.shop2020.crm.handler.ActivityHandler;
import in.shop2020.crm.handler.TicketHandler;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserCommunication;
import in.shop2020.model.v1.user.UserCommunicationException;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.thrift.clients.UserClient;

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

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

public class UserCommunicationProcessorTask {

    private static Log log = LogFactory.getLog(UserCommunicationProcessorTask.class);
    private TicketHandler      ticketHandler;
    private ActivityHandler    activityHandler;
    private static List<UserCommunication> userCommunications;

    private static final long     ADMIN_AGENT_ID = 1;
    private static final long     DEF_ASSIGNEE_FOR_ORDER_CANCEL = 34; //Assigning to Amit Sirohi
    private static final  List<String> BULK_ORDER_EMAILS = Arrays.asList("manoj.kumar@saholic.com","chandan.kumar@saholic.com",
            "khushal.bhatia@saholic.com");
    private static final  List<String> BULK_ORDER_EMAILS_CC = Arrays.asList("rajneesh.arora@saholic.com","khushal.bhatia@saholic.com","kshitij.sood@saholic.com",
            "amit.sirohi@saholic.com","chaitnaya.vats@saholic.com");
    //TODO Above field should be configurable


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

    public static void main(String[] args) {
        UserCommunicationProcessorTask newTask = new UserCommunicationProcessorTask();
        in.shop2020.model.v1.user.UserContextService.Client client;
        try {
            client = new UserClient().getClient();
            userCommunications = client.getAllUserCommunications();
        } catch(UserCommunicationException e ) {
            log.fatal("Not able to remove UserCommunication" + e);
            System.exit(1);
        } catch(TException  ex) {
            log.fatal("Not able to remove UserCommunication" + ex);
            System.exit(1);
        }
        if (userCommunications != null && !userCommunications.isEmpty()) {
            log.info("Fetched " + userCommunications.size() + " UserCommunications");
            for (UserCommunication userCommunication : userCommunications) {
                if(newTask.processUserCommunication(userCommunication)) {
                    try {
                        client = new UserClient().getClient();
                        client.removeUserCommunication(userCommunication.getId());
                    } catch(UserCommunicationException e) {
                        log.error("Not able to remove UserCommunication" + e);
                    } catch(TException ex) {
                        log.error("Not able to remove UserCommunication" + ex);
                    }
                }
            }
        }
    }

    private boolean processUserCommunication(UserCommunication userCommunication) {
        try{
            log.info("Processing userCommunication : " + userCommunication.getId());

            Ticket ticket = new Ticket();

            ticket.setProductName(userCommunication.getProductName());
            TicketCategory tktCategory = TicketCategory.findByValue
            ((int)userCommunication.getCommunicationType().getValue());
            if(tktCategory != null) {
                ticket.setCategory(tktCategory);
            } else {
                ticket.setCategory(TicketCategory.OTHER);
            }
            if(ticket.getCategory().equals(TicketCategory.Bulk_Order_ENQUIRY)){
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
                        "\n\nSubject: " + userCommunication.getSubject() + 
                        "\n\nBody: " + userCommunication.getMessage());
                ticket.setCustomerMobileNumber(userCommunication.getAirwaybillNo());

            }
            else if(!ticket.getCategory().equals(TicketCategory.RECHARGE_RELATED)) {
                ticket.setAirwayBillNo(userCommunication.getAirwaybillNo());
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
                        "\n\nSubject: " + userCommunication.getSubject() + 
                        "\n\nBody: " + userCommunication.getMessage());
            }
            else {
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
                        "\n\nSubject: " + userCommunication.getSubject() + 
                        "\n\nBody: " + userCommunication.getMessage() +
                        "\n\nDevice Number:" + userCommunication.getAirwaybillNo());
            }
            if(userCommunication.getCommunicationType().getValue() == 2 || userCommunication.getCommunicationType().getValue() == 21) {
                ticket.setAssigneeId(DEF_ASSIGNEE_FOR_ORDER_CANCEL);
            }
            ticket.setCustomerEmailId(userCommunication.getReplyTo());
            ticket.setOrderId(userCommunication.getOrderId());
            ticket.setCreatorId(ADMIN_AGENT_ID);
            ticket.setPriority(TicketPriority.MEDIUM);
            ticket.setStatus(TicketStatus.OPEN);

            log.info("Creating activity!");
            Activity activity = new Activity();
            activity.setCreatorId(ADMIN_AGENT_ID);
            activity.setDescription("Ticket created");
            activity.setTicketCategory(ticket.getCategory());
            activity.setTicketDescription(ticket.getDescription());
            activity.setCustomerEmailId(ticket.getCustomerEmailId());
            activity.setTicketPriority(ticket.getPriority());
            activity.setTicketStatus(ticket.getStatus());
            activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);

            if (userCommunication.getUserId() > 0) {
                activity.setCustomerId(userCommunication.getUserId());
                ticket.setCustomerId(userCommunication.getUserId());
            } else {
                try {
                    in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
                    .getClient();
                    User user = userClient.getUserByEmail(userCommunication.getReplyTo());
                    if (user != null && user.getUserId() != -1) {
                        log.info("Found registered user for: " + userCommunication.getReplyTo());
                        ticket.setCustomerId(user.getUserId());
                        activity.setCustomerId(user.getUserId());
                    } else {
                        log.info("Setting customer email id to: " + userCommunication.getReplyTo());
                        ticket.setCustomerEmailId(userCommunication.getReplyTo());
                    }
                } catch(UserContextException ex) {
                    log.error("Exception while getting user By email " +
                            "or getting email from userCommunication"+ex);
                } catch (TException e) {
                    log.error("Exception while getting user By email " +
                            "or getting email from userCommunication"+e);
                }
            }
            log.info("Creating ticket!");
            createTicket(ticket, activity);
            if(userCommunication.getCommunicationType().getValue() == 21) {
                sendEmail(userCommunication);
            }
        } catch(Exception e) {
            log.error("Exception while creating ticket for commId : " +
                    userCommunication.getId() + "\n" + e);
            return false;
        }
        return true;
    }

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

    private boolean sendEmail(UserCommunication uc) {
        try {
            System.out.println("Product Name : "+uc.getProductName()+"\n"+uc.getMessage());
            HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
            helperClient.getClient().saveUserEmailForSending(BULK_ORDER_EMAILS, "help@saholic.com", "Bulk Order Enquiry by "+ uc.getReplyTo(), uc.getMessage()+"\nCommunication Timestamp :"+new java.util.Date(uc.getCommunication_timestamp()).toLocaleString() , "", "BulkEnquiry", BULK_ORDER_EMAILS_CC, new ArrayList<String>(), 1);
        } catch (Exception e) {
            log.error("Unexpected error while mailing the new password");
            return false;
        }
        return true;
    }

}