Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
4793 amar.kumar 1
package in.shop2020.serving.controllers;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import in.shop2020.crm.Agent;
7
import in.shop2020.crm.SearchFilter;
8
import in.shop2020.serving.auth.CRMAuthorizingRealm;
9
import in.shop2020.thrift.clients.CRMClient;
10
 
11
import org.apache.shiro.crypto.hash.Sha256Hash;
12
import org.apache.thrift.TException;
5168 amar.kumar 13
import org.apache.thrift.transport.TTransportException;
4793 amar.kumar 14
 
15
public class AgentController extends BaseController {
16
 
5937 amar.kumar 17
	private static final String adminEmailId = "admin@shop2020.in";
18
 
4793 amar.kumar 19
	private List<Agent>  agents = new ArrayList<Agent>();
20
	private int id;
21
	private String name;
22
	private String emailId;
23
	private String password;
24
	private List<String> role;
25
	private String managerEmailId;
26
	private String newPassword;
27
	private int managerId;
28
 
29
	private static final long serialVersionUID = 7884813082235471597L;
30
 
31
	public AgentController()	{
32
		super();
33
	}
34
 
35
	public String index() {
36
		try {
37
        	SearchFilter searchFilter = new SearchFilter();
38
        	crmServiceClient = new CRMClient().getClient();
39
        	agents = crmServiceClient.getAgents(searchFilter);
40
        } catch (TException e) {
41
            log.error("Error while getting agents", e);
42
        }
43
        return INDEX;
44
    }
45
 
46
	public String createAgent(){
47
		try{
48
			Agent agent = new Agent();
49
			agent.setName(name);
50
			agent.setEmailId(emailId);
51
			agent.setManagerId(managerId);  
52
			agent.setPassword(new Sha256Hash(password).toHex());
53
			agent.setIs_active(true);
54
			crmServiceClient = new CRMClient().getClient();
55
			crmServiceClient.insertAgent(agent,role);
56
			SearchFilter searchfilter = new SearchFilter();
57
			searchfilter.setAgentEmailId(emailId);
58
			List<Agent> createdAgent = crmServiceClient.getAgents(searchfilter);
59
			CRMAuthorizingRealm.addAgent(createdAgent.get(0));
60
			log.info("Agent Creation complete");
61
		} catch (TException e) {
62
			log.error("Error while creating agent", e);
63
			return EXCEPTION;
64
		}
65
		return index();
66
	}
67
 
68
 
69
	public String changePassword(){
70
		try {
71
			crmServiceClient = new CRMClient().getClient();
72
			crmServiceClient.updatePasswordForAgent(emailId, new Sha256Hash(newPassword).toHex());
5286 amar.kumar 73
 
74
			// Dummy call to reload agents
75
	        CRMAuthorizingRealm.getAgent(-1);
4793 amar.kumar 76
		} catch (TException e) {
77
			log.error("Error while changing password of agent", e);
78
			e.printStackTrace();
79
			return EXCEPTION;
80
		}
81
		return index();
82
	}
83
 
84
	public String deActivateAgent(){
85
		try {
86
			crmServiceClient = new CRMClient().getClient();
87
			crmServiceClient.changeAgentStatus(false, emailId);
88
			crmServiceClient.unassignAgentTickets(id);
89
			CRMAuthorizingRealm.removeAgent(new Long((long)id), emailId);
90
		} catch (TException e) {
91
			log.error("Error while deactivating agent", e);
92
			e.printStackTrace();
93
			return EXCEPTION; 
94
		}
95
		return index();
96
	}
97
 
5168 amar.kumar 98
	public String changeAgentRole(){
99
		try{
100
			log.info("Agent id : "+id +" role : " + role);
101
			crmServiceClient = new CRMClient().getClient();
102
			crmServiceClient.changeAgentRole(new Long((long)id),role);
103
		} catch (Exception e) {
104
 
105
		}
106
		return index();
107
	}
108
 
109
	public String getOpenTicketCount(long id) {
110
		try {
111
			crmServiceClient = new CRMClient().getClient();
112
			return (new Integer(crmServiceClient.getOpenTicketCountForAgent(id))).toString();
113
		} catch (TTransportException e) {
114
			e.printStackTrace();
115
		} catch (TException e) {
116
			e.printStackTrace();
117
		}
118
		return "-1";
119
	}
120
 
4793 amar.kumar 121
	public String getName() {
122
		return name;
123
	}
124
 
125
	public void setName(String name) {
126
		this.name = name;
127
	}
128
 
129
	public String getPassword() {
130
		return password;
131
	}
132
 
133
	public void setPassword(String password) {
134
		this.password = password;
135
	}
136
 
137
	public List<String> getRole() {
138
		return role;
139
	}
140
 
141
	public void setRole(List<String> role) {
142
		this.role = role;
143
	}
144
 
145
	public String getEmailId() {
146
		return emailId;
147
	}
148
 
149
	public void setEmailId(String emailId) {
150
		this.emailId = emailId;
151
	}
152
 
153
	public String getNewPassword() {
154
		return newPassword;
155
	}
156
 
157
	public void setNewPassword(String newPassword) {
158
		this.newPassword = newPassword;
159
	}
160
 
161
	public List<Agent> getAgents() {
5937 amar.kumar 162
		String currentEmailId = getCurrentAgentEmailId();
163
		long managerId = 0;
164
		List<Agent> managedAgents = new ArrayList<Agent>();
165
		for(Agent tempAgent : agents) {
166
			if(tempAgent.getEmailId().equals(currentEmailId)) {
167
				managerId = tempAgent.getId();
168
				break;
169
			}
170
		}
171
 
172
		if(!currentEmailId.equals(adminEmailId)) {
173
			return agents;
174
		} else {
175
			if(managerId!=0) {
176
				for(Agent tempAgent : agents) {
177
					if(tempAgent.getManagerId() == managerId){
178
						managedAgents.add(tempAgent);
179
					}
180
				}
181
				return managedAgents;
182
			}
183
			else {
184
				return agents;
185
			}
186
 
187
		}
188
 
4793 amar.kumar 189
	}
190
 
191
	public void setAgents(List<Agent> agents) {
192
		this.agents = agents;
193
	}
194
 
195
	public String getManagerEmailId() {
196
		return managerEmailId;
197
	}
198
 
199
	public void setManagerEmailId(String managerEmailId) {
200
		this.managerEmailId = managerEmailId;
201
	}
202
 
203
	public int getManagerId() {
204
		return managerId;
205
	}
206
 
207
 
208
	public void setManagerId(int managerId) {
209
		this.managerId = managerId;
210
	}
211
 
212
	public int getId() {
213
		return id;
214
	}
215
 
216
	public void setId(int id) {
217
		this.id = id;
218
	}
5286 amar.kumar 219
 
220
	public static void main(String args[]) throws TException {
221
		SearchFilter searchFilter = new SearchFilter();
222
		searchFilter.setAgentId(10);
223
		in.shop2020.crm.CRMService.Client crmServiceClient = new CRMClient().getClient();
224
    	List<Agent> agents = crmServiceClient.getAgents(searchFilter);
225
    	System.out.println(agents);
226
	}
4793 amar.kumar 227
}