Subversion Repositories SmartDukaan

Rev

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