Subversion Repositories SmartDukaan

Rev

Rev 5916 | Rev 7596 | 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;
3024 mandeep.dh 9
import in.shop2020.crm.Activity;
3390 mandeep.dh 10
import in.shop2020.crm.ActivityType;
3024 mandeep.dh 11
import in.shop2020.crm.Agent;
5936 amar.kumar 12
import in.shop2020.crm.TicketStatus;
3024 mandeep.dh 13
import in.shop2020.crm.CRMService.Iface;
3390 mandeep.dh 14
import in.shop2020.crm.SearchFilter;
3024 mandeep.dh 15
import in.shop2020.crm.Ticket;
5916 amar.kumar 16
import in.shop2020.crm.TicketCategory;
3024 mandeep.dh 17
import in.shop2020.crm.handler.ActivityHandler;
18
import in.shop2020.crm.handler.AgentHandler;
19
import in.shop2020.crm.handler.TicketHandler;
5851 amar.kumar 20
import in.shop2020.thrift.clients.AlertClient;
3024 mandeep.dh 21
 
22
import java.text.ParseException;
23
import java.util.ArrayList;
5851 amar.kumar 24
import java.util.Calendar;
3339 mandeep.dh 25
import java.util.Date;
3024 mandeep.dh 26
import java.util.List;
5909 amar.kumar 27
import java.util.Map;
3024 mandeep.dh 28
 
3499 mandeep.dh 29
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
3024 mandeep.dh 31
import org.apache.thrift.TException;
32
import org.springframework.context.ApplicationContext;
33
import org.springframework.context.support.ClassPathXmlApplicationContext;
34
import org.springframework.stereotype.Service;
3206 mandeep.dh 35
import org.springframework.transaction.annotation.Transactional;
3024 mandeep.dh 36
 
37
/**
38
 * Implementation of the interface/services exposed by thrift to clients!
39
 * 
40
 * @author mandeep
41
 */
42
@Service
43
public class CRMServiceHandler implements Iface {
3499 mandeep.dh 44
    private static final Log log = LogFactory.getLog(CRMServiceHandler.class);
45
 
4008 mandeep.dh 46
    private TicketHandler      ticketHandler;
47
    private ActivityHandler    activityHandler;
48
    private AgentHandler       agentHandler;
49
 
50
    public CRMServiceHandler() {
51
        log.info("Creating context");
52
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
53
        ticketHandler              = context.getBean(TicketHandler.class);
54
        activityHandler            = context.getBean(ActivityHandler.class);
55
        agentHandler               = context.getBean(AgentHandler.class);
56
    }
57
 
3390 mandeep.dh 58
    public List<Ticket> getTickets(SearchFilter searchFilter) throws TException {
3024 mandeep.dh 59
        List<Ticket> ttickets = new ArrayList<Ticket>();
3390 mandeep.dh 60
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
61
                .getTickets(in.shop2020.crm.domain.SearchFilter
62
                        .create(searchFilter))) {
3024 mandeep.dh 63
            ttickets.add(ticket.getThriftTicket());
64
        }
65
 
66
        return ttickets;
67
    }
68
 
3390 mandeep.dh 69
    public List<Ticket> getUnassignedTickets() throws TException {
70
        List<Ticket> tickets = new ArrayList<Ticket>();
71
 
72
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
73
                .getUnassignedTickets()) {
74
            tickets.add(ticket.getThriftTicket());
75
        }
76
 
77
        return tickets;
78
    }
79
 
3206 mandeep.dh 80
    @Transactional
3390 mandeep.dh 81
    public void updateTicket(Ticket ticket, Activity activity)
82
            throws TException {
3024 mandeep.dh 83
        try {
84
            ticketHandler.updateTicket(in.shop2020.crm.domain.Ticket
85
                    .create(ticket));
3206 mandeep.dh 86
            activity.setTicketId(ticket.getId());
3390 mandeep.dh 87
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity
88
                    .create(activity));
5851 amar.kumar 89
            try{
90
	            /*For now monitoring is only done for New tickets and hence once any 
91
	        	 * activity is done on a ticket after its creation, we stop monitoring that ticket*/
5916 amar.kumar 92
            	if(activity.getType()== ActivityType.SEND_EMAIL_TO_CUSTOMER || 
93
            			activity.getType()== ActivityType.ESCALATE_TICKET ||
5936 amar.kumar 94
            			activity.getType()== ActivityType.CALLED_CUSTOMER || 
95
            			activity.getTicketStatus().equals(TicketStatus.CLOSED)) {
5916 amar.kumar 96
		        	AlertService.Client alertClient = new AlertClient().getClient();
97
		            alertClient.endMonitoringEntity(EntityType.TICKET, "ticketId = " + ticket.getId());
98
            	}
5851 amar.kumar 99
            } catch(Exception ex) {
100
            	log.error("Exception while ending monitoring for ticketId " + ticket.getId());
101
            	log.error(ex);
102
            }
3024 mandeep.dh 103
        } catch (ParseException e) {
104
            throw new TException("Could not update " + ticket, e);
105
        }
106
    }
107
 
3206 mandeep.dh 108
    @Transactional
3390 mandeep.dh 109
    public long insertTicket(Ticket ticket, Activity activity)
110
            throws TException {
3024 mandeep.dh 111
        try {
3390 mandeep.dh 112
            long ticketId = ticketHandler
113
                    .insertTicket(in.shop2020.crm.domain.Ticket.create(ticket));
3206 mandeep.dh 114
            activity.setTicketId(ticketId);
3390 mandeep.dh 115
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity
116
                    .create(activity));
5851 amar.kumar 117
            try {
5916 amar.kumar 118
            	if(ticket.getCategory()!= TicketCategory.COD_VERIFICATION &&
119
            			ticket.getCategory()!= TicketCategory.UNDELIVERED &&
120
            			ticket.getCategory()!= TicketCategory.STORE_PICKUP) {
121
		            MonitoredEntity entity = new MonitoredEntity();
122
		            entity.setEntityType(EntityType.TICKET);
123
		            entity.setEventType(0);
124
		            entity.setEntityIdentifier("ticketId = " + new Long(ticketId).toString());
125
		            entity.setDescription(ticket.getDescription());
126
		            Calendar thresholdTime = Calendar.getInstance();
127
		            thresholdTime.add(Calendar.HOUR, 4);
128
		            entity.setWarnExpiryTime(thresholdTime.getTimeInMillis());
129
		            thresholdTime.add(Calendar.HOUR, 2);
130
		            entity.setCriticalExpiryTime(thresholdTime.getTimeInMillis());
131
		            AlertService.Client alertClient = new AlertClient().getClient();
132
		            alertClient.scheduleAlert(entity);
133
            	}
5851 amar.kumar 134
            } catch (Exception ex) {
135
            	log.error("Exception while scheduling alert for Ticket Id "+ticketId);
136
            	log.error(ex);
137
            }
3206 mandeep.dh 138
            return ticketId;
3024 mandeep.dh 139
        } catch (ParseException e) {
140
            throw new TException("Could not insert " + ticket, e);
141
        }
142
    }
143
 
3390 mandeep.dh 144
    public List<Activity> getActivities(SearchFilter searchFilter)
145
            throws TException {
3024 mandeep.dh 146
        List<Activity> tactivities = new ArrayList<Activity>();
3168 mandeep.dh 147
        List<in.shop2020.crm.domain.Activity> activities = activityHandler
3390 mandeep.dh 148
                .getActivities(in.shop2020.crm.domain.SearchFilter
149
                        .create(searchFilter));
150
 
3168 mandeep.dh 151
        if (activities != null) {
152
            for (in.shop2020.crm.domain.Activity ticket : activities) {
153
                tactivities.add(ticket.getThriftActivity());
154
            }
3024 mandeep.dh 155
        }
3390 mandeep.dh 156
 
3024 mandeep.dh 157
        return tactivities;
158
    }
159
 
3405 mandeep.dh 160
    public long insertActivity(Activity activity) throws TException {
3024 mandeep.dh 161
        try {
3405 mandeep.dh 162
            return activityHandler.insertActivity(in.shop2020.crm.domain.Activity
3024 mandeep.dh 163
                    .create(activity));
164
        } catch (ParseException e) {
165
            throw new TException("Could not insert " + activity, e);
166
        }
167
    }
168
 
3390 mandeep.dh 169
    @Transactional
170
    public void markAsRead(long activityId, long agentId) throws TException {
171
        in.shop2020.crm.domain.SearchFilter searchFilter = new in.shop2020.crm.domain.SearchFilter();
172
        searchFilter.setActivityId(activityId);
173
        activityHandler.markAsRead(activityId);
3168 mandeep.dh 174
        in.shop2020.crm.domain.Activity activity = activityHandler
3390 mandeep.dh 175
                .getActivities(searchFilter).get(0);
4089 mandeep.dh 176
 
177
        // Setting activity fields from latest ticket fields
178
        if (activity.getTicketId() != null) {
179
            searchFilter.setTicketId(activity.getTicketId());
180
            in.shop2020.crm.domain.Ticket ticket = ticketHandler.getTickets(searchFilter).get(0);
181
            activity.setTicketAssigneeId(ticket.getAssigneeId());
182
            activity.setTicketCategory(ticket.getCategory());
183
            activity.setTicketDescription(ticket.getDescription());
184
            activity.setTicketPriority(ticket.getPriority());
185
            activity.setTicketStatus(ticket.getStatus());
186
        }
187
 
3390 mandeep.dh 188
        activity.setCreatorId(agentId);
189
        activity.setDescription("Marked as read ticketId: "
190
                + activity.getTicketId() + ", activityId: " + activityId);
191
        activity.setType(ActivityType.OTHER);
192
        activityHandler.insertActivity(activity);
3024 mandeep.dh 193
    }
194
 
3390 mandeep.dh 195
    public List<Agent> getAgents(SearchFilter searchFilter) throws TException {
196
        List<Agent> agents = new ArrayList<Agent>();
3024 mandeep.dh 197
 
3390 mandeep.dh 198
        for (in.shop2020.crm.domain.Agent agent : agentHandler
199
                .getAgents(in.shop2020.crm.domain.SearchFilter
200
                        .create(searchFilter))) {
201
            agents.add(agent.getThriftAgent());
3024 mandeep.dh 202
        }
203
 
3390 mandeep.dh 204
        return agents;
3024 mandeep.dh 205
    }
5286 amar.kumar 206
 
207
    public List<Agent> getInactiveAgents(SearchFilter searchFilter) throws TException {
208
        List<Agent> agents = new ArrayList<Agent>();
3024 mandeep.dh 209
 
5286 amar.kumar 210
        for (in.shop2020.crm.domain.Agent agent : agentHandler
211
                .getInactiveAgents(in.shop2020.crm.domain.SearchFilter
212
                        .create(searchFilter))) {
213
            agents.add(agent.getThriftAgent());
214
        }
215
 
216
        return agents;
217
    }
218
 
3390 mandeep.dh 219
    public void updatePasswordForAgent(String agentEmailId, String password)
3339 mandeep.dh 220
            throws TException {
3390 mandeep.dh 221
        agentHandler.updatePasswordForAgent(agentEmailId, password);
222
    }
3339 mandeep.dh 223
 
3390 mandeep.dh 224
    public long getLastEmailProcessedTimestamp() throws TException {
225
        return agentHandler.getLastEmailProcessedTimestamp().getTime();
3339 mandeep.dh 226
    }
227
 
3390 mandeep.dh 228
    public void updateLastEmailProcessedTimestamp(long timestamp)
3024 mandeep.dh 229
            throws TException {
3390 mandeep.dh 230
        agentHandler.updateLastEmailProcessedTimestamp(new Date(timestamp));
3024 mandeep.dh 231
    }
232
 
3088 mandeep.dh 233
    public List<String> getRoleNamesForAgent(String agentEmailId)
3168 mandeep.dh 234
            throws TException {
3088 mandeep.dh 235
        return agentHandler.getRoleNamesForAgent(agentEmailId);
236
    }
237
 
238
    public List<String> getPermissionsForRoleName(String roleName)
3168 mandeep.dh 239
            throws TException {
3088 mandeep.dh 240
        return agentHandler.getPermissionsForRoleName(roleName);
241
    }
4793 amar.kumar 242
 
243
    public void changeAgentStatus(boolean status, String emailId)
244
    		throws TException {
245
    	agentHandler.changeAgentStatus(status, emailId);
246
    }
3088 mandeep.dh 247
 
3375 rajveer 248
	public boolean isAlive() throws TException {
4624 mandeep.dh 249
        try {
250
            return !agentHandler.getAgents(null).isEmpty();
251
        } catch (Exception e) {
252
            log.error("Could not fetch agents", e);
253
        }
3375 rajveer 254
 
4624 mandeep.dh 255
        return false;
256
    }
257
 
3375 rajveer 258
	public void closeSession() throws TException {
259
	}
4793 amar.kumar 260
 
261
	public void insertAgent(Agent agent, List<String> role) throws TException {
262
		agentHandler.insertAgent(in.shop2020.crm.domain.Agent.create(agent), role);
263
	}
264
 
265
	public void unassignAgentTickets(int assigneeId) {
266
		ticketHandler.unassignAgentTickets(assigneeId);
267
	}
5168 amar.kumar 268
 
269
	public void changeAgentRole(long id, List<String> role) {
270
		agentHandler.changeAgentRole(id,role);
271
	}
272
 
273
	public int getOpenTicketCountForAgent(long agentId) {
274
		return ticketHandler.getOpenTicketCountForAgent(agentId);
275
	}
5909 amar.kumar 276
 
277
	public Map<String,Long> getOpenTicketsMap() {
278
		return ticketHandler.getOpenTicketsMap();
279
	}
3024 mandeep.dh 280
}