Subversion Repositories SmartDukaan

Rev

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