Subversion Repositories SmartDukaan

Rev

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