-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeLeadsController.java
160 lines (136 loc) · 4.84 KB
/
MergeLeadsController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.tadigital.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.eclipsesource.json.JsonObject;
import com.tadigital.dao.LeadDao;
import com.tadigital.entity.Leads;
import com.tadigital.leadActions.MergeLeads;
@WebServlet("/MergeLeadsController")
public class MergeLeadsController extends HttpServlet {
private static final long serialVersionUID = 1L;
public MergeLeadsController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LeadDao ld = new LeadDao();
List<Leads> leads = new ArrayList<Leads>();
HttpSession session = request.getSession();
boolean dbUpdateStatus = false;
leads = ld.displayAllData();
if (leads == null || leads.size() == 0) {
session.setAttribute("CONSOLEMESSAGE", "No Duplicate Records Found");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
try {
rd.forward(request, response);
} catch (Exception e) {
System.out.println(e);
}
} else {
MergeLeads mergeLeads = new MergeLeads();
String winningEmail = null, result = " "/* , delRecordId = null */;
// String leadEmail [];
ArrayList list = new ArrayList();
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
int list_size = leads.size(), flag = 0;
System.out.println("I am List Size " + list_size);
for (int i = 0; i < list_size; i++) {
if (request.getParameter(leads.get(i).getID()) != null && flag == 0) {
mergeLeads.winningId = Integer.parseInt(leads.get(i).getID());
winningEmail = leads.get(i).getEmail();
flag = 1;
}
else if (request.getParameter(leads.get(i).getID()) != null && flag != 0) {
// mergeLeads.leadIds = new int[]{Integer.parseInt(leads.get(i).getID())};
list2.add(Integer.parseInt(leads.get(i).getID()));
list.add(leads.get(i).getEmail());
list1.add(leads.get(i).getID());
list3.add(leads.get(i).getSFDC());
flag = flag + 1;
}
/*
* if (flag == 2) { break; }
*/
}
System.out.println("I am Flag: " + flag);
String[] leadEmail = new String[list.size()];
for (int j = 0; j < leadEmail.length; j++)
leadEmail[j] = list.get(j).toString();
String[] delRecordId = new String[list1.size()];
for (int j = 0; j < delRecordId.length; j++)
delRecordId[j] = list1.get(j).toString();
int[] LeadID = new int[list2.size()];
for (int j = 0; j < LeadID.length; j++) {
LeadID[j] = (int) list2.get(j);
}
String[] sfdcType = new String[list3.size()];
for (int j = 0; j < sfdcType.length; j++) {
sfdcType[j] = list3.get(j).toString();
}
int flag_main = 1;
int flag_sfdc = 1;
for (int i = 0; i < flag - 1; i++) {
if (!(winningEmail.equals(leadEmail[i]))) {
flag_main = 0;
result = "Mail Ids are not same";
}
}
for (int a = 0; a < sfdcType.length - 1; a++) {
for (int b = a + 1; b < sfdcType.length; b++) {
if (a != b) {
if ((sfdcType[a].equals("Lead") && sfdcType[b].equals("Contact"))
|| (sfdcType[a].equals("Contact") && sfdcType[b].equals("Lead")) ) {
flag_sfdc = 0;
result = "SFDC Type Mismatch Error!";
break;
}
}
}
}
if (flag_main == 1 && flag_sfdc == 1) {
mergeLeads.leadIds = LeadID;
System.out.println(
"LeadID Length" + LeadID.length + "MergeLeads.LEADIDS LENGTH" + mergeLeads.leadIds.length);
result = mergeLeads.postData();
for (int i = 0; i < flag - 1; i++) {
dbUpdateStatus = ld.deleteRecord(delRecordId[i]);
System.out.println(dbUpdateStatus);
}
}
System.out.println(result);
if (flag_main == 1) {
JsonObject JSONObject = JsonObject.readFrom(result.toString());
String resp = JSONObject.get("success").toString();
System.out.println(resp);
if (resp.equals("true")) {
resp = "Records Merged Successfully";
} else {
resp = "Error Merging Records";
}
session.setAttribute("CONSOLEMESSAGE", resp);
} else {
session.setAttribute("CONSOLEMESSAGE", result);
}
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
try {
rd.forward(request, response);
} catch (Exception e) {
System.out.println(e);
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}