Subversion Repositories SmartDukaan

Rev

Rev 11942 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5407 amar.kumar 1
package in.shop2020.crm.util;
2
 
3
import in.shop2020.crm.ActivityType;
4
import in.shop2020.crm.TicketCategory;
5
import in.shop2020.crm.TicketPriority;
6
import in.shop2020.crm.TicketStatus;
7
import in.shop2020.crm.domain.Activity;
8
import in.shop2020.crm.domain.Ticket;
9
import in.shop2020.crm.handler.ActivityHandler;
10
import in.shop2020.crm.handler.TicketHandler;
11
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.UserCommunication;
13
import in.shop2020.model.v1.user.UserCommunicationException;
14
import in.shop2020.model.v1.user.UserContextException;
11942 kshitij.so 15
import in.shop2020.thrift.clients.HelperClient;
5407 amar.kumar 16
import in.shop2020.thrift.clients.UserClient;
17
 
11942 kshitij.so 18
import java.util.ArrayList;
19
import java.util.Arrays;
5407 amar.kumar 20
import java.util.List;
21
 
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
24
import org.apache.thrift.TException;
25
import org.springframework.context.ApplicationContext;
26
import org.springframework.context.support.ClassPathXmlApplicationContext;
27
import org.springframework.transaction.annotation.Transactional;
28
 
29
public class UserCommunicationProcessorTask {
30
 
11942 kshitij.so 31
    private static Log log = LogFactory.getLog(UserCommunicationProcessorTask.class);
32
    private TicketHandler      ticketHandler;
5407 amar.kumar 33
    private ActivityHandler    activityHandler;
34
    private static List<UserCommunication> userCommunications;
11942 kshitij.so 35
 
5407 amar.kumar 36
    private static final long     ADMIN_AGENT_ID = 1;
11942 kshitij.so 37
    private static final long	  DEF_ASSIGNEE_FOR_ORDER_CANCEL = 34; //Assigning to Amit Sirohi
38
    private static final  List<String> BULK_ORDER_EMAILS = Arrays.asList("manoj.kumar@saholic.com","chandan.kumar@saholic.com",
11950 kshitij.so 39
            "khushal.bhatia@saholic.com");
11942 kshitij.so 40
    private static final  List<String> BULK_ORDER_EMAILS_CC = Arrays.asList("rajneesh.arora@saholic.com","kshitij.sood@saholic.com",
11950 kshitij.so 41
            "amit.sirohi@saholic.com","chaitnaya.vats@saholic.com");
5407 amar.kumar 42
    //TODO Above field should be configurable
43
 
11942 kshitij.so 44
 
45
    public UserCommunicationProcessorTask()
5407 amar.kumar 46
    {
47
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
48
        ticketHandler              = context.getBean(TicketHandler.class);
49
        activityHandler            = context.getBean(ActivityHandler.class);
50
    }
11942 kshitij.so 51
 
52
    public static void main(String[] args) {
53
        UserCommunicationProcessorTask newTask = new UserCommunicationProcessorTask();
54
        in.shop2020.model.v1.user.UserContextService.Client client;
55
        try {
56
            client = new UserClient().getClient();
57
            userCommunications = client.getAllUserCommunications();
58
        } catch(UserCommunicationException e ) {
59
            log.fatal("Not able to remove UserCommunication" + e);
60
            System.exit(1);
61
        } catch(TException  ex) {
62
            log.fatal("Not able to remove UserCommunication" + ex);
63
            System.exit(1);
64
        }
5407 amar.kumar 65
        if (userCommunications != null && !userCommunications.isEmpty()) {
66
            log.info("Fetched " + userCommunications.size() + " UserCommunications");
67
            for (UserCommunication userCommunication : userCommunications) {
5435 amar.kumar 68
                if(newTask.processUserCommunication(userCommunication)) {
11942 kshitij.so 69
                    try {
70
                        client = new UserClient().getClient();
71
                        client.removeUserCommunication(userCommunication.getId());
72
                    } catch(UserCommunicationException e) {
73
                        log.error("Not able to remove UserCommunication" + e);
74
                    } catch(TException ex) {
75
                        log.error("Not able to remove UserCommunication" + ex);
76
                    }
5407 amar.kumar 77
                }
78
            }
79
        }
11942 kshitij.so 80
    }
5407 amar.kumar 81
 
11942 kshitij.so 82
    private boolean processUserCommunication(UserCommunication userCommunication) {
83
        try{
84
            log.info("Processing userCommunication : " + userCommunication.getId());
85
 
86
            Ticket ticket = new Ticket();
87
 
88
            ticket.setProductName(userCommunication.getProductName());
89
            TicketCategory tktCategory = TicketCategory.findByValue
90
            ((int)userCommunication.getCommunicationType().getValue());
91
            if(tktCategory != null) {
92
                ticket.setCategory(tktCategory);
93
            } else {
94
                ticket.setCategory(TicketCategory.OTHER);
95
            }
96
            if(ticket.getCategory().equals(TicketCategory.Bulk_Order_ENQUIRY)){
97
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
11890 kshitij.so 98
                        "\n\nSubject: " + userCommunication.getSubject() + 
99
                        "\n\nBody: " + userCommunication.getMessage());
11942 kshitij.so 100
                ticket.setCustomerMobileNumber(userCommunication.getAirwaybillNo());
101
 
102
            }
103
            else if(!ticket.getCategory().equals(TicketCategory.RECHARGE_RELATED)) {
104
                ticket.setAirwayBillNo(userCommunication.getAirwaybillNo());
105
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
106
                        "\n\nSubject: " + userCommunication.getSubject() + 
107
                        "\n\nBody: " + userCommunication.getMessage());
108
            }
109
            else {
110
                ticket.setDescription("From: " + userCommunication.getReplyTo() + 
111
                        "\n\nSubject: " + userCommunication.getSubject() + 
112
                        "\n\nBody: " + userCommunication.getMessage() +
113
                        "\n\nDevice Number:" + userCommunication.getAirwaybillNo());
114
            }
115
            if(userCommunication.getCommunicationType().getValue() == 2 || userCommunication.getCommunicationType().getValue() == 21) {
116
                ticket.setAssigneeId(DEF_ASSIGNEE_FOR_ORDER_CANCEL);
117
            }
118
            ticket.setCustomerEmailId(userCommunication.getReplyTo());
119
            ticket.setOrderId(userCommunication.getOrderId());
120
            ticket.setCreatorId(ADMIN_AGENT_ID);
121
            ticket.setPriority(TicketPriority.MEDIUM);
122
            ticket.setStatus(TicketStatus.OPEN);
123
 
124
            log.info("Creating activity!");
125
            Activity activity = new Activity();
126
            activity.setCreatorId(ADMIN_AGENT_ID);
127
            activity.setDescription("Ticket created");
128
            activity.setTicketCategory(ticket.getCategory());
129
            activity.setTicketDescription(ticket.getDescription());
130
            activity.setCustomerEmailId(ticket.getCustomerEmailId());
131
            activity.setTicketPriority(ticket.getPriority());
132
            activity.setTicketStatus(ticket.getStatus());
133
            activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
134
 
135
            if (userCommunication.getUserId() > 0) {
136
                activity.setCustomerId(userCommunication.getUserId());
137
                ticket.setCustomerId(userCommunication.getUserId());
138
            } else {
139
                try {
140
                    in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
141
                    .getClient();
142
                    User user = userClient.getUserByEmail(userCommunication.getReplyTo());
143
                    if (user != null && user.getUserId() != -1) {
144
                        log.info("Found registered user for: " + userCommunication.getReplyTo());
145
                        ticket.setCustomerId(user.getUserId());
146
                        activity.setCustomerId(user.getUserId());
147
                    } else {
148
                        log.info("Setting customer email id to: " + userCommunication.getReplyTo());
149
                        ticket.setCustomerEmailId(userCommunication.getReplyTo());
150
                    }
151
                } catch(UserContextException ex) {
152
                    log.error("Exception while getting user By email " +
153
                            "or getting email from userCommunication"+ex);
154
                } catch (TException e) {
155
                    log.error("Exception while getting user By email " +
156
                            "or getting email from userCommunication"+e);
157
                }
158
            }
159
            log.info("Creating ticket!");
5407 amar.kumar 160
            createTicket(ticket, activity);
11942 kshitij.so 161
            if(userCommunication.getCommunicationType().getValue() == 21) {
162
                sendEmail(userCommunication);
163
            }
164
        } catch(Exception e) {
165
            log.error("Exception while creating ticket for commId : " +
166
                    userCommunication.getId() + "\n" + e);
167
            return false;
168
        }
169
        return true;
170
    }
171
 
172
    @Transactional
5407 amar.kumar 173
    private void createTicket(Ticket ticket, Activity activity) {
174
        ticketHandler.insertTicket(ticket);
175
        activity.setTicketId(ticket.getId());
176
        activityHandler.insertActivity(activity);
177
    }
11942 kshitij.so 178
 
179
    private boolean sendEmail(UserCommunication uc) {
180
        try {
181
            System.out.println("Product Name : "+uc.getProductName()+"\n"+uc.getMessage());
182
            HelperClient helperClient = new HelperClient("helper_service_server","helper_service_server_port");
183
            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);
184
        } catch (Exception e) {
185
            log.error("Unexpected error while mailing the new password");
186
            return false;
187
        }
188
        return true;
189
    }
190
 
5407 amar.kumar 191
}