Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2674 vikas 1
package in.shop2020.serving.controllers;
2
 
3090 mandeep.dh 3
import in.shop2020.thrift.clients.CRMServiceClient;
4
import in.shop2020.thrift.clients.TransactionServiceClient;
5
import in.shop2020.thrift.clients.UserContextServiceClient;
6
 
7
import java.text.SimpleDateFormat;
8
import java.util.Date;
2674 vikas 9
import java.util.Map;
10
 
11
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletResponse;
13
import javax.servlet.http.HttpSession;
14
 
15
import org.apache.log4j.Logger;
3090 mandeep.dh 16
import org.apache.shiro.SecurityUtils;
2674 vikas 17
import org.apache.struts2.interceptor.ServletRequestAware;
18
import org.apache.struts2.interceptor.ServletResponseAware;
19
import org.apache.struts2.interceptor.SessionAware;
3090 mandeep.dh 20
import org.apache.thrift.TException;
2674 vikas 21
 
22
import com.opensymphony.xwork2.ValidationAwareSupport;
23
 
24
/**
25
 * Base class for all user action handlers i.e. controllers
26
 * 
27
 * @author Vikas
28
 */
29
public abstract class BaseController extends ValidationAwareSupport implements
30
        ServletResponseAware, ServletRequestAware, SessionAware
31
{
32
    private static final long serialVersionUID = 3339523094497219816L;
3090 mandeep.dh 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
 
41
    protected final SimpleDateFormat SDF = new SimpleDateFormat("dd MMM, yyyy hh:mm a");
42
 
2674 vikas 43
    protected HttpServletResponse response;
44
    protected HttpServletRequest request;
45
    protected HttpSession session;
46
    protected Map<String, Object> sessionMap;
47
 
3090 mandeep.dh 48
    // Clients used at many places
49
    protected in.shop2020.model.v1.user.UserContextService.Client userContextServiceClient;
50
    protected in.shop2020.model.v1.order.TransactionService.Client transactionServiceClient;
51
    protected in.shop2020.crm.CRMService.Client crmServiceClient;
52
 
53
    protected String currentAgentEmailId = (String) SecurityUtils.getSubject().getPrincipal();
54
 
55
    protected void createServiceClients()
56
    {
57
        try {
58
            userContextServiceClient = new UserContextServiceClient().getClient();
59
            transactionServiceClient = new TransactionServiceClient().getClient();
60
            crmServiceClient         = new CRMServiceClient().getClient();
61
        }
62
        catch (Exception e) {
63
            log.error("Could not get client!", e);
64
        }
65
    }
66
 
2674 vikas 67
    public void setServletResponse(HttpServletResponse response) {
68
        this.response = response;
69
    }
70
 
71
    public void setServletRequest(HttpServletRequest request) {
72
        this.request = request;
73
    }
74
 
75
    public void setSession(Map<String, Object> sessionMap) {
76
        this.session = request.getSession();
77
        this.sessionMap = sessionMap;
78
    }
3090 mandeep.dh 79
 
80
    /**
81
     * Utility method to convert a date to a readable format 
82
     */
83
    public String convertDate(long date) {
84
        if (date == 0) {
85
            return "N/A";
86
        }
87
 
88
        return SDF.format(new Date(date));
89
    }
90
 
91
    public String getAgentName() throws TException
92
    {
93
        createServiceClients();
94
        return crmServiceClient.getAgentByEmailId(currentAgentEmailId).getName();
95
    }
96
 
97
    public String getCurrentAgentEmailId() {
98
        return currentAgentEmailId;
99
    }
100
 
101
    public String getLoggerRole() throws TException {
102
        createServiceClients();
103
        return crmServiceClient.getRoleNamesForAgent(currentAgentEmailId).get(0);
104
    }
105
}