Subversion Repositories SmartDukaan

Rev

Rev 14904 | Rev 14911 | 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;
14898 manish.sha 49
    private String			  notShowPmTickets;
3339 mandeep.dh 50
 
51
    public String index() {
52
        try {
3390 mandeep.dh 53
            SearchFilter searchFilter = new SearchFilter();
14898 manish.sha 54
 
14906 manish.sha 55
            if(notShowPmTickets!=null && !notShowPmTickets.isEmpty() && "yes".equalsIgnoreCase(notShowPmTickets)){
56
            	searchFilter.setNotShowPmTickets(true);
14898 manish.sha 57
            }
3405 mandeep.dh 58
            if (activityId != null && !activityId.isEmpty()) {
59
                searchFilter.setActivityId(Long.parseLong(activityId));
60
            } else if (userId != null && !userId.isEmpty()) {
61
                searchFilter.setCustomerId(Long.parseLong(userId));
62
            } else if (creatorId != null && !creatorId.isEmpty()) {
63
                searchFilter.setActivityCreatorIds(Collections
64
                        .singletonList(Long.parseLong(creatorId)));
3390 mandeep.dh 65
                searchFilter.setIsActivityRead(false);
3405 mandeep.dh 66
            } else if (agentIds != null && agentIds.length != 0) {
3397 mandeep.dh 67
                searchFilter.setActivityCreatorIds(new ArrayList<Long>());
68
                for (String agentId : agentIds) {
3405 mandeep.dh 69
                    searchFilter.getActivityCreatorIds().add(
70
                            CRMAuthorizingRealm.getAgent(agentId).getId());
3397 mandeep.dh 71
                }
72
 
4142 mandeep.dh 73
                if (type != null && !type.isEmpty()) {
74
                    searchFilter.setActivityType(ActivityType.valueOf(type));
75
                }
76
 
77
                if (category != null && !category.isEmpty()) {
78
                    searchFilter.setTicketCategory(TicketCategory.valueOf(category));
79
                }
7595 kshitij.so 80
 
81
                //Start:- Added By Manish Sharma for Searching Tickets/Activities using  Mobile No. on 21-Jun-2013
82
                if(customerMobileNumber!=null && !customerMobileNumber.isEmpty()){
83
                	searchFilter.setCustomerMobileNumber(customerMobileNumber);
84
                }
85
                //End:- Added By Manish Sharma for Searching Tickets/Activities using  Mobile No. on 21-Jun-2013
4142 mandeep.dh 86
 
3397 mandeep.dh 87
                try {
88
                    if (startTimestamp != null && !startTimestamp.isEmpty()) {
3405 mandeep.dh 89
                        searchFilter.setStartTimestamp(SDF
90
                                .parse(startTimestamp).getTime());
3397 mandeep.dh 91
                    }
92
 
93
                    if (endTimestamp != null && !endTimestamp.isEmpty()) {
3405 mandeep.dh 94
                        searchFilter.setEndTimestamp(SDF.parse(endTimestamp)
95
                                .getTime());
3397 mandeep.dh 96
                    }
3405 mandeep.dh 97
                } catch (ParseException e) {
3397 mandeep.dh 98
                    log.error("Error parsing timestamps", e);
99
                }
3405 mandeep.dh 100
            } else {
101
                searchFilter.setActivityCreatorIds(Collections
102
                        .singletonList(CRMAuthorizingRealm.getAgent(
103
                                currentAgentEmailId).getId()));
3397 mandeep.dh 104
            }
105
 
3405 mandeep.dh 106
            crmServiceClient = new CRMClient().getClient();
3397 mandeep.dh 107
            activities = crmServiceClient.getActivities(searchFilter);
3339 mandeep.dh 108
        } catch (TException e) {
109
            log.error("Error while retrieving activities for "
110
                    + currentAgentEmailId, e);
3405 mandeep.dh 111
        } catch (NumberFormatException e) {
112
            log.error("Error parsing userId: " + userId, e);
3339 mandeep.dh 113
        }
114
 
115
        return INDEX;
116
    }
117
 
3405 mandeep.dh 118
    public String editNew() {
119
        return EDIT_NEW;
120
    }
121
 
3339 mandeep.dh 122
    public String create() throws Exception {
123
        Activity activity = new Activity();
5203 amar.kumar 124
        if(commonDescription!=null && !commonDescription.isEmpty()){
125
        	if(description!=null) {
126
        		description = commonDescription + "  " +description;
127
        	} else {
128
        		description = commonDescription;
129
        	}
130
        }
3339 mandeep.dh 131
        activity.setDescription(description);
132
        activity.setType(ActivityType.valueOf(type));
3405 mandeep.dh 133
        activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId)
134
                .getId());
3339 mandeep.dh 135
 
3405 mandeep.dh 136
        if (userId != null && !userId.isEmpty()) {
137
            activity.setCustomerId(Long.parseLong(userId));
5224 amar.kumar 138
            if(customerMobileNumber != null
139
                        && !customerMobileNumber.isEmpty()) {
140
            	activity.setCustomerMobileNumber(customerMobileNumber);
141
            }
3405 mandeep.dh 142
        } else {
143
            User user = null;
144
            userContextServiceClient = new UserClient().getClient();
145
 
146
            try {
147
                if (customerName != null && !customerName.isEmpty()) {
148
                    activity.setCustomerName(customerName);
149
                }
150
 
151
                if (customerEmailId != null && !customerEmailId.isEmpty()) {
152
                    activity.setCustomerEmailId(customerEmailId);
153
                    user = userContextServiceClient
154
                            .getUserByEmail(customerEmailId);
155
                }
156
 
157
                if ((user == null || user.getUserId() == -1)
158
                        && customerMobileNumber != null
159
                        && !customerMobileNumber.isEmpty()) {
160
                    activity.setCustomerMobileNumber(customerMobileNumber);
161
                    user = userContextServiceClient.getUserByMobileNumber(Long
162
                            .parseLong(customerMobileNumber));
163
                }
164
            } catch (UserContextException e) {
165
                log.error("Could not fetch user for: " + customerEmailId + " "
166
                        + customerMobileNumber + " " + customerName, e);
167
            }
168
 
169
            if (user != null && user.getUserId() != -1) {
170
                activity.setCustomerId(user.getUserId());
171
            }
3339 mandeep.dh 172
        }
14898 manish.sha 173
 
3339 mandeep.dh 174
 
3405 mandeep.dh 175
        crmServiceClient = new CRMClient().getClient();
176
        activityId = String.valueOf(crmServiceClient.insertActivity(activity));
3339 mandeep.dh 177
 
178
        return index();
179
    }
180
 
3405 mandeep.dh 181
    public String markAsRead() {
3390 mandeep.dh 182
        try {
3405 mandeep.dh 183
            crmServiceClient = new CRMClient().getClient();
184
            crmServiceClient.markAsRead(Long.parseLong(activityId),
185
                    CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3390 mandeep.dh 186
        } catch (NumberFormatException e) {
187
            log.error("Could not parse activity id from" + activityId, e);
188
        } catch (TException e) {
3405 mandeep.dh 189
            log.error("Could not mark the activityId: " + activityId
190
                    + " as read", e);
3390 mandeep.dh 191
        }
192
 
3405 mandeep.dh 193
        return index();
3390 mandeep.dh 194
    }
195
 
3711 mandeep.dh 196
    public String getUnreadActivities() {
197
        try {
198
            SearchFilter searchFilter = new SearchFilter();
199
            searchFilter.setIsActivityRead(false);
200
            searchFilter.setActivityCreatorIds(Collections.singletonList(CRMConstants.ADMIN_AGENT_ID));
201
 
14906 manish.sha 202
            if(notShowPmTickets!=null && !notShowPmTickets.isEmpty() && "yes".equalsIgnoreCase(notShowPmTickets)){
203
            	searchFilter.setNotShowPmTickets(true);
14903 manish.sha 204
            }
205
 
3711 mandeep.dh 206
            searchFilter.setTicketAssigneeIds(Collections
207
                    .singletonList(CRMAuthorizingRealm.getAgent(
208
                            currentAgentEmailId).getId()));
209
 
210
            crmServiceClient = new CRMClient().getClient();
211
            activities = crmServiceClient.getActivities(searchFilter);
212
        } catch (TException e) {
213
            log.error("Could not get agentId for: " + currentAgentEmailId, e);
214
        }
215
 
216
        return INDEX;
217
    }
218
 
3339 mandeep.dh 219
    public ActivityType[] getActivityTypes() {
220
        return ActivityType.values();
221
    }
222
 
223
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 224
        return CRMAuthorizingRealm.getAgent(agentId);
3339 mandeep.dh 225
    }
226
 
5203 amar.kumar 227
    public ActivityCommonDescriptionMatrix[] getActivityCommonDescriptionMatrix () {
228
        return CRMConstants.ActivityCommonDescriptionMatrix.values();
229
    }
230
 
3339 mandeep.dh 231
    public String getDescription() {
232
        return description;
233
    }
234
 
235
    public void setDescription(String description) {
236
        this.description = description;
237
    }
238
 
5203 amar.kumar 239
    public String getCommonDescription() {
240
		return commonDescription;
241
	}
242
 
243
	public void setCommonDescription(String commonDescription) {
244
		this.commonDescription = commonDescription;
245
	}
246
 
247
	public String getType() {
3339 mandeep.dh 248
        return type;
249
    }
250
 
251
    public void setType(String type) {
252
        this.type = type;
253
    }
254
 
255
    public String getCustomerMobileNumber() {
256
        return customerMobileNumber;
257
    }
258
 
259
    public void setCustomerMobileNumber(String customerMobileNumber) {
260
        this.customerMobileNumber = customerMobileNumber;
261
    }
262
 
263
    public List<Activity> getActivities() {
264
        return activities;
265
    }
266
 
267
    public String getCreatorId() {
268
        return creatorId;
269
    }
270
 
271
    public void setCreatorId(String creatorId) {
272
        this.creatorId = creatorId;
273
    }
3390 mandeep.dh 274
 
275
    public String getActivityId() {
276
        return activityId;
277
    }
278
 
279
    public void setActivityId(String activityId) {
280
        this.activityId = activityId;
281
    }
3397 mandeep.dh 282
 
283
    public String[] getAgentIds() {
284
        return agentIds;
285
    }
286
 
287
    public void setAgentIds(String[] agentIds) {
288
        this.agentIds = agentIds;
289
    }
290
 
291
    public String getStartTimestamp() {
292
        return startTimestamp;
293
    }
294
 
295
    public void setStartTimestamp(String startTimestamp) {
296
        this.startTimestamp = startTimestamp;
297
    }
298
 
299
    public String getEndTimestamp() {
300
        return endTimestamp;
301
    }
302
 
303
    public void setEndTimestamp(String endTimestamp) {
304
        this.endTimestamp = endTimestamp;
305
    }
306
 
307
    public void setActivities(List<Activity> activities) {
308
        this.activities = activities;
309
    }
3405 mandeep.dh 310
 
311
    public String getCustomerEmailId() {
312
        return customerEmailId;
313
    }
314
 
315
    public void setCustomerEmailId(String customerEmailId) {
316
        this.customerEmailId = customerEmailId;
317
    }
318
 
319
    public String getUserId() {
320
        return userId;
321
    }
322
 
323
    public void setUserId(String userId) {
324
        this.userId = userId;
325
    }
326
 
327
    public User getUser(Long userId) {
328
        User user = null;
329
 
330
        try {
331
            userContextServiceClient = new UserClient().getClient();
332
            user = userContextServiceClient.getUserById(userId);
333
        } catch (UserContextException e) {
334
            String errorString = "Could not fetch user for " + userId;
335
            log.error(errorString, e);
336
            addActionError(errorString);
337
        } catch (TException e) {
338
            String errorString = "Could not create client";
339
            log.error(errorString, e);
340
            addActionError(errorString);
341
        }
342
 
343
        return user;
344
    }
345
 
346
    public String getCustomerName() {
347
        return customerName;
348
    }
349
 
350
    public void setCustomerName(String customerName) {
351
        this.customerName = customerName;
352
    }
4142 mandeep.dh 353
 
354
    public String getCategory() {
355
        return category;
356
    }
357
 
358
    public void setCategory(String category) {
359
        this.category = category;
360
    }
14898 manish.sha 361
 
362
	public String getNotShowPmTickets() {
363
		return notShowPmTickets;
364
	}
365
 
14904 manish.sha 366
	public void setNotShowPmTickets(String notShowPmTickets) {
14898 manish.sha 367
		this.notShowPmTickets = notShowPmTickets;
368
	}
3339 mandeep.dh 369
}