PersonResponse returnData = null;
if (req.isResponseRequested()) { // Has the client requested a response?
returnData = new PersonResponse();
returnData.setSuccessful(false); // Until we succeed, assume that we failed.
}
Person p = req.getPerson().clone(); // Clone because we may modify our copy below.
if (p == null) {
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE, "CREATE PERSON called with no person data.");
return returnData;
}
//Check to see if this person's hdssid, if available, already exists in the mpi. Log error if it does.
String existingId = null;
if (p.getPersonIdentifierList() != null
&& !p.getPersonIdentifierList().isEmpty()) {
for (PersonIdentifier personIdentifier : p.getPersonIdentifierList()) {
if (personIdentifier.getIdentifierType() == PersonIdentifier.Type.kisumuHdssId) {
String pi = personIdentifier.getIdentifier();
if (pi != null && !pi.isEmpty()) {
if (hdssIdMap.containsKey(pi)) {
existingId = pi;
break;
}
}
}
}
}
if (existingId != null) {
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE,
"CREATE PERSON called with existing kisumuhdssid person identifier {0}.", existingId);
return returnData;
}
Connection conn = Sql.connect();
ResultSet rs = Sql.query(conn, "SELECT UUID() AS uuid");
String guid = null;
try {
rs.next();
guid = rs.getString("uuid");
Sql.close(rs);
} catch (SQLException ex) { // Won't happen
Logger.getLogger(PersonList.class.getName()).log(Level.SEVERE, null, ex);
}
p.setPersonGuid(guid);
String sex = ValueMap.SEX.getDb().get(p.getSex());
String villageId = Sql.getVillageId(conn, p.getVillageName());
String maritalStatusId = Sql.getMaritalStatusId(conn, p.getMaritalStatus());
String consentSigned = ValueMap.CONSENT_SIGNED.getDb().get(p.getConsentSigned());
String sql = "INSERT INTO person (person_guid, first_name, middle_name, last_name,\n"
+ " other_name, clan_name, sex, birthdate, deathdate,\n"
+ " mothers_first_name, mothers_middle_name, mothers_last_name,\n"
+ " fathers_first_name, fathers_middle_name, fathers_last_name,\n"
+ " compoundhead_first_name, compoundhead_middle_name, compoundhead_last_name,\n"
+ " village_id, marital_status, consent_signed, date_created) values (\n "
+ Sql.quote(guid) + ", "
+ Sql.quote(p.getFirstName()) + ", "
+ Sql.quote(p.getMiddleName()) + ", "
+ Sql.quote(p.getLastName()) + ",\n "
+ Sql.quote(p.getOtherName()) + ", "
+ Sql.quote(p.getClanName()) + ", "
+ Sql.quote(sex) + ", "
+ Sql.quote(p.getBirthdate()) + ", "
+ Sql.quote(p.getDeathdate()) + ",\n "
+ Sql.quote(p.getMothersFirstName()) + ", "
+ Sql.quote(p.getMothersMiddleName()) + ", "
+ Sql.quote(p.getMothersLastName()) + ",\n "
+ Sql.quote(p.getFathersFirstName()) + ", "
+ Sql.quote(p.getFathersMiddleName()) + ", "
+ Sql.quote(p.getFathersLastName()) + ",\n "
+ Sql.quote(p.getCompoundHeadFirstName()) + ", "
+ Sql.quote(p.getCompoundHeadMiddleName()) + ", "
+ Sql.quote(p.getCompoundHeadLastName()) + ",\n "
+ villageId + ", "
+ maritalStatusId + ", "
+ consentSigned + ", "
+ "NOW()"
+ ");";
Sql.startTransaction(conn);
boolean successful = Sql.execute(conn, sql);
if (successful) {
int dbPersonId = Integer.parseInt(Sql.getLastInsertId(conn));
PersonIdentifierList.update(conn, dbPersonId, p.getPersonIdentifierList(), null);
FingerprintList.update(conn, dbPersonId, p.getFingerprintList(), null);
VisitList.update(conn, Sql.REGULAR_VISIT_TYPE_ID, dbPersonId, p.getLastRegularVisit());
VisitList.update(conn, Sql.ONE_OFF_VISIT_TYPE_ID, dbPersonId, p.getLastOneOffVisit());
PersonMatch newPer = new PersonMatch(p.clone()); // Clone to protect from unit test modifications.
newPer.setDbPersonId(dbPersonId);
this.add(newPer);
SearchHistory.update(req, null, null); // Update search history showing that no candidate was selected.
}
Sql.commit(conn);