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.datamodel.aries.grants.ForCodesTest.java

License:Open Source License

/**
 * Queries for the fields of research in the database and prints them out
 *//*from  w  w w.j a v  a 2s .  c  o  m*/
@Test
public void test() {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();

    try {
        Query query = session.createQuery("FROM ForCodes WHERE chrForObjectiveCode = :code");
        query.setParameter("code", "111706");

        ForCodes forSubject = (ForCodes) query.uniqueResult();

        assertNotNull("No Field of Research Found", forSubject);
        assertEquals("No subject description found", "Epidemiology", forSubject.getChrForDescription());
        assertEquals("No subject division code found", "11", forSubject.getChrForDivisionCode());
        assertEquals("No subject group code found", "1117", forSubject.getChrForGroupCode());
        assertEquals("No subject objective code found", "111706", forSubject.getChrForObjectiveCode());
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.harvester.Harvest.java

License:Open Source License

/**
 * Harvest the records from the given system
 * //from   w w  w .  j av a2  s .  c om
 * @param harvestSystem The string of the system to harvest records for
 * @throws HarvestException
 */
public void harvest(String harvestSystem) throws HarvestException {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    Location location = null;
    try {
        Query query = session.createQuery("FROM Location WHERE system = :system");
        query.setParameter("system", harvestSystem);

        location = (Location) query.uniqueResult();
    } finally {
        session.close();
    }
    if (location == null) {
        harvest(location);
    } else {
        throw new HarvestException("No location found for the system: " + harvestSystem);
    }
}

From source file:au.edu.anu.metadatastores.harvester.HarvestContentService.java

License:Open Source License

/**
 * Get the next row of harvest content to process
 * /*from w  w w  .jav a2s .  co m*/
 * @param system The system to get the harvest content for
 * @return The harvest content record
 */
public HarvestContent getNextHarvestContent(String system) {
    Session session = HarvesterHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("FROM HarvestContent WHERE system = :system ORDER BY hid");
        query.setParameter("system", system);

        HarvestContent content = (HarvestContent) query.setMaxResults(1).uniqueResult();

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

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

License:Open Source License

/**
 * Get the university ids of the investigators for the given contract
 * //w w w .  j av  a  2  s  .  c o m
 * @param contractId The id of the contract to retrieve university ids for
 * @return The associated university ids
 */
public String[] getInvestigatorUniId(String contractId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session
                .createQuery("from ContractsGrantsInvestigators where chrContractCode = :contractId");
        query.setParameter("contractId", contractId);
        @SuppressWarnings("unchecked")
        List<ContractsGrantsInvestigators> investigators = query.list();
        List<String> staffIds = new ArrayList<String>();

        for (ContractsGrantsInvestigators investigator : investigators) {
            if (investigator != null) {
                if (investigator.getChrStaffNumber() != null
                        && !staffIds.contains(investigator.getChrStaffNumber())) {
                    staffIds.add(investigator.getChrStaffNumber());
                }
            }
        }

        return staffIds.toArray(new String[0]);
    } finally {
        session.close();
    }
}

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

License:Open Source License

/**
 * Get the funds provider reference number of the grant
 * //from w  w  w  .j  av a2 s .  c o m
 * @param anuActivityId The grant id
 * @return The reference number
 */
public String getANUActivityAuthorizeID(String anuActivityId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ContractsGrantsMain where chrContractCode = :activityId");
        query.setParameter("activityId", anuActivityId);

        @SuppressWarnings("unchecked")
        List<ContractsGrantsMain> grants = query.list();

        String authorId = null;
        if (grants != null && grants.size() > 0) {
            authorId = grants.get(0).getChrSchemeRef();
        }

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

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

License:Open Source License

/**
 * Get the grant description//from   w  ww.java  2  s  .com
 * 
 * @param anuActivityId The grant id
 * @return The description
 */
public String getANUActivityDesc(String anuActivityId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ContractsGrantsMain where chrContractCode = :activityId");
        query.setParameter("activityId", anuActivityId);

        @SuppressWarnings("unchecked")
        List<ContractsGrantsMain> grants = query.list();

        String description = null;

        if (grants != null && grants.size() > 0) {
            description = grants.get(0).getChrShortTitle();
        }

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

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

License:Open Source License

/**
 * Get the grants associated with the given university
 * /*from w  w w  .  j  av  a 2 s  .  co m*/
 * @param uniID The university id
 * @return The array of grants
 */
public ANUActivity[] getActivitiesByUniID(String uniID) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ContractsGrantsInvestigators where chrStaffNumber = :uniID");
        query.setParameter("uniID", uniID);
        @SuppressWarnings("unchecked")
        List<ContractsGrantsInvestigators> investigators = query.list();
        List<ANUActivity> activities = new ArrayList<ANUActivity>();

        ANUActivityImpl tempActivity = null;
        String investigatorCode = null;

        for (ContractsGrantsInvestigators investigator : investigators) {
            investigatorCode = investigator.getChrContractInvestigatorCode();
            investigatorCode = investigatorCode.substring(0, investigatorCode.indexOf(uniID));
            if (investigatorCode != null) {
                tempActivity = new ANUActivityImpl();
                tempActivity.setActivityId(investigatorCode);
                //TODO flesh this out
                //   tempActivity.setFirstInvestigatorId(uniID);
                activities.add(tempActivity);
            }
        }

        return activities.toArray(new ANUActivity[0]);
    } finally {
        session.close();
    }

}

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

License:Open Source License

/**
 * Get a single publication with the given aries publication id
 * //from  ww w.j ava  2 s . co m
 * @param publicationId The publication id
 * @return The publication
 */
public Publication getSinglePublication(String publicationId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ResearchOutputsData1 where chrOutput6code = :publicationId");
        query.setParameter("publicationId", publicationId);

        ResearchOutputsData1 researchOutput = (ResearchOutputsData1) query.uniqueResult();
        Publication tempPublication = getPublication(researchOutput);

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

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

License:Open Source License

/**
 * Retrieve all the publications for a given year
 * //from w w w  .j a  v  a 2  s .com
 * @param year The year to retrieve publications for
 * @return The list of publications
 */
public Publication[] getPublicationsForYear(String year) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ResearchOutputsData1 where chrReportingYear = :year");
        query.setParameter("year", year);

        Publication tempPublication = null;
        List<Publication> publications = new ArrayList<Publication>();

        @SuppressWarnings("unchecked")
        List<ResearchOutputsData1> researchOutputs = query.list();
        for (ResearchOutputsData1 output : researchOutputs) {
            tempPublication = getPublication(output);
            publications.add(tempPublication);
        }

        return publications.toArray(new Publication[0]);
    } finally {
        session.close();
    }
}

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

License:Open Source License

/**
 * Retrieve the first authors //from w w w .  j a  va 2  s  .co m
 * 
 * @param publicationCodes The publication codes
 * @return The university ids of the first authors
 */
public String[] getFirstAuthorsUniIDs(String[] publicationCodes) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();

    try {
        Query query = session.createQuery(
                "from ResearchOutputsDataAuthors where chrOutputInvestigatorCode like :publicationCode and chrOrder = '01'");

        List<ResearchOutputsDataAuthors> outputAuthors = null;

        String outputInvestigatorCode = null;

        List<String> uniIDs = new ArrayList<String>();
        String staffCode = null;

        for (String publicationCode : publicationCodes) {
            query.setParameter("publicationCode", publicationCode + "x%");

            //@SuppressWarnings("unchecked")
            outputAuthors = query.list();
            for (ResearchOutputsDataAuthors outputAuthor : outputAuthors) {
                outputInvestigatorCode = outputAuthor.getChrOutputInvestigatorCode();
                if (publicationCode != null && outputInvestigatorCode.indexOf(publicationCode) != -1) {
                    staffCode = outputAuthor.getChrStaffNumber();
                    uniIDs.add(staffCode);
                }
            }
        }

        return uniIDs.toArray(new String[0]);
    } finally {
        session.close();
    }
}