) {
JSONObject jSONObject = new JSONObject();
//Add the link to hr->user linkage table
PersistenceService persistenceSvc = PersistenceService.getInstance();
EntityManager em = persistenceSvc.getEntityManager();
String approvalRequest = new String();
try {
if (! securityContext.isUserInRole(UserConfig.ROLE_PATIENT)) throw new Exception("Not in patient role for approval request");
persistenceSvc.beginTx();
HealthrecordRequest approvedRequest = em.find(HealthrecordRequest.class, requestId);
//ensure we are linking a request that is of our own record
if (approvedRequest.getRecIdRequested() == getEntity().getHealthRecordId()) {
//find the user we are giving access to hr for
User approvedUser = em.find(
User.class,
new Long(approvedRequest.getUserIdRequestor())
);
//find the hrid we want to give access to
HealthRecord approvedRecord = em.find(
HealthRecord.class,
new Long(approvedRequest.getRecIdRequested())
);
//add the healthrecord to user object
List<HealthRecord> currentRecords = approvedUser.getHealthRecords();
currentRecords.add(approvedRecord);
approvedUser.setHealthRecords(currentRecords);
//delete the request now that it is satisfied
em.remove(approvedRequest);
}
persistenceSvc.commitTx();
approvalRequest = "success";
} catch (Exception ex) {
logger.error("Exception encountered processing approval request: {}", ex);
approvalRequest = "error";
} finally {
persistenceSvc.close();
}
try {
jSONObject.put("status", approvalRequest);
} catch (JSONException ex) {