Subversion Repositories SmartDukaan

Rev

Rev 4624 | Rev 5142 | 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
 
6
import in.shop2020.crm.Activity;
3390 mandeep.dh 7
import in.shop2020.crm.ActivityType;
3024 mandeep.dh 8
import in.shop2020.crm.Agent;
9
import in.shop2020.crm.CRMService.Iface;
3390 mandeep.dh 10
import in.shop2020.crm.SearchFilter;
3024 mandeep.dh 11
import in.shop2020.crm.Ticket;
12
import in.shop2020.crm.handler.ActivityHandler;
13
import in.shop2020.crm.handler.AgentHandler;
14
import in.shop2020.crm.handler.TicketHandler;
4008 mandeep.dh 15
import in.shop2020.crm.util.DelayedOrderProcessorTask;
16
import in.shop2020.crm.util.PaymentProcessorTask;
17
import in.shop2020.crm.util.CODTransactionProcessorTask;
3024 mandeep.dh 18
 
19
import java.text.ParseException;
20
import java.util.ArrayList;
3339 mandeep.dh 21
import java.util.Date;
3024 mandeep.dh 22
import java.util.List;
4008 mandeep.dh 23
import java.util.concurrent.Executors;
24
import java.util.concurrent.ScheduledExecutorService;
25
import java.util.concurrent.TimeUnit;
3024 mandeep.dh 26
 
3499 mandeep.dh 27
import org.apache.commons.logging.Log;
28
import org.apache.commons.logging.LogFactory;
3024 mandeep.dh 29
import org.apache.thrift.TException;
30
import org.springframework.context.ApplicationContext;
31
import org.springframework.context.support.ClassPathXmlApplicationContext;
32
import org.springframework.stereotype.Service;
3206 mandeep.dh 33
import org.springframework.transaction.annotation.Transactional;
3024 mandeep.dh 34
 
35
/**
36
 * Implementation of the interface/services exposed by thrift to clients!
37
 * 
38
 * @author mandeep
39
 */
40
@Service
41
public class CRMServiceHandler implements Iface {
3499 mandeep.dh 42
    private static final Log log = LogFactory.getLog(CRMServiceHandler.class);
43
 
4008 mandeep.dh 44
    private ScheduledExecutorService scheduler;
45
    private TicketHandler      ticketHandler;
46
    private ActivityHandler    activityHandler;
47
    private AgentHandler       agentHandler;
48
 
49
    public CRMServiceHandler() {
50
        log.info("Creating context");
51
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
52
        ticketHandler              = context.getBean(TicketHandler.class);
53
        activityHandler            = context.getBean(ActivityHandler.class);
54
        agentHandler               = context.getBean(AgentHandler.class);
55
        scheduler                  = Executors.newSingleThreadScheduledExecutor();
56
 
57
        log.info("Scheduling tasks");
58
        scheduler.scheduleAtFixedRate(new CODTransactionProcessorTask(ticketHandler, activityHandler), 2, 5, TimeUnit.MINUTES);
59
        scheduler.scheduleAtFixedRate(new PaymentProcessorTask(ticketHandler, activityHandler), 4, 5, TimeUnit.MINUTES);
60
        scheduler.scheduleAtFixedRate(new DelayedOrderProcessorTask(ticketHandler, activityHandler), 6, 5, TimeUnit.MINUTES);
61
    }
62
 
3390 mandeep.dh 63
    public List<Ticket> getTickets(SearchFilter searchFilter) throws TException {
3024 mandeep.dh 64
        List<Ticket> ttickets = new ArrayList<Ticket>();
3390 mandeep.dh 65
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
66
                .getTickets(in.shop2020.crm.domain.SearchFilter
67
                        .create(searchFilter))) {
3024 mandeep.dh 68
            ttickets.add(ticket.getThriftTicket());
69
        }
70
 
71
        return ttickets;
72
    }
73
 
3390 mandeep.dh 74
    public List<Ticket> getUnassignedTickets() throws TException {
75
        List<Ticket> tickets = new ArrayList<Ticket>();
76
 
77
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
78
                .getUnassignedTickets()) {
79
            tickets.add(ticket.getThriftTicket());
80
        }
81
 
82
        return tickets;
83
    }
84
 
3206 mandeep.dh 85
    @Transactional
3390 mandeep.dh 86
    public void updateTicket(Ticket ticket, Activity activity)
87
            throws TException {
3024 mandeep.dh 88
        try {
89
            ticketHandler.updateTicket(in.shop2020.crm.domain.Ticket
90
                    .create(ticket));
3206 mandeep.dh 91
            activity.setTicketId(ticket.getId());
3390 mandeep.dh 92
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity
93
                    .create(activity));
3024 mandeep.dh 94
        } catch (ParseException e) {
95
            throw new TException("Could not update " + ticket, e);
96
        }
97
    }
98
 
3206 mandeep.dh 99
    @Transactional
3390 mandeep.dh 100
    public long insertTicket(Ticket ticket, Activity activity)
101
            throws TException {
3024 mandeep.dh 102
        try {
3390 mandeep.dh 103
            long ticketId = ticketHandler
104
                    .insertTicket(in.shop2020.crm.domain.Ticket.create(ticket));
3206 mandeep.dh 105
            activity.setTicketId(ticketId);
3390 mandeep.dh 106
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity
107
                    .create(activity));
3206 mandeep.dh 108
            return ticketId;
3024 mandeep.dh 109
        } catch (ParseException e) {
110
            throw new TException("Could not insert " + ticket, e);
111
        }
112
    }
113
 
3390 mandeep.dh 114
    public List<Activity> getActivities(SearchFilter searchFilter)
115
            throws TException {
3024 mandeep.dh 116
        List<Activity> tactivities = new ArrayList<Activity>();
3168 mandeep.dh 117
        List<in.shop2020.crm.domain.Activity> activities = activityHandler
3390 mandeep.dh 118
                .getActivities(in.shop2020.crm.domain.SearchFilter
119
                        .create(searchFilter));
120
 
3168 mandeep.dh 121
        if (activities != null) {
122
            for (in.shop2020.crm.domain.Activity ticket : activities) {
123
                tactivities.add(ticket.getThriftActivity());
124
            }
3024 mandeep.dh 125
        }
3390 mandeep.dh 126
 
3024 mandeep.dh 127
        return tactivities;
128
    }
129
 
3405 mandeep.dh 130
    public long insertActivity(Activity activity) throws TException {
3024 mandeep.dh 131
        try {
3405 mandeep.dh 132
            return activityHandler.insertActivity(in.shop2020.crm.domain.Activity
3024 mandeep.dh 133
                    .create(activity));
134
        } catch (ParseException e) {
135
            throw new TException("Could not insert " + activity, e);
136
        }
137
    }
138
 
3390 mandeep.dh 139
    @Transactional
140
    public void markAsRead(long activityId, long agentId) throws TException {
141
        in.shop2020.crm.domain.SearchFilter searchFilter = new in.shop2020.crm.domain.SearchFilter();
142
        searchFilter.setActivityId(activityId);
143
        activityHandler.markAsRead(activityId);
3168 mandeep.dh 144
        in.shop2020.crm.domain.Activity activity = activityHandler
3390 mandeep.dh 145
                .getActivities(searchFilter).get(0);
4089 mandeep.dh 146
 
147
        // Setting activity fields from latest ticket fields
148
        if (activity.getTicketId() != null) {
149
            searchFilter.setTicketId(activity.getTicketId());
150
            in.shop2020.crm.domain.Ticket ticket = ticketHandler.getTickets(searchFilter).get(0);
151
            activity.setTicketAssigneeId(ticket.getAssigneeId());
152
            activity.setTicketCategory(ticket.getCategory());
153
            activity.setTicketDescription(ticket.getDescription());
154
            activity.setTicketPriority(ticket.getPriority());
155
            activity.setTicketStatus(ticket.getStatus());
156
        }
157
 
3390 mandeep.dh 158
        activity.setCreatorId(agentId);
159
        activity.setDescription("Marked as read ticketId: "
160
                + activity.getTicketId() + ", activityId: " + activityId);
161
        activity.setType(ActivityType.OTHER);
162
        activityHandler.insertActivity(activity);
3024 mandeep.dh 163
    }
164
 
3390 mandeep.dh 165
    public List<Agent> getAgents(SearchFilter searchFilter) throws TException {
166
        List<Agent> agents = new ArrayList<Agent>();
3024 mandeep.dh 167
 
3390 mandeep.dh 168
        for (in.shop2020.crm.domain.Agent agent : agentHandler
169
                .getAgents(in.shop2020.crm.domain.SearchFilter
170
                        .create(searchFilter))) {
171
            agents.add(agent.getThriftAgent());
3024 mandeep.dh 172
        }
173
 
3390 mandeep.dh 174
        return agents;
3024 mandeep.dh 175
    }
176
 
3390 mandeep.dh 177
    public void updatePasswordForAgent(String agentEmailId, String password)
3339 mandeep.dh 178
            throws TException {
3390 mandeep.dh 179
        agentHandler.updatePasswordForAgent(agentEmailId, password);
180
    }
3339 mandeep.dh 181
 
3390 mandeep.dh 182
    public long getLastEmailProcessedTimestamp() throws TException {
183
        return agentHandler.getLastEmailProcessedTimestamp().getTime();
3339 mandeep.dh 184
    }
185
 
3390 mandeep.dh 186
    public void updateLastEmailProcessedTimestamp(long timestamp)
3024 mandeep.dh 187
            throws TException {
3390 mandeep.dh 188
        agentHandler.updateLastEmailProcessedTimestamp(new Date(timestamp));
3024 mandeep.dh 189
    }
190
 
3088 mandeep.dh 191
    public List<String> getRoleNamesForAgent(String agentEmailId)
3168 mandeep.dh 192
            throws TException {
3088 mandeep.dh 193
        return agentHandler.getRoleNamesForAgent(agentEmailId);
194
    }
195
 
196
    public List<String> getPermissionsForRoleName(String roleName)
3168 mandeep.dh 197
            throws TException {
3088 mandeep.dh 198
        return agentHandler.getPermissionsForRoleName(roleName);
199
    }
4793 amar.kumar 200
 
201
    public void changeAgentStatus(boolean status, String emailId)
202
    		throws TException {
203
    	agentHandler.changeAgentStatus(status, emailId);
204
    }
3088 mandeep.dh 205
 
3375 rajveer 206
	public boolean isAlive() throws TException {
4624 mandeep.dh 207
        try {
208
            return !agentHandler.getAgents(null).isEmpty();
209
        } catch (Exception e) {
210
            log.error("Could not fetch agents", e);
211
        }
3375 rajveer 212
 
4624 mandeep.dh 213
        return false;
214
    }
215
 
3375 rajveer 216
	public void closeSession() throws TException {
217
	}
4793 amar.kumar 218
 
219
	public void insertAgent(Agent agent, List<String> role) throws TException {
220
		agentHandler.insertAgent(in.shop2020.crm.domain.Agent.create(agent), role);
221
	}
222
 
223
	public void unassignAgentTickets(int assigneeId) {
224
		ticketHandler.unassignAgentTickets(assigneeId);
225
	}
3024 mandeep.dh 226
}