package pt.utl.ist.scripts.runOnce.student; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.contacts.EmailAddress; import net.sourceforge.fenixedu.domain.contacts.MobilePhone; import net.sourceforge.fenixedu.domain.contacts.Phone; import pt.utl.ist.scripts.commons.AtomicScript; public class PersonSimilarityAnalyser extends AtomicScript { @Override protected void run() throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String command; System.out.println("\n\n\n» Ultimate Clone Analyser «\n\n"); System.out.println("Type IST Usernames (ist1xxxxx ist4xxxx):"); while ((command = in.readLine()) != null) { if (command.equals("quit")) { return; } String[] args = command.split(" "); if (args.length != 2) { System.out.println("Invalid command: " + command); continue; } Person guy1 = Person.readPersonByUsername(args[0]); Person guy2 = Person.readPersonByUsername(args[1]); if (guy1 == null || guy2 == null) { System.out.println("Invalid command: " + command); continue; } analyseMatch(guy1, guy2); System.out.println("Type student number:"); } } private void analyseMatch(Person p1, Person p2) { System.out.println("\n\n Matching Profile:\t\t" + p1.getIstUsername() + "\t\t\t\t" + p2.getIstUsername() + "\n"); if (nameMatch(p1, p2)) { System.out.println("\t\tName:" + "\t\t" + p1.getName() + "\t\t" + p2.getName()); } if (bdayMatch(p1, p2)) { System.out.println("\t\tBirthday:" + "\t" + p1.getDateOfBirthYearMonthDay().toString("dd-MM-yyyy") + "\t\t\t\t" + p2.getDateOfBirthYearMonthDay().toString("dd-MM-yyyy")); } if (irsMatch(p1, p2)) { System.out.println("\t\tIRS:" + "\t\t" + p1.getSocialSecurityNumber() + "\t\t\t\t" + p2.getSocialSecurityNumber()); } boolean firstLine = true; for (Entry entry : emailMatch(p1, p2).entrySet()) { if (firstLine) { System.out.println("\t\tEmails:" + "\t\t" + entry.getKey() + "\t\t\t" + entry.getValue()); } firstLine = false; System.out.println("\t\t\t\t" + entry.getKey() + "\t\t\t" + entry.getValue()); } firstLine = true; for (Entry entry : phoneMatch(p1, p2).entrySet()) { if (firstLine) { System.out.println("\t\tPhones:" + "\t\t" + entry.getKey() + "\t\t\t\t" + entry.getValue()); } firstLine = false; System.out.println("\t\t\t\t" + entry.getKey() + "\t\t\t\t" + entry.getValue()); } firstLine = true; for (Entry entry : cellMatch(p1, p2).entrySet()) { if (firstLine) { System.out.println("\t\tMobiles:" + "\t" + entry.getKey() + "\t\t\t\t" + entry.getValue()); } firstLine = false; System.out.println("\t\t\t\t" + entry.getKey() + "\t\t\t\t" + entry.getValue()); } if (addressMatch(p1, p2)) { System.out.println("\t\tAddress:" + "\t" + p1.getAddress() + "\t" + p2.getAddress()); } if (dadNameMatch(p1, p2)) { System.out.println("\t\tDadName:" + "\t" + p1.getNameOfFather() + "\t\t" + p2.getNameOfFather()); } if (momNameMatch(p1, p2)) { System.out.println("\t\tMomName:" + "\t" + p1.getNameOfMother() + "\t\t" + p2.getNameOfMother()); } System.out.println("\n\n"); } private boolean nameMatch(Person p1, Person p2) { if (p1.getName() == null) { return false; } if (p2.getName() == null) { return false; } return p1.getName().equals(p2.getName()); } private boolean bdayMatch(Person p1, Person p2) { if (p1.getDateOfBirthYearMonthDay() == null) { return false; } if (p2.getDateOfBirthYearMonthDay() == null) { return false; } return p1.getDateOfBirthYearMonthDay().equals(p2.getDateOfBirthYearMonthDay()); } private boolean irsMatch(Person p1, Person p2) { if (p1.getSocialSecurityNumber() == null) { return false; } if (p2.getSocialSecurityNumber() == null) { return false; } return p1.getSocialSecurityNumber().equals(p2.getSocialSecurityNumber()); } private Map emailMatch(Person p1, Person p2) { Map results = new HashMap(); for (EmailAddress email1 : p1.getEmailAddresses()) { for (EmailAddress email2 : p2.getEmailAddresses()) { if (email1.getPresentationValue().equals(email2.getPresentationValue())) { results.put(email1.getPresentationValue(), email2.getPresentationValue()); } } } return results; } private Map phoneMatch(Person p1, Person p2) { Map results = new HashMap(); for (Phone phone1 : p1.getPhones()) { for (Phone phone2 : p2.getPhones()) { if (phone1.getPresentationValue().equals(phone2.getPresentationValue())) { results.put(phone1.getPresentationValue(), phone2.getPresentationValue()); } } } return results; } private Map cellMatch(Person p1, Person p2) { Map results = new HashMap(); for (MobilePhone cell1 : p1.getMobilePhones()) { for (MobilePhone cell2 : p2.getMobilePhones()) { if (cell1.getPresentationValue().equals(cell2.getPresentationValue())) { results.put(cell1.getPresentationValue(), cell2.getPresentationValue()); } } } return results; } private boolean addressMatch(Person p1, Person p2) { if (p1.getAddress() == null) { return false; } if (p2.getAddress() == null) { return false; } return p1.getAddress().equals(p2.getAddress()); } private boolean dadNameMatch(Person p1, Person p2) { if (p1.getNameOfFather() == null) { return false; } if (p2.getNameOfFather() == null) { return false; } return p1.getNameOfFather().equals(p2.getNameOfFather()); } private boolean momNameMatch(Person p1, Person p2) { if (p1.getNameOfMother() == null) { return false; } if (p2.getNameOfMother() == null) { return false; } return p1.getNameOfMother().equals(p2.getNameOfMother()); } public static void main(String[] args) { process(new PersonSimilarityAnalyser()); System.exit(0); } }