| 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
|
| 33390 |
amit.gupta |
39 |
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));
|
| 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();
|
| 30511 |
amit.gupta |
84 |
log.info("SecurityUtils.getSubject().getPrincipal().toString() " + SecurityUtils.getSubject().getPrincipal().toString() );
|
| 7410 |
amar.kumar |
85 |
allowedWarehouseIds = helperClient.getWarehouseIdsForAgent(SecurityUtils.getSubject().getPrincipal().toString());
|
|
|
86 |
if(allowedWarehouseIds.contains(0L)) {
|
|
|
87 |
allowedWarehouseIds = PHYSICAL_WAREHOUSES;
|
|
|
88 |
}
|
| 22864 |
amit.gupta |
89 |
this.session.setAttribute(SESSION_WAREHOUSE_IDS, allowedWarehouseIds);
|
| 7410 |
amar.kumar |
90 |
} catch (Exception e) {
|
|
|
91 |
e.printStackTrace();
|
|
|
92 |
}
|
| 7820 |
amar.kumar |
93 |
} else {
|
| 7410 |
amar.kumar |
94 |
allowedWarehouseIds = (List<Long>) this.session.getAttribute(SESSION_WAREHOUSE_IDS);
|
| 7820 |
amar.kumar |
95 |
}
|
| 7410 |
amar.kumar |
96 |
this.sessionMap = sessionMap;
|
| 4687 |
mandeep.dh |
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Utility method to convert a date to a readable format
|
|
|
101 |
*/
|
|
|
102 |
public String convertDate(Long date) {
|
|
|
103 |
if (date == null || date == 0) {
|
|
|
104 |
return "N/A";
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return SDF.format(new Date(date));
|
|
|
108 |
}
|
|
|
109 |
|
| 5368 |
mandeep.dh |
110 |
public String index() {
|
|
|
111 |
return INDEX;
|
|
|
112 |
}
|
|
|
113 |
|
| 4687 |
mandeep.dh |
114 |
public String editNew() {
|
|
|
115 |
return EDIT_NEW;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public String edit() {
|
|
|
119 |
return EDIT;
|
|
|
120 |
}
|
| 4754 |
mandeep.dh |
121 |
|
|
|
122 |
public boolean isPermitted(String permission) {
|
| 30512 |
amit.gupta |
123 |
log.info("Permission === " + permission + ", " +SecurityUtils.getSubject().isPermitted(permission));
|
| 4754 |
mandeep.dh |
124 |
return SecurityUtils.getSubject().isPermitted(permission);
|
|
|
125 |
}
|
| 7410 |
amar.kumar |
126 |
|
|
|
127 |
@SuppressWarnings("unchecked")
|
|
|
128 |
public boolean isAutorizedToAccessWarehouse(Long warehouseId) {
|
|
|
129 |
List<Long> warehouseIds;
|
|
|
130 |
warehouseIds = (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
|
|
|
131 |
|
|
|
132 |
if(warehouseIds == null || warehouseIds.size() == 0){
|
|
|
133 |
return false;
|
|
|
134 |
} else if(warehouseIds.contains(0l)){
|
|
|
135 |
return true;
|
|
|
136 |
} else {
|
|
|
137 |
if(warehouseId == null) {
|
|
|
138 |
return false;
|
|
|
139 |
} else {
|
|
|
140 |
if(warehouseIds.contains(warehouseId)){
|
|
|
141 |
return true;
|
|
|
142 |
} else {
|
|
|
143 |
return false;
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
@SuppressWarnings("unchecked")
|
|
|
150 |
public List<Long> getAuthorizedWarehousesForCurrentUser(){
|
|
|
151 |
return (List<Long>)this.session.getAttribute(SESSION_WAREHOUSE_IDS);
|
|
|
152 |
}
|
| 4687 |
mandeep.dh |
153 |
}
|