Subversion Repositories SmartDukaan

Rev

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