Subversion Repositories SmartDukaan

Rev

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