Subversion Repositories SmartDukaan

Rev

Rev 5435 | Go to most recent revision | Details | 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) {
61
                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
                }
70
            }
71
        }
72
	}
73
 
74
	private void processUserCommunication(UserCommunication userCommunication) {
75
		try{
76
			log.info("Processing userCommunication : " + userCommunication.getId());
77
 
78
	        Ticket ticket = new Ticket();
79
	        ticket.setAirwayBillNo(userCommunication.getAirwaybillNo());
80
	        ticket.setProductName(userCommunication.getProductName());
81
	        TicketCategory tktCategory = TicketCategory.findByValue
82
    			((int)userCommunication.getCommunicationType().getValue());
83
	        if(tktCategory != null) {
84
	        	ticket.setCategory(tktCategory);
85
	        } else {
86
	        	ticket.setCategory(TicketCategory.OTHER);
87
	        }
88
 
89
	        ticket.setDescription("From: " + userCommunication.getReplyTo() + 
90
	        		"\n\nSubject: " + userCommunication.getSubject() + 
91
	        		"\n\nBody: " + userCommunication.getMessage());
92
	        if(userCommunication.getCommunicationType().getValue() == 2) {
93
	            ticket.setAssigneeId(DEF_ASSIGNEE_FOR_ORDER_CANCEL);
94
	        }
95
	        ticket.setCustomerEmailId(userCommunication.getReplyTo());
96
	        ticket.setOrderId(userCommunication.getOrderId());
97
	        ticket.setCreatorId(ADMIN_AGENT_ID);
98
	        ticket.setPriority(TicketPriority.MEDIUM);
99
	        ticket.setStatus(TicketStatus.OPEN);
100
 
101
	        log.info("Creating activity!");
102
	        Activity activity = new Activity();
103
	        activity.setCreatorId(ADMIN_AGENT_ID);
104
	        activity.setDescription("Ticket created");
105
	        activity.setTicketCategory(ticket.getCategory());
106
	        activity.setTicketDescription(ticket.getDescription());
107
	        activity.setCustomerEmailId(ticket.getCustomerEmailId());
108
	        activity.setTicketPriority(ticket.getPriority());
109
	        activity.setTicketStatus(ticket.getStatus());
110
	        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
111
 
112
	        if (userCommunication.getUserId() > 0) {
113
	            activity.setCustomerId(userCommunication.getUserId());
114
	            ticket.setCustomerId(userCommunication.getUserId());
115
	        } else {
116
	        	try {
117
		        	in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
118
					.getClient();
119
		            User user = userClient.getUserByEmail(userCommunication.getReplyTo());
120
		            if (user != null && user.getUserId() != -1) {
121
		                log.info("Found registered user for: " + userCommunication.getReplyTo());
122
		                ticket.setCustomerId(user.getUserId());
123
		                activity.setCustomerId(user.getUserId());
124
		            } else {
125
		                log.info("Setting customer email id to: " + userCommunication.getReplyTo());
126
		                ticket.setCustomerEmailId(userCommunication.getReplyTo());
127
		            }
128
	        	} catch(UserContextException ex) {
129
	        		log.error("Exception while getting user By email " +
130
	        				"or getting email from userCommunication"+ex);
131
	        	} catch (TException e) {
132
	        		log.error("Exception while getting user By email " +
133
	        				"or getting email from userCommunication"+e);
134
				}
135
	        }
136
	        log.info("Creating ticket!");
137
            createTicket(ticket, activity);
138
		} catch(Exception e) {
139
			log.error("Exception while creating ticket for commId : " +
140
					userCommunication.getId() + "\n" + e);
141
		}
142
	}
143
 
144
	@Transactional
145
    private void createTicket(Ticket ticket, Activity activity) {
146
        ticketHandler.insertTicket(ticket);
147
        activity.setTicketId(ticket.getId());
148
        activityHandler.insertActivity(activity);
149
    }
150
 
151
}