Subversion Repositories SmartDukaan

Rev

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