Subversion Repositories SmartDukaan

Rev

Rev 32312 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.inventory.controllers;

import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.thrift.clients.WarehouseClient;
import in.shop2020.utils.HelperService;
import in.shop2020.warehouse.WarehouseService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
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 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);
    
    //TODO get it from db
    public static final List<Long>PHYSICAL_WAREHOUSES = new ArrayList<Long>(Arrays.asList(7573L,7678L, 7681L, 8468L, 7720L, 8889L, 8947L, 9203L, 10010L, 9213L, 9349L, 9470L, 9513L,9514L));
    
    private String output;
    
    
    
    public String getOutput() {
                return output;
        }

        public void setOutput(String output) {
                this.output = output;
        }

        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 static final String OUTPUT = "output";
    public static final String SESSION_WAREHOUSE_IDS = "allowedWarehouseIds";
    public static final String UNAUTHORIZED_ACCESS_ERROR = "Unauthorized Access ";
    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;

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

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    
    @SuppressWarnings("unchecked")
        public void setSession(Map<String, Object> sessionMap) {
        this.session = request.getSession();
        List<Long> allowedWarehouseIds = null;
        if(this.session==null || this.session.getAttribute(SESSION_WAREHOUSE_IDS)==null) {
                try {
                HelperService.Client helperClient = new HelperClient().getClient();
                log.info("SecurityUtils.getSubject().getPrincipal().toString()  " + SecurityUtils.getSubject().getPrincipal().toString() );
                allowedWarehouseIds = helperClient.getWarehouseIdsForAgent(SecurityUtils.getSubject().getPrincipal().toString());
                if(allowedWarehouseIds.contains(0L)) {
                        allowedWarehouseIds = PHYSICAL_WAREHOUSES;
                }
                this.session.setAttribute(SESSION_WAREHOUSE_IDS, allowedWarehouseIds);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        } else {
                allowedWarehouseIds = (List<Long>) this.session.getAttribute(SESSION_WAREHOUSE_IDS);
        }
        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 index() {
        return INDEX;
    }

    public String editNew() {
        return EDIT_NEW;
    }

    public String edit() {
        return EDIT;
    }

    public boolean isPermitted(String permission) {
        log.info("Permission === " + permission + ", " +SecurityUtils.getSubject().isPermitted(permission));
        return SecurityUtils.getSubject().isPermitted(permission);
    }
    
    @SuppressWarnings("unchecked")
        public boolean isAutorizedToAccessWarehouse(Long warehouseId) {
        List<Long> warehouseIds;
                warehouseIds = (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
                
                if(warehouseIds == null || warehouseIds.size() == 0){
                        return false;
                } else if(warehouseIds.contains(0l)){
                        return true; 
                } else {
                        if(warehouseId == null) {
                                return false;
                        } else {
                                if(warehouseIds.contains(warehouseId)){
                                        return true;
                                } else {
                                        return false;
                                }
                        }
                }
    }
    
    @SuppressWarnings("unchecked")
        public List<Long> getAuthorizedWarehousesForCurrentUser(){
        return (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
    }
}