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

License:Open Source License

/**
 * Set the National Library of Australia relationships
 * /*from   w  w  w .ja va  2s.  co m*/
 * @param item The item to add the relationships to
 * @param relationParts The relationship parts
 * @param itemRelations The existing relationships
 * @param session The session object
 */
private void setNLAIdentifierRelations(Item item, List<RelationPart> relationParts,
        List<ItemRelation> itemRelations, Session session) {
    String nlaPrefix = StoreProperties.getProperty("nla.prefix");

    Query query = session.createQuery(
            "SELECT p FROM PersonItem p join p.itemAttributes nlaId WHERE nlaId.attrType = :nlaType and nlaId.attrValue = :nlaValue");
    query.setParameter("nlaType", StoreAttributes.NLA_ID);

    for (RelationPart relationPart : relationParts) {
        LOGGER.debug("Relation: {}, {}", relationPart.getValue(), relationPart.getType());
        if (relationPart.getValue().startsWith(nlaPrefix)) {
            LOGGER.debug("Is a nla identifier: {}", relationPart.getValue());
            query.setParameter("nlaValue", relationPart.getValue());
            @SuppressWarnings("unchecked")
            List<Item> people = query.list();
            String relationType = getRelationType(relationPart.getType());
            for (Item person : people) {
                LOGGER.debug("ID: {}, Title: {}", person.getIid(), person.getTitle());
                ItemRelationId itemRelationId = new ItemRelationId(item.getIid(), relationType,
                        person.getIid());
                ItemRelation itemRelation = new ItemRelation();
                itemRelation.setId(itemRelationId);
                itemRelations.add(itemRelation);
            }
        }
    }
    LOGGER.debug("Item Relation Size: {}", item.getItemRelationsForIid().size());
}

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

License:Open Source License

/**
 * Set the reverse relationships//www . j av  a 2s  .c  o m
 * 
 * @param item The dublin core item to set the relation for
 * @param dublinCore the dublin core to set relations for
 * @param session2 The hibernate session
 */
protected void setReverseRelations(DublinCoreItem item, DublinCore dublinCore, Session session2) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session
                .createQuery("FROM ItemAttribute WHERE attrType = :attrType AND attrValue = :attrValue");
        query.setParameter("attrType", StoreAttributes.RELATION_VALUE);
        dublinCore.getIdentifiers();
        String identifier = null;
        for (String id : dublinCore.getIdentifiers()) {
            if (id.startsWith(StoreProperties.getProperty("relation.id.format.datacommons"))) {
                identifier = id;
                break;
            }
        }
        if (identifier != null) {
            query.setParameter("attrValue", identifier);
            @SuppressWarnings("unchecked")
            List<ItemAttribute> relatedAttributes = query.list();

            LOGGER.debug("Number of reverse relationships: {}", relatedAttributes.size());
            ItemRelationId id = null;
            for (ItemAttribute relatedAttribute : relatedAttributes) {
                String relationText = relatedAttribute.getItemAttribute().getAttrValue();

                String[] relationParts = getRelationParts(relationText);
                String relationType = getRelationType(relationParts[0]);

                id = new ItemRelationId(relatedAttribute.getItem().getIid(), relationType, item.getIid());
                ItemRelation relation = (ItemRelation) session.get(ItemRelation.class, id);
                if (relation == null) {
                    LOGGER.debug("Does not have relation");
                    ItemRelation newRelation = new ItemRelation();
                    newRelation.setId(id);
                    item.getItemRelationsForRelatedIid().add(newRelation);
                } else {
                    LOGGER.debug("has relation");
                }
            }
        }
    } 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  av a  2 s . co 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

/**
 * Set the status of the record to deleted
 * /* www.jav  a2s  . co  m*/
 * @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//  w  w  w.j a  v  a  2s.c o  m
 * 
 * @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 w  w  w  .j ava 2s . 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/*from ww w . ja v a  2  s.  com*/
 * 
 * @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 w ww . j  a v  a2 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  w  w  w. j  a va  2s .  co 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  www .jav a2s.c  o  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();
    }
}