Rev 3397 | Rev 3422 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.serving.controllers;import in.shop2020.crm.Activity;import in.shop2020.crm.ActivityType;import in.shop2020.crm.Agent;import in.shop2020.crm.SearchFilter;import in.shop2020.crm.Ticket;import in.shop2020.crm.TicketCategory;import in.shop2020.crm.TicketPriority;import in.shop2020.crm.TicketStatus;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.serving.auth.CRMAuthorizingRealm;import in.shop2020.thrift.clients.CRMClient;import in.shop2020.thrift.clients.HelperClient;import in.shop2020.thrift.clients.UserClient;import in.shop2020.util.CRMConstants;import in.shop2020.utils.HelperService.Client;import in.shop2020.utils.HelperServiceException;import java.text.ParseException;import java.util.ArrayList;import java.util.Collections;import java.util.Date;import java.util.List;import org.apache.shiro.SecurityUtils;import org.apache.thrift.TException;/*** @author mandeep**/public class TicketsController extends BaseController {/****/private static final long serialVersionUID = 1L;List<Ticket> tickets = new ArrayList<Ticket>();private String customerEmailId;private String description;private String assigneeEmailId;private String priority;private String category;private String orderId;private String[] agentIds;private String startTimestamp;private String endTimestamp;private String userId;private String id;private String activityDescription;private String status;private String activityType;private Ticket ticket;private List<Activity> activities;private String subject;private String body;private String customerName;private String customerMobileNumber;public String index() {try {if (id != null && !id.isEmpty()) {SearchFilter searchFilter = new SearchFilter();crmServiceClient = new CRMClient().getClient();searchFilter.setTicketId(Long.parseLong(id));tickets = crmServiceClient.getTickets(searchFilter);}} catch (TException e) {log.error("Error while getting tickets", e);return EXCEPTION;}return INDEX;}public String edit() {try {long ticketId = Long.parseLong(id);SearchFilter searchFilter = new SearchFilter();searchFilter.setTicketId(ticketId);crmServiceClient = new CRMClient().getClient();ticket = crmServiceClient.getTickets(searchFilter).get(0);activities = crmServiceClient.getActivities(searchFilter);subject = createSubjectString(ticket);if (ticket.isSetCustomerId()) {userId = String.valueOf(ticket.getCustomerId());}customerEmailId = ticket.getCustomerEmailId();} catch (TException e) {log.error("Error while loading edit page", e);return EXCEPTION;}return EDIT;}private String createSubjectString(Ticket ticket) {return CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID + ticket.getId()+ " " + ticket.getCategory().name();}public String create() {try {long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();Ticket ticket = new Ticket();ticket.setDescription(description);ticket.setCreatorId(creatorId);ticket.setStatus(TicketStatus.OPEN);ticket.setPriority(TicketPriority.valueOf(priority));ticket.setCategory(TicketCategory.valueOf(category));Activity activity = new Activity();activity.setDescription("Creating Ticket");activity.setType(ActivityType.OTHER);activity.setTicketPriority(TicketPriority.valueOf(priority));activity.setTicketStatus(TicketStatus.OPEN);activity.setCreatorId(creatorId);activity.setTicketCategory(TicketCategory.valueOf(category));activity.setTicketDescription(description);if (orderId != null && !orderId.isEmpty()) {ticket.setOrderId(Long.parseLong(orderId));}if (userId != null && !userId.isEmpty()) {ticket.setCustomerId(Long.parseLong(userId));activity.setCustomerId(Long.parseLong(userId));}else {User user = null;userContextServiceClient = new UserClient().getClient();try {if (customerName != null && !customerName.isEmpty()) {ticket.setCustomerName(customerName);activity.setCustomerName(customerName);}if (customerEmailId != null && !customerEmailId.isEmpty()) {ticket.setCustomerEmailId(customerEmailId);activity.setCustomerEmailId(customerEmailId);user = userContextServiceClient.getUserByEmail(customerEmailId);}if ((user == null || user.getUserId() == -1) && customerMobileNumber != null && !customerMobileNumber.isEmpty()) {ticket.setCustomerMobileNumber(customerMobileNumber);activity.setCustomerMobileNumber(customerMobileNumber);user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(customerMobileNumber));}} catch (UserContextException e) {log.error("Could not fetch user for: " + customerEmailId + " "+ customerMobileNumber + " " + customerName, e);}if (user != null && user.getUserId() != -1) {ticket.setCustomerId(user.getUserId());activity.setCustomerId(user.getUserId());}}// handling null values appropriatelyif (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId).getId();ticket.setAssigneeId(assigneeId);activity.setTicketAssigneeId(assigneeId);}crmServiceClient = new CRMClient().getClient();id = String.valueOf(crmServiceClient.insertTicket(ticket, activity));} catch (TException e) {log.error("Error while creating ticket", e);return EXCEPTION;}return index();}public boolean isAssigneeEditable() {return SecurityUtils.getSubject().hasRole("TeamLead");}public String searchTickets() throws ParseException {try {SearchFilter searchFilter = new SearchFilter();if (userId != null && !userId.isEmpty()) {searchFilter.setCustomerId(Long.parseLong(userId));}if (agentIds != null && agentIds.length != 0) {searchFilter.setTicketAssigneeIds(new ArrayList<Long>());for (String agentId : agentIds) {searchFilter.getTicketAssigneeIds().add(CRMAuthorizingRealm.getAgent(agentId).getId());}}if (startTimestamp != null && !startTimestamp.isEmpty()) {searchFilter.setStartTimestamp(SDF.parse(startTimestamp).getTime());}if (endTimestamp != null && !endTimestamp.isEmpty()) {searchFilter.setEndTimestamp(SDF.parse(endTimestamp).getTime());}crmServiceClient = new CRMClient().getClient();tickets = crmServiceClient.getTickets(searchFilter);} catch (TException e) {String errorString = "Error getting tickets for "+ currentAgentEmailId;log.error(errorString, e);addActionError(errorString);}return index();}/*** Returns tickets assigned to the current logged in agent** @return*/public String getMyOpenTickets() {try {Agent agent = CRMAuthorizingRealm.getAgent(currentAgentEmailId);SearchFilter searchFilter = new SearchFilter();searchFilter.setTicketAssigneeIds(Collections.singletonList(agent.getId()));crmServiceClient = new CRMClient().getClient();for (Ticket ticket : crmServiceClient.getTickets(searchFilter)) {if (TicketStatus.OPEN.equals(ticket.getStatus())) {tickets.add(ticket);}}} catch (TException e) {String errorString = "Error getting tickets for "+ currentAgentEmailId;log.error(errorString, e);addActionError(errorString);}return index();}public String getUnassignedTickets() {try {crmServiceClient = new CRMClient().getClient();tickets = crmServiceClient.getUnassignedTickets();} catch (TException e) {String errorString = "Error getting tickets for "+ currentAgentEmailId;log.error(errorString, e);addActionError(errorString);}return index();}public String update() {try {long creatorId = CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId();// Only subject and close date are editable fields for a ticketTicket ticket = new Ticket();ticket.setId(Long.parseLong(id));ticket.setDescription(description);ticket.setCategory(TicketCategory.valueOf(category));ticket.setPriority(TicketPriority.valueOf(priority));ticket.setStatus(TicketStatus.valueOf(status));// Update when a ticket is closed!if (TicketStatus.CLOSED.name().equals(status)) {ticket.setCloseDate(new Date().getTime());}if (activityDescription == null || activityDescription.isEmpty()) {activityDescription = "Updating ticket fields";}Activity activity = new Activity();activity.setDescription(activityDescription);activity.setType(ActivityType.valueOf(activityType));activity.setTicketPriority(TicketPriority.valueOf(priority));activity.setTicketStatus(TicketStatus.valueOf(status));activity.setCreatorId(creatorId);activity.setTicketCategory(TicketCategory.valueOf(category));activity.setTicketDescription(description);if (userId != null && !userId.isEmpty()) {activity.setCustomerId(Long.parseLong(userId));}log.info(activityType);if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {log.info("Sending mail");Client helperClient = new HelperClient().getClient();activity.setUserEmailId(helperClient.saveUserEmailForSending(customerEmailId, CRMConstants.CRM_EMAIL_SENDOR, subject,"<pre>" + body + "</pre>", null, CRMConstants.CRM_EMAIL_TYPE));// We change activityType to OTHER when pop up box for email// closesactivity.setDescription("Subject: " + subject + "\n\n"+ "Body: " + body);}// handling null values appropriatelyif (assigneeEmailId != null && !assigneeEmailId.isEmpty()) {long assigneeId = CRMAuthorizingRealm.getAgent(assigneeEmailId).getId();ticket.setAssigneeId(assigneeId);activity.setTicketAssigneeId(assigneeId);}User user = null;userContextServiceClient = new UserClient().getClient();try {if (customerName != null && !customerName.isEmpty()) {ticket.setCustomerName(customerName);activity.setCustomerName(customerName);}if (customerEmailId != null && !customerEmailId.isEmpty()) {ticket.setCustomerEmailId(customerEmailId);activity.setCustomerEmailId(customerEmailId);user = userContextServiceClient.getUserByEmail(customerEmailId);}if ((user == null || user.getUserId() == -1) && customerMobileNumber != null && !customerMobileNumber.isEmpty()) {ticket.setCustomerMobileNumber(customerMobileNumber);activity.setCustomerMobileNumber(customerMobileNumber);user = userContextServiceClient.getUserByMobileNumber(Long.parseLong(customerMobileNumber));}} catch (UserContextException e) {log.error("Could not fetch user for: " + customerEmailId + " "+ customerMobileNumber + " " + customerName, e);}if (user != null && user.getUserId() != -1) {ticket.setCustomerId(user.getUserId());activity.setCustomerId(user.getUserId());}crmServiceClient = new CRMClient().getClient();crmServiceClient.updateTicket(ticket, activity);} catch (TException e) {log.error("Error while updating ticket", e);return EXCEPTION;} catch (HelperServiceException hse) {log.error("Error while sending mail", hse);return EXCEPTION;}return index();}public User getUser(Long userId) {User user = null;try {userContextServiceClient = new UserClient().getClient();user = userContextServiceClient.getUserById(userId);} catch (UserContextException e) {String errorString = "Could not fetch user for " + userId;log.error(errorString, e);addActionError(errorString);} catch (TException e) {String errorString = "Could not create client";log.error(errorString, e);addActionError(errorString);}return user;}public ActivityType[] getActivityTypes() {return ActivityType.values();}public TicketStatus[] getTicketStatuses() {return TicketStatus.values();}public Agent getAgent(long agentId) throws TException {return CRMAuthorizingRealm.getAgent(agentId);}public List<Agent> getAllAgents() {return CRMAuthorizingRealm.getAgents();}public TicketCategory[] getTicketCategories() {return TicketCategory.values();}public TicketPriority[] getTicketPriorities() {return TicketPriority.values();}public List<Ticket> getTickets() {return tickets;}public void setTickets(List<Ticket> tickets) {this.tickets = tickets;}public String getCustomerEmailId() {return customerEmailId;}public void setCustomerEmailId(String customerEmailId) {this.customerEmailId = customerEmailId;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getAssigneeEmailId() {return assigneeEmailId;}public void setAssigneeEmailId(String assigneeEmailId) {this.assigneeEmailId = assigneeEmailId;}public String getPriority() {return priority;}public void setPriority(String priority) {this.priority = priority;}public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}public String getOrderId() {return orderId;}public void setOrderId(String orderId) {this.orderId = orderId;}public String[] getAgentIds() {return agentIds;}public void setAgentIds(String[] agentIds) {this.agentIds = agentIds;}public String getStartTimestamp() {return startTimestamp;}public void setStartTimestamp(String startTimestamp) {this.startTimestamp = startTimestamp;}public String getEndTimestamp() {return endTimestamp;}public void setEndTimestamp(String endTimestamp) {this.endTimestamp = endTimestamp;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getActivityDescription() {return activityDescription;}public void setActivityDescription(String activityDescription) {this.activityDescription = activityDescription;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public String getActivityType() {return activityType;}public void setActivityType(String activityType) {this.activityType = activityType;}public Ticket getTicket() {return ticket;}public void setTicket(Ticket ticket) {this.ticket = ticket;}public List<Activity> getActivities() {return activities;}public void setActivities(List<Activity> activities) {this.activities = activities;}public String getSubject() {return subject;}public void setSubject(String subject) {this.subject = subject;}public String getBody() {return body;}public void setBody(String body) {this.body = body;}public String getCustomerName() {return customerName;}public void setCustomerName(String customerName) {this.customerName = customerName;}public String getCustomerMobileNumber() {return customerMobileNumber;}public void setCustomerMobileNumber(String customerMobileNumber) {this.customerMobileNumber = customerMobileNumber;}}