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

License:Open Source License

/**
 * Process records that have the deleted status from oai-pmh
 * //from   www.j av  a 2s . c om
 * @param content The harvested record to process
 */
private void processDeleted(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();

        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.setDeleted(Boolean.TRUE);
            session.merge(item);
        } else {
            LOGGER.debug("No record to be deleted: {}", content.getIdentifier());
        }
        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

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

License:Open Source License

/**
 * Process a record that has not been deleted
 * /*  w w  w.j  a  v a  2  s.co 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 {/* ww w  .j a  v  a 2 s .  com*/
        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

/**
 * Set the status of the record to deleted
 * /*from   w  ww  . j  a v  a  2  s  .  c om*/
 * @param content The stub of the content to delete
 */
private void processDeleted(HarvestContent content) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.beginTransaction();

        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.setDeleted(Boolean.TRUE);
            session.merge(item);
        } else {
            LOGGER.debug("No record to be deleted: {}", content.getIdentifier());
        }
        session.getTransaction().commit();
    } finally {
        session.close();
    }
}

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

License:Open Source License

/**
 * Process the record//ww  w  .j a  v  a 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.digitalcollections.DigitalCollectionsService.java

License:Open Source License

public DigitalCollection getDigitalCollection(String id) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFetchProfile("attributes");
    //DublinCore digitalCollection = null;
    try {/*from  ww w.  j  a v  a 2 s.  c o  m*/
        Query query = session.createQuery("FROM DigitalCollectionItem WHERE extId = :extId");
        query.setParameter("extId", id);

        DigitalCollectionsItem item = (DigitalCollectionsItem) query.uniqueResult();
        //digitalCollection = getDigitalCollection(item);
        return getDigitalCollection(item);
    } finally {
        session.close();
    }

    //return digitalCollection;
}

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

License:Open Source License

/**
 * Save a single E Press record/*  w  ww .  j  a  v  a  2 s . c o  m*/
 * 
 * @param epress The E Press record t osave
 * @return The E Press record item
 */
public EpressItem saveEpress(Epress epress, Boolean userUpdated) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    try {
        session.beginTransaction();

        Query query = session.createQuery("FROM EpressItem WHERE extId = :extId");
        query.setParameter("extId", epress.getExtId());
        EpressItem item = (EpressItem) query.uniqueResult();

        Date lastModified = new Date();

        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;
        newItem = parser.getItem(epress, Boolean.FALSE, lastModified);

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

        updateAttributesFromItem(item, newItem, session, lastModified);

        session.getTransaction().commit();
        return item;
    } catch (IllegalAccessException e) {
        LOGGER.error("Exception accessing field when trying to get an e press item", e);
    } catch (InvocationTargetException e) {
        LOGGER.error("Exception invoking method when trying to get an e press item", e);
    } finally {
        session.close();
    }

    return null;
}

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)
 * //from  www.j a v  a  2 s .  c om
 * @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  w  ww .j a v  a  2  s . c  o  m
 * 
 * @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//from w  w  w. jav  a  2s . c  om
 * 
 * @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();
    }
}