Subversion Repositories SmartDukaan

Rev

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