Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3024 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.crm.service.handler;
5
 
5851 amar.kumar 6
import in.shop2020.alert.AlertService;
7
import in.shop2020.alert.EntityType;
8
import in.shop2020.alert.MonitoredEntity;
8371 manish.sha 9
import in.shop2020.config.ConfigException;
3024 mandeep.dh 10
import in.shop2020.crm.Activity;
3390 mandeep.dh 11
import in.shop2020.crm.ActivityType;
3024 mandeep.dh 12
import in.shop2020.crm.Agent;
5936 amar.kumar 13
import in.shop2020.crm.TicketStatus;
3024 mandeep.dh 14
import in.shop2020.crm.CRMService.Iface;
3390 mandeep.dh 15
import in.shop2020.crm.SearchFilter;
3024 mandeep.dh 16
import in.shop2020.crm.Ticket;
5916 amar.kumar 17
import in.shop2020.crm.TicketCategory;
3024 mandeep.dh 18
import in.shop2020.crm.handler.ActivityHandler;
19
import in.shop2020.crm.handler.AgentHandler;
20
import in.shop2020.crm.handler.TicketHandler;
8371 manish.sha 21
import in.shop2020.serving.model.ShipmentUpdate;
22
import in.shop2020.serving.services.FedExTrackingService;
5851 amar.kumar 23
import in.shop2020.thrift.clients.AlertClient;
9338 manish.sha 24
import in.shop2020.thrift.clients.InventoryClient;
8371 manish.sha 25
import in.shop2020.thrift.clients.TransactionClient;
26
import in.shop2020.thrift.clients.config.ConfigClient;
9338 manish.sha 27
import in.shop2020.model.v1.inventory.WarehouseLocation;
28
import in.shop2020.model.v1.inventory.Warehouse;
8371 manish.sha 29
import in.shop2020.model.v1.order.Order;
30
import in.shop2020.model.v1.order.TransactionServiceException;
3024 mandeep.dh 31
 
32
import java.text.ParseException;
33
import java.util.ArrayList;
5851 amar.kumar 34
import java.util.Calendar;
3339 mandeep.dh 35
import java.util.Date;
9338 manish.sha 36
import java.util.HashMap;
3024 mandeep.dh 37
import java.util.List;
5909 amar.kumar 38
import java.util.Map;
3024 mandeep.dh 39
 
3499 mandeep.dh 40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
3024 mandeep.dh 42
import org.apache.thrift.TException;
8371 manish.sha 43
import org.apache.thrift.transport.TTransportException;
3024 mandeep.dh 44
import org.springframework.context.ApplicationContext;
45
import org.springframework.context.support.ClassPathXmlApplicationContext;
46
import org.springframework.stereotype.Service;
3206 mandeep.dh 47
import org.springframework.transaction.annotation.Transactional;
3024 mandeep.dh 48
 
49
/**
50
 * Implementation of the interface/services exposed by thrift to clients!
51
 * 
52
 * @author mandeep
53
 */
54
@Service
55
public class CRMServiceHandler implements Iface {
9338 manish.sha 56
	private static final Log log = LogFactory.getLog(CRMServiceHandler.class);
3499 mandeep.dh 57
 
9338 manish.sha 58
	private TicketHandler      ticketHandler;
59
	private ActivityHandler    activityHandler;
60
	private AgentHandler       agentHandler;
61
	private FedExTrackingService fedexTrackClientMumbai;
62
	private FedExTrackingService fedexTrackClientDelhi;
4008 mandeep.dh 63
 
9338 manish.sha 64
	public CRMServiceHandler() {
65
		log.info("Creating context");
66
		ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
67
		ticketHandler              = context.getBean(TicketHandler.class);
68
		activityHandler            = context.getBean(ActivityHandler.class);
69
		agentHandler               = context.getBean(AgentHandler.class);
70
	}
4008 mandeep.dh 71
 
9338 manish.sha 72
	public List<Ticket> getTickets(SearchFilter searchFilter) throws TException {
73
		List<Ticket> ttickets = new ArrayList<Ticket>();
74
		for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
75
				.getTickets(in.shop2020.crm.domain.SearchFilter
76
						.create(searchFilter))) {
77
			ttickets.add(ticket.getThriftTicket());
78
		}
3024 mandeep.dh 79
 
9338 manish.sha 80
		return ttickets;
81
	}
3024 mandeep.dh 82
 
9338 manish.sha 83
	public List<Ticket> getUnassignedTickets() throws TException {
84
		List<Ticket> tickets = new ArrayList<Ticket>();
3390 mandeep.dh 85
 
9338 manish.sha 86
		for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
87
				.getUnassignedTickets()) {
88
			tickets.add(ticket.getThriftTicket());
89
		}
3390 mandeep.dh 90
 
9338 manish.sha 91
		return tickets;
92
	}
3390 mandeep.dh 93
 
9338 manish.sha 94
	@Transactional
95
	public void updateTicket(Ticket ticket, Activity activity)
96
	throws TException {
97
		try {
98
			ticketHandler.updateTicket(in.shop2020.crm.domain.Ticket
99
					.create(ticket));
100
			activity.setTicketId(ticket.getId());
101
			activityHandler.insertActivity(in.shop2020.crm.domain.Activity
102
					.create(activity));
103
			try{
104
				/*For now monitoring is only done for New tickets and hence once any 
105
				 * activity is done on a ticket after its creation, we stop monitoring that ticket*/
106
				if(activity.getType()== ActivityType.SEND_EMAIL_TO_CUSTOMER || 
107
						activity.getType()== ActivityType.ESCALATE_TICKET ||
108
						activity.getType()== ActivityType.CALLED_CUSTOMER || 
109
						activity.getTicketStatus().equals(TicketStatus.CLOSED)) {
110
					AlertService.Client alertClient = new AlertClient().getClient();
111
					alertClient.endMonitoringEntity(EntityType.TICKET, "ticketId = " + ticket.getId());
112
				}
113
			} catch(Exception ex) {
114
				log.error("Exception while ending monitoring for ticketId " + ticket.getId());
115
				log.error(ex);
116
			}
117
		} catch (ParseException e) {
118
			throw new TException("Could not update " + ticket, e);
119
		}
120
	}
3024 mandeep.dh 121
 
9338 manish.sha 122
	@Transactional
123
	public long insertTicket(Ticket ticket, Activity activity)
124
	throws TException {
125
		try {
126
			long ticketId = ticketHandler
127
			.insertTicket(in.shop2020.crm.domain.Ticket.create(ticket));
128
			activity.setTicketId(ticketId);
129
			activityHandler.insertActivity(in.shop2020.crm.domain.Activity
130
					.create(activity));
131
			try {
132
				if(ticket.getCategory()!= TicketCategory.DELIVERY_PROBLEM &&
133
						ticket.getCategory()!= TicketCategory.PRODUCT_PROCUREMENT &&
134
						ticket.getCategory()!= TicketCategory.DOA_RECEIVED) {
135
					MonitoredEntity entity = new MonitoredEntity();
136
					entity.setEntityType(EntityType.TICKET);
137
					entity.setEventType(0);
138
					entity.setEntityIdentifier("ticketId = " + new Long(ticketId).toString());
139
					entity.setDescription(ticket.getDescription());
140
					Calendar thresholdTime = Calendar.getInstance();
141
					thresholdTime.add(Calendar.HOUR, 4);
142
					entity.setWarnExpiryTime(thresholdTime.getTimeInMillis());
143
					thresholdTime.add(Calendar.HOUR, 2);
144
					entity.setCriticalExpiryTime(thresholdTime.getTimeInMillis());
145
					AlertService.Client alertClient = new AlertClient().getClient();
146
					alertClient.scheduleAlert(entity);
147
				}
148
			} catch (Exception ex) {
149
				log.error("Exception while scheduling alert for Ticket Id "+ticketId);
150
				log.error(ex);
151
			}
152
			return ticketId;
153
		} catch (ParseException e) {
154
			throw new TException("Could not insert " + ticket, e);
155
		}
156
	}
3024 mandeep.dh 157
 
9338 manish.sha 158
	public List<Activity> getActivities(SearchFilter searchFilter)
159
	throws TException {
160
		List<Activity> tactivities = new ArrayList<Activity>();
14902 manish.sha 161
		log.info("Search Filter..."+searchFilter.isNotShowPmTickets());
9338 manish.sha 162
		in.shop2020.crm.domain.SearchFilter filter_to_check  =in.shop2020.crm.domain.SearchFilter.create(searchFilter);
14902 manish.sha 163
		log.info("Search Filter after conversion..."+filter_to_check.getNotShowPmTickets());
9338 manish.sha 164
		List<in.shop2020.crm.domain.Activity> activities = activityHandler
165
		.getActivities(filter_to_check);
166
		in.shop2020.crm.domain.SearchFilter new_searchFilter = new in.shop2020.crm.domain.SearchFilter();
3390 mandeep.dh 167
 
9338 manish.sha 168
		if (activities != null) {
169
			for (in.shop2020.crm.domain.Activity ticket : activities) {
170
				in.shop2020.crm.Activity activity = ticket.getThriftActivity();
171
				if(filter_to_check.getIsActivityRead()!=null && !filter_to_check.getIsActivityRead()){
172
					if(activity.getTicketId() > 0){
173
						new_searchFilter.setTicketId(activity.getTicketId());
174
						List<in.shop2020.crm.domain.Ticket> tickets_as_per_activity= ticketHandler.getTickets(new_searchFilter);
175
						activity.setTicketStatus(tickets_as_per_activity.get(0).getStatus());
176
					}
177
				}
178
				tactivities.add(activity);
179
			}
180
		}
3024 mandeep.dh 181
 
9338 manish.sha 182
		return tactivities;
183
	}
3024 mandeep.dh 184
 
9338 manish.sha 185
	public long insertActivity(Activity activity) throws TException {
186
		try {
187
			return activityHandler.insertActivity(in.shop2020.crm.domain.Activity
188
					.create(activity));
189
		} catch (ParseException e) {
190
			throw new TException("Could not insert " + activity, e);
191
		}
192
	}
4089 mandeep.dh 193
 
9338 manish.sha 194
	@Transactional
195
	public void markAsRead(long activityId, long agentId) throws TException {
196
		in.shop2020.crm.domain.SearchFilter searchFilter = new in.shop2020.crm.domain.SearchFilter();
197
		searchFilter.setActivityId(activityId);
198
		activityHandler.markAsRead(activityId);
199
		in.shop2020.crm.domain.Activity activity = activityHandler
200
		.getActivities(searchFilter).get(0);
4089 mandeep.dh 201
 
9338 manish.sha 202
		// Setting activity fields from latest ticket fields
203
		if (activity.getTicketId() != null) {
204
			searchFilter.setTicketId(activity.getTicketId());
205
			in.shop2020.crm.domain.Ticket ticket = ticketHandler.getTickets(searchFilter).get(0);
206
			activity.setTicketAssigneeId(ticket.getAssigneeId());
207
			activity.setTicketCategory(ticket.getCategory());
208
			activity.setTicketDescription(ticket.getDescription());
209
			activity.setTicketPriority(ticket.getPriority());
210
			activity.setTicketStatus(ticket.getStatus());
211
		}
3024 mandeep.dh 212
 
9338 manish.sha 213
		activity.setCreatorId(agentId);
214
		activity.setDescription("Marked as read ticketId: "
215
				+ activity.getTicketId() + ", activityId: " + activityId);
216
		activity.setType(ActivityType.OTHER);
217
		activityHandler.insertActivity(activity);
218
	}
3024 mandeep.dh 219
 
9338 manish.sha 220
	public List<Agent> getAgents(SearchFilter searchFilter) throws TException {
221
		List<Agent> agents = new ArrayList<Agent>();
3024 mandeep.dh 222
 
9338 manish.sha 223
		for (in.shop2020.crm.domain.Agent agent : agentHandler
224
				.getAgents(in.shop2020.crm.domain.SearchFilter
225
						.create(searchFilter))) {
226
			agents.add(agent.getThriftAgent());
227
		}
3024 mandeep.dh 228
 
9338 manish.sha 229
		return agents;
230
	}
5286 amar.kumar 231
 
9338 manish.sha 232
	public List<Agent> getInactiveAgents(SearchFilter searchFilter) throws TException {
233
		List<Agent> agents = new ArrayList<Agent>();
5286 amar.kumar 234
 
9338 manish.sha 235
		for (in.shop2020.crm.domain.Agent agent : agentHandler
236
				.getInactiveAgents(in.shop2020.crm.domain.SearchFilter
237
						.create(searchFilter))) {
238
			agents.add(agent.getThriftAgent());
239
		}
3339 mandeep.dh 240
 
9338 manish.sha 241
		return agents;
242
	}
3339 mandeep.dh 243
 
9338 manish.sha 244
	public void updatePasswordForAgent(String agentEmailId, String password)
245
	throws TException {
246
		agentHandler.updatePasswordForAgent(agentEmailId, password);
247
	}
3024 mandeep.dh 248
 
9338 manish.sha 249
	public long getLastEmailProcessedTimestamp() throws TException {
250
		return agentHandler.getLastEmailProcessedTimestamp().getTime();
251
	}
3088 mandeep.dh 252
 
9338 manish.sha 253
	public void updateLastEmailProcessedTimestamp(long timestamp)
254
	throws TException {
255
		agentHandler.updateLastEmailProcessedTimestamp(new Date(timestamp));
256
	}
3088 mandeep.dh 257
 
9338 manish.sha 258
	public List<String> getRoleNamesForAgent(String agentEmailId)
259
	throws TException {
260
		return agentHandler.getRoleNamesForAgent(agentEmailId);
261
	}
262
 
263
	public List<String> getPermissionsForRoleName(String roleName)
264
	throws TException {
265
		return agentHandler.getPermissionsForRoleName(roleName);
266
	}
267
 
268
	public void changeAgentStatus(boolean status, String emailId)
269
	throws TException {
270
		agentHandler.changeAgentStatus(status, emailId);
271
	}
272
 
3375 rajveer 273
	public boolean isAlive() throws TException {
9338 manish.sha 274
		try {
275
			return !agentHandler.getAgents(null).isEmpty();
276
		} catch (Exception e) {
277
			log.error("Could not fetch agents", e);
278
		}
3375 rajveer 279
 
9338 manish.sha 280
		return false;
281
	}
4624 mandeep.dh 282
 
3375 rajveer 283
	public void closeSession() throws TException {
284
	}
9338 manish.sha 285
 
4793 amar.kumar 286
	public void insertAgent(Agent agent, List<String> role) throws TException {
287
		agentHandler.insertAgent(in.shop2020.crm.domain.Agent.create(agent), role);
288
	}
9338 manish.sha 289
 
4793 amar.kumar 290
	public void unassignAgentTickets(int assigneeId) {
291
		ticketHandler.unassignAgentTickets(assigneeId);
292
	}
9338 manish.sha 293
 
5168 amar.kumar 294
	public void changeAgentRole(long id, List<String> role) {
295
		agentHandler.changeAgentRole(id,role);
296
	}
9338 manish.sha 297
 
5168 amar.kumar 298
	public int getOpenTicketCountForAgent(long agentId) {
299
		return ticketHandler.getOpenTicketCountForAgent(agentId);
300
	}
9338 manish.sha 301
 
5909 amar.kumar 302
	public Map<String,Long> getOpenTicketsMap() {
303
		return ticketHandler.getOpenTicketsMap();
304
	}
8371 manish.sha 305
 
9338 manish.sha 306
 
8371 manish.sha 307
	public Map<String, String> getFedexReconciliationDataMap(
308
			List<Long> order_ids, String method_key) {
8384 manish.sha 309
		log.info("Into Method of CRM Service to get Fedex Reconciliation Data");
9338 manish.sha 310
		Map<String, String> fedexReconciliationDataMap = new HashMap<String,String>();
311
		Map<String, String> fedexReconciliationDataMap1 = null;
312
		Map<String, String> fedexReconciliationDataMap2 = null;
8371 manish.sha 313
		try{
314
			TransactionClient transactionServiceClient = new TransactionClient();
315
			List<Order> orderList= transactionServiceClient.getClient().getOrderList(order_ids);
9338 manish.sha 316
			List<Order> orderListDelhi = new ArrayList<Order>();
317
			List<Order> orderListMumbai = new ArrayList<Order>();
318
			for(Order ord1: orderList){
319
				Warehouse warehouse = null;
320
				try{
321
					InventoryClient isc = new InventoryClient();
322
					warehouse = isc.getClient().getWarehouse(ord1.getWarehouse_id());
323
				} catch(Exception e) {
324
					log.error("Unable to get warehouse for id : " + ord1.getWarehouse_id(),e);
325
				}
326
				if(warehouse!=null){
327
					if(WarehouseLocation.Mumbai==warehouse.getLogisticsLocation()){
328
						orderListMumbai.add(ord1);
329
					}
330
					else{
331
						orderListDelhi.add(ord1);
332
					}
333
				}
334
			}
335
 
336
			fedexTrackClientMumbai = new FedExTrackingService(WarehouseLocation.Mumbai);
337
			fedexTrackClientDelhi = new FedExTrackingService(WarehouseLocation.Delhi);
338
 
8371 manish.sha 339
			if("delivered_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 340
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExDeliveredOrdersMap(orderListMumbai);
8371 manish.sha 341
			}
9338 manish.sha 342
			if("delivered_orders".equalsIgnoreCase(method_key)){
343
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExDeliveredOrdersMap(orderListDelhi);
344
			}
8371 manish.sha 345
			if("returned_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 346
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExReturnedOrdersMap(orderListMumbai);
8371 manish.sha 347
			}
9338 manish.sha 348
			if("returned_orders".equalsIgnoreCase(method_key)){
349
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExReturnedOrdersMap(orderListDelhi);
350
			}
8371 manish.sha 351
			if("undelivered_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 352
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExUnDeliveredOrdersMap(orderListMumbai);
8371 manish.sha 353
			}
9338 manish.sha 354
			if("undelivered_orders".equalsIgnoreCase(method_key)){
355
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExUnDeliveredOrdersMap(orderListDelhi);
356
			}
8371 manish.sha 357
			if("first_delivery_attempted_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 358
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExFirstDeliveryAttemptedOrdersMap(orderListMumbai);
8371 manish.sha 359
			}
9338 manish.sha 360
			if("first_delivery_attempted_orders".equalsIgnoreCase(method_key)){
361
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExFirstDeliveryAttemptedOrdersMap(orderListDelhi);
362
			}
8371 manish.sha 363
			if("destination_city_reached_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 364
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExReachedDestinationOrdersMap(orderListMumbai);
8371 manish.sha 365
			}
9338 manish.sha 366
			if("destination_city_reached_orders".equalsIgnoreCase(method_key)){
367
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExReachedDestinationOrdersMap(orderListDelhi);
368
			}
8371 manish.sha 369
			if("picked_up_orders".equalsIgnoreCase(method_key) || "local_connection_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 370
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExPickupOrdersMap(orderListMumbai);
8371 manish.sha 371
			}
9338 manish.sha 372
			if("picked_up_orders".equalsIgnoreCase(method_key) || "local_connection_orders".equalsIgnoreCase(method_key)){
373
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExPickupOrdersMap(orderListDelhi);
374
			}
375
			fedexReconciliationDataMap.putAll(fedexReconciliationDataMap1);
376
			fedexReconciliationDataMap.putAll(fedexReconciliationDataMap2);
377
			// TODO Auto-generated method stub
8371 manish.sha 378
		}
379
		catch (TTransportException e) {
8384 manish.sha 380
			log.error("Unable to create thrift Client from fedex reconciliation method.... ", e);
8371 manish.sha 381
		} catch (TException e) {
8384 manish.sha 382
			log.error("Unable to get thrift Client from fedex reconciliation method.... ", e);
8371 manish.sha 383
		}
8384 manish.sha 384
		log.info("Into Method of CRM Service to get Fedex Reconciliation Data....and got it successfully");
8371 manish.sha 385
		return fedexReconciliationDataMap;
386
	}
9338 manish.sha 387
}