Example usage for org.hibernate Query list

List of usage examples for org.hibernate Query list

Introduction

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

Prototype

List<R> list();

Source Link

Document

Return the query results as a List.

Usage

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

License:Open Source License

/**
 * Find external staff by their name//w  w  w. jav  a2s . c o  m
 * 
 * @param surname The surname of the person to find
 * @param givenName The given name of the person to find
 * @return A list of external staff members who match the provided name
 */
public ExternalStaff[] findExternalStaff(String surname, String givenName) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        List<ExternalStaff> externalStaff = new ArrayList<ExternalStaff>();
        Query query = session.createQuery(
                "from ExternalUsers where lower(chrFirstname) = :firstname and lower(chrSurname) = :surname");
        query.setParameter("firstname", givenName);
        query.setParameter("surname", surname);

        @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.services.aries.StaffId.java

License:Open Source License

/**
 * Find external staff by their surname//ww w .  j  a va2  s  .  c o  m
 * 
 * @param surname The surname to search on
 * @return The external staff members with the provided surname
 */
public ExternalStaff[] findExternalStaffBySurname(String surname) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        List<ExternalStaff> externalStaff = new ArrayList<ExternalStaff>();
        Query query = session.createQuery("from ExternalUsers where lower(chrSurname) = :surname");
        query.setParameter("surname", surname.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.services.aries.StaffId.java

License:Open Source License

/**
 * Find external staff by their given name
 * //from  ww  w .  j  a  va 2 s . c o m
 * @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

/**
 * Add relationships based on identifiers in the item
 * //  w ww .j  av a  2  s .c o m
 * @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 ww. j  a  v a  2 s .  c  om
 * @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 w  w  w  . j a  v a  2  s.  c  o 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/*from w  w w.j a  v  a2s .c  om*/
 * 
 * @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.grants.GrantService.java

License:Open Source License

/**
 * Retrieves a list of grants associated with the person with the given id
 * //  ww  w.ja  v  a 2s . 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
 * //www  .  ja  va2 s . com
 * @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.misc.RelationService.java

License:Open Source License

/**
 * Get the potential relations//from w  w w  .j  a  v  a2s . c o m
 * 
 * @return The potential relations
 */
public List<Relation> getPotentialRelations() {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery(
                "SELECT new au.edu.anu.metadatastores.store.misc.Relation(pr.itemByIid.iid, pr.itemByIid.title, pr.id.relationValue, pr.itemByRelatedIid.iid, pr.itemByRelatedIid.title) FROM PotentialRelation pr WHERE pr.requireCheck = true");
        @SuppressWarnings("unchecked")
        List<Relation> relations = query.list();

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