Subversion Repositories SmartDukaan

Rev

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