Subversion Repositories SmartDukaan

Rev

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