Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.serving.controllers;

import in.shop2020.model.v1.order.LineItem;
import in.shop2020.model.v1.order.RechargeType;
import in.shop2020.serving.auth.CRMAuthorizingRealm;
import in.shop2020.thrift.clients.TransactionClient;
import in.shop2020.util.CRMConstants;
import in.shop2020.util.CRMConstants.CODCancelMatrix;
import in.shop2020.utils.ModelUtils;

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 OPEN = "open";
    protected static final String EXCEPTION = "exception";
    
    static Map<Long, String> providersMap;

    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;
    }
    
    static {
        TransactionClient tcl;
        try {
            tcl = new TransactionClient();
            providersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, false);
            providersMap.putAll(tcl.getClient().getServiceProviders(RechargeType.DTH, false));
        } catch (Exception e) {
            log.error("Could not get providers", e);
        }
    }
    
    /**
     * 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 getProductName(LineItem lineItem) {
        String name = ModelUtils.extractProductNameFromLineItem(lineItem);

        if (lineItem.getColor() != null && !lineItem.getColor().isEmpty()) {
            name += "(" + lineItem.getColor() + ")";
        }

        return name;
    }

    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");
    }
    
    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");
    }
    
    public boolean canViewStorePickupTickets() {
        return (SecurityUtils.getSubject().hasRole("Outbound"));
    }

    public boolean canViewAllOpenTickets() {
        return (SecurityUtils.getSubject().hasRole("TeamLead")||SecurityUtils.getSubject().hasRole("Outbound"));
    }
    
    public boolean canViewLowInventoryCancellation() { 
        return (SecurityUtils.getSubject().hasRole("TeamLead")||SecurityUtils.getSubject().hasRole("Outbound"));
    }
    
    //Start:- Added By Manish Sharma for Creating a new Ticket: Category- RTO Refund on 21-Jun-2013
    public boolean canViewRTORefunds() { 
        return (SecurityUtils.getSubject().hasRole("TeamLead")||SecurityUtils.getSubject().hasRole("Outbound"));
    }
    //End:- Added By Manish Sharma for Creating a new Ticket: Category- RTO Refund on 21-Jun-2013
    
    public boolean canViewBulkOrderEnquiry() { 
        return (SecurityUtils.getSubject().hasRole("TeamLead"));
    }
    
    public boolean canViewProfitMandiTicket(){
        return (SecurityUtils.getSubject().hasRole("TeamLead")||SecurityUtils.getSubject().hasRole("Outbound"));
    }
    
    public String editNew() {
        return EDIT_NEW;
    }

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