Example usage for org.hibernate Query uniqueResult

List of usage examples for org.hibernate Query uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Query uniqueResult.

Prototype

R uniqueResult();

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:au.edu.anu.metadatastores.store.misc.Mappings.java

License:Open Source License

public static RelationMapping getMappingByDescription(String description) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    Query query = session.createQuery("FROM RelationMapping WHERE description = :description");
    query.setParameter("description", description);

    RelationMapping mapping = (RelationMapping) query.uniqueResult();

    session.close();/*w w w  .  jav a  2 s . c o m*/

    return mapping;
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

/**
 * Create a stub of a person with just their name and a staff type of 'Unknown'
 * //from ww  w.j  a  v  a2s  .  com
 * @param givenName The given name of the person
 * @param surname The surname of the person
 * @return The created person object
 */
private Person createBasicPerson(String givenName, String surname) {
    Person person = new Person();
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query idQuery = session.createSQLQuery("SELECT nextval('person_seq')");
        BigInteger id = (BigInteger) idQuery.uniqueResult();

        person.setExtId("mu" + id.toString());
        person.setGivenName(givenName);
        person.setSurname(surname);
        person.setStaffType("Unknown");

        return person;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

/**
 * Find a person by a uid//w w w  .j a  va 2  s. c o m
 * 
 * @param uid The uid of the person to find
 * @return Get the Item of the person with the given uid
 */
private PersonItem queryPersonByUid(String uid) {
    LOGGER.info("uid: {}", uid);
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery("from PersonItem where lower(extId) = :extId");
        query.setParameter("extId", uid.toLowerCase());

        PersonItem personItem = (PersonItem) query.uniqueResult();

        return personItem;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

/**
 * Save the person's information//from w w  w.j  ava  2s.com
 * 
 * @param person The person to save
 * @param userUpdated Indicates whether the information is user updated or not
 * @return The item information for the person
 */
public PersonItem savePerson(Person person, Boolean userUpdated) {
    if (person.getExtId() == null) {
        return null;
    }

    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.enableFilter("attributes");

        Query query = session.createQuery("from PersonItem where extId = :extId");
        query.setParameter("extId", person.getExtId());

        PersonItem item = (PersonItem) query.uniqueResult();
        String title = person.getFullName();

        Date lastModified = new Date();
        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;

        try {
            newItem = parser.getItem(person, userUpdated, lastModified);
        } catch (Exception e) {
            LOGGER.error("Exception transforming person to an item", e);
        }

        if (item == null) {
            item = new PersonItem();
            item.setExtId(person.getExtId().toLowerCase());
            item.setTitle(title);
            item = (PersonItem) session.merge(item);
        } else {
            item.setTitle(title);
        }

        updateAttributesFromItem(item, newItem, session, lastModified);

        item = (PersonItem) session.merge(item);
        session.getTransaction().commit();
        return item;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

/**
 * Retrieve basic information about a person e.g. their name
 * /*  w  ww .j av  a  2  s  .  c  om*/
 * @param uid The unique id of the person to retrieve information about
 * @return The information about the person
 */
public Person getPerson(String uid) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        Transaction transaction = session.beginTransaction();

        Query query = session.createQuery("from PersonItem where extId = :extId");
        query.setParameter("extId", uid);

        PersonItem item = (PersonItem) query.uniqueResult();

        Person person = null;
        if (item != null) {
            person = getPerson(item);
        }
        transaction.commit();

        return person;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

/**
 * Retrieve basic information about a person e.g. their name
 * /* ww  w .  j  ava2  s. co m*/
 * @param uid The unique id of the person to retrieve information about
 * @param extraInfo Indicates whether to retrieve the type, institution, country and organisational unit about the person
 * @return Information about the given person
 */
public Person getBasicPerson(String uid, boolean extraInfo) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        Transaction transaction = session.beginTransaction();

        Query query = session.createQuery(
                "select pi from PersonItem pi join fetch pi.itemAttributes where pi.extId = :extId");
        query.setParameter("extId", uid);

        PersonItem item = (PersonItem) query.uniqueResult();
        Person person = null;
        if (item != null) {
            person = getBasicPerson(item, extraInfo);
        }
        transaction.commit();

        return person;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Save the publication//from  www .j  a v a 2  s .com
 * 
 * @param publication The publication to save
 * @param userUpdated Indicates whether the update is user updated
 * @return The publication item
 */
public PublicationItem savePublication(Publication publication, Boolean userUpdated) {
    if (publication.getTitle() == null || publication.getTitle().trim().length() == 0) {
        return null;
    }

    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.enableFilter("attributes");

        //note this may need to be updated if we retrieve publications from other systems without an aries id
        Query query = session.createQuery(
                "SELECT pi FROM PublicationItem as pi inner join pi.itemAttributes as pia WHERE pia.attrType = :attrType and pia.attrValue = :attrValue");
        query.setParameter("attrType", StoreAttributes.ARIES_ID);
        query.setParameter("attrValue", publication.getAriesId());

        PublicationItem item = (PublicationItem) query.uniqueResult();

        Date lastModified = new Date();
        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;
        try {
            newItem = parser.getItem(publication, userUpdated, lastModified);
        } catch (Exception e) {
            LOGGER.error("Exception transforming grant to an item", e);
        }

        if (item == null) {
            item = new PublicationItem();
            Query idQuery = session.createSQLQuery("SELECT nextval('publication_seq')");
            BigInteger id = (BigInteger) idQuery.uniqueResult();
            item.setExtId("p" + id.toString());
            item.setTitle(publication.getTitle());
            item = (PublicationItem) session.merge(item);
        } else if (publication.getTitle() != null && publication.getTitle().trim().length() > 0) {
            item.setTitle(publication.getTitle());
        }
        updateAttributesFromItem(item, newItem, session, lastModified);

        //TODO remove people who are no longer related
        Item personItem = null;
        ItemRelation itemRelation = null;
        ItemRelationId id = null;
        List<Item> peopleItems = new ArrayList<Item>();
        for (Person person : publication.getAuthors()) {
            personItem = personService_.getPersonItem(person.getUid());
            if (personItem != null) {
                peopleItems.add(personItem);
            } else {
                LOGGER.error("No person found to add relation for id: {}", person.getUid());
            }
        }
        boolean hasPerson = false;
        for (Item item2 : peopleItems) {
            for (ItemRelation relation : item.getItemRelationsForIid()) {
                if (relation.getId().getRelatedIid().equals(item2.getIid())) {
                    hasPerson = true;
                    break;
                }
            }
            if (!hasPerson) {
                itemRelation = new ItemRelation();
                id = new ItemRelationId(item.getIid(), StoreProperties.getProperty("publication.author.type"),
                        item2.getIid());
                itemRelation.setId(id);
                item.getItemRelationsForIid().add(itemRelation);
            }
            hasPerson = false;
        }

        item = (PublicationItem) session.merge(item);

        session.getTransaction().commit();

        return item;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Tries to find an aries record within the database.  If it does not find it it searches the aries database and saves it.
 * /*from   w w w  .jav  a 2  s  .c o  m*/
 * @param ariesId The id of the aries record to return an item for
 * @return The publication item for the given aries id
 */
public PublicationItem getPublicationItem(String ariesId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        session.beginTransaction();

        Query query = session.createQuery(
                "SELECT pi FROM PublicationItem as pi inner join pi.itemAttributes as pia WHERE pia.attrType = :attrType and pia.attrValue = :attrValue");
        query.setParameter("attrType", StoreAttributes.ARIES_ID);
        query.setParameter("attrValue", ariesId);

        PublicationItem item = (PublicationItem) query.uniqueResult();
        if (item != null) {
            return item;
        }

        Publication publication = fetchPublication(ariesId);
        item = savePublication(publication);

        session.getTransaction().commit();

        return item;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Get the publication by the aries id//w  w w  .  j  a v  a 2 s  .c o m
 * 
 * @param ariesId The aries id to get publications for
 * @return The publication
 */
public Publication getPublicationByAriesId(String ariesId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        session.beginTransaction();

        Query query = session.createQuery(
                "SELECT pi FROM PublicationItem as pi inner join pi.itemAttributes as pia WHERE pia.attrType = :attrType and pia.attrValue = :attrValue");
        query.setParameter("attrType", StoreAttributes.ARIES_ID);
        query.setParameter("attrValue", ariesId);

        PublicationItem item = (PublicationItem) query.uniqueResult();

        Publication publication = null;

        if (item != null) {
            publication = getPublication(item);
        }

        session.getTransaction().commit();

        return publication;
    } finally {
        session.close();
    }
}

From source file:au.org.theark.core.dao.StudyDao.java

License:Open Source License

public long countNumberOfSubjectsThatAlreadyExistWithTheseUIDs(Study study, Collection<String> subjectUids) {
    String queryString = "select count(*) " + "from LinkSubjectStudy subject " + "where study =:study "
            + "and subjectUID in  (:subjects) ";
    Query query = getSession().createQuery(queryString);
    query.setParameter("study", study);
    query.setParameterList("subjects", subjectUids);

    return (Long) query.uniqueResult();
}