Subversion Repositories SmartDukaan

Rev

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