Subversion Repositories SmartDukaan

Rev

Rev 25112 | Rev 27376 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4687 mandeep.dh 1
package in.shop2020.inventory.controllers;
2
 
7410 amar.kumar 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
 
4687 mandeep.dh 8
import java.text.SimpleDateFormat;
7410 amar.kumar 9
import java.util.ArrayList;
10
import java.util.Arrays;
4687 mandeep.dh 11
import java.util.Date;
7410 amar.kumar 12
import java.util.List;
4687 mandeep.dh 13
import java.util.Map;
14
 
15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
17
import javax.servlet.http.HttpSession;
18
 
19
import org.apache.log4j.Logger;
4754 mandeep.dh 20
import org.apache.shiro.SecurityUtils;
4687 mandeep.dh 21
import org.apache.struts2.interceptor.ServletRequestAware;
22
import org.apache.struts2.interceptor.ServletResponseAware;
23
import org.apache.struts2.interceptor.SessionAware;
24
 
25
import com.opensymphony.xwork2.ValidationAwareSupport;
26
 
27
/**
28
 * Base class for all user action handlers i.e. controllers
29
 * 
30
 * @author Vikas
31
 */
32
public abstract class BaseController extends ValidationAwareSupport implements
33
        ServletResponseAware, ServletRequestAware, SessionAware
34
{
35
    private static final long serialVersionUID = 3339523094497219816L;
36
    protected static Logger log = Logger.getLogger(BaseController.class);
7410 amar.kumar 37
 
38
    //TODO get it from db
25482 amit.gupta 39
    public static final List<Long>PHYSICAL_WAREHOUSES = new ArrayList<Long>(Arrays.asList(7573L,7678L, 7681L, 8468L, 7720L));
7410 amar.kumar 40
 
25112 amit.gupta 41
    private String output;
42
 
43
 
44
 
45
    public String getOutput() {
46
		return output;
47
	}
48
 
49
	public void setOutput(String output) {
50
		this.output = output;
51
	}
52
 
53
	protected static final String INPUT = "input";
4687 mandeep.dh 54
    protected static final String INDEX = "index";
55
    protected static final String EDIT_NEW = "editNew";
56
    protected static final String EDIT = "edit";
57
    protected static final String SHOW = "show";
58
    protected static final String EXCEPTION = "exception";
7410 amar.kumar 59
    protected static final String OUTPUT = "output";
60
    public static final String SESSION_WAREHOUSE_IDS = "allowedWarehouseIds";
61
    public static final String UNAUTHORIZED_ACCESS_ERROR = "Unauthorized Access ";
4687 mandeep.dh 62
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
63
 
64
    protected HttpServletResponse response;
65
    protected HttpServletRequest request;
66
    protected HttpSession session;
67
    protected Map<String, Object> sessionMap;
68
 
69
    public void setServletResponse(HttpServletResponse response) {
70
        this.response = response;
71
    }
72
 
73
    public void setServletRequest(HttpServletRequest request) {
74
        this.request = request;
75
    }
76
 
7410 amar.kumar 77
    @SuppressWarnings("unchecked")
78
	public void setSession(Map<String, Object> sessionMap) {
22864 amit.gupta 79
    	this.session = request.getSession();
7410 amar.kumar 80
    	List<Long> allowedWarehouseIds = null;
7820 amar.kumar 81
    	if(this.session==null || this.session.getAttribute(SESSION_WAREHOUSE_IDS)==null) {
7410 amar.kumar 82
        	try {
83
    	    	HelperService.Client helperClient = new HelperClient().getClient();
84
    	    	allowedWarehouseIds = helperClient.getWarehouseIdsForAgent(SecurityUtils.getSubject().getPrincipal().toString());
85
    	    	if(allowedWarehouseIds.contains(0L)) {
86
    	    		allowedWarehouseIds = PHYSICAL_WAREHOUSES;
87
    	    	}
22864 amit.gupta 88
    	    	this.session.setAttribute(SESSION_WAREHOUSE_IDS, allowedWarehouseIds);
7410 amar.kumar 89
        	} catch (Exception e) {
90
        		e.printStackTrace();
91
        	}
7820 amar.kumar 92
    	} else {
7410 amar.kumar 93
    		allowedWarehouseIds = (List<Long>) this.session.getAttribute(SESSION_WAREHOUSE_IDS);
7820 amar.kumar 94
    	}
7410 amar.kumar 95
    	this.sessionMap = sessionMap;
4687 mandeep.dh 96
    }
97
 
98
    /**
99
     * Utility method to convert a date to a readable format 
100
     */
101
    public String convertDate(Long date) {
102
        if (date == null || date == 0) {
103
            return "N/A";
104
        }
105
 
106
        return SDF.format(new Date(date));
107
    }
108
 
5368 mandeep.dh 109
    public String index() {
110
        return INDEX;
111
    }
112
 
4687 mandeep.dh 113
    public String editNew() {
114
        return EDIT_NEW;
115
    }
116
 
117
    public String edit() {
118
        return EDIT;
119
    }
4754 mandeep.dh 120
 
121
    public boolean isPermitted(String permission) {
122
        return SecurityUtils.getSubject().isPermitted(permission);
123
    }
7410 amar.kumar 124
 
125
    @SuppressWarnings("unchecked")
126
	public boolean isAutorizedToAccessWarehouse(Long warehouseId) {
127
    	List<Long> warehouseIds;
128
		warehouseIds = (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
129
 
130
		if(warehouseIds == null || warehouseIds.size() == 0){
131
			return false;
132
		} else if(warehouseIds.contains(0l)){
133
			return true; 
134
		} else {
135
			if(warehouseId == null) {
136
				return false;
137
			} else {
138
				if(warehouseIds.contains(warehouseId)){
139
					return true;
140
				} else {
141
					return false;
142
				}
143
			}
144
		}
145
    }
146
 
147
    @SuppressWarnings("unchecked")
148
	public List<Long> getAuthorizedWarehousesForCurrentUser(){
149
    	return (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
150
    }
4687 mandeep.dh 151
}