Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
3339 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.crm.Activity;
7
import in.shop2020.crm.ActivityType;
8
import in.shop2020.crm.Agent;
3390 mandeep.dh 9
import in.shop2020.crm.SearchFilter;
4142 mandeep.dh 10
import in.shop2020.crm.TicketCategory;
3405 mandeep.dh 11
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.UserContextException;
3390 mandeep.dh 13
import in.shop2020.serving.auth.CRMAuthorizingRealm;
14
import in.shop2020.thrift.clients.CRMClient;
3405 mandeep.dh 15
import in.shop2020.thrift.clients.UserClient;
3339 mandeep.dh 16
import in.shop2020.util.CRMConstants;
5203 amar.kumar 17
import in.shop2020.util.CRMConstants.ActivityCommonDescriptionMatrix;
3339 mandeep.dh 18
 
3397 mandeep.dh 19
import java.text.ParseException;
20
import java.util.ArrayList;
3390 mandeep.dh 21
import java.util.Collections;
3339 mandeep.dh 22
import java.util.List;
23
 
24
import org.apache.thrift.TException;
25
 
26
/**
27
 * @author mandeep
28
 * 
29
 */
30
public class ActivityController extends BaseController {
31
    /**
32
     * 
33
     */
34
    private static final long serialVersionUID = 1L;
35
    private List<Activity>    activities;
3390 mandeep.dh 36
    private String            activityId;
3339 mandeep.dh 37
    private String            description;
5203 amar.kumar 38
    private String			  commonDescription;
3339 mandeep.dh 39
    private String            type;
4142 mandeep.dh 40
    private String            category;
3339 mandeep.dh 41
    private String            customerMobileNumber;
42
    private String            creatorId;
3397 mandeep.dh 43
    private String[]          agentIds;
44
    private String            startTimestamp;
45
    private String            endTimestamp;
3405 mandeep.dh 46
    private String            customerEmailId;
47
    private String            customerName;
48
    private String            userId;
3339 mandeep.dh 49
 
50
    public String index() {
51
        try {
3390 mandeep.dh 52
            SearchFilter searchFilter = new SearchFilter();
53
 
3405 mandeep.dh 54
            if (activityId != null && !activityId.isEmpty()) {
55
                searchFilter.setActivityId(Long.parseLong(activityId));
56
            } else if (userId != null && !userId.isEmpty()) {
57
                searchFilter.setCustomerId(Long.parseLong(userId));
58
            } else if (creatorId != null && !creatorId.isEmpty()) {
59
                searchFilter.setActivityCreatorIds(Collections
60
                        .singletonList(Long.parseLong(creatorId)));
3390 mandeep.dh 61
                searchFilter.setIsActivityRead(false);
3405 mandeep.dh 62
            } else if (agentIds != null && agentIds.length != 0) {
3397 mandeep.dh 63
                searchFilter.setActivityCreatorIds(new ArrayList<Long>());
64
                for (String agentId : agentIds) {
3405 mandeep.dh 65
                    searchFilter.getActivityCreatorIds().add(
66
                            CRMAuthorizingRealm.getAgent(agentId).getId());
3397 mandeep.dh 67
                }
68
 
4142 mandeep.dh 69
                if (type != null && !type.isEmpty()) {
70
                    searchFilter.setActivityType(ActivityType.valueOf(type));
71
                }
72
 
73
                if (category != null && !category.isEmpty()) {
74
                    searchFilter.setTicketCategory(TicketCategory.valueOf(category));
75
                }
76
 
3397 mandeep.dh 77
                try {
78
                    if (startTimestamp != null && !startTimestamp.isEmpty()) {
3405 mandeep.dh 79
                        searchFilter.setStartTimestamp(SDF
80
                                .parse(startTimestamp).getTime());
3397 mandeep.dh 81
                    }
82
 
83
                    if (endTimestamp != null && !endTimestamp.isEmpty()) {
3405 mandeep.dh 84
                        searchFilter.setEndTimestamp(SDF.parse(endTimestamp)
85
                                .getTime());
3397 mandeep.dh 86
                    }
3405 mandeep.dh 87
                } catch (ParseException e) {
3397 mandeep.dh 88
                    log.error("Error parsing timestamps", e);
89
                }
3405 mandeep.dh 90
            } else {
91
                searchFilter.setActivityCreatorIds(Collections
92
                        .singletonList(CRMAuthorizingRealm.getAgent(
93
                                currentAgentEmailId).getId()));
3397 mandeep.dh 94
            }
95
 
3405 mandeep.dh 96
            crmServiceClient = new CRMClient().getClient();
3397 mandeep.dh 97
            activities = crmServiceClient.getActivities(searchFilter);
3339 mandeep.dh 98
        } catch (TException e) {
99
            log.error("Error while retrieving activities for "
100
                    + currentAgentEmailId, e);
3405 mandeep.dh 101
        } catch (NumberFormatException e) {
102
            log.error("Error parsing userId: " + userId, e);
3339 mandeep.dh 103
        }
104
 
105
        return INDEX;
106
    }
107
 
3405 mandeep.dh 108
    public String editNew() {
109
        return EDIT_NEW;
110
    }
111
 
3339 mandeep.dh 112
    public String create() throws Exception {
113
        Activity activity = new Activity();
5203 amar.kumar 114
        if(commonDescription!=null && !commonDescription.isEmpty()){
115
        	if(description!=null) {
116
        		description = commonDescription + "  " +description;
117
        	} else {
118
        		description = commonDescription;
119
        	}
120
        }
3339 mandeep.dh 121
        activity.setDescription(description);
122
        activity.setType(ActivityType.valueOf(type));
3405 mandeep.dh 123
        activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId)
124
                .getId());
3339 mandeep.dh 125
 
3405 mandeep.dh 126
        if (userId != null && !userId.isEmpty()) {
127
            activity.setCustomerId(Long.parseLong(userId));
5224 amar.kumar 128
            if(customerMobileNumber != null
129
                        && !customerMobileNumber.isEmpty()) {
130
            	activity.setCustomerMobileNumber(customerMobileNumber);
131
            }
3405 mandeep.dh 132
        } else {
133
            User user = null;
134
            userContextServiceClient = new UserClient().getClient();
135
 
136
            try {
137
                if (customerName != null && !customerName.isEmpty()) {
138
                    activity.setCustomerName(customerName);
139
                }
140
 
141
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
142
                    activity.setCustomerEmailId(customerEmailId);
143
                    user = userContextServiceClient
144
                            .getUserByEmail(customerEmailId);
145
                }
146
 
147
                if ((user == null || user.getUserId() == -1)
148
                        && customerMobileNumber != null
149
                        && !customerMobileNumber.isEmpty()) {
150
                    activity.setCustomerMobileNumber(customerMobileNumber);
151
                    user = userContextServiceClient.getUserByMobileNumber(Long
152
                            .parseLong(customerMobileNumber));
153
                }
154
            } catch (UserContextException e) {
155
                log.error("Could not fetch user for: " + customerEmailId + " "
156
                        + customerMobileNumber + " " + customerName, e);
157
            }
158
 
159
            if (user != null && user.getUserId() != -1) {
160
                activity.setCustomerId(user.getUserId());
161
            }
3339 mandeep.dh 162
        }
163
 
3405 mandeep.dh 164
        crmServiceClient = new CRMClient().getClient();
165
        activityId = String.valueOf(crmServiceClient.insertActivity(activity));
3339 mandeep.dh 166
 
167
        return index();
168
    }
169
 
3405 mandeep.dh 170
    public String markAsRead() {
3390 mandeep.dh 171
        try {
3405 mandeep.dh 172
            crmServiceClient = new CRMClient().getClient();
173
            crmServiceClient.markAsRead(Long.parseLong(activityId),
174
                    CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3390 mandeep.dh 175
        } catch (NumberFormatException e) {
176
            log.error("Could not parse activity id from" + activityId, e);
177
        } catch (TException e) {
3405 mandeep.dh 178
            log.error("Could not mark the activityId: " + activityId
179
                    + " as read", e);
3390 mandeep.dh 180
        }
181
 
3405 mandeep.dh 182
        return index();
3390 mandeep.dh 183
    }
184
 
3711 mandeep.dh 185
    public String getUnreadActivities() {
186
        try {
187
            SearchFilter searchFilter = new SearchFilter();
188
            searchFilter.setIsActivityRead(false);
189
            searchFilter.setActivityCreatorIds(Collections.singletonList(CRMConstants.ADMIN_AGENT_ID));
190
 
191
            searchFilter.setTicketAssigneeIds(Collections
192
                    .singletonList(CRMAuthorizingRealm.getAgent(
193
                            currentAgentEmailId).getId()));
194
 
195
            crmServiceClient = new CRMClient().getClient();
196
            activities = crmServiceClient.getActivities(searchFilter);
197
        } catch (TException e) {
198
            log.error("Could not get agentId for: " + currentAgentEmailId, e);
199
        }
200
 
201
        return INDEX;
202
    }
203
 
3339 mandeep.dh 204
    public ActivityType[] getActivityTypes() {
205
        return ActivityType.values();
206
    }
207
 
208
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 209
        return CRMAuthorizingRealm.getAgent(agentId);
3339 mandeep.dh 210
    }
211
 
5203 amar.kumar 212
    public ActivityCommonDescriptionMatrix[] getActivityCommonDescriptionMatrix () {
213
        return CRMConstants.ActivityCommonDescriptionMatrix.values();
214
    }
215
 
3339 mandeep.dh 216
    public String getDescription() {
217
        return description;
218
    }
219
 
220
    public void setDescription(String description) {
221
        this.description = description;
222
    }
223
 
5203 amar.kumar 224
    public String getCommonDescription() {
225
		return commonDescription;
226
	}
227
 
228
	public void setCommonDescription(String commonDescription) {
229
		this.commonDescription = commonDescription;
230
	}
231
 
232
	public String getType() {
3339 mandeep.dh 233
        return type;
234
    }
235
 
236
    public void setType(String type) {
237
        this.type = type;
238
    }
239
 
240
    public String getCustomerMobileNumber() {
241
        return customerMobileNumber;
242
    }
243
 
244
    public void setCustomerMobileNumber(String customerMobileNumber) {
245
        this.customerMobileNumber = customerMobileNumber;
246
    }
247
 
248
    public List<Activity> getActivities() {
249
        return activities;
250
    }
251
 
252
    public String getCreatorId() {
253
        return creatorId;
254
    }
255
 
256
    public void setCreatorId(String creatorId) {
257
        this.creatorId = creatorId;
258
    }
3390 mandeep.dh 259
 
260
    public String getActivityId() {
261
        return activityId;
262
    }
263
 
264
    public void setActivityId(String activityId) {
265
        this.activityId = activityId;
266
    }
3397 mandeep.dh 267
 
268
    public String[] getAgentIds() {
269
        return agentIds;
270
    }
271
 
272
    public void setAgentIds(String[] agentIds) {
273
        this.agentIds = agentIds;
274
    }
275
 
276
    public String getStartTimestamp() {
277
        return startTimestamp;
278
    }
279
 
280
    public void setStartTimestamp(String startTimestamp) {
281
        this.startTimestamp = startTimestamp;
282
    }
283
 
284
    public String getEndTimestamp() {
285
        return endTimestamp;
286
    }
287
 
288
    public void setEndTimestamp(String endTimestamp) {
289
        this.endTimestamp = endTimestamp;
290
    }
291
 
292
    public void setActivities(List<Activity> activities) {
293
        this.activities = activities;
294
    }
3405 mandeep.dh 295
 
296
    public String getCustomerEmailId() {
297
        return customerEmailId;
298
    }
299
 
300
    public void setCustomerEmailId(String customerEmailId) {
301
        this.customerEmailId = customerEmailId;
302
    }
303
 
304
    public String getUserId() {
305
        return userId;
306
    }
307
 
308
    public void setUserId(String userId) {
309
        this.userId = userId;
310
    }
311
 
312
    public User getUser(Long userId) {
313
        User user = null;
314
 
315
        try {
316
            userContextServiceClient = new UserClient().getClient();
317
            user = userContextServiceClient.getUserById(userId);
318
        } catch (UserContextException e) {
319
            String errorString = "Could not fetch user for " + userId;
320
            log.error(errorString, e);
321
            addActionError(errorString);
322
        } catch (TException e) {
323
            String errorString = "Could not create client";
324
            log.error(errorString, e);
325
            addActionError(errorString);
326
        }
327
 
328
        return user;
329
    }
330
 
331
    public String getCustomerName() {
332
        return customerName;
333
    }
334
 
335
    public void setCustomerName(String customerName) {
336
        this.customerName = customerName;
337
    }
4142 mandeep.dh 338
 
339
    public String getCategory() {
340
        return category;
341
    }
342
 
343
    public void setCategory(String category) {
344
        this.category = category;
345
    }
3339 mandeep.dh 346
}