Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3090 mandeep.dh 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.crm.Activity;
3106 mandeep.dh 7
import in.shop2020.crm.ActivityType;
3090 mandeep.dh 8
import in.shop2020.crm.Agent;
3390 mandeep.dh 9
import in.shop2020.crm.SearchFilter;
3090 mandeep.dh 10
import in.shop2020.crm.Ticket;
11
import in.shop2020.crm.TicketPriority;
12
import in.shop2020.crm.TicketStatus;
13
import in.shop2020.model.v1.user.User;
14
import in.shop2020.model.v1.user.UserContextException;
3390 mandeep.dh 15
import in.shop2020.serving.auth.CRMAuthorizingRealm;
16
import in.shop2020.thrift.clients.CRMClient;
3228 mandeep.dh 17
import in.shop2020.thrift.clients.HelperClient;
3390 mandeep.dh 18
import in.shop2020.thrift.clients.UserClient;
3228 mandeep.dh 19
import in.shop2020.util.CRMConstants;
20
import in.shop2020.utils.HelperService.Client;
3090 mandeep.dh 21
 
22
import java.util.LinkedHashMap;
23
import java.util.List;
24
import java.util.Map;
25
 
3390 mandeep.dh 26
import org.apache.shiro.SecurityUtils;
3090 mandeep.dh 27
import org.apache.thrift.TException;
28
 
29
/**
30
 * Action class for activity pages in CRM.
3106 mandeep.dh 31
 * 
3090 mandeep.dh 32
 * @author mandeep
33
 */
34
public class UserActivityController extends BaseController {
35
    /**
36
     * 
37
     */
38
    private static final long   serialVersionUID = 1L;
39
 
40
    private Map<Long, Activity> activities       = new LinkedHashMap<Long, Activity>();
3269 mandeep.dh 41
 
42
    private String              userId;
3090 mandeep.dh 43
    private String              description;
44
    private User                user;
3106 mandeep.dh 45
    private String              type;
3228 mandeep.dh 46
    private String              subject;
47
    private String              body;
48
    private String              userEmailId;
3090 mandeep.dh 49
 
50
    public String index() throws TException, UserContextException {
3390 mandeep.dh 51
        SearchFilter searchFilter = new SearchFilter();
52
        searchFilter.setCustomerId(Long.parseLong(userId));
53
        crmServiceClient         = new CRMClient().getClient();
54
 
55
        List<Activity> activityList = crmServiceClient.getActivities(searchFilter);
3090 mandeep.dh 56
        if (activityList != null) {
57
            for (Activity activity : activityList) {
58
                activities.put(activity.getId(), activity);
59
            }
60
        }
61
 
3269 mandeep.dh 62
        user = userContextServiceClient.getUserById(Long.parseLong(userId));
3090 mandeep.dh 63
 
64
        return INDEX;
65
    }
66
 
3106 mandeep.dh 67
    public String editNew() {
3228 mandeep.dh 68
        try {
3390 mandeep.dh 69
            if (userId != null && !userId.isEmpty()) {
70
                userContextServiceClient = new UserClient().getClient();
3269 mandeep.dh 71
                user = userContextServiceClient.getUserById(Long.parseLong(userId));
72
            }
3228 mandeep.dh 73
            subject = "";
74
        } catch (UserContextException e) {
75
            String errorMessage = "Could not fetch user for " + userId;
76
            addActionError(errorMessage);
77
            log.error(errorMessage, e);
78
            return EXCEPTION;
79
        } catch (TException e) {
80
            String errorMessage = "Could not fetch user for " + userId;
81
            addActionError(errorMessage);
82
            log.error(errorMessage, e);
83
            return EXCEPTION;
84
        }
85
 
3090 mandeep.dh 86
        return EDIT_NEW;
87
    }
88
 
89
    public Map<Long, Activity> getActivities() {
90
        return activities;
91
    }
92
 
93
    public TicketStatus[] getTicketStatuses() {
94
        return TicketStatus.values();
95
    }
96
 
97
    public TicketPriority[] getTicketPriorities() {
98
        return TicketPriority.values();
99
    }
100
 
3106 mandeep.dh 101
    public ActivityType[] getActivityTypes() {
102
        return ActivityType.values();
3090 mandeep.dh 103
    }
104
 
105
    public User getUser() {
106
        return user;
107
    }
108
 
109
    public Ticket getTicket(long activityId) throws TException {
3390 mandeep.dh 110
        SearchFilter searchFilter = new SearchFilter();
111
        searchFilter.setActivityId(activityId);
112
        crmServiceClient         = new CRMClient().getClient();
113
 
3090 mandeep.dh 114
        Ticket ticket = null;
3390 mandeep.dh 115
        Activity activity = null;
116
        List<Activity> activities = crmServiceClient.getActivities(searchFilter);
117
        if (activities != null && !activities.isEmpty()) {
118
            activity = activities.get(0);
3090 mandeep.dh 119
        }
120
 
3390 mandeep.dh 121
        if (activity != null && activity.isSetTicketId()) {
122
            searchFilter.setTicketId(activity.getTicketId());
123
            List<Ticket> tickets = crmServiceClient.getTickets(searchFilter);
124
            if (tickets != null && !tickets.isEmpty()) {
125
                ticket = tickets.get(0);
126
            }
127
        }
128
 
3090 mandeep.dh 129
        return ticket;
130
    }
131
 
132
    public String create() throws Exception {
133
        Activity activity = new Activity();
3106 mandeep.dh 134
        activity.setDescription(description);
135
        activity.setType(ActivityType.valueOf(type));
3390 mandeep.dh 136
        activity.setCreatorId(CRMAuthorizingRealm.getAgent(currentAgentEmailId).getId());
3090 mandeep.dh 137
 
3269 mandeep.dh 138
        if (userId != null && !userId.isEmpty()) {
139
            activity.setCustomerId(Long.parseLong(userId));
140
        }
141
 
142
        if (ActivityType.SEND_EMAIL_TO_CUSTOMER.equals(activity.getType())) {
3228 mandeep.dh 143
            log.info("Sending mail");
144
            Client helperClient = new HelperClient().getClient();
3390 mandeep.dh 145
            activity.setUserEmailId(helperClient.saveUserEmailForSending(
3228 mandeep.dh 146
                    userEmailId, CRMConstants.CRM_EMAIL_SENDOR, subject,
147
                    body, null, CRMConstants.CRM_EMAIL_TYPE));
148
 
149
            // We change activityType to OTHER when pop up box for email
150
            // closes
3234 mandeep.dh 151
            activity.setDescription("Subject: " + subject + "\n\n" + "Body: " + body);
3228 mandeep.dh 152
        }
153
 
3390 mandeep.dh 154
        crmServiceClient         = new CRMClient().getClient();
3090 mandeep.dh 155
        crmServiceClient.insertActivity(activity);
156
 
157
        return index();
158
    }
159
 
3390 mandeep.dh 160
    public boolean isAssigneeEditable() {
161
        return SecurityUtils.getSubject().hasRole("TeamLead");
3106 mandeep.dh 162
    }
163
 
164
    public List<Agent> getAllAgents() throws TException {
3390 mandeep.dh 165
        return CRMAuthorizingRealm.getAgents();
3090 mandeep.dh 166
    }
167
 
168
    public Agent getAgent(long agentId) throws TException {
3390 mandeep.dh 169
        return CRMAuthorizingRealm.getAgent(agentId);
3090 mandeep.dh 170
    }
171
 
172
    public void setDescription(String description) {
173
        this.description = description;
174
    }
175
 
3269 mandeep.dh 176
    public String getUserId() {
3106 mandeep.dh 177
        return userId;
3090 mandeep.dh 178
    }
179
 
3106 mandeep.dh 180
    public String getType() {
181
        return type;
3090 mandeep.dh 182
    }
183
 
3106 mandeep.dh 184
    public void setType(String type) {
185
        this.type = type;
3090 mandeep.dh 186
    }
187
 
3106 mandeep.dh 188
    public String getDescription() {
189
        return description;
3090 mandeep.dh 190
    }
191
 
3106 mandeep.dh 192
    public void setActivities(Map<Long, Activity> activities) {
193
        this.activities = activities;
3090 mandeep.dh 194
    }
195
 
3269 mandeep.dh 196
    public void setUserId(String userId) {
3106 mandeep.dh 197
        this.userId = userId;
3090 mandeep.dh 198
    }
3228 mandeep.dh 199
 
200
    public String getSubject() {
201
        return subject;
202
    }
203
 
204
    public void setSubject(String subject) {
205
        this.subject = subject;
206
    }
207
 
208
    public String getBody() {
209
        return body;
210
    }
211
 
212
    public void setBody(String body) {
213
        this.body = body;
214
    }
215
 
216
    public String getUserEmailId() {
217
        return userEmailId;
218
    }
219
 
220
    public void setUserEmailId(String userEmailId) {
221
        this.userEmailId = userEmailId;
222
    }
3090 mandeep.dh 223
}