Subversion Repositories SmartDukaan

Rev

Rev 21888 | 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());
21885 amit.gupta 139
					entity.setDescription(ticket.getDescription().substring(0, Math.min(ticket.getDescription().length(), 2048)-1));
9338 manish.sha 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);
21887 amit.gupta 150
				ex.printStackTrace();
9338 manish.sha 151
			}
152
			return ticketId;
153
		} catch (ParseException e) {
154
			throw new TException("Could not insert " + ticket, e);
155
		}
156
	}
3024 mandeep.dh 157
 
21888 amit.gupta 158
	public List<Activity> getActivities(SearchFilter searchFilter) throws TException {
9338 manish.sha 159
		List<Activity> tactivities = new ArrayList<Activity>();
14902 manish.sha 160
		log.info("Search Filter..."+searchFilter.isNotShowPmTickets());
9338 manish.sha 161
		in.shop2020.crm.domain.SearchFilter filter_to_check  =in.shop2020.crm.domain.SearchFilter.create(searchFilter);
14902 manish.sha 162
		log.info("Search Filter after conversion..."+filter_to_check.getNotShowPmTickets());
9338 manish.sha 163
		List<in.shop2020.crm.domain.Activity> activities = activityHandler
164
		.getActivities(filter_to_check);
165
		in.shop2020.crm.domain.SearchFilter new_searchFilter = new in.shop2020.crm.domain.SearchFilter();
3390 mandeep.dh 166
 
9338 manish.sha 167
		if (activities != null) {
168
			for (in.shop2020.crm.domain.Activity ticket : activities) {
169
				in.shop2020.crm.Activity activity = ticket.getThriftActivity();
170
				if(filter_to_check.getIsActivityRead()!=null && !filter_to_check.getIsActivityRead()){
171
					if(activity.getTicketId() > 0){
172
						new_searchFilter.setTicketId(activity.getTicketId());
173
						List<in.shop2020.crm.domain.Ticket> tickets_as_per_activity= ticketHandler.getTickets(new_searchFilter);
174
						activity.setTicketStatus(tickets_as_per_activity.get(0).getStatus());
175
					}
176
				}
177
				tactivities.add(activity);
178
			}
179
		}
180
		return tactivities;
181
	}
3024 mandeep.dh 182
 
9338 manish.sha 183
	public long insertActivity(Activity activity) throws TException {
184
		try {
185
			return activityHandler.insertActivity(in.shop2020.crm.domain.Activity
186
					.create(activity));
187
		} catch (ParseException e) {
188
			throw new TException("Could not insert " + activity, e);
189
		}
190
	}
4089 mandeep.dh 191
 
9338 manish.sha 192
	@Transactional
193
	public void markAsRead(long activityId, long agentId) throws TException {
194
		in.shop2020.crm.domain.SearchFilter searchFilter = new in.shop2020.crm.domain.SearchFilter();
195
		searchFilter.setActivityId(activityId);
196
		activityHandler.markAsRead(activityId);
197
		in.shop2020.crm.domain.Activity activity = activityHandler
198
		.getActivities(searchFilter).get(0);
4089 mandeep.dh 199
 
9338 manish.sha 200
		// Setting activity fields from latest ticket fields
201
		if (activity.getTicketId() != null) {
202
			searchFilter.setTicketId(activity.getTicketId());
203
			in.shop2020.crm.domain.Ticket ticket = ticketHandler.getTickets(searchFilter).get(0);
204
			activity.setTicketAssigneeId(ticket.getAssigneeId());
205
			activity.setTicketCategory(ticket.getCategory());
206
			activity.setTicketDescription(ticket.getDescription());
207
			activity.setTicketPriority(ticket.getPriority());
208
			activity.setTicketStatus(ticket.getStatus());
209
		}
3024 mandeep.dh 210
 
9338 manish.sha 211
		activity.setCreatorId(agentId);
212
		activity.setDescription("Marked as read ticketId: "
213
				+ activity.getTicketId() + ", activityId: " + activityId);
214
		activity.setType(ActivityType.OTHER);
215
		activityHandler.insertActivity(activity);
216
	}
3024 mandeep.dh 217
 
9338 manish.sha 218
	public List<Agent> getAgents(SearchFilter searchFilter) throws TException {
219
		List<Agent> agents = new ArrayList<Agent>();
3024 mandeep.dh 220
 
21888 amit.gupta 221
		for (in.shop2020.crm.domain.Agent agent : agentHandler.getAgents(in.shop2020.crm.domain.SearchFilter.create(searchFilter))) {
9338 manish.sha 222
			agents.add(agent.getThriftAgent());
223
		}
3024 mandeep.dh 224
 
9338 manish.sha 225
		return agents;
226
	}
5286 amar.kumar 227
 
9338 manish.sha 228
	public List<Agent> getInactiveAgents(SearchFilter searchFilter) throws TException {
229
		List<Agent> agents = new ArrayList<Agent>();
5286 amar.kumar 230
 
9338 manish.sha 231
		for (in.shop2020.crm.domain.Agent agent : agentHandler
232
				.getInactiveAgents(in.shop2020.crm.domain.SearchFilter
233
						.create(searchFilter))) {
234
			agents.add(agent.getThriftAgent());
235
		}
3339 mandeep.dh 236
 
9338 manish.sha 237
		return agents;
238
	}
3339 mandeep.dh 239
 
9338 manish.sha 240
	public void updatePasswordForAgent(String agentEmailId, String password)
241
	throws TException {
242
		agentHandler.updatePasswordForAgent(agentEmailId, password);
243
	}
3024 mandeep.dh 244
 
9338 manish.sha 245
	public long getLastEmailProcessedTimestamp() throws TException {
246
		return agentHandler.getLastEmailProcessedTimestamp().getTime();
247
	}
3088 mandeep.dh 248
 
9338 manish.sha 249
	public void updateLastEmailProcessedTimestamp(long timestamp)
250
	throws TException {
251
		agentHandler.updateLastEmailProcessedTimestamp(new Date(timestamp));
252
	}
3088 mandeep.dh 253
 
9338 manish.sha 254
	public List<String> getRoleNamesForAgent(String agentEmailId)
255
	throws TException {
256
		return agentHandler.getRoleNamesForAgent(agentEmailId);
257
	}
258
 
259
	public List<String> getPermissionsForRoleName(String roleName)
260
	throws TException {
261
		return agentHandler.getPermissionsForRoleName(roleName);
262
	}
263
 
264
	public void changeAgentStatus(boolean status, String emailId)
265
	throws TException {
266
		agentHandler.changeAgentStatus(status, emailId);
267
	}
268
 
3375 rajveer 269
	public boolean isAlive() throws TException {
9338 manish.sha 270
		try {
271
			return !agentHandler.getAgents(null).isEmpty();
272
		} catch (Exception e) {
273
			log.error("Could not fetch agents", e);
274
		}
3375 rajveer 275
 
9338 manish.sha 276
		return false;
277
	}
4624 mandeep.dh 278
 
3375 rajveer 279
	public void closeSession() throws TException {
280
	}
9338 manish.sha 281
 
4793 amar.kumar 282
	public void insertAgent(Agent agent, List<String> role) throws TException {
283
		agentHandler.insertAgent(in.shop2020.crm.domain.Agent.create(agent), role);
284
	}
9338 manish.sha 285
 
4793 amar.kumar 286
	public void unassignAgentTickets(int assigneeId) {
287
		ticketHandler.unassignAgentTickets(assigneeId);
288
	}
9338 manish.sha 289
 
5168 amar.kumar 290
	public void changeAgentRole(long id, List<String> role) {
291
		agentHandler.changeAgentRole(id,role);
292
	}
9338 manish.sha 293
 
5168 amar.kumar 294
	public int getOpenTicketCountForAgent(long agentId) {
295
		return ticketHandler.getOpenTicketCountForAgent(agentId);
296
	}
9338 manish.sha 297
 
5909 amar.kumar 298
	public Map<String,Long> getOpenTicketsMap() {
299
		return ticketHandler.getOpenTicketsMap();
300
	}
8371 manish.sha 301
 
9338 manish.sha 302
 
8371 manish.sha 303
	public Map<String, String> getFedexReconciliationDataMap(
304
			List<Long> order_ids, String method_key) {
8384 manish.sha 305
		log.info("Into Method of CRM Service to get Fedex Reconciliation Data");
9338 manish.sha 306
		Map<String, String> fedexReconciliationDataMap = new HashMap<String,String>();
307
		Map<String, String> fedexReconciliationDataMap1 = null;
308
		Map<String, String> fedexReconciliationDataMap2 = null;
8371 manish.sha 309
		try{
310
			TransactionClient transactionServiceClient = new TransactionClient();
311
			List<Order> orderList= transactionServiceClient.getClient().getOrderList(order_ids);
9338 manish.sha 312
			List<Order> orderListDelhi = new ArrayList<Order>();
313
			List<Order> orderListMumbai = new ArrayList<Order>();
314
			for(Order ord1: orderList){
315
				Warehouse warehouse = null;
316
				try{
317
					InventoryClient isc = new InventoryClient();
318
					warehouse = isc.getClient().getWarehouse(ord1.getWarehouse_id());
319
				} catch(Exception e) {
320
					log.error("Unable to get warehouse for id : " + ord1.getWarehouse_id(),e);
321
				}
322
				if(warehouse!=null){
323
					if(WarehouseLocation.Mumbai==warehouse.getLogisticsLocation()){
324
						orderListMumbai.add(ord1);
325
					}
326
					else{
327
						orderListDelhi.add(ord1);
328
					}
329
				}
330
			}
331
 
332
			fedexTrackClientMumbai = new FedExTrackingService(WarehouseLocation.Mumbai);
333
			fedexTrackClientDelhi = new FedExTrackingService(WarehouseLocation.Delhi);
334
 
8371 manish.sha 335
			if("delivered_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 336
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExDeliveredOrdersMap(orderListMumbai);
8371 manish.sha 337
			}
9338 manish.sha 338
			if("delivered_orders".equalsIgnoreCase(method_key)){
339
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExDeliveredOrdersMap(orderListDelhi);
340
			}
8371 manish.sha 341
			if("returned_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 342
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExReturnedOrdersMap(orderListMumbai);
8371 manish.sha 343
			}
9338 manish.sha 344
			if("returned_orders".equalsIgnoreCase(method_key)){
345
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExReturnedOrdersMap(orderListDelhi);
346
			}
8371 manish.sha 347
			if("undelivered_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 348
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExUnDeliveredOrdersMap(orderListMumbai);
8371 manish.sha 349
			}
9338 manish.sha 350
			if("undelivered_orders".equalsIgnoreCase(method_key)){
351
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExUnDeliveredOrdersMap(orderListDelhi);
352
			}
8371 manish.sha 353
			if("first_delivery_attempted_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 354
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExFirstDeliveryAttemptedOrdersMap(orderListMumbai);
8371 manish.sha 355
			}
9338 manish.sha 356
			if("first_delivery_attempted_orders".equalsIgnoreCase(method_key)){
357
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExFirstDeliveryAttemptedOrdersMap(orderListDelhi);
358
			}
8371 manish.sha 359
			if("destination_city_reached_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 360
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExReachedDestinationOrdersMap(orderListMumbai);
8371 manish.sha 361
			}
9338 manish.sha 362
			if("destination_city_reached_orders".equalsIgnoreCase(method_key)){
363
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExReachedDestinationOrdersMap(orderListDelhi);
364
			}
8371 manish.sha 365
			if("picked_up_orders".equalsIgnoreCase(method_key) || "local_connection_orders".equalsIgnoreCase(method_key)){
9338 manish.sha 366
				fedexReconciliationDataMap1 = fedexTrackClientMumbai.readFedExPickupOrdersMap(orderListMumbai);
8371 manish.sha 367
			}
9338 manish.sha 368
			if("picked_up_orders".equalsIgnoreCase(method_key) || "local_connection_orders".equalsIgnoreCase(method_key)){
369
				fedexReconciliationDataMap2 = fedexTrackClientDelhi.readFedExPickupOrdersMap(orderListDelhi);
370
			}
371
			fedexReconciliationDataMap.putAll(fedexReconciliationDataMap1);
372
			fedexReconciliationDataMap.putAll(fedexReconciliationDataMap2);
373
			// TODO Auto-generated method stub
8371 manish.sha 374
		}
375
		catch (TTransportException e) {
8384 manish.sha 376
			log.error("Unable to create thrift Client from fedex reconciliation method.... ", e);
8371 manish.sha 377
		} catch (TException e) {
8384 manish.sha 378
			log.error("Unable to get thrift Client from fedex reconciliation method.... ", e);
8371 manish.sha 379
		}
8384 manish.sha 380
		log.info("Into Method of CRM Service to get Fedex Reconciliation Data....and got it successfully");
8371 manish.sha 381
		return fedexReconciliationDataMap;
382
	}
9338 manish.sha 383
}