Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import in.shop2020.serving.auth.CRMAuthorizingRealm;
import in.shop2020.util.CRMConstants;
import in.shop2020.util.CRMConstants.CODCancelMatrix;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.thrift.TException;

import com.opensymphony.xwork2.ValidationAwareSupport;

/**
 * Base class for all user action handlers i.e. controllers
 * 
 * @author Vikas
 */
public abstract class BaseController extends ValidationAwareSupport implements
        ServletResponseAware, ServletRequestAware, SessionAware
{
    private static final long serialVersionUID = 3339523094497219816L;
    protected static Logger log = Logger.getLogger(BaseController.class);

    protected static final String INPUT = "input";
    protected static final String INDEX = "index";
    protected static final String EDIT_NEW = "editNew";
    protected static final String EDIT = "edit";
    protected static final String SHOW = "show";
    protected static final String EXCEPTION = "exception";

    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");

    protected HttpServletResponse response;
    protected HttpServletRequest request;
    protected HttpSession session;
    protected Map<String, Object> sessionMap;

    // Clients used at many places
    protected in.shop2020.model.v1.user.UserContextService.Client userContextServiceClient;
    protected in.shop2020.model.v1.order.TransactionService.Client transactionServiceClient;
    protected in.shop2020.crm.CRMService.Client crmServiceClient;

    protected String currentAgentEmailId = (String) SecurityUtils.getSubject().getPrincipal();

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    
    public void setSession(Map<String, Object> sessionMap) {
        this.session = request.getSession();
        this.sessionMap = sessionMap;
    }
    
    /**
     * Utility method to convert a date to a readable format 
     */
    public String convertDate(Long date) {
        if (date == null || date == 0) {
            return "N/A";
        }

        return SDF.format(new Date(date));
    }

    public String getCurrentAgentEmailId() {
        return currentAgentEmailId;
    }

    public String getAgentName() throws TException
    {
        return CRMAuthorizingRealm.getAgent(currentAgentEmailId).getName();
    }

    public boolean canVerifyCOD() {
        return SecurityUtils.getSubject().hasRole("Outbound");
    }
    
    public boolean canViewFailedPayments() {
        return SecurityUtils.getSubject().hasRole("Outbound");
    }

    public boolean canViewDelayedDeliveries() {
        return SecurityUtils.getSubject().hasRole("Outbound");
    }
    
    public boolean canViewFlaggedPayments() {
        return (SecurityUtils.getSubject().hasRole("Outbound") && SecurityUtils.getSubject().hasRole("TeamLead"));
    }
    
    public boolean canViewDoaRequests() {
        return (SecurityUtils.getSubject().hasRole("Outbound") && SecurityUtils.getSubject().hasRole("TeamLead"));
    }
    
    public boolean canViewReturnRequests() {
        return (SecurityUtils.getSubject().hasRole("Outbound") && SecurityUtils.getSubject().hasRole("TeamLead"));
    }
    
    public boolean canManageAgents() {
        return (SecurityUtils.getSubject().hasRole("TeamLead"));
    }
    
    public boolean canViewOrderCancellation() {
        return (SecurityUtils.getSubject().hasRole("Outbound") && SecurityUtils.getSubject().hasRole("TeamLead"));
    }

    public String editNew() {
        return EDIT_NEW;
    }

    public String edit() {
        return EDIT;
    }
    
    public CODCancelMatrix[] getCODCancelMatrix () {
        return CRMConstants.CODCancelMatrix.values();
    }
}