Subversion Repositories SmartDukaan

Rev

Rev 4241 | Rev 5224 | 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));
128
        } else {
129
            User user = null;
130
            userContextServiceClient = new UserClient().getClient();
131
 
132
            try {
133
                if (customerName != null && !customerName.isEmpty()) {
134
                    activity.setCustomerName(customerName);
135
                }
136
 
137
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
138
                    activity.setCustomerEmailId(customerEmailId);
139
                    user = userContextServiceClient
140
                            .getUserByEmail(customerEmailId);
141
                }
142
 
143
                if ((user == null || user.getUserId() == -1)
144
                        && customerMobileNumber != null
145
                        && !customerMobileNumber.isEmpty()) {
146
                    activity.setCustomerMobileNumber(customerMobileNumber);
147
                    user = userContextServiceClient.getUserByMobileNumber(Long
148
                            .parseLong(customerMobileNumber));
149
                }
150
            } catch (UserContextException e) {
151
                log.error("Could not fetch user for: " + customerEmailId + " "
152
                        + customerMobileNumber + " " + customerName, e);
153
            }
154
 
155
            if (user != null && user.getUserId() != -1) {
156
                activity.setCustomerId(user.getUserId());
157
            }
3339 mandeep.dh 158
        }
159
 
3405 mandeep.dh 160
        crmServiceClient = new CRMClient().getClient();
161
        activityId = String.valueOf(crmServiceClient.insertActivity(activity));
3339 mandeep.dh 162
 
163
        return index();
164
    }
165
 
3405 mandeep.dh 166
    public String markAsRead() {
3390 mandeep.dh 167
        try {
3405 mandeep.dh 168
            crmServiceClient = new CRMClient().getClient();
169
            crmServiceClient.markAsRead(Long.parseLong(activityId),
170
                    CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3390 mandeep.dh 171
        } catch (NumberFormatException e) {
172
            log.error("Could not parse activity id from" + activityId, e);
173
        } catch (TException e) {
3405 mandeep.dh 174
            log.error("Could not mark the activityId: " + activityId
175
                    + " as read", e);
3390 mandeep.dh 176
        }
177
 
3405 mandeep.dh 178
        return index();
3390 mandeep.dh 179
    }
180
 
3711 mandeep.dh 181
    public String getUnreadActivities() {
182
        try {
183
            SearchFilter searchFilter = new SearchFilter();
184
            searchFilter.setIsActivityRead(false);
185
            searchFilter.setActivityCreatorIds(Collections.singletonList(CRMConstants.ADMIN_AGENT_ID));
186
 
187
            searchFilter.setTicketAssigneeIds(Collections
188
                    .singletonList(CRMAuthorizingRealm.getAgent(
189
                            currentAgentEmailId).getId()));
190
 
191
            crmServiceClient = new CRMClient().getClient();
192
            activities = crmServiceClient.getActivities(searchFilter);
193
        } catch (TException e) {
194
            log.error("Could not get agentId for: " + currentAgentEmailId, e);
195
        }
196
 
197
        return INDEX;
198
    }
199
 
3339 mandeep.dh 200
    public ActivityType[] getActivityTypes() {
201
        return ActivityType.values();
202
    }
203
 
204
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 205
        return CRMAuthorizingRealm.getAgent(agentId);
3339 mandeep.dh 206
    }
207
 
5203 amar.kumar 208
    public ActivityCommonDescriptionMatrix[] getActivityCommonDescriptionMatrix () {
209
        return CRMConstants.ActivityCommonDescriptionMatrix.values();
210
    }
211
 
3339 mandeep.dh 212
    public String getDescription() {
213
        return description;
214
    }
215
 
216
    public void setDescription(String description) {
217
        this.description = description;
218
    }
219
 
5203 amar.kumar 220
    public String getCommonDescription() {
221
		return commonDescription;
222
	}
223
 
224
	public void setCommonDescription(String commonDescription) {
225
		this.commonDescription = commonDescription;
226
	}
227
 
228
	public String getType() {
3339 mandeep.dh 229
        return type;
230
    }
231
 
232
    public void setType(String type) {
233
        this.type = type;
234
    }
235
 
236
    public String getCustomerMobileNumber() {
237
        return customerMobileNumber;
238
    }
239
 
240
    public void setCustomerMobileNumber(String customerMobileNumber) {
241
        this.customerMobileNumber = customerMobileNumber;
242
    }
243
 
244
    public List<Activity> getActivities() {
245
        return activities;
246
    }
247
 
248
    public String getCreatorId() {
249
        return creatorId;
250
    }
251
 
252
    public void setCreatorId(String creatorId) {
253
        this.creatorId = creatorId;
254
    }
3390 mandeep.dh 255
 
256
    public String getActivityId() {
257
        return activityId;
258
    }
259
 
260
    public void setActivityId(String activityId) {
261
        this.activityId = activityId;
262
    }
3397 mandeep.dh 263
 
264
    public String[] getAgentIds() {
265
        return agentIds;
266
    }
267
 
268
    public void setAgentIds(String[] agentIds) {
269
        this.agentIds = agentIds;
270
    }
271
 
272
    public String getStartTimestamp() {
273
        return startTimestamp;
274
    }
275
 
276
    public void setStartTimestamp(String startTimestamp) {
277
        this.startTimestamp = startTimestamp;
278
    }
279
 
280
    public String getEndTimestamp() {
281
        return endTimestamp;
282
    }
283
 
284
    public void setEndTimestamp(String endTimestamp) {
285
        this.endTimestamp = endTimestamp;
286
    }
287
 
288
    public void setActivities(List<Activity> activities) {
289
        this.activities = activities;
290
    }
3405 mandeep.dh 291
 
292
    public String getCustomerEmailId() {
293
        return customerEmailId;
294
    }
295
 
296
    public void setCustomerEmailId(String customerEmailId) {
297
        this.customerEmailId = customerEmailId;
298
    }
299
 
300
    public String getUserId() {
301
        return userId;
302
    }
303
 
304
    public void setUserId(String userId) {
305
        this.userId = userId;
306
    }
307
 
308
    public User getUser(Long userId) {
309
        User user = null;
310
 
311
        try {
312
            userContextServiceClient = new UserClient().getClient();
313
            user = userContextServiceClient.getUserById(userId);
314
        } catch (UserContextException e) {
315
            String errorString = "Could not fetch user for " + userId;
316
            log.error(errorString, e);
317
            addActionError(errorString);
318
        } catch (TException e) {
319
            String errorString = "Could not create client";
320
            log.error(errorString, e);
321
            addActionError(errorString);
322
        }
323
 
324
        return user;
325
    }
326
 
327
    public String getCustomerName() {
328
        return customerName;
329
    }
330
 
331
    public void setCustomerName(String customerName) {
332
        this.customerName = customerName;
333
    }
4142 mandeep.dh 334
 
335
    public String getCategory() {
336
        return category;
337
    }
338
 
339
    public void setCategory(String category) {
340
        this.category = category;
341
    }
3339 mandeep.dh 342
}