Subversion Repositories SmartDukaan

Rev

Rev 3390 | Rev 3499 | 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.ActivityType;
import in.shop2020.crm.Agent;
import in.shop2020.crm.CRMService.Iface;
import in.shop2020.crm.SearchFilter;
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(SearchFilter searchFilter) throws TException {
        List<Ticket> ttickets = new ArrayList<Ticket>();
        for (in.shop2020.crm.domain.Ticket ticket : ticketHandler
                .getTickets(in.shop2020.crm.domain.SearchFilter
                        .create(searchFilter))) {
            ttickets.add(ticket.getThriftTicket());
        }

        return ttickets;
    }

    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;
    }

    @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(SearchFilter searchFilter)
            throws TException {
        List<Activity> tactivities = new ArrayList<Activity>();
        List<in.shop2020.crm.domain.Activity> activities = activityHandler
                .getActivities(in.shop2020.crm.domain.SearchFilter
                        .create(searchFilter));

        if (activities != null) {
            for (in.shop2020.crm.domain.Activity ticket : activities) {
                tactivities.add(ticket.getThriftActivity());
            }
        }

        return tactivities;
    }

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

    @Transactional
    public void markAsRead(long activityId, long agentId) throws TException {
        in.shop2020.crm.domain.SearchFilter searchFilter = new in.shop2020.crm.domain.SearchFilter();
        searchFilter.setActivityId(activityId);
        activityHandler.markAsRead(activityId);
        in.shop2020.crm.domain.Activity activity = activityHandler
                .getActivities(searchFilter).get(0);
        activity.setCreatorId(agentId);
        activity.setDescription("Marked as read ticketId: "
                + activity.getTicketId() + ", activityId: " + activityId);
        activity.setType(ActivityType.OTHER);
        activityHandler.insertActivity(activity);
    }

    public List<Agent> getAgents(SearchFilter searchFilter) throws TException {
        List<Agent> agents = new ArrayList<Agent>();

        for (in.shop2020.crm.domain.Agent agent : agentHandler
                .getAgents(in.shop2020.crm.domain.SearchFilter
                        .create(searchFilter))) {
            agents.add(agent.getThriftAgent());
        }

        return agents;
    }

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

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

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

    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 boolean isAlive() throws TException {
                // TODO Auto-generated method stub
                return true;
        }

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