if (newPerson == null) {
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE, "MODIFY PERSON called with no person data.");
return returnData;
}
// For modify, we should have either a local person GUID or a HDSSID to reference the existing (old) entry.
PersonMatch oldPersonMatch = null;
String personGuid = newPerson.getPersonGuid();
if (personGuid != null) {
oldPersonMatch = this.get(personGuid);
if (oldPersonMatch == null) {
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE, "MODIFY PERSON GUID {0} not found.", personGuid);
return returnData;
}
} else {
List<PersonIdentifier> piList = newPerson.getPersonIdentifierList();
if (piList != null && !piList.isEmpty()) {
for (PersonIdentifier pi : piList) {
if (pi.getIdentifierType() == PersonIdentifier.Type.kisumuHdssId) {
String hdssId = pi.getIdentifier();
if (hdssId != null && !hdssId.isEmpty()) {
oldPersonMatch = hdssIdMap.get(hdssId);
if (oldPersonMatch != null) {
break;
}
}
}
}
}
}
if (oldPersonMatch == null) {
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE, "MODIFY PERSON called with no person GUID or matching HDSSID.");
return returnData;
}
SearchHistory.update(req, oldPersonMatch, newPerson); // Log the search result (if any) BEFORE modifying the person.
int dbPersonId = oldPersonMatch.getDbPersonId();
Person oldPerson = oldPersonMatch.getPerson();
Connection conn = Sql.connect();
String sex = ValueMap.SEX.getDb().get(newPerson.getSex());
String villageId = Sql.getVillageId(conn, newPerson.getVillageName());
String maritalStatusId = Sql.getMaritalStatusId(conn, newPerson.getMaritalStatus());
String consentSigned = ValueMap.CONSENT_SIGNED.getDb().get(newPerson.getConsentSigned());
int columnCount = 0;
String sql = "UPDATE person SET\n";
if (newPerson.getFirstName() != null) {
if (newPerson.getFirstName().isEmpty()) {
sql += (separate(columnCount) + "first_name = NULL\n");
} else {
sql += (separate(columnCount) + "first_name = " + Sql.quote(newPerson.getFirstName()) + "\n");
}
columnCount++;
}
if (newPerson.getMiddleName() != null) {
if (newPerson.getMiddleName().isEmpty()) {
sql += (separate(columnCount) + "middle_name = NULL\n");
} else {
sql += (separate(columnCount) + "middle_name = " + Sql.quote(newPerson.getMiddleName()) + "\n");
}
columnCount++;
}
if (newPerson.getLastName() != null) {
if (newPerson.getLastName().isEmpty()) {
sql += (separate(columnCount) + "last_name = NULL\n");
} else {
sql += (separate(columnCount) + "last_name = " + Sql.quote(newPerson.getLastName()) + "\n");
}
columnCount++;
}
if (newPerson.getOtherName() != null) {
if (newPerson.getOtherName().isEmpty()) {
sql += (separate(columnCount) + "other_name = NULL\n");
} else {
sql += (separate(columnCount) + "other_name = " + Sql.quote(newPerson.getOtherName()) + "\n");
}
columnCount++;
}
if (newPerson.getClanName() != null) {
if (newPerson.getClanName().isEmpty()) {
sql += (separate(columnCount) + "clan_name = NULL\n");
} else {
sql += (separate(columnCount) + "clan_name = " + Sql.quote(newPerson.getClanName()) + "\n");
}
columnCount++;
}
if (newPerson.getSex() != null) {
sql += (separate(columnCount) + "sex = " + Sql.quote(sex) + "\n");
columnCount++;
}
if (newPerson.getBirthdate() != null) {
sql += (separate(columnCount) + "birthdate = " + Sql.quote(newPerson.getBirthdate()) + "\n");
columnCount++;
}
if (newPerson.getDeathdate() != null) {
sql += (separate(columnCount) + "deathdate = " + Sql.quote(newPerson.getDeathdate()) + "\n");
columnCount++;
}
if (newPerson.getMothersFirstName() != null) {
if (newPerson.getMothersFirstName().isEmpty()) {
sql += (separate(columnCount) + "mothers_first_name = NULL\n");
} else {
sql += (separate(columnCount) + "mothers_first_name = " + Sql.quote(newPerson.getMothersFirstName()) + "\n");
}
columnCount++;
}
if (newPerson.getMothersMiddleName() != null) {
if (newPerson.getMothersMiddleName().isEmpty()) {
sql += (separate(columnCount) + "mothers_middle_name = NULL\n");
} else {
sql += (separate(columnCount) + "mothers_middle_name = " + Sql.quote(newPerson.getMothersMiddleName()) + "\n");
}
columnCount++;
}
if (newPerson.getMothersLastName() != null) {
if (newPerson.getMothersLastName().isEmpty()) {
sql += (separate(columnCount) + "mothers_last_name = NULL\n");
} else {
sql += (separate(columnCount) + "mothers_last_name = " + Sql.quote(newPerson.getMothersLastName()) + "\n");
}
columnCount++;
}
if (newPerson.getFathersFirstName() != null) {
if (newPerson.getFathersFirstName().isEmpty()) {
sql += (separate(columnCount) + "fathers_first_name = NULL\n");
} else {
sql += (separate(columnCount) + "fathers_first_name = " + Sql.quote(newPerson.getFathersFirstName()) + "\n");
}
columnCount++;
}
if (newPerson.getFathersMiddleName() != null) {
if (newPerson.getFathersMiddleName().isEmpty()) {
sql += (separate(columnCount) + "fathers_middle_name = NULL\n");
} else {
sql += (separate(columnCount) + "fathers_middle_name = " + Sql.quote(newPerson.getFathersMiddleName()) + "\n");
}
columnCount++;
}
if (newPerson.getFathersLastName() != null) {
if (newPerson.getFathersLastName().isEmpty()) {
sql += (separate(columnCount) + "fathers_last_name = NULL\n");
} else {
sql += (separate(columnCount) + "fathers_last_name = " + Sql.quote(newPerson.getFathersLastName()) + "\n");
}
columnCount++;
}
if (newPerson.getCompoundHeadFirstName() != null) {
if (newPerson.getCompoundHeadFirstName().isEmpty()) {
sql += (separate(columnCount) + "compoundhead_first_name = NULL\n");
} else {
sql += (separate(columnCount) + "compoundhead_first_name = " + Sql.quote(newPerson.getCompoundHeadFirstName()) + "\n");
}
columnCount++;
}
if (newPerson.getCompoundHeadMiddleName() != null) {
if (newPerson.getCompoundHeadMiddleName().isEmpty()) {
sql += (separate(columnCount) + "compoundhead_middle_name = NULL\n");
} else {
sql += (separate(columnCount) + "compoundhead_middle_name = " + Sql.quote(newPerson.getCompoundHeadMiddleName()) + "\n");
}
columnCount++;
}
if (newPerson.getCompoundHeadLastName() != null) {
if (newPerson.getCompoundHeadLastName().isEmpty()) {
sql += (separate(columnCount) + "compoundhead_last_name = NULL\n");
} else {
sql += (separate(columnCount) + "compoundhead_last_name = " + Sql.quote(newPerson.getCompoundHeadLastName()) + "\n");
}
columnCount++;
}
if (newPerson.getVillageName() != null) {
if (newPerson.getVillageName().isEmpty()) {
sql += (separate(columnCount) + "village_id = NULL\n");
} else {
sql += (separate(columnCount) + "village_id = " + Sql.quote(villageId) + "\n");
}
columnCount++;
}
if (newPerson.getMaritalStatus() != null) {
sql += (separate(columnCount) + "marital_status = " + Sql.quote(maritalStatusId) + "\n");
columnCount++;
}
if (newPerson.getConsentSigned() != null) {
sql += (separate(columnCount) + "consent_signed = " + Sql.quote(consentSigned) + "\n");
columnCount++;
}
sql += " WHERE person_id = " + dbPersonId;
if (columnCount != 0) {//some 'real' mpi data has changed and needs to be updated
Sql.startTransaction(conn);
Sql.execute(conn, sql);
List<PersonIdentifier> pList = PersonIdentifierList.update(conn, dbPersonId, newPerson.getPersonIdentifierList(), oldPerson.getPersonIdentifierList());
newPerson.setPersonIdentifierList(pList);
List<Fingerprint> fList = FingerprintList.update(conn, dbPersonId, newPerson.getFingerprintList(), oldPerson.getFingerprintList());
newPerson.setFingerprintList(fList);
VisitList.update(conn, Sql.REGULAR_VISIT_TYPE_ID, dbPersonId, newPerson.getLastRegularVisit());
VisitList.update(conn, Sql.ONE_OFF_VISIT_TYPE_ID, dbPersonId, newPerson.getLastOneOffVisit());
if (newPerson.getLastRegularVisit() == null) {
newPerson.setLastRegularVisit(oldPerson.getLastRegularVisit());
}
if (newPerson.getLastOneOffVisit() == null) {
newPerson.setLastOneOffVisit(oldPerson.getLastOneOffVisit());
}
Sql.commit(conn);
}
Sql.close(conn);
if (newPerson.getLastMoveDate() != null) {
newPerson.setPreviousVillageName(oldPerson.getVillageName());
}
Person mergedPerson = merge(newPerson, oldPersonMatch.getPerson());//merge old and new person
PersonMatch newPersonMatch = new PersonMatch(mergedPerson);
newPersonMatch.setDbPersonId(dbPersonId);
this.remove(oldPersonMatch); // Remove old person from our in-memory list.
this.add(newPersonMatch); // Add new person to our in-memory list.
Notifier.notify(mergedPerson);
if (returnData != null) {
List<Person> returnList = new ArrayList<Person>();