Example usage for org.hibernate Session enableFilter

List of usage examples for org.hibernate Session enableFilter

Introduction

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

Prototype

Filter enableFilter(String filterName);

Source Link

Document

Enable the named filter for this current session.

Usage

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

License:Open Source License

/**
 * Save the person's information//from  w  w  w . ja  v  a  2 s . co  m
 * 
 * @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  w w .ja v a 2s . c  o m*/
 * @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
 * //from   ww  w  .j  a  v  a  2 s.c om
 * @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   w w w.  j a  v  a  2s . 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  ww .ja v  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

/**
 * Searches for the people that match the publication with an author that has the provided given and surnames
 * //  w  w  w .j  ava  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/*w  w w. j  av a 2s.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.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Get the publications by year/* ww w.java2s .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
 * /*www . j  av a  2s .co  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

@Override
public List<ItemDTO> search(String value) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFilter("attributes");
    try {//from w  ww  .j a v  a  2 s .  c o m
        List<ItemDTO> results = executeQueryWithParameters(session, getFirstQueryString(), value);
        List<ItemDTO> items = new ArrayList<ItemDTO>(results);

        results = executeQueryWithParameters(session, getSecondQueryString(), value);
        items.addAll(results);
        return items;
    } finally {
        session.close();
    }
}