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

License:Open Source License

/**
 * Retrieve all the publication codes from Aries
 * //w ww  . j  av a2  s . c  om
 * @return An array of publication codes
 */
public String[] getAllPublicationCodes() {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        @SuppressWarnings("unchecked")
        List<ResearchOutputsData1> researchOutputs = session.createQuery("from ResearchOutputsData1").list();
        List<String> outputCodes = new ArrayList<String>();

        for (ResearchOutputsData1 researchOutput : researchOutputs) {
            if (researchOutput != null) {
                outputCodes.add(researchOutput.getChrOutput6code());
            }
        }

        return outputCodes.toArray(new String[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
 * // w  w  w  .j  ava2 s . c o  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.c  o m*/
 * @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();
    }
}

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

License:Open Source License

/**
 * Retrieve the first authors/*from   w  w  w .  j av a 2 s . c  o  m*/
 * 
 * @param publicationCodes The publication codes
 * @return The first named authors
 */
public String[] getFirstAuthors(String[] publicationCodes) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session
                .createQuery("from ResearchOutputsData1 where chrOutput6code in :publicationCodes");
        query.setParameterList("publicationCodes", publicationCodes);

        @SuppressWarnings("unchecked")
        List<ResearchOutputsData1> researchOutputs = query.list();

        List<String> firstAuthors = new ArrayList<String>();
        for (ResearchOutputsData1 researchOutput : researchOutputs) {
            if (researchOutput != null && researchOutput.getChrFirstNamedAuthor() != null) {
                firstAuthors.add(researchOutput.getChrFirstNamedAuthor());
            }
        }

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

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

License:Open Source License

/**
 * Get the contracts/grants for the given staff member
 * //from   w  w  w  .j  a  v a 2s .  c  om
 * @param staffId The id of the staff member
 * @return The activities associated with the staff member
 */
public ANUActivity[] getContracts(String staffId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ContractsGrantsInvestigators where chrStaffNumber = :staffId");
        query.setParameter("staffId", staffId);

        @SuppressWarnings("unchecked")
        List<ContractsGrantsInvestigators> results = query.list();

        ANUActivityImpl tempActivity = null;
        List<ANUActivity> activityResults = new ArrayList<ANUActivity>();

        ContractsGrantsInvestigators investigator = null;
        ContractsGrantsMain contract = null;
        for (int i = 0; i < results.size(); i++) {
            investigator = results.get(i);
            contract = investigator.getContractsGrantsMain();

            if (investigator.getChrStaffNumber() != null
                    && investigator.getChrContractInvestigatorCode() != null) {
                tempActivity = new ANUActivityImpl();
                tempActivity.setActivityId(contract.getChrContractCode());
                List<String> invesitgatorIds = new ArrayList<String>();
                for (ContractsGrantsInvestigators inv : contract.getContractsGrantsInvestigators()) {
                    invesitgatorIds.add(inv.getChrStaffNumber());
                    if ("Yes".equals(inv.getChrPrimary())) {
                        tempActivity.setFirstInvestigatorId(inv.getChrStaffNumber());
                    }
                }
                tempActivity.setInvestigators(invesitgatorIds.toArray(new String[0]));
                tempActivity.setActivityTitle(contract.getChrShortTitle());
                tempActivity.setStartDate(contract.getChrGrantStartDate());
                tempActivity.setEndDate(contract.getChrCompletionDate());
                tempActivity.setStatus(contract.getChrStatus());

                if (contract.getForCodes1() != null) {
                    Subject forSubject = new FORSubjectImpl(contract.getForCodes1().getChrForObjectiveCode(),
                            contract.getForCodes1().getChrForDescription(), contract.getChrForpercentage1());
                    tempActivity.setForSubject1(forSubject);
                }
                if (contract.getForCodes2() != null) {
                    Subject forSubject = new FORSubjectImpl(contract.getForCodes2().getChrForObjectiveCode(),
                            contract.getForCodes2().getChrForDescription(), contract.getChrForpercentage2());
                    tempActivity.setForSubject2(forSubject);
                }
                if (contract.getForCodes3() != null) {
                    Subject forSubject = new FORSubjectImpl(contract.getForCodes3().getChrForObjectiveCode(),
                            contract.getForCodes3().getChrForDescription(), contract.getChrForpercentage3());
                    tempActivity.setForSubject3(forSubject);
                }

                tempActivity.setFundsProvider(contract.getChrPrimaryFundsProvider());
                try {
                    String schemeRef = new String(contract.getChrSchemeRef().getBytes(), "UTF8");
                    //Check for if the schema is null, and if it is a non-breaking space
                    //The database character encoding appears to at least sometimes be in Cp1250 (i.e. windows encoding)
                    if (schemeRef != null && !"&nbsp;".equals(schemeRef)
                            && (new String(schemeRef.getBytes("Cp1250"), "UTF8").replaceFirst("^[\\xA0]+", "")
                                    .trim().length() > 0)) {
                        tempActivity.setSchemeReference(schemeRef);
                    }
                } catch (UnsupportedEncodingException e) {
                    LOGGER.error("Unsupported encoding UTF8");
                }

                activityResults.add(tempActivity);
            }
        }

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

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

License:Open Source License

/**
 * Get the publications that the staff member has been involved with
 * /*  w w w  .j  a  v  a2 s .co  m*/
 * @param staffId The staff id to find publications for
 * @return A list of publications
 */
public Publication[] getPublications(String staffId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session
                .createQuery("from ResearchOutputsDataAuthors where lower(chrStaffNumber) = :staffId");
        query.setParameter("staffId", staffId);

        @SuppressWarnings("unchecked")
        List<ResearchOutputsDataAuthors> authors = query.list();

        Vector<String> uniqueOutputs = new Vector<String>();

        String outputDataCode = "";

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

        for (ResearchOutputsDataAuthors author : authors) {
            outputDataCode = author.getResearchOutputsData1().getChrOutput6code();
            if (!uniqueOutputs.contains(outputDataCode)) {
                outputCode = author.getResearchOutputsData1();
                tempPublication = new PublicationImpl();

                // It appears that some rows have a number in an appropriate row to fetch data however there is no data associated with that row
                try {
                    if (outputCode.getResearchOutputsJournals() != null) {
                        tempPublication.setPublicationType("Journal");
                        tempPublication.setPublicationName(
                                outputCode.getResearchOutputsJournals().getChrJournalName());
                        tempPublication.setISSN(outputCode.getResearchOutputsJournals().getChrISSN());
                    } else if (outputCode.getResearchOutputsConferences() != null) {
                        tempPublication.setPublicationType("Conference");
                        tempPublication.setPublicationName(
                                outputCode.getResearchOutputsConferences().getChrConferenceName());
                        tempPublication.setISBN(outputCode.getResearchOutputsConferences().getChrISBN());
                    } else if (outputCode.getResearchOutputsBooks() != null) {
                        tempPublication.setPublicationType("Book Chapter");
                        tempPublication
                                .setPublicationName(outputCode.getResearchOutputsBooks().getChrBookName());
                        tempPublication.setISBN(outputCode.getResearchOutputsBooks().getChrISBN());
                    }
                } catch (ObjectNotFoundException e) {
                    LOGGER.error(
                            "Error retrieving either Journal, Conference or Book Information for record: {}",
                            outputCode.getChrOutput6code());
                }
                tempPublication.setPublicationDate(outputCode.getChrReportingYear());
                tempPublication.setPublicationCategory(
                        outputCode.getResearchOutputsLevel2().getChrOutput2Description());
                List<String> authorsList = new ArrayList<String>();

                for (ResearchOutputsDataAuthors auth : outputCode.getResearchOutputsDataAuthorses()) {
                    authorsList.add(auth.getChrStaffNumber());
                }
                tempPublication.setAuthors(authorsList.toArray(new String[0]));

                tempPublication.setPublicationTitle(outputCode.getChrPublicationTitle());

                if (outputCode.getForCodes1() != null) {
                    Subject forSubject = new FORSubjectImpl(outputCode.getForCodes1().getChrForObjectiveCode(),
                            outputCode.getForCodes1().getChrForDescription(),
                            outputCode.getChrForpercentage1());
                    tempPublication.setForSubject1(forSubject);
                }
                if (outputCode.getForCodes2() != null) {
                    Subject forSubject = new FORSubjectImpl(outputCode.getForCodes2().getChrForObjectiveCode(),
                            outputCode.getForCodes2().getChrForDescription(),
                            outputCode.getChrForpercentage2());
                    tempPublication.setForSubject2(forSubject);
                }
                if (outputCode.getForCodes3() != null) {
                    Subject forSubject = new FORSubjectImpl(outputCode.getForCodes3().getChrForObjectiveCode(),
                            outputCode.getForCodes3().getChrForDescription(),
                            outputCode.getChrForpercentage3());
                    tempPublication.setForSubject3(forSubject);
                }

                tempPublication.setAriesId(outputCode.getChrOutput6code());

                publications.add(tempPublication);
            }
        }

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

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

License:Open Source License

/**
 * Get the information about the staff member
 * //from   w w  w . j  a  v a2s.c  o  m
 * @param staffId The staff member to get information about
 * @return The staff members information
 */
public ANUStaff getStaffInformation(String staffId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from Useraccounts where lower(chrStaffNumber) = :staffId");
        query.setParameter("staffId", staffId.toLowerCase());

        Useraccounts userAccount = (Useraccounts) query.uniqueResult();

        if (userAccount == null) {
            return null;
        }
        ANUStaff staff = setStaffInformation(userAccount, session);

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

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

License:Open Source License

/**
 * Get external staff members information
 * //from  w  w  w . j  a va 2  s . co  m
 * @param staffId The id of the person to get staff information from
 * @return The external staff member information
 */
public ExternalStaff getExternalStaffInformation(String staffId) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery("from ExternalUsers where lower(chrCode) = :staffId");
        query.setParameter("staffId", staffId);

        ExternalUsers externalUser = (ExternalUsers) query.uniqueResult();
        if (externalUser == null) {
            return null;
        }
        ExternalStaff externalStaff = setExternalStaffInformation(externalUser);

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

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

License:Open Source License

/**
 * Find the staff by a name/*from   ww  w  .  ja  v a  2  s  .c  o m*/
 * 
 * @param surname The surname of the person to find
 * @param givenName The given name of the person to find
 * @return An array of staff
 */
public ANUStaff[] findStaff(String surname, String givenName) {
    Session session = AriesHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery(
                "from Useraccounts where lower(chrFirstname) = :firstname and lower(chrSurname) = :surname");
        query.setParameter("firstname", givenName);
        query.setParameter("surname", surname);

        @SuppressWarnings("unchecked")
        List<Useraccounts> users = query.list();
        List<ANUStaff> anuStaff = new ArrayList<ANUStaff>();
        ANUStaff staff = null;
        for (Useraccounts user : users) {
            staff = setStaffInformation(user, session);
            anuStaff.add(staff);
        }

        return anuStaff.toArray(new ANUStaff[0]);
    } finally {
        session.close();
    }
}