Subversion Repositories SmartDukaan

Rev

Rev 19707 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7226 anupam.sin 1
package in.shop2020.serving.controllers;
2
 
7272 amit.gupta 3
 
7343 anupam.sin 4
import in.shop2020.model.v1.order.HotspotStore;
7747 anupam.sin 5
import in.shop2020.thrift.clients.config.ConfigClient;
7343 anupam.sin 6
 
7226 anupam.sin 7
import java.text.SimpleDateFormat;
8
import java.util.Date;
7272 amit.gupta 9
import java.util.HashMap;
7226 anupam.sin 10
import java.util.Map;
11
 
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletResponse;
14
import javax.servlet.http.HttpSession;
15
 
19706 amit.gupta 16
import org.apache.commons.lang.StringUtils;
7226 anupam.sin 17
import org.apache.log4j.Logger;
18
import org.apache.shiro.SecurityUtils;
19
import org.apache.struts2.interceptor.ServletRequestAware;
20
import org.apache.struts2.interceptor.ServletResponseAware;
21
import org.apache.struts2.interceptor.SessionAware;
22
 
23
import com.opensymphony.xwork2.ValidationAwareSupport;
24
 
25
/**
26
 * Base class for all user action handlers i.e. controllers
27
 * 
28
 */
29
public abstract class BaseController extends ValidationAwareSupport implements
30
        ServletResponseAware, ServletRequestAware, SessionAware
31
{
32
    private static final long serialVersionUID = 3339523094497219816L;
33
    protected static Logger log = Logger.getLogger(BaseController.class);
34
 
35
    protected static final String INPUT = "input";
36
    protected static final String INDEX = "index";
37
    protected static final String EDIT_NEW = "editNew";
38
    protected static final String EDIT = "edit";
39
    protected static final String SHOW = "show";
40
    protected static final String EXCEPTION = "exception";
41
 
42
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
7343 anupam.sin 43
 
44
    public static Map<Long, HotspotStore> hotspotStores = new HashMap<Long, HotspotStore>();
7226 anupam.sin 45
 
46
    protected HttpServletResponse response;
47
    protected HttpServletRequest request;
48
    protected HttpSession session;
49
    protected Map<String, Object> sessionMap;
50
    protected long sourceId = -1;
7272 amit.gupta 51
    protected Map<String, String> htmlSnippets;
19706 amit.gupta 52
    private boolean OCRSession = false;
7343 anupam.sin 53
 
54
    protected Long storeId;
7747 anupam.sin 55
    protected static boolean showTestMessage = false;
56
 
7226 anupam.sin 57
 
7747 anupam.sin 58
    static {
59
        try{
60
            setShowTestMessageField(Boolean.parseBoolean(ConfigClient.getClient().get("showTestMessageOnStoreWebsite")));
61
        } catch(Exception e){
62
            log.error("Unable to get value from config service", e);
63
        }
64
    }
65
 
7272 amit.gupta 66
    public BaseController() {
67
		htmlSnippets = new HashMap<String, String>();
68
	}
69
 
7226 anupam.sin 70
    public void setServletResponse(HttpServletResponse response) {
71
        this.response = response;
72
    }
73
 
74
    public void setServletRequest(HttpServletRequest request) {
75
        this.request = request;
7343 anupam.sin 76
        this.session = request.getSession();
19708 amit.gupta 77
        if(request.getSession() != null) {
78
			OCRSession = StringUtils.isNotEmpty((String)request.getSession().getAttribute("OCR_SESSION"));
79
		} else {
80
			OCRSession = false;
81
		}
7226 anupam.sin 82
    }
83
 
84
    public void setSession(Map<String, Object> sessionMap) {
85
        this.sessionMap = sessionMap;
86
    }
87
 
88
    /**
89
     * Utility method to convert a date to a readable format 
90
     */
91
    public String convertDate(Long date) {
92
        if (date == null || date == 0) {
93
            return "N/A";
94
        }
95
 
96
        return SDF.format(new Date(date));
97
    }
98
 
7386 anupam.sin 99
    public double getDifference(double num1, double num2) {
100
        return num1-num2;
101
    }
102
 
7226 anupam.sin 103
    public void setSourceId(long sourceId){
104
        this.sourceId = sourceId;
105
    }
106
 
107
    public String index() {
108
        return INDEX;
109
    }
110
 
111
    public String editNew() {
112
        return EDIT_NEW;
113
    }
114
 
115
    public String edit() {
116
        return EDIT;
117
    }
118
 
119
    public boolean isPermitted(String permission) {
120
        return SecurityUtils.getSubject().isPermitted(permission);
121
    }
7343 anupam.sin 122
 
123
    public Long getStoreId() {
124
        return storeId;
125
    }
126
 
127
    public void setStoreId(Long storeId) {
128
        this.storeId = storeId;
129
    }
7386 anupam.sin 130
 
131
    public String getStoreCode(){
132
        return hotspotStores.get(storeId).getHotspotId();   
133
    }
7747 anupam.sin 134
 
135
    public static void setShowTestMessageField(boolean showTestMessage) {
136
        BaseController.showTestMessage = showTestMessage;
137
    }
138
 
139
    public static boolean shouldShowTestMessage() {
140
        return showTestMessage;
141
    }
19706 amit.gupta 142
 
143
	public void setOCRSession(boolean oCRSession) {
144
		OCRSession = oCRSession;
145
	}
146
 
147
	public boolean isOCRSession() {
148
		return OCRSession;
149
	}
7226 anupam.sin 150
}