Example usage for org.hibernate Session createQuery

List of usage examples for org.hibernate Session createQuery

Introduction

In this page you can find the example usage for org.hibernate Session createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);

Source Link

Usage

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
 * /*from w  w w  .j  ava2s .  c o  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.people.PersonService.java

License:Open Source License

/**
 * Retrieves a list of people with the given ids
 * //ww  w . j  ava 2 s.  c om
 * @param extIds The ids to retrieve information about people for
 * @param extraInfo Indicates whether to also retrieve the type, institution, country and organisational unit about the person
 * @return A list of people containing basic information
 */
public List<Person> getBasicPeople(List<String> extIds, boolean extraInfo) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery(
                "select distinct pi from PersonItem as pi left join fetch pi.itemAttributes where pi.extId in (:extIds)");
        query.setParameterList("extIds", extIds);

        @SuppressWarnings("unchecked")
        List<PersonItem> personItems = query.list();
        LOGGER.debug("Number of People Found: {}", personItems.size());
        List<Person> people = new ArrayList<Person>();
        for (PersonItem personItem : personItems) {
            Person person = getBasicPerson(personItem, extraInfo);
            if (!people.contains(person)) {
                people.add(person);
            }
        }

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

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

License:Open Source License

public List<Person> getCurrentAriesPeople() {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {/*from   w w w . j a va2  s.com*/
        //TODO limit this by current people?
        Query query = session
                .createQuery("FROM PersonItem item WHERE EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :ariesAttrType) "
                        + "AND EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :uidAttrType) "
                        + "AND EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :activeAttr " + "AND ia.attrValue = :activeValue) ");
        query.setParameter("ariesAttrType", StoreAttributes.ARIES_ID);
        query.setParameter("uidAttrType", StoreAttributes.UNIVERSITY_ID);
        query.setParameter("activeAttr", StoreAttributes.ACTIVE);
        query.setParameter("activeValue", Boolean.TRUE.toString());
        @SuppressWarnings("unchecked")
        List<PersonItem> peopleItems = query.list();
        LOGGER.info("Number of people: {}", peopleItems.size());
        List<Person> people = getPeople(peopleItems.subList(0, Math.min(peopleItems.size(), 100)), true);
        //List<Person> people = getPeople(peopleItems, true);
        return people;
    } finally {
        session.close();
    }
}

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

License:Open Source License

/**
 * Save the publication//  w  w w  . j  av a  2 s.c  om
 * 
 * @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 .  ja  va  2s.co 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

/**
 * Searches for the people that match the publication with an author that has the provided given and surnames
 * /*from   w w w  . ja v  a  2 s. c o  m*/
 * @param iid The item id of the publication
 * @param givenName The given name
 * @param surname The surname
 * @return The list of people that match
 */
public List<PersonItem> searchPublicationPerson(Long iid, String givenName, String surname) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery(
                "SELECT pi FROM ItemRelation ir join ir.itemByRelatedIid pi join pi.givenNames gn join pi.surnames sn WHERE ir.id.iid = :iid AND pi.extSystem = :extSystem AND lower(gn.attrValue) like :givenName and lower(sn.attrValue) = :surname");
        query.setParameter("iid", iid);
        query.setParameter("extSystem", "PERSON");
        query.setParameter("givenName", givenName.toLowerCase() + "%");
        query.setParameter("surname", surname.toLowerCase());

        @SuppressWarnings("unchecked")
        List<PersonItem> people = query.list();
        return people;
    } 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/*from www .j  av  a  2s  . com*/
 * 
 * @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.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Get the publications by year// ww w  .  ja  v  a2 s.  c  o  m
 * 
 * @param year The year of publication to get
 * @return The a list of publications that were published in the given year
 */
public List<Publication> getPublicationsByYear(String year) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        Date startDate = new Date();
        Query query = session.createQuery(
                "SELECT DISTINCT pub FROM PublicationItem pub inner join pub.itemAttributes pubYear join fetch pub.itemAttributes attrs left join fetch attrs.itemAttributes WHERE pubYear.attrType = :yearType and pubYear.attrValue = :yearValue");
        query.setParameter("yearType", StoreAttributes.YEAR);
        query.setParameter("yearValue", year);

        @SuppressWarnings("unchecked")
        List<PublicationItem> items = query.list();
        Date endDate = new Date();
        long difference = endDate.getTime() - startDate.getTime();
        LOGGER.debug("Time For Query: {}, Number of Records: {}", difference, items.size());

        List<Publication> publications = new ArrayList<Publication>();
        Publication publication = null;
        for (PublicationItem item : items) {
            publication = getPublication(item, true);
            publications.add(publication);
        }
        LOGGER.debug("Number of Publications: {}", items.size());

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

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

License:Open Source License

/**
 * Get the publications associated with the user id
 * //from   ww  w .j  a v  a  2 s. c o m
 * @param uid The user id to get publications for
 * @return A list of publications
 */
public List<Publication> getPersonsPublications(String uid) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");
        session.beginTransaction();

        Query query = session.createQuery(
                "SELECT pub FROM PersonItem person inner join person.itemRelationsForRelatedIid pirfri inner join pirfri.itemByIid pubItem, PublicationItem pub WHERE person.extId = :extId and pubItem = pub");

        query.setParameter("extId", uid);

        @SuppressWarnings("unchecked")
        List<PublicationItem> publicationItems = query.list();

        List<Publication> publications = new ArrayList<Publication>();
        for (PublicationItem publicationItem : publicationItems) {
            Publication publication = getPublication(publicationItem);
            publications.add(publication);
        }

        LOGGER.debug("Number of publications: {}", publications.size());

        session.getTransaction().commit();

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

From source file:au.edu.anu.metadatastores.store.search.DBSearch.java

License:Open Source License

/**
 * Execute the query //from w ww  .  ja va 2 s.c  o  m
 * 
 * @param session The hibernate session
 * @param queryString The query string
 * @param queryValue The value to search for
 * @return
 */
private List<ItemDTO> executeQueryWithParameters(Session session, String queryString, String queryValue) {
    Query query = session.createQuery(queryString);
    query.setParameter("attrValue", "%" + queryValue.toLowerCase() + "%");
    @SuppressWarnings("unchecked")
    List<ItemDTO> items = query.list();
    return items;
}