Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
7068 anupam.sin 1
package in.shop2020.recharge.controllers;
2
 
7207 anupam.sin 3
import in.shop2020.model.v1.order.HotspotStore;
7996 anupam.sin 4
import in.shop2020.model.v1.order.RechargePlan;
5
import in.shop2020.model.v1.order.RechargeType;
6
import in.shop2020.thrift.clients.TransactionClient;
7207 anupam.sin 7
 
7068 anupam.sin 8
import java.text.SimpleDateFormat;
9
import java.util.Date;
7207 anupam.sin 10
import java.util.HashMap;
7996 anupam.sin 11
import java.util.List;
7068 anupam.sin 12
import java.util.Map;
7996 anupam.sin 13
import java.util.Map.Entry;
7068 anupam.sin 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;
20
import org.apache.shiro.SecurityUtils;
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);
37
 
38
    protected static final String INPUT = "input";
39
    protected static final String INDEX = "index";
40
    protected static final String EDIT_NEW = "editNew";
41
    protected static final String EDIT = "edit";
42
    protected static final String SHOW = "show";
43
    protected static final String EXCEPTION = "exception";
7207 anupam.sin 44
 
45
    public static Map<Long, HotspotStore> hotspotStores = new HashMap<Long, HotspotStore>();
7068 anupam.sin 46
 
47
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
48
 
49
    protected HttpServletResponse response;
50
    protected HttpServletRequest request;
51
    protected HttpSession session;
52
    protected Map<String, Object> sessionMap;
7207 anupam.sin 53
 
54
    protected Long storeId;
7996 anupam.sin 55
    public static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR";
56
    public static Map<Long, String> mobileProvidersMap;
57
    public static Map<Long, String> dthProvidersMap;
58
    public static Map<Long, List<RechargePlan>> operatorPlanMap;
59
    public static Map<String, Long> operatorNametoValueMap = new HashMap<String, Long>();
60
 
61
    static {
62
        TransactionClient tcl;
63
        try {
64
            operatorPlanMap = new HashMap<Long, List<RechargePlan>>();
65
            tcl = new TransactionClient();
66
            mobileProvidersMap = tcl.getClient().getServiceProviders(RechargeType.MOBILE, true);
67
            dthProvidersMap = tcl.getClient().getServiceProviders(RechargeType.DTH, true);
68
            for (Long operatorId : mobileProvidersMap.keySet()) {
69
                List<RechargePlan> plans = tcl.getClient().getPlansForOperator(operatorId);
70
                if (!plans.isEmpty()) {
71
                    operatorPlanMap.put(operatorId, plans);
72
                }
73
            }
74
            for (Entry<Long, String> entry : mobileProvidersMap.entrySet()) {
75
                operatorNametoValueMap.put(entry.getValue(), entry.getKey());
76
            }
77
        } catch(Exception e) {
78
            log.error("Could not get all plans", e);
79
        }
80
    }
81
 
7068 anupam.sin 82
    public void setServletResponse(HttpServletResponse response) {
83
        this.response = response;
84
    }
85
 
86
    public void setServletRequest(HttpServletRequest request) {
87
        this.request = request;
7207 anupam.sin 88
        this.session = request.getSession();
7068 anupam.sin 89
    }
90
 
91
    public void setSession(Map<String, Object> sessionMap) {
92
        this.sessionMap = sessionMap;
93
    }
94
 
95
    /**
96
     * Utility method to convert a date to a readable format 
97
     */
98
    public String convertDate(Long date) {
99
        if (date == null || date == 0) {
100
            return "N/A";
101
        }
102
 
103
        return SDF.format(new Date(date));
104
    }
7996 anupam.sin 105
 
106
    public String remoteAddr(HttpServletRequest request) {
107
        String remoteAddr = "";
108
        String x;
109
        x = request.getHeader(HEADER_X_FORWARDED_FOR);
110
        if (x != null && !x.isEmpty()) {
111
            remoteAddr = x;
112
            int idx = remoteAddr.lastIndexOf(',');
113
            if (idx > -1) {
114
                remoteAddr = remoteAddr.substring(idx + 1).trim();
115
            }
116
        } else {
117
            remoteAddr = request.getRemoteAddr();
118
        }
119
        return remoteAddr;
120
    }
7068 anupam.sin 121
 
122
    public String index() {
123
        return INDEX;
124
    }
125
 
126
    public String editNew() {
127
        return EDIT_NEW;
128
    }
129
 
130
    public String edit() {
131
        return EDIT;
132
    }
133
 
134
    public boolean isPermitted(String permission) {
135
        return SecurityUtils.getSubject().isPermitted(permission);
136
    }
7207 anupam.sin 137
 
138
    public String getStoreCode(){
139
        return hotspotStores.get(storeId).getHotspotId();   
140
    }
141
 
142
    public String getCollectedAmount(){
143
        return hotspotStores.get(storeId).getCollectedAmount()+"";
144
    }
145
 
146
    public Long getStoreId() {
147
        return storeId;
148
    }
149
 
150
    public void setStoreId(Long storeId) {
151
        this.storeId = storeId;
152
    }
7996 anupam.sin 153
 
154
    public static void setDthProvidersMap(Map<Long, String> dthProvidersMap) {
155
        BaseController.dthProvidersMap = dthProvidersMap;
156
    }
157
 
158
    public static Map<Long, String> getDthProvidersMap() {
159
        return dthProvidersMap;
160
    }
161
 
162
    public static void setMobileProvidersMap(Map<Long, String> mobileProvidersMap) {
163
        BaseController.mobileProvidersMap = mobileProvidersMap;
164
    }
165
 
166
    public static Map<Long, String> getMobileProvidersMap() {
167
        return mobileProvidersMap;
168
    }
169
 
7068 anupam.sin 170
}