Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

In this page you can find the example usage for org.hibernate Query setParameter.

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:au.edu.anu.metadatastores.services.aries.StaffId.java

License:Open Source License

/**
 * Find external staff by their given name
 * //from  w  ww. java 2s.c  om
 * @param givenName The given name to search on
 * @return The external staff members with the provided given name
 */
public ExternalStaff[] findExternalStaffByGivenName(String givenName) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        List<ExternalStaff> externalStaff = new ArrayList<ExternalStaff>();
        Query query = session.createQuery("from ExternalUsers where lower(chrFirstname) = :firstname");
        query.setParameter("firstname", givenName.toLowerCase());

        @SuppressWarnings("unchecked")
        List<ExternalUsers> users = query.list();

        ExternalStaff staff = null;
        for (ExternalUsers user : users) {
            staff = setExternalStaffInformation(user);
            externalStaff.add(staff);
        }

        return externalStaff.toArray(new ExternalStaff[0]);
    } finally {
        session.close();
    }
}

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 w ww  .  ja  v a  2  s. c  o m
 * @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
 * /*from   w w w  . j a v a2s  . com*/
 * @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

/**
 * Add relationships based on identifiers in the item
 * //from www.jav  a  2  s  . c om
 * @param item The item to add relationships to
 * @param relationParts The relationship parts
 * @param itemRelations The existing relationships
 * @param session The session
 */
private void setIdentifierRelations(Item item, List<RelationPart> relationParts,
        List<ItemRelation> itemRelations, Session session) {
    Query query = session.createQuery(
            "SELECT i FROM Item i join i.itemAttributes ia WHERE ia.attrType = :attrType AND ia.attrValue = :attrValue");
    query.setParameter("attrType", StoreAttributes.IDENTIFIER);

    for (RelationPart relationPart : relationParts) {
        query.setParameter("attrValue", relationPart.getValue());
        LOGGER.debug("Item: {}, Value: {}", relationPart.getValue(), relationPart.getType());

        @SuppressWarnings("unchecked")
        List<Item> relItems = query.list();

        String relationType = getRelationType(relationPart.getType());

        for (Item relItem : relItems) {
            ItemRelationId itemRelationId = new ItemRelationId(item.getIid(), relationType, relItem.getIid());
            ItemRelation itemRelation = new ItemRelation();
            itemRelation.setId(itemRelationId);
            itemRelations.add(itemRelation);
        }
    }
}

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

License:Open Source License

/**
 * Set the grant identifier relationships
 * //from   w w w.j  a va  2  s.  com
 * @param item The item to add relationships to
 * @param relationParts The relationship parts
 * @param itemRelations The existing item relations
 * @param session The session
 * @param fundsPrefix The prefix of the grant
 * @param fundsProvider The name of the funds provider
 */
private void setGrantIdentifierRelations(Item item, List<RelationPart> relationParts,
        List<ItemRelation> itemRelations, Session session, String fundsPrefix, String fundsProvider) {
    Query query = session.createQuery(
            "SELECT gr from GrantItem gr join gr.itemAttributes fundProv join gr.itemAttributes refNum where fundProv.attrType = :fundType and fundProv.attrValue = :fundProvValue and refNum.attrType = :refType and refNum.attrValue = :refValue");
    query.setParameter("fundType", StoreAttributes.FUNDS_PROVIDER);
    query.setParameter("refType", StoreAttributes.REFERENCE_NUMBER);
    query.setParameter("fundProvValue", fundsProvider);

    int prefixLength = fundsPrefix.length();
    for (RelationPart relationPart : relationParts) {
        LOGGER.debug("Relation: {}, {}", relationPart.getValue(), relationPart.getType());
        if (relationPart.getValue().startsWith(fundsPrefix)) {
            LOGGER.debug("Is a Grant: {}", relationPart.getValue());
            String id = relationPart.getValue().substring(prefixLength);
            query.setParameter("refValue", id);
            @SuppressWarnings("unchecked")
            List<Item> grants = query.list();
            String relationType = getRelationType(relationPart.getType());
            for (Item grant : grants) {
                LOGGER.debug("ID: {}, Title: {}", grant.getIid(), grant.getTitle());
                ItemRelationId itemRelationId = new ItemRelationId(item.getIid(), relationType, grant.getIid());
                ItemRelation itemRelation = new ItemRelation();
                itemRelation.setId(itemRelationId);
                itemRelations.add(itemRelation);
            }
        }
    }
}

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

License:Open Source License

/**
 * Set the National Library of Australia relationships
 * /*from   ww  w.  j  a  v a 2s.  c om*/
 * @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/*from w w  w.  ja  v  a 2 s . 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 {/*from   w ww. java2s .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
 * //from www.ja va 2  s. c  o  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//  ww  w. j a  v a2 s . 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();
    }
}