Subversion Repositories SmartDukaan

Rev

Rev 6811 | 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;
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;
6811 anupam.sin 34
    private static final long	  DEF_ASSIGNEE_FOR_ORDER_CANCEL = 34; //Assigning to Amit Sirohi 
5407 amar.kumar 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();
6176 amit.gupta 80
 
5407 amar.kumar 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
	        }
11890 kshitij.so 89
	        if(ticket.getCategory().equals(TicketCategory.Bulk_Order_ENQUIRY)){
90
	            ticket.setDescription("From: " + userCommunication.getReplyTo() + 
91
                        "\n\nSubject: " + userCommunication.getSubject() + 
92
                        "\n\nBody: " + userCommunication.getMessage());
93
	        }
94
	        else if(!ticket.getCategory().equals(TicketCategory.RECHARGE_RELATED)) {
6176 amit.gupta 95
	        	ticket.setAirwayBillNo(userCommunication.getAirwaybillNo());
96
	        	ticket.setDescription("From: " + userCommunication.getReplyTo() + 
97
	        			"\n\nSubject: " + userCommunication.getSubject() + 
98
	        			"\n\nBody: " + userCommunication.getMessage());
11890 kshitij.so 99
	        }
100
	        else {
6176 amit.gupta 101
	        	ticket.setDescription("From: " + userCommunication.getReplyTo() + 
102
	        			"\n\nSubject: " + userCommunication.getSubject() + 
103
	        			"\n\nBody: " + userCommunication.getMessage() +
104
	        			"\n\nDevice Number:" + userCommunication.getAirwaybillNo());
105
	        }
11890 kshitij.so 106
	        if(userCommunication.getCommunicationType().getValue() == 2 || userCommunication.getCommunicationType().getValue() == 21) {
5407 amar.kumar 107
	            ticket.setAssigneeId(DEF_ASSIGNEE_FOR_ORDER_CANCEL);
108
	        }
109
	        ticket.setCustomerEmailId(userCommunication.getReplyTo());
110
	        ticket.setOrderId(userCommunication.getOrderId());
111
	        ticket.setCreatorId(ADMIN_AGENT_ID);
112
	        ticket.setPriority(TicketPriority.MEDIUM);
113
	        ticket.setStatus(TicketStatus.OPEN);
114
 
115
	        log.info("Creating activity!");
116
	        Activity activity = new Activity();
117
	        activity.setCreatorId(ADMIN_AGENT_ID);
118
	        activity.setDescription("Ticket created");
119
	        activity.setTicketCategory(ticket.getCategory());
120
	        activity.setTicketDescription(ticket.getDescription());
121
	        activity.setCustomerEmailId(ticket.getCustomerEmailId());
122
	        activity.setTicketPriority(ticket.getPriority());
123
	        activity.setTicketStatus(ticket.getStatus());
124
	        activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
125
 
126
	        if (userCommunication.getUserId() > 0) {
127
	            activity.setCustomerId(userCommunication.getUserId());
128
	            ticket.setCustomerId(userCommunication.getUserId());
129
	        } else {
130
	        	try {
131
		        	in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
132
					.getClient();
133
		            User user = userClient.getUserByEmail(userCommunication.getReplyTo());
134
		            if (user != null && user.getUserId() != -1) {
135
		                log.info("Found registered user for: " + userCommunication.getReplyTo());
136
		                ticket.setCustomerId(user.getUserId());
137
		                activity.setCustomerId(user.getUserId());
138
		            } else {
139
		                log.info("Setting customer email id to: " + userCommunication.getReplyTo());
140
		                ticket.setCustomerEmailId(userCommunication.getReplyTo());
141
		            }
142
	        	} catch(UserContextException ex) {
143
	        		log.error("Exception while getting user By email " +
144
	        				"or getting email from userCommunication"+ex);
145
	        	} catch (TException e) {
146
	        		log.error("Exception while getting user By email " +
147
	        				"or getting email from userCommunication"+e);
148
				}
149
	        }
150
	        log.info("Creating ticket!");
151
            createTicket(ticket, activity);
152
		} catch(Exception e) {
153
			log.error("Exception while creating ticket for commId : " +
154
					userCommunication.getId() + "\n" + e);
5435 amar.kumar 155
			return false;
5407 amar.kumar 156
		}
5435 amar.kumar 157
		return true;
5407 amar.kumar 158
	}
159
 
160
	@Transactional
161
    private void createTicket(Ticket ticket, Activity activity) {
162
        ticketHandler.insertTicket(ticket);
163
        activity.setTicketId(ticket.getId());
164
        activityHandler.insertActivity(activity);
165
    }
166
 
167
}