Package com.krminc.phr.domain

Examples of com.krminc.phr.domain.User


     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
    protected Address updateEntity(Address entity, Address newEntity) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        User user = entity.getUser();
        User userNew = newEntity.getUser();
        entity = em.merge(newEntity);
        if (user != null && !user.equals(userNew)) {
            user.getAddresses().remove(entity);
        }
        if (userNew != null && !userNew.equals(user)) {
            userNew.getAddresses().add(entity);
        }
        return entity;
    }
View Full Code Here


     *
     * @param entity the entity to deletle
     */
    protected void deleteEntity(Address entity) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        User user = entity.getUser();
        if (user != null) {
            user.getAddresses().remove(entity);
        }
        em.remove(entity);
    }
View Full Code Here

            PersistenceService persistenceSvc = PersistenceService.getInstance();

            try {
                EntityManager em = PersistenceService.getInstance().getEntityManager();
                User self = null;

                try {
                    persistenceSvc.beginTx();

                    //get user
                     self = getLocalUser(); //requires tx started

                     //find the correct hr
                     List<HealthRecord> healthRecords = self.getHealthRecords();
                     boolean shouldRemove = false;
                     HealthRecord toRemove = null;

                     for (HealthRecord hr : healthRecords) {
                         //prepare to remove link
                         if (healthRecordIdToRemove.compareTo(hr.getHealthRecordId()) == 0) {
                             toRemove = hr;
                             logger.debug("Ready to remove healthRecord {} from user {}", hr, self);
                             shouldRemove = true;
                         }
                     }

                     if (shouldRemove) {
                        healthRecords.remove(toRemove);
                        self.setHealthRecords(healthRecords);
                        em.flush();
                        persistenceSvc.commitTx();
                        returnType = true;
                     } else {
                         returnType = false;
View Full Code Here

        @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);
View Full Code Here

    public Response post(UserConverter data) {
        PersistenceService persistenceSvc = PersistenceService.getInstance();
        try {
            persistenceSvc.beginTx();
            EntityManager em = persistenceSvc.getEntityManager();
            User entity = data.resolveEntity(em);
            createEntity(data.resolveEntity(em));
            persistenceSvc.commitTx();
            return Response.created(uriInfo.getAbsolutePath().resolve(entity.getUserId() + "/")).build();
        } finally {
            persistenceSvc.close();
        }
    }
View Full Code Here

     */
    protected void createEntity(HealthRecord entity) {
        entity.setHealthRecordId(null);
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        em.persist(entity);
        User user = entity.getUser();
        if (user != null) {
            user.getHealthRecords().add(entity);
        }
    }
View Full Code Here

     */
    protected void createEntity(Address entity) {
        entity.setAddressId(null);
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        em.persist(entity);
        User user = entity.getUser();
        if (user != null) {
            user.getAddresses().add(entity);
        }
    }
View Full Code Here

     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
    protected HealthRecord updateEntity(HealthRecord entity, HealthRecord newEntity) {
        EntityManager em = PersistenceService.getInstance().getEntityManager();
        User user = entity.getUser();
        User userNew = newEntity.getUser();
        entity = em.merge(newEntity);
        if (user != null && !user.equals(userNew)) {
            user.getHealthRecords().remove(entity);
        }
        if (userNew != null && !userNew.equals(user)) {
            userNew.getHealthRecords().add(entity);
        }
        return entity;
    }
View Full Code Here

    private URI uri;
    private int expandLevel;
 
    /** Creates a new instance of UserConverter */
    public UserConverter() {
        entity = new User();
    }
View Full Code Here

     * Returns the resolved HealthRecord entity.
     *
     * @return an resolved entity
     */
    public HealthRecord resolveEntity(EntityManager em) {
        User user = entity.getUser();
        if (user != null) {
            entity.setUser(em.getReference(User.class, user.getUserId()));
        }
        return entity;
    }
View Full Code Here

TOP

Related Classes of com.krminc.phr.domain.User

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.