-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunR.java
More file actions
483 lines (411 loc) · 18 KB
/
funR.java
File metadata and controls
483 lines (411 loc) · 18 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package main;
import main.BiologicalRelation;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
/*This class contains all the required Reporting
* functions of the program it's been called
* into the App.java file*/
public class funR {
ArrayList<PersonIdentity> allNodes;
//this method retrives all the people from the database and creates the node
void getPersonIdfromDb() throws SQLException {
allNodes = new ArrayList<PersonIdentity>();
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this code creates a new node for every person in the database
rs = st.executeQuery("select PersonId from FamilyTree");
//adding each node into the arraylist
while (rs.next()){
PersonIdentity n = new PersonIdentity(Integer.parseInt(rs.getString(1)));
allNodes.add(n);
}
}
//this method creates the tree from the database to derive the relations
void treeCreationfromDb() throws SQLException {
//setting up the connection
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this function gets nodes from the tree
getPersonIdfromDb();
//adding joins of the each member
rs= st.executeQuery("Select * from treeCreation");
//this lines of code will compare the Ids from database with allNodes
while (rs.next()){
int val=0;
int parent=Integer.parseInt(rs.getString(1));
int child=Integer.parseInt(rs.getString(2));
//traversing through each and every node
for(int i=0;i<allNodes.size();i++){
if(parent == allNodes.get(i).PersonId) {
for (int j = 0; j < allNodes.size(); j++) {
//when the child node matches it assigns the value to parent child and also checks
// for having two parents
if (child == allNodes.get(j).PersonId) {
if(allNodes.get(j).parent1==null){
allNodes.get(i).child.add(allNodes.get(j)) ;
allNodes.get(j).parent1= allNodes.get(i);
}else if(allNodes.get(j).parent2==null){
allNodes.get(i).child.add(allNodes.get(j)) ;
allNodes.get(j).parent2= allNodes.get(i);
}
else {
//if User tries to insert 3rd parent
System.out.println("Both parents occupide INVALId OPERATION");
}
break;
}
}
}
}
}
}
//this function will find the PersonIdentity from the name
PersonIdentity findPerson( String name ){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//getting the person's Id from his name
rs = st.executeQuery("select PersonId from FamilyTree where Name = '"+name+"'");
rs.next();
//this will set up the nodes in array list
getPersonIdfromDb();
for(int i=0;i<allNodes.size();i++){
if(allNodes.get(i).PersonId == Integer.parseInt(rs.getString(1))){
return allNodes.get(i);
}
}
return null;
} catch (Exception e) {
return null;
}
}
//this function will return the FileIdentifier from the name of the file
FileIdentifier findMediaFile( String name ){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this query will get the id of media file from the database
rs = st.executeQuery("select MediaId from MediaArchive where Name = '"+name+"'");
rs.next();
return new FileIdentifier(Integer.parseInt(rs.getString(1)));
} catch (Exception e) {
return null;
}
}
//this function will retrive the id from the name
String findName( PersonIdentity id ){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this will retrive the name from the ID
rs = st.executeQuery("select Name from FamilyTree where PersonId = '"+id.PersonId+"'");
rs.next();
return rs.getString(1);
} catch (Exception e) {
return null;
}
}
//this method will get the media file from the fileId
String findMediaFile( FileIdentifier fileId ){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this query will retrive the media name from it's Id
rs = st.executeQuery("select Name from MediaArchive where MediaId = '"+fileId.FileId+"'");
rs.next();
return rs.getString(1);
} catch (Exception e) {
return null;
}
}
/*this lists will be used to store the
* outputs of every nodes later in the program
* and find interconnections by comparing them*/
List<Integer> temp = new ArrayList<>();
List<Integer> temp1 = new ArrayList<>();
List<List<Integer> > finAns = new ArrayList<>();
//this code will find all the parents for any particular node
List<Integer> ParentFinder(PersonIdentity person){
//adding the id of current node
temp.add(person.PersonId);
//till the person have parent1, traversing to them
if((person.parent1 == null && person.parent2!= null) ){
//adding the ids for current recursive iteration, will be
// used later if the future iteration will fail, this will
//remain as a backup
temp1.add(person.parent2.PersonId);
}
//till the person have parent1, traversing to them
if (person.parent1 != null && person.parent2 == null){
//adding the ids for current recursive iteration, will be
// used later if the future iteration will fail, this will
//remain as a backup
temp1.add(person.parent1.PersonId);
}
//calling recursive function by passing the prent
if(person.parent1 !=null){
temp=ParentFinder(person.parent1);
}
//calling recursive function by passing the prent
if(person.parent2 !=null){
temp=ParentFinder(person.parent2);
}
if(person.parent1 == null && person.parent2 == null){
//returning the final temp
finAns.add(temp);
return temp1;
}
return null;
}
//this function will find the relationship between two people, it will use ParentFinder recursive
// function and treeCreationfromDb() to achieve this goal
BiologicalRelation findRelation( PersonIdentity person1, PersonIdentity person2 ){
try {
//first of all it creates tree from the database
treeCreationfromDb();
List<List<Integer>> p1 = new ArrayList<>();
List<List<Integer>> p2 = new ArrayList<>();
//if there is no relation then it will show -2,-2
int degofCous=-2;
int degOfRem=-2;
//if the person1's Id and the id of any node will math then that node
// will be passed into parentFinder program
for (PersonIdentity allNode : allNodes) {
if (Objects.equals(allNode.PersonId, person1.PersonId)) {
System.out.println("yes");
temp1.add(allNode.PersonId);
ParentFinder(allNode);
temp = new ArrayList<>();
temp1 = new ArrayList<>();
p1 = finAns;
finAns = new ArrayList<>();
break;
}
}
//if the person1's Id and the id of any node will math then that node
// will be passed into parentFinder program
for (PersonIdentity allNode : allNodes) {
if (Objects.equals(person2.PersonId, allNode.PersonId)) {
temp1.add(allNode.PersonId);
ParentFinder(allNode);
temp = new ArrayList<>();
temp1 = new ArrayList<>();
p2 = finAns;
finAns = new ArrayList<>();
break;
}
}
//this function will find the intersection and calculate
// the
for (List<Integer> integers : p1) {
for (List<Integer> integerList : p2) {
for (int k = 0; k < integers.size(); k++) {
for (int l = 0; l < integerList.size(); l++) {
if (Objects.equals(integers.get(k), integerList.get(l))) {
//this eqn derives the values of deg of cousinship and
// deg of removal
degofCous = (Math.min(k, l) - 1);
degOfRem = Math.abs(k - l);
}
}
}
}
}
//returning the new BiologicalRelation() object with required values
return new BiologicalRelation(degofCous,degOfRem,findName(person1),findName(person2));
} catch (Exception e) {
return null;
}
}
ArrayList<Integer> al1 = new ArrayList<Integer>();
//recursive function to find decendents
void decRecurr(PersonIdentity person, Integer gene){
al1.add(person.PersonId);
//recursion till the given max generation
for(PersonIdentity p : person.child){
if (gene != 0) {
decRecurr(p, gene - 1);
}
}
}
//this will return the set of personIdentity of all the people available
// in the given generation
Set<PersonIdentity> descendents( PersonIdentity person, Integer generations ){
try {
Set<PersonIdentity> temp = new HashSet<>();
//creation of the tree
treeCreationfromDb();
//whenever the node will match it will pass that into the recursive function
for (PersonIdentity allNode : allNodes) {
if (Objects.equals(person.PersonId, allNode.PersonId)) {
decRecurr(allNode, generations);
break;
}
}
//adding all the people returned by the recursive function
for (Integer integer : al1) {
temp.add(new PersonIdentity(integer));
}
al1 = new ArrayList<>();
return temp;
} catch (Exception e) {
return null;
}
}
ArrayList<Integer> al = new ArrayList<Integer>();
//recursive function to find ancestors
void ancestorRecurr(PersonIdentity person, Integer gene){
al.add(person.PersonId);
//recursion till any parent is available
if(person.parent1 != null && gene !=0){
ancestorRecurr(person.parent1,gene-1);
}
if(person.parent2 != null && gene !=0){
ancestorRecurr(person.parent2,gene-1);
}
}
//this will return the set of personIdentity of all the people available
// in the given generation
Set<PersonIdentity> ancestores( PersonIdentity person, Integer generations ){
try {
Set<PersonIdentity> temp = new HashSet<>();
//creation of the tree
treeCreationfromDb();
//whenever the node will match it will pass that into the recursive function
for (PersonIdentity allNode : allNodes) {
if (Objects.equals(person.PersonId, allNode.PersonId)) {
ancestorRecurr(allNode, generations);
break;
}
}
//adding all the people returned by the recursive function
for (Integer integer : al) {
temp.add(new PersonIdentity(integer));
}
al = new ArrayList<>();
return temp;
} catch (Exception e) {
return null;
}
}
//this function will return the notes and references of the user
List<String> notesAndReferences( PersonIdentity person ){
try {
//don't forget to sequence them.
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//retriving the notes and references of the person
rs = st.executeQuery("select details from notesAndRef where PersonId = '"+person.PersonId+"'");
List<String> NandR = new ArrayList<String>();
//storing them into the db
while (rs.next()){
NandR.add(rs.getString(1));
}
return NandR;
} catch (Exception e) {
return null;
}
}
//this function will find media by tags and within given time frame
Set<FileIdentifier> findMediaByTag( String tag , String startDate, String endDate){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this query will retrive the mediaId from the tag and given dates
rs = st.executeQuery("select MediaId from MediaArchive where Tags like '%"+tag+"%' AND Date between '"+startDate+"' AND '"+endDate+"'");
Set<FileIdentifier> Ids = new HashSet<FileIdentifier>();
//storing the resultset into the set
while (rs.next()){
Ids.add(new FileIdentifier(Integer.parseInt(rs.getString(1))));
}
return Ids;
} catch (Exception e) {
System.out.println("masti");
return null;
}
}
//this function will find media by location and within given time frame
Set<FileIdentifier> findMediaByLocation( String location, String startDate, String endDate){
try {
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//this query will retrive the mediaId from the location and given dates
rs = st.executeQuery("select MediaId from MediaArchive where Location ='"+ location +"'AND Date between '"+startDate+"' AND '"+endDate+"'");
Set<FileIdentifier> Ids = new HashSet<FileIdentifier>();
//adding the resultset into the set
while (rs.next()){
Ids.add(new FileIdentifier(Integer.parseInt(rs.getString(1))));
}
return Ids;
} catch (Exception e) {
return null;
}
}
// this function will find media by people and within given time frame
List<FileIdentifier> findIndividualsMedia( Set<PersonIdentity> people, String startDate, String endDate){
try {
//the query size will be indefinite so we need to use temp string
String temp = "select distinct peopleInMedia.MediaId,count(distinct peopleInMedia.PersonId) as count from peopleInMedia Inner join MediaArchive as ma On peopleInMedia.MediaId= ma.MediaId where ";
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs=null;
//adding the part of the query for every person
for (PersonIdentity f: people){
temp+= "peopleInMedia.PersonId ='"+f.PersonId+"' or ";
}
temp=temp.substring(0,temp.length()-4);
//ending the query by adding time limits
temp+=" And Date between '"+startDate+"' AND '"+endDate+"'";
rs = st.executeQuery(temp);
List<FileIdentifier> media = new ArrayList<FileIdentifier>();
//adding the new objects of fileIdentifier into the list and returning it
while (rs.next()){
media.add(new FileIdentifier(Integer.parseInt(rs.getString(1))));
}
return media;
} catch (Exception e) {
return null;
}
}
// this function will find media by a single person
List<FileIdentifier> findBiologicalFamilyMedia(PersonIdentity person){
try {
List<FileIdentifier> media = new ArrayList<FileIdentifier>();
App.DbConnection db = new App.DbConnection();
Connection conn= db.createDBConnection();
Statement st= conn.createStatement();
ResultSet rs= null;
//this query will return the distinct media ids of the people
rs = st.executeQuery("select distinct MediaId from peopleInMedia where PersonId like '%"+person.PersonId+"%'");
//adding the new objects of fileIdentifier into the list and returning it
while (rs.next()){
media.add(new FileIdentifier(Integer.parseInt(rs.getString(1))));
}
return media;
} catch (Exception e) {
return null;
}
}
}