Subversion Repositories SmartDukaan

Rev

Rev 5368 | Rev 7799 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5368 Rev 7410
Line 1... Line 1...
1
package in.shop2020.inventory.controllers;
1
package in.shop2020.inventory.controllers;
2
 
2
 
-
 
3
import in.shop2020.thrift.clients.HelperClient;
-
 
4
import in.shop2020.thrift.clients.WarehouseClient;
-
 
5
import in.shop2020.utils.HelperService;
-
 
6
import in.shop2020.warehouse.WarehouseService;
-
 
7
 
3
import java.text.SimpleDateFormat;
8
import java.text.SimpleDateFormat;
-
 
9
import java.util.ArrayList;
-
 
10
import java.util.Arrays;
4
import java.util.Date;
11
import java.util.Date;
-
 
12
import java.util.List;
5
import java.util.Map;
13
import java.util.Map;
6
 
14
 
7
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
16
import javax.servlet.http.HttpServletResponse;
9
import javax.servlet.http.HttpSession;
17
import javax.servlet.http.HttpSession;
Line 24... Line 32...
24
public abstract class BaseController extends ValidationAwareSupport implements
32
public abstract class BaseController extends ValidationAwareSupport implements
25
        ServletResponseAware, ServletRequestAware, SessionAware
33
        ServletResponseAware, ServletRequestAware, SessionAware
26
{
34
{
27
    private static final long serialVersionUID = 3339523094497219816L;
35
    private static final long serialVersionUID = 3339523094497219816L;
28
    protected static Logger log = Logger.getLogger(BaseController.class);
36
    protected static Logger log = Logger.getLogger(BaseController.class);
-
 
37
    
-
 
38
    //TODO get it from db
-
 
39
    public static final List<Long>PHYSICAL_WAREHOUSES = new ArrayList<Long>(Arrays.asList(7L,12L,13L));
29
 
40
    
30
    protected static final String INPUT = "input";
41
    protected static final String INPUT = "input";
31
    protected static final String INDEX = "index";
42
    protected static final String INDEX = "index";
32
    protected static final String EDIT_NEW = "editNew";
43
    protected static final String EDIT_NEW = "editNew";
33
    protected static final String EDIT = "edit";
44
    protected static final String EDIT = "edit";
34
    protected static final String SHOW = "show";
45
    protected static final String SHOW = "show";
35
    protected static final String EXCEPTION = "exception";
46
    protected static final String EXCEPTION = "exception";
36
 
-
 
-
 
47
    protected static final String OUTPUT = "output";
-
 
48
    public static final String SESSION_WAREHOUSE_IDS = "allowedWarehouseIds";
-
 
49
    public static final String UNAUTHORIZED_ACCESS_ERROR = "Unauthorized Access ";
37
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
50
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
38
 
51
 
39
    protected HttpServletResponse response;
52
    protected HttpServletResponse response;
40
    protected HttpServletRequest request;
53
    protected HttpServletRequest request;
41
    protected HttpSession session;
54
    protected HttpSession session;
Line 47... Line 60...
47
 
60
 
48
    public void setServletRequest(HttpServletRequest request) {
61
    public void setServletRequest(HttpServletRequest request) {
49
        this.request = request;
62
        this.request = request;
50
    }
63
    }
51
    
64
    
-
 
65
    @SuppressWarnings("unchecked")
52
    public void setSession(Map<String, Object> sessionMap) {
66
	public void setSession(Map<String, Object> sessionMap) {
-
 
67
    	List<Long> allowedWarehouseIds = null;
-
 
68
    	if(this.session==null || this.session.getAttribute(SESSION_WAREHOUSE_IDS)==null) {
-
 
69
        	try {
-
 
70
    	    	HelperService.Client helperClient = new HelperClient().getClient();
-
 
71
    	    	allowedWarehouseIds = helperClient.getWarehouseIdsForAgent(SecurityUtils.getSubject().getPrincipal().toString());
-
 
72
    	    	if(allowedWarehouseIds.contains(0L)) {
-
 
73
    	    		allowedWarehouseIds = PHYSICAL_WAREHOUSES;
-
 
74
    	    	}
-
 
75
        	} catch (Exception e) {
-
 
76
        		e.printStackTrace();
-
 
77
        	}
-
 
78
    	} else {
-
 
79
    		allowedWarehouseIds = (List<Long>) this.session.getAttribute(SESSION_WAREHOUSE_IDS);
-
 
80
    	}
53
        this.session = request.getSession();
81
    	this.session = request.getSession();
-
 
82
    	this.session.setAttribute(SESSION_WAREHOUSE_IDS, allowedWarehouseIds);
54
        this.sessionMap = sessionMap;
83
    	this.sessionMap = sessionMap;
55
    }
84
    }
56
    
85
    
57
    /**
86
    /**
58
     * Utility method to convert a date to a readable format 
87
     * Utility method to convert a date to a readable format 
59
     */
88
     */
Line 78... Line 107...
78
    }
107
    }
79
 
108
 
80
    public boolean isPermitted(String permission) {
109
    public boolean isPermitted(String permission) {
81
        return SecurityUtils.getSubject().isPermitted(permission);
110
        return SecurityUtils.getSubject().isPermitted(permission);
82
    }
111
    }
-
 
112
    
-
 
113
    @SuppressWarnings("unchecked")
-
 
114
	public boolean isAutorizedToAccessWarehouse(Long warehouseId) {
-
 
115
    	List<Long> warehouseIds;
-
 
116
		warehouseIds = (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
-
 
117
		
-
 
118
		if(warehouseIds == null || warehouseIds.size() == 0){
-
 
119
			return false;
-
 
120
		} else if(warehouseIds.contains(0l)){
-
 
121
			return true; 
-
 
122
		} else {
-
 
123
			if(warehouseId == null) {
-
 
124
				return false;
-
 
125
			} else {
-
 
126
				if(warehouseIds.contains(warehouseId)){
-
 
127
					return true;
-
 
128
				} else {
-
 
129
					return false;
-
 
130
				}
-
 
131
			}
-
 
132
		}
-
 
133
    }
-
 
134
    
-
 
135
    @SuppressWarnings("unchecked")
-
 
136
	public List<Long> getAuthorizedWarehousesForCurrentUser(){
-
 
137
    	return (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
-
 
138
    }
83
}
139
}