Subversion Repositories SmartDukaan

Rev

Rev 11890 | Rev 11950 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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