Subversion Repositories SmartDukaan

Rev

Rev 3339 | Rev 3390 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.crm.service.handler;

import in.shop2020.crm.Activity;
import in.shop2020.crm.Agent;
import in.shop2020.crm.CRMService.Iface;
import in.shop2020.crm.Ticket;
import in.shop2020.crm.handler.ActivityHandler;
import in.shop2020.crm.handler.AgentHandler;
import in.shop2020.crm.handler.TicketHandler;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.thrift.TException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of the interface/services exposed by thrift to clients!
 * 
 * @author mandeep
 */
@Service
public class CRMServiceHandler implements Iface {
    ApplicationContext context         = new ClassPathXmlApplicationContext(
                                               "context.xml");
    TicketHandler      ticketHandler   = context.getBean(TicketHandler.class);
    ActivityHandler    activityHandler = context.getBean(ActivityHandler.class);
    AgentHandler       agentHandler    = context.getBean(AgentHandler.class);

    public List<Ticket> getTickets(long customerId) throws TException {
        List<Ticket> ttickets = new ArrayList<Ticket>();
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler.getTickets(customerId)) {
            ttickets.add(ticket.getThriftTicket());
        }

        return ttickets;
    }

    @Transactional
    public void updateTicket(Ticket ticket, Activity activity) throws TException {
        try {
            ticketHandler.updateTicket(in.shop2020.crm.domain.Ticket
                    .create(ticket));
            activity.setTicketId(ticket.getId());
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity.create(activity));
        } catch (ParseException e) {
            throw new TException("Could not update " + ticket, e);
        }
    }

    @Transactional
    public long insertTicket(Ticket ticket, Activity activity) throws TException {
        try {
            long ticketId = ticketHandler.insertTicket(in.shop2020.crm.domain.Ticket
                    .create(ticket));
            activity.setTicketId(ticketId);
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity.create(activity));
            return ticketId;
        } catch (ParseException e) {
            throw new TException("Could not insert " + ticket, e);
        }
    }

    public List<Activity> getActivities(long customerId) throws TException {
        List<Activity> tactivities = new ArrayList<Activity>();
        List<in.shop2020.crm.domain.Activity> activities = activityHandler
                .getActivities(customerId);
        if (activities != null) {
            for (in.shop2020.crm.domain.Activity ticket : activities) {
                tactivities.add(ticket.getThriftActivity());
            }
        }
        return tactivities;
    }

    public void insertActivity(Activity activity) throws TException {
        try {
            activityHandler.insertActivity(in.shop2020.crm.domain.Activity
                    .create(activity));
        } catch (ParseException e) {
            throw new TException("Could not insert " + activity, e);
        }
    }

    public Ticket getTicket(long ticketId) throws TException {
        Ticket thriftTicket = null;

        in.shop2020.crm.domain.Ticket ticket = ticketHandler
                .getTicket(ticketId);

        if (ticket != null) {
            thriftTicket = ticket.getThriftTicket();
        }

        return thriftTicket;
    }

    public Activity getActivity(long activityId) throws TException {
        in.shop2020.crm.domain.Activity activity = activityHandler
                .getActivity(activityId);
        Activity thriftActivity = null;

        if (activity != null) {
            thriftActivity = activity.getThriftActivity();
        }

        return thriftActivity;
    }

    public Activity getLastActivity(long ticketId) throws TException {
        in.shop2020.crm.domain.Activity lastActivity = activityHandler
                .getLastActivity(ticketId);
        Activity lastThriftActivity = null;

        if (lastActivity != null) {
            lastThriftActivity = lastActivity.getThriftActivity();
        }

        return lastThriftActivity;
    }

    public List<Activity> getActivitiesByCreator(long creatorId)
            throws TException {
        List<Activity> tactivities = new ArrayList<Activity>();

        for (in.shop2020.crm.domain.Activity ticket : activityHandler
                .getActivitiesByCreator(creatorId)) {
            tactivities.add(ticket.getThriftActivity());
        }

        return tactivities;
    }

    public List<Activity> getActivitiesForTicket(long ticketId)
            throws TException {
        List<Activity> tactivities = new ArrayList<Activity>();

        for (in.shop2020.crm.domain.Activity ticket : activityHandler
                .getActivitiesForTicket(ticketId)) {
            tactivities.add(ticket.getThriftActivity());
        }

        return tactivities;
    }

    public Agent getAgent(long agentId) throws TException {
        in.shop2020.crm.domain.Agent agent = agentHandler.getAgent(agentId);
        Agent thriftAgent = null;

        if (agent != null) {
            thriftAgent = agent.getThriftAgent();
        }

        return thriftAgent;
    }

    public Agent getAgentByEmailId(String agentEmailId) throws TException {
        in.shop2020.crm.domain.Agent agentByEmail = agentHandler.getAgentByEmail(agentEmailId);
        Agent thriftAgent = null;

        if (agentByEmail != null) {
            thriftAgent = agentByEmail.getThriftAgent();
        }

        return thriftAgent;
    }

    public List<String> getRoleNamesForAgent(String agentEmailId)
            throws TException {
        return agentHandler.getRoleNamesForAgent(agentEmailId);
    }

    public List<String> getPermissionsForRoleName(String roleName)
            throws TException {
        return agentHandler.getPermissionsForRoleName(roleName);
    }

    public List<Ticket> getAssignedTickets(long agentId) throws TException {
        List<Ticket> tickets = new ArrayList<Ticket>();

        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
                .getAssignedTickets(agentId)) {
            tickets.add(ticket.getThriftTicket());
        }

        return tickets;
    }

    public List<Agent> getAllAgents() throws TException {
        List<Agent> agents = new ArrayList<Agent>();

        for (in.shop2020.crm.domain.Agent agent : agentHandler.getAllAgents()) {
            agents.add(agent.getThriftAgent());
        }

        return agents;
    }

    public void updatePasswordForAgent(String agentEmailId, String password)
            throws TException {
        agentHandler.updatePasswordForAgent(agentEmailId, password);
    }

    public List<Ticket> getUnassignedTickets() throws TException {
        List<Ticket> tickets = new ArrayList<Ticket>();

        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
                .getUnassignedTickets()) {
            tickets.add(ticket.getThriftTicket());
        }

        return tickets;
    }

    public List<Ticket> getAllTickets(long agentId) throws TException {
        List<in.shop2020.crm.domain.Ticket> tickets = ticketHandler
                .getAssignedTickets(agentId);

        for (in.shop2020.crm.domain.Agent agent : agentHandler
                .getReportees(agentId)) {
            tickets.addAll(ticketHandler.getAssignedTickets(agent.getId()));
        }

        List<Ticket> ttickets = new ArrayList<Ticket>();

        for (in.shop2020.crm.domain.Ticket ticket : tickets) {
            ttickets.add(ticket.getThriftTicket());
        }

        return ttickets;
    }

    public long getLastEmailProcessedTimestamp() throws TException {
        return agentHandler.getLastEmailProcessedTimestamp().getTime();
    }

    public void updateLastEmailProcessedTimestamp(long timestamp)
            throws TException {
        agentHandler.updateLastEmailProcessedTimestamp(new Date(timestamp));
    }

        public boolean isAlive() throws TException {
                // TODO Auto-generated method stub
                return true;
        }

        public void closeSession() throws TException {
                // TODO Auto-generated method stub
                
        }
}