@QueryParam("u") @DefaultValue("") String username,
@QueryParam("e") @DefaultValue("") String email
) {
JSONObject jsonResult = new JSONObject();
User patient = null;
boolean error = false;
if (!username.isEmpty()) {
//query on username
PersistenceService persistenceSvc = PersistenceService.getInstance();
try {
EntityManager em = PersistenceService.getInstance().getEntityManager();
try {
persistenceSvc.beginTx();
patient = (User) em.createNamedQuery("User.findByUsername")
.setParameter("username", username)
.setMaxResults(1)
.getSingleResult();
persistenceSvc.commitTx();
logger.debug("Found User by username: {}", patient);
}
catch (NoResultException ex) {
logger.error("Unable to find User object for username: {}", username);
error = true;
jsonResult.put("error", "Unable to find user with that name.");
}
}
catch (Exception ex) {
logger.error("requestAccess encountered exception: {}", ex);
error = true;
try {
jsonResult.put("error", "Unable to find user with that name.");
}
catch (JSONException ex2) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
} finally {
persistenceSvc.close();
}
} else if (!email.isEmpty()) {
//query on email
PersistenceService persistenceSvc = PersistenceService.getInstance();
try {
EntityManager em = persistenceSvc.getEntityManager();
try {
persistenceSvc.beginTx();
patient = (User) em.createNamedQuery("User.findByEmail")
.setParameter("email", email)
.setMaxResults(1)
.getSingleResult();
persistenceSvc.commitTx();
logger.debug("Found User by email: {}", patient);
}
catch (NoResultException ex) {
error = true;
logger.error("Unable to find User object for email: {}", email);
jsonResult.put("error", "Unable to find user with that email address.");
}
}
catch (Exception ex) {
error = true;
logger.error("requestAccess encountered exception: {}", ex);
try {
jsonResult.put("error", "Unable to find user with that email address.");
}
catch (JSONException ex2) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
} finally {
persistenceSvc.close();
}
} else {
//no params -- cant do anything
error = true;
try {
jsonResult.put("error", "Invalid search parameters.");
}
catch (JSONException ex) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
}
//check that we don't already have an outstanding requests for this patient
if (!error) { //dont bother if we've already got issues
PersistenceService persistenceSvc = PersistenceService.getInstance();
List<HealthrecordRequest> requests = null;
try {
EntityManager em = persistenceSvc.getEntityManager();
try {
//grab all existing requests made by this caretaker
requests = (List<HealthrecordRequest>) em.createNamedQuery("HealthrecordRequest.findByUserIdRequestor")
.setParameter("userIdRequestor", getLocalUser().getUserId())
.getResultList();
}
catch (NoResultException ex) {
//ignore -- this is fine
}
}
catch (Exception ex) {
error = true;
logger.error("requestAccess encountered exception: {}", ex);
try {
jsonResult.put("error", "Unable to lookup current outstanding requests.");
}
catch (JSONException ex2) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
} finally {
persistenceSvc.close();
}
if ((requests != null) && (!error)) {
for (HealthrecordRequest hrr : requests) {
//check each request's requested record id - does it match the current request attempt?
if (hrr.getRecIdRequested() == patient.getPrimaryHealthRecord().getHealthRecordId()) {
error = true;
try {
jsonResult.put("error", "A request for this patient's data already exists.");
}
catch (JSONException ex2) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
}
}
}
}
//check patient is not already under our care
if (!error) {
for (User u : patient.getPrimaryHealthRecord().getUserList()) {
if (u.getUserId().compareTo(getLocalUser().getUserId()) == 0) {
error = true;
try {
jsonResult.put("error", "Patient is already under your care.");
}
catch (JSONException ex) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);
}
}
}
}
//createDbRequest does the real work
if (!error && createDbRequest(patient.getPrimaryHealthRecord().getHealthRecordId() )) {
try {
jsonResult.put("success", "Your request has been sent.");
}
catch (JSONException ex) {
throw new WebApplicationException(Response.Status.PRECONDITION_FAILED);