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.datacommons.DataCommonsService.java

License:Open Source License

/**
 * Process a record that has not been deleted
 * //from  www. j a  v a  2s  .c o m
 * @param content The harvested record to process
 */
private void processRecord(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.enableFilter("attributes");

        Query query = session
                .createQuery("FROM DataCommonsItem WHERE extSystem = :extSystem AND extId = :extId");
        query.setParameter("extSystem", extSystem);
        query.setParameter("extId", content.getIdentifier());

        DataCommonsItem item = (DataCommonsItem) query.uniqueResult();
        if (item == null) {
            item = new DataCommonsItem();
            item.setExtId(content.getIdentifier());
            session.save(item);
        }

        try {
            JAXBContext context = JAXBContext.newInstance(DublinCore.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();

            DublinCore dublinCore = (DublinCore) unmarshaller.unmarshal(new StringReader(content.getContent()));
            Date lastModified = new Date();
            super.processRecord((DublinCoreItem) item, dublinCore, session, lastModified);
        } catch (JAXBException e) {
            LOGGER.error("Exception transforming document", e);
        } catch (InvocationTargetException e) {
            LOGGER.error("Error invoking method", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("Error accessing method", e);
        }

        session.merge(item);

        LOGGER.debug("Item Numbers: {}", item.getItemAttributes().size());

        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.datacommons.DataCommonsService.java

License:Open Source License

public DataCommonsObject getDataCommonsObject(String id) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFilter("attributes");
    //DublinCore dataCommonsObject = null;
    try {//  w w  w .  j a  va 2  s.c  o m
        Query query = session.createQuery("FROM DataCommonsItem WHERE extId = :extId");
        query.setParameter("extId", id);

        DataCommonsItem item = (DataCommonsItem) query.uniqueResult();
        DataCommonsObject dataCommonsObject = getDataCommonsObject(item);
        return dataCommonsObject;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.digitalcollections.DigitalCollectionsService.java

License:Open Source License

/**
 * Process the record//from  www  .j ava  2s.c om
 * 
 * @param content The harvested content
 */
private void processRecord(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.enableFilter("attributes");
        LOGGER.debug("Identifier: {}", content.getIdentifier());

        Query query = session
                .createQuery("FROM DigitalCollectionsItem WHERE extSystem = :extSystem AND extId = :extId");
        query.setParameter("extSystem", extSystem_);
        query.setParameter("extId", content.getIdentifier());

        DigitalCollectionsItem item = (DigitalCollectionsItem) query.uniqueResult();
        if (item == null) {
            item = new DigitalCollectionsItem();
            item.setExtSystem(extSystem_);
            item.setExtId(content.getIdentifier());
            session.save(item);
        }

        try {
            JAXBContext context = JAXBContext.newInstance(DublinCore.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();

            DublinCore dublinCore = (DublinCore) unmarshaller.unmarshal(new StringReader(content.getContent()));
            Date lastModified = new Date();
            super.processRecord((DublinCoreItem) item, dublinCore, session, lastModified);
        } catch (JAXBException e) {
            LOGGER.error("Exception transforming document", e);
        } catch (InvocationTargetException e) {
            LOGGER.error("Error invoking method", e);
        } catch (IllegalAccessException e) {
            LOGGER.error("Error accessing method", e);
        }

        session.merge(item);

        LOGGER.info("Item Numbers: {}", item.getItemAttributes().size());

        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.epress.EpressService.java

License:Open Source License

/**
 * Get the E Press record by its ext id (N.B. this is the ISBN or ISSN)
 * /* w w  w. j  a  v a 2 s.c o m*/
 * @param epressId The external id
 * @return The E Press record that was found
 */
public Epress getEpress(String epressId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFilter("attributes");
    try {
        Query query = session.createQuery("FROM EpressItem WHERE extId = :extId");
        query.setParameter("extId", epressId);

        EpressItem item = (EpressItem) query.uniqueResult();
        Epress epress = getEpress(item);
        return epress;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Save the grant information/*from  ww  w . j a  v  a  2 s  .com*/
 * 
 * @param grant The grant to save
 * @param userUpdated Indicator of whether it is user updated or system updated
 * @return The GrantItem that has been created/updated
 */
public GrantItem saveGrant(Grant grant, Boolean userUpdated) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    GrantItem item = null;
    try {
        session.enableFilter("attributes");
        session.beginTransaction();

        Query query = session.createQuery("from GrantItem where extId = :extId");
        query.setParameter("extId", grant.getContractCode());

        item = (GrantItem) query.uniqueResult();

        Date lastModified = new Date();
        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;
        newItem = parser.getItem(grant, userUpdated, lastModified);

        if (item == null) {
            item = new GrantItem();
            if (newItem.getExtId() == null) {
                return null;
            }
            item.setExtId(newItem.getExtId());
            session.save(item);
        }

        updateAttributesFromItem(item, newItem, session, lastModified);

        LOGGER.debug("Number of item attributes before: {}", item.getItemAttributes().size());

        associatePeople(item, grant, session);

        LOGGER.debug("Number of Item Attributes after: {}", item.getItemAttributes().size());

        item = (GrantItem) session.merge(item);
        session.getTransaction().commit();
    } catch (IllegalAccessException e) {
        LOGGER.error("Exception accessing field when trying to get a grant item", e);
    } catch (InvocationTargetException e) {
        LOGGER.error("Exception invoking method when trying to get a grant item", e);
    } catch (Exception e) {
        if (item == null) {
            LOGGER.error("Exception querying item", e);
        } else {
            LOGGER.error("Error Merging Item {}", item.getIid(), e);
            LOGGER.info("Error with item: {}, Title: {}, System: {}, Ext Id: {}", item.getIid(),
                    item.getTitle(), item.getExtSystem(), item.getExtId());
            for (ItemAttribute attr : item.getItemAttributes()) {
                LOGGER.info("AID: {}, IID: {}, Type: {}, Value: {}", new Object[] { attr.getAid(),
                        attr.getItem().getIid(), attr.getAttrType(), attr.getAttrValue() });
            }
            for (HistItemAttribute attr : item.getHistItemAttributes()) {
                LOGGER.info("AID: {}, Date: {}, Type: {}, Value: {}", new Object[] { attr.getId().getAid(),
                        attr.getId().getHistDatetime(), attr.getAttrType(), attr.getAttrValue() });
            }
        }
    } finally {
        session.close();
    }

    return item;
}

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Return the Grant with the given id/* ww  w .j a v  a2 s .  co  m*/
 * 
 * @param grantId The id of the grant to retrieve
 * @return The grant information
 */
public Grant getGrant(String grantId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery("FROM GrantItem WHERE extId = :extId");
        query.setParameter("extId", grantId);

        GrantItem item = (GrantItem) query.uniqueResult();
        Grant grant = getGrant(item);
        return grant;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Retrieves a list of grants associated with the person with the given id
 * /*from  w  w  w  .  ja  v  a2  s  . c  o m*/
 * @param staffId The staff id of the person to retrieve grants for
 * @return The list of grants
 */
public List<Grant> getGrantsForPerson(String staffId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery(
                "SELECT grant FROM GrantItem grant, PersonItem person join person.itemRelationsForRelatedIid personRelation WHERE personRelation.itemByIid = grant and person.extId = :staffId");
        query.setParameter("staffId", staffId);
        @SuppressWarnings("unchecked")
        List<GrantItem> grantItems = query.list();
        List<Grant> grants = new ArrayList<Grant>();
        Grant grant = null;
        for (GrantItem grantItem : grantItems) {
            grant = getGrant(grantItem);
            if (grant != null) {
                grants.add(grant);
            }
        }

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

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Find grants with the given attributes and attribute values
 * /*from w  w  w. ja  v a 2  s  . co m*/
 * @param attributes The attributes to query on
 * @return The grants
 */
public List<Grant> queryGrantsByAttributes(Map<String, String> attributes) {
    List<Grant> grants = new ArrayList<Grant>();
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFilter("attributes");

    try {
        List<String> parameters = new ArrayList<String>();

        StringBuilder fromString = new StringBuilder();
        StringBuilder whereString = new StringBuilder();

        fromString.append(" FROM GrantItem gi");
        whereString.append(" WHERE");
        int i = 0;
        for (Entry<String, String> entry : attributes.entrySet()) {
            fromString.append(" LEFT JOIN gi.itemAttributes gia");
            fromString.append(i);
            if (i > 0) {
                whereString.append(" AND");
            }
            whereString.append(" gia");
            whereString.append(i);
            whereString.append(".attrType = ? AND lower(gia");
            whereString.append(i);
            whereString.append(".attrValue) like ?");
            parameters.add(entry.getKey());
            parameters.add("%" + entry.getValue().toLowerCase() + "%");

            i++;
        }
        String queryString = "SELECT gi " + fromString.toString() + " " + whereString.toString();
        LOGGER.info("Query: {}", queryString);
        LOGGER.info("Number of parameters: {}", parameters.size());
        Query query = session.createQuery(queryString);
        for (i = 0; i < parameters.size(); i++) {
            query.setParameter(i, parameters.get(i));
        }

        @SuppressWarnings("unchecked")
        List<GrantItem> grantItems = query.list();
        Grant grant = null;
        for (GrantItem grantItem : grantItems) {
            grant = getGrant(grantItem);
            grants.add(grant);
        }
    } finally {
        session.close();
    }

    return grants;
}

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

License:Open Source License

/**
 * Find a person by a uid/*from  ww 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

/**
 * Find people by name/* w w  w .j av  a  2  s.  co  m*/
 * 
 * @param givenName The given name to search on
 * @param surname The surname to search on
 * @return The list of people with the name
 */
private List<PersonItem> queryPeopleByName(String givenName, String surname) {
    LOGGER.debug("Given Name: {}, Surname: {}", givenName, surname);
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery(
                "select pi from PersonItem pi join pi.givenNames gn join pi.surnames sn where lower(gn.attrValue) = :givenName and lower(sn.attrValue) = :surname");
        query.setParameter("givenName", givenName.toLowerCase());
        query.setParameter("surname", surname.toLowerCase());

        @SuppressWarnings("unchecked")
        List<PersonItem> people = query.list();
        if (people != null) {
            LOGGER.debug("Number of people found in first query: {}", people.size());
        } else {
            LOGGER.debug("No people found in first query");
        }

        if (people == null || people.size() == 0) {
            query = session.createQuery(
                    "select pi from PersonItem pi join pi.commonNames cn where lower(cn.attrValue) = :commonName");
            query.setParameter("commonName", givenName.toLowerCase() + " " + surname.toLowerCase());

            people = query.list();
            if (people != null) {
                LOGGER.debug("Number of people found in second query: {}", people.size());
            } else {
                LOGGER.debug("No people found in second query");
            }
        }

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