Subversion Repositories SmartDukaan

Rev

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