-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateAnswerTable.java
214 lines (187 loc) · 8.29 KB
/
populateAnswerTable.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;
/**
* POPULATING answerthread table. Question has answer. Answers may have replies.
* Program assumes that each question has 10 answers. Randomly some number n of
* them will answers to questions and rest (10 - n) will be answers to answers.
*
* @author Anup_Dell
*/
public class populateAnswerTable {
/**
* @param args the command line arguments
*/
private static final String CHAR_LIST
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final int RANDOM_STRING_LENGTH = 10;
private static final int MAX_ARRAY_LENGTH = 20000;
public static void main(String[] args) {
// TODO code application logic here
try {
populateAnswerTable ja = new populateAnswerTable();
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5433/Intell_v4";
Connection conn = DriverManager.getConnection(url, "postgres", "wipro123");
// read all university ids
String updateString = "Select discussionforumid, researcher_researcherid, subtopics_subtopicid, subtopics_topics_topicid from question order by discussionforumid";
PreparedStatement updateSales = null;
ResultSet rs = null;
updateSales = conn.prepareStatement(updateString);
rs = updateSales.executeQuery();
int unicount = 0;
long researcherids[] = new long[MAX_ARRAY_LENGTH];
long forumid[] = new long[MAX_ARRAY_LENGTH];
long majorid[] = new long[MAX_ARRAY_LENGTH];
long subid[] = new long[MAX_ARRAY_LENGTH];
while (rs.next()) {
forumid[unicount] = rs.getInt("discussionforumid");
researcherids[unicount] = rs.getInt("researcher_researcherid");
majorid[unicount] = rs.getInt("subtopics_topics_topicid");
subid[unicount] = rs.getInt("subtopics_subtopicid");
System.out.println(researcherids[unicount]);
unicount++;
}
long answerid = 0;
Random randomGenerator = new Random();
// FOR EACH QUESTION
for (int i = 0; i < unicount; i++) {
int ansNum = 10;
long replierid[] = new long[10];
int replierCnt = 0;
// ASSUMING QUESTIONS WILL BE ANSWERED BY RESEARCHER WITH SAME SPECIALIZATION OR RESEARCHER WITH SAME MAJOR
updateString = "Select researcherid from researcher where specialization_id = " + subid[i];
updateSales = null;
rs = null;
updateSales = conn.prepareStatement(updateString);
rs = updateSales.executeQuery();
boolean moreRepNeeded = true;
while (rs.next()) {
replierid[replierCnt] = rs.getInt("researcherid");
replierCnt++;
if (replierCnt > 9) {
moreRepNeeded = false;
break;
}
}
if (moreRepNeeded) {
updateString = "Select researcherid from researcher where major = " + majorid[i];
updateSales = null;
rs = null;
updateSales = conn.prepareStatement(updateString);
rs = updateSales.executeQuery();
while (rs.next()) {
replierid[replierCnt] = rs.getInt("researcherid");
replierCnt++;
if (replierCnt > 9) {
moreRepNeeded = false;
break;
}
}
}
//immediate parents
int num = randBetween(1, 5);
int loopsize = replierid.length - num;
long insertedIds[] = new long[loopsize];
int ins = 0;
// IMMEDIATE ANSWERS
for (int k = 0; k < loopsize; k++) {
System.out.println("Inserting ...");
String sql = "INSERT INTO answerthread(question_discussionforumid, answerid, answer, numberofupvotes, numberofdownvotes, replierid) VALUES(?, ?, ?, ?, ?, ?)";
PreparedStatement ps1 = conn.prepareStatement(sql);
ps1.setLong(1, forumid[i]);
ps1.setLong(2, answerid);
ps1.setString(3, ja.getDescription());
int numberofupvotes = randomGenerator.nextInt(200);
int numberofdownvotes = randomGenerator.nextInt(50);
ps1.setInt(4, numberofupvotes);
ps1.setInt(5, numberofdownvotes);
ps1.setLong(6, replierid[k]);
insertedIds[ins] = answerid;
ins++;
int val = ps1.executeUpdate();
System.out.println(researcherids[i] + " inserted : ");
answerid++;
}
// ANSWERS TO ANSWERS
for (int j = 0; j < num; j++) {
System.out.println("Inserting ...");
String sql = "INSERT INTO answerthread(question_discussionforumid, answerid, immediateparentanswerid, answer, numberofupvotes, numberofdownvotes, replierid) VALUES(?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps1 = conn.prepareStatement(sql);
ps1.setLong(1, forumid[i]);
ps1.setLong(2, answerid);
int index = randBetween(0, (insertedIds.length - 1));
ps1.setLong(3, insertedIds[index]);
ps1.setString(4, ja.getDescription());
int numberofupvotes = randomGenerator.nextInt(200);
int numberofdownvotes = randomGenerator.nextInt(50);
ps1.setInt(5, numberofupvotes);
ps1.setInt(6, numberofdownvotes);
ps1.setLong(7, replierid[loopsize]);
loopsize++;
//insertedIds[ins] = answerid;
//ins ++;
int val = ps1.executeUpdate();
System.out.println(researcherids[i] + " inserted : ");
answerid++;
}
}
} catch (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public static int randBetween(int start, int end) {
return start + (int) Math.round(Math.random() * (end - start));
}
public String generateRandomString(int wordlength) {
StringBuffer randStr = new StringBuffer();
for (int i = 0; i < wordlength; i++) {
int number = getRandomNumber();
char ch = CHAR_LIST.charAt(number);
randStr.append(ch);
}
return randStr.toString();
}
private int getRandomNumber() {
int randomInt = 0;
Random randomGenerator = new Random();
randomInt = randomGenerator.nextInt(CHAR_LIST.length());
if (randomInt - 1 == -1) {
return randomInt;
} else {
return randomInt - 1;
}
}
String getDescription() {
String description = null;
Random randomGenerator = new Random();
int desLen = randomGenerator.nextInt(2000);
if (desLen < 50) {
desLen = desLen + 100;
}
int worldLen = randomGenerator.nextInt(10);
while (desLen > 10) {
String word = generateRandomString(worldLen);
if (description == null) {
description = word;
} else {
description = description + " " + word;
}
desLen = desLen - word.length();
worldLen = randomGenerator.nextInt(10);
}
if (description.length() > 2997) {
description = description.substring(0, 2997);
}
description = description + ".";
return description;
}
}