Example usage for org.hibernate StatelessSession createQuery

List of usage examples for org.hibernate StatelessSession createQuery

Introduction

In this page you can find the example usage for org.hibernate StatelessSession createQuery.

Prototype

@Override
    org.hibernate.query.Query createQuery(String queryString);

Source Link

Usage

From source file:edu.psu.iam.cpr.core.database.batch.PersonBio.java

License:Creative Commons License

/**
 * This method is use to obtain a campus code record based on the campus code.
 * @param campusCode contains the campus code to search for.
 * @return will return reference to a campus code bean if found.
 * @throws CprException will be thrown if the record was not found.
 *//* w  ww  .j  a v  a  2s . c  o  m*/
public CampusCs getCampusBean(final String campusCode) throws CprException {

    if (ValidateHelper.isFieldEmpty(campusCode)) {
        return null;
    }

    final StatelessSession session = getDatabaseSession();
    CampusCs campusCsBean = null;
    final Query query = session
            .createQuery("from CampusCs where campusCode = :campus_code and activeFlag = 'Y'");
    query.setParameter("campus_code", campusCode.toUpperCase());
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        campusCsBean = (CampusCs) it.next();
    }

    if (campusCsBean == null) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, "Campus Code");
    }
    return campusCsBean;
}

From source file:edu.psu.iam.cpr.core.database.batch.PersonBio.java

License:Creative Commons License

/**
 * This helper method is used to obtain a country bean based on the input country code.
 * @param countryCode contains the country code to search for.
 * @return will return a reference to the country bean.
 * @throws CprException will be thrown if there are any CPR related problems.
 *///from  w  w  w.j  ava2  s .  c  o m
public Country getCountryBean(final String countryCode) throws CprException {

    Country countryBean = null;
    Query query = null;
    final StatelessSession session = getDatabaseSession();

    // Two character codes are from IBIS.
    if (countryCode.length() == TWO_CHAR_COUNTRY_LENGTH) {
        final StringBuilder sb = new StringBuilder(300);
        sb.append("from Country where countryKey IN ");
        sb.append(
                "(SELECT countryKey FROM IrsCountry WHERE irsCountryCode = :country_code AND endDate IS NULL) ");
        sb.append("AND endDate IS NULL");
        query = session.createQuery(sb.toString());
    }

    // Three character codes are from ISIS.
    else if (countryCode.length() == THREE_CHAR_COUNTRY_LENGTH) {
        query = session.createQuery("from Country where countryCodeThree = :country_code and endDate is NULL");
    }

    // Something is wrong.
    else {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, "Country Code");
    }

    query.setParameter("country_code", countryCode.toUpperCase());
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        countryBean = (Country) it.next();
    }

    if (countryBean == null) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, "Country Code");
    }
    return countryBean;
}

From source file:edu.psu.iam.cpr.core.database.batch.StudentInfo.java

License:Creative Commons License

/**
 * Update or create student information.
 * @param semesterCode contains the semester code.
 * @param campusCode contains the campus code.
 * @param registrationStatus contains the student's registration status.
 * @param academicLevel contains the student's academic level.
 * @param yearTerm contains the student's year termination status.
 * @param graduatingFlag contains the graduating flag.
 * @param semLoaStart contains the LOA start date.
 * @param semLoaEnd contains the LOA end date.
 * @param dormLocation contains the dorm location code.
 * @param classLoad contains the class load.
 * @param studentAidFlag contains the student aid flag
 * @throws CprException will be thrown if there are any CPR related problems.
 *//*from w w  w.  ja  v  a2  s .co m*/
public void updateStudent(final String semesterCode, final String campusCode, final String registrationStatus,
        final String academicLevel, final String yearTerm, final String graduatingFlag,
        final String semLoaStart, final String semLoaEnd, final String dormLocation, final String classLoad,
        final String studentAidFlag) throws CprException {

    // do not attempt to update without a semester code.
    if (ValidateHelper.isFieldEmpty(semesterCode)) {
        return;
    }

    boolean matchFound = false;
    boolean changeFound = false;
    final Date d = getUpdateDate();

    // Perform a query to find the student record for the user based on semester code, there should only ever be one.
    final StatelessSession session = getDatabaseSession();
    final String updatedBy = getBatchDataSource().toString();
    final Query query = session
            .createQuery("from Student where personId = :person_id and semesterCode = :semester_code");
    query.setParameter(PERSON_ID, getPersonId());
    query.setParameter(SEMESTER_CODE, semesterCode);

    // Loop through all of the results.
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        matchFound = true;

        final Student bean = (Student) it.next();

        // Save the current student to the oldStudent for the history table using copy constructors.
        setOldStudent(new Student(bean));
        setNewStudent(new Student(bean));

        // If a campus code was specified, we need to obtain its key for storage in the database.
        if (!ValidateHelper.isFieldEmpty(campusCode)) {
            try {
                final CampusCs campusCsBean = getCampusBean(campusCode);
                if (!Utility.areLongFieldsEqual(bean.getCampusCodeKey(), campusCsBean.getCampusCodeKey())) {
                    newStudent.setCampusCodeKey(campusCsBean.getCampusCodeKey());
                    changeFound = true;
                }
            } catch (CprException e) {
                LOG4J_LOGGER.warn("Unknown campus code: " + campusCode + " for record " + e);
            }
        }

        if (!Utility.areStringFieldsEqual(bean.getAcademicLevel(), academicLevel)) {
            newStudent.setAcademicLevel(academicLevel);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getClassLoad(), classLoad)) {
            newStudent.setClassLoad(classLoad);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getDormLocation(), dormLocation)) {
            newStudent.setDormLocation(dormLocation);
            changeFound = true;
        }

        final String tempGraduatingFlag = Validate.isValidYesNo(graduatingFlag);
        if (!Utility.areStringFieldsEqual(bean.getGraduatingFlag(), tempGraduatingFlag)) {
            newStudent.setGraduatingFlag(tempGraduatingFlag);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getRegistrationStatus(), registrationStatus)) {
            newStudent.setRegistrationStatus(registrationStatus);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getSemLoaEnd(), semLoaEnd)) {
            newStudent.setSemLoaEnd(semLoaEnd);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getSemLoaStart(), semLoaStart)) {
            newStudent.setSemLoaStart(semLoaStart);
            changeFound = true;
        }

        if (!Utility.areStringFieldsEqual(bean.getYearTerm(), yearTerm)) {
            newStudent.setYearTerm(yearTerm);
            changeFound = true;
        }

        final String tempStudentAidFlag = Validate.isValidYesNo(studentAidFlag);
        if (!Utility.areStringFieldsEqual(bean.getStudentAidFlag(), tempStudentAidFlag)) {
            newStudent.setStudentAidFlag(tempStudentAidFlag);
            changeFound = true;
        }

        if (changeFound) {
            saveStudentHistory();

            newStudent.setStartDate(d);

            newStudent.setLastUpdateBy(updatedBy);
            newStudent.setLastUpdateOn(d);

            newStudent.setImportFrom(updatedBy);
            newStudent.setImportDate(d);

            getDatabaseSession().update(newStudent);
        }

        // Student found, but no changes, reset the beans to indicate that we do not need to do messaging.
        else {
            setOldStudent(null);
            setNewStudent(null);
        }

    }
    if (!matchFound) {
        addStudent(semesterCode, campusCode, registrationStatus, academicLevel, yearTerm, graduatingFlag,
                semLoaStart, semLoaEnd, dormLocation, classLoad, studentAidFlag);
    }

}

From source file:edu.psu.iam.cpr.core.database.batch.StudentInfo.java

License:Creative Commons License

/**
 * Update the student's academic college.
 * @param semesterCode contains the semester code. 
 * @param academicCollege contains the academic college code.
 * @param rank contains the rank./*from   w w  w  .ja v  a 2 s  . co  m*/
 * @throws CprException will be thrown if there are any cpr related problems.
 */
public void updateStudentAcademicCollege(final String semesterCode, final String academicCollege,
        final Long rank) throws CprException {
    // do not attempt to update without mandatory fields.
    if (ValidateHelper.isFieldEmpty(semesterCode) || ValidateHelper.isFieldEmpty(academicCollege)) {
        return;
    }
    boolean matchFound = false;
    boolean changeFound = false;
    final Date d = getUpdateDate();

    // Perform a query to find the student record for the user based on semester code, there should only ever be one.
    final StatelessSession session = getDatabaseSession();
    final String updatedBy = getBatchDataSource().toString();

    // See if they have a record for this rank
    final Query query = session.createQuery(
            "from StudentAcademicCollege where personId = :person_id and semesterCode = :semester_code and rank = :rank");
    query.setParameter(PERSON_ID, getPersonId());
    query.setParameter(SEMESTER_CODE, semesterCode);
    query.setParameter(RANK_LEVEL, rank);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        matchFound = true;
        final StudentAcademicCollege bean = (StudentAcademicCollege) it.next();

        setOldStudentAcademicCollege(new StudentAcademicCollege(bean));
        setNewStudentAcademicCollege(new StudentAcademicCollege(bean));

        if (!Utility.areStringFieldsEqual(bean.getCollegeCode(), academicCollege)) {
            newStudentAcademicCollege.setCollegeCode(academicCollege);
            changeFound = true;
        }

        if (changeFound) {
            saveStudAcademicCollegeHistory();

            newStudentAcademicCollege.setStartDate(d);

            newStudentAcademicCollege.setLastUpdateBy(updatedBy);
            newStudentAcademicCollege.setLastUpdateOn(d);

            newStudentAcademicCollege.setImportFrom(updatedBy);
            newStudentAcademicCollege.setImportDate(d);

            getDatabaseSession().update(newStudentAcademicCollege);
        }

        // Reset the beans because no changes were found.
        else {
            setOldStudentAcademicCollege(null);
            setNewStudentAcademicCollege(null);
        }
    }

    if ((!matchFound) && (!ValidateHelper.isFieldEmpty(academicCollege))) {
        addStudentAcademicCollege(semesterCode, academicCollege, rank);
    }
}

From source file:edu.psu.iam.cpr.core.database.batch.StudentInfo.java

License:Creative Commons License

/**
 * Update student's major./*from   www  . j  a  v a 2s .c om*/
 * @param semesterCode contains the semester code.
 * @param major contains the major code.
 * @param rank contains the rank.
 * @throws CprException will be thrown if there are any CPR related problems.
 */
public void updateStudentMajor(final String semesterCode, final String major, final Long rank)
        throws CprException {

    // do not attempt to update without mandatory fields.
    if (ValidateHelper.isFieldEmpty(semesterCode) || ValidateHelper.isFieldEmpty(major)) {
        return;
    }
    boolean matchFound = false;
    boolean changeFound = false;
    final Date d = getUpdateDate();

    // Perform a query to find the student record for the user based on semester code, there should only ever be one.
    final StatelessSession session = getDatabaseSession();
    final String updatedBy = getBatchDataSource().toString();

    // See if they have a record for this rank
    final Query query = session.createQuery(
            "from StudentMajor where personId = :person_id and semesterCode = :semester_code and rank = :rank");
    query.setParameter(PERSON_ID, getPersonId());
    query.setParameter(SEMESTER_CODE, semesterCode);
    query.setParameter(RANK_LEVEL, rank);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        matchFound = true;
        final StudentMajor bean = (StudentMajor) it.next();

        setOldStudentMajor(new StudentMajor(bean));
        setNewStudentMajor(new StudentMajor(bean));

        if (!Utility.areStringFieldsEqual(bean.getMajorCode(), major)) {
            newStudentMajor.setMajorCode(major);
            changeFound = true;
        }

        if (changeFound) {
            saveStudentMajorHistory();

            newStudentMajor.setStartDate(d);

            newStudentMajor.setLastUpdateBy(updatedBy);
            newStudentMajor.setLastUpdateOn(d);

            newStudentMajor.setImportFrom(updatedBy);
            newStudentMajor.setImportDate(d);

            getDatabaseSession().update(newStudentMajor);
        }

        // Reset the major beans, because no changes were made.
        else {
            setOldStudentMajor(null);
            setNewStudentMajor(null);
        }
    }

    if ((!matchFound) && (!ValidateHelper.isFieldEmpty(major))) {
        addStudentMajor(semesterCode, major, rank);
    }

}

From source file:edu.psu.iam.cpr.core.database.batch.StudentInfo.java

License:Creative Commons License

/**
 * Update student's academic department.
 * @param semesterCode contains the semester code that is being processed.
 * @param academicDepartment contains the student's academic department code.
 * @param rank contains the rank./*from   w w w . j a va  2 s  .  c o  m*/
 * @throws CprException will be thrown if there are any CPR related problems.
 */
public void updateStudentAcademicDepartment(final String semesterCode, final String academicDepartment,
        final Long rank) throws CprException {

    // do not attempt to update without mandatory fields.
    if (ValidateHelper.isFieldEmpty(semesterCode) || ValidateHelper.isFieldEmpty(academicDepartment)) {
        return;
    }

    boolean matchFound = false;
    boolean changeFound = false;
    final Date d = getUpdateDate();

    // Perform a query to find the student record for the user based on semester code, there should only ever be one.
    final StatelessSession session = getDatabaseSession();
    final String updatedBy = getBatchDataSource().toString();

    // See if they have a record for this rank
    final Query query = session.createQuery(
            "from StudentAcademicDepartment where personId = :person_id and semesterCode = :semester_code and rank = :rank");
    query.setParameter(PERSON_ID, getPersonId());
    query.setParameter(SEMESTER_CODE, semesterCode);
    query.setParameter(RANK_LEVEL, rank);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        matchFound = true;
        final StudentAcademicDepartment bean = (StudentAcademicDepartment) it.next();

        // Save off two copies of the beans using copy constructors.
        setOldStudentAcademicDepartment(new StudentAcademicDepartment(bean));
        setNewStudentAcademicDepartment(new StudentAcademicDepartment(bean));

        if (!Utility.areStringFieldsEqual(bean.getDepartmentCode(), academicDepartment)) {
            newStudentAcademicDepartment.setDepartmentCode(academicDepartment);
            changeFound = true;
        }

        if (changeFound) {
            saveStudentAcademicDepartmentHistory();

            newStudentAcademicDepartment.setStartDate(d);

            newStudentAcademicDepartment.setLastUpdateBy(updatedBy);
            newStudentAcademicDepartment.setLastUpdateOn(d);

            newStudentAcademicDepartment.setImportFrom(updatedBy);
            newStudentAcademicDepartment.setImportDate(d);

            getDatabaseSession().update(newStudentAcademicDepartment);
        }

        // No changes were major for student academic department.
        else {
            setOldStudentAcademicDepartment(null);
            setNewStudentAcademicDepartment(null);
        }
    }

    if ((!matchFound) && (!ValidateHelper.isFieldEmpty(academicDepartment))) {
        addStudentAcademicDepartment(semesterCode, academicDepartment, rank);
    }
}

From source file:edu.psu.iam.cpr.core.database.postprocess.AddressPostProcessor.java

License:Creative Commons License

/**
 * This method is used to process an address change for a person and determine what records need to be reconciled based on the 
 * type of change./*from  ww  w  .  j a  v a  2 s . co  m*/
 * @param personId contains the person identifier whose address was changed.
 * @param addressType contains the type of address to be processed.
 */
public void processAddressChange(final long personId, final AddressType addressType) {

    resetHistoryBeans();

    // For the person, find all of the address changes, order by descending order, so the most recent one will be at the top.
    final StatelessSession db = getStatelessSession();
    final Query query = db.createQuery(
            "from AddressStaging where personId = :person_id AND dataTypeKey = :data_type_key ORDER BY importDate DESC");
    query.setParameter(PERSON_ID, personId);
    query.setParameter(DATA_TYPE_KEY, addressType.index());

    final int numberOfResults = query.list().size();

    AddressStaging bean = null;

    // Did we get any results back?
    if (numberOfResults >= ONE) {

        // The first result is the most recent.
        bean = (AddressStaging) query.list().get(0);

        // If there is only one address change, that address should be set as the new address
        if (numberOfResults == ONE) {
            setChangedAddress(personId, addressType, bean);
        }

        // If there is more than one address, we need to apply the business rules.
        else if (numberOfResults > ONE) {
            applyAddressRules(personId, addressType, bean);
        }
    }
}

From source file:edu.psu.iam.cpr.core.database.postprocess.AddressPostProcessor.java

License:Creative Commons License

/**
 * This is the method that sets the final permanent address.
 * @param personId contains the person identifier whose address is to be set.
 * @param addressType contains the type of address to be set.
 * @param addressStagingBean contains the address to be set.
 *//*from w  ww . jav  a  2s .  co  m*/
public void setChangedAddress(final long personId, final AddressType addressType,
        final AddressStaging addressStagingBean) {

    final StatelessSession db = getStatelessSession();

    final Query query = db.createQuery(
            "from Addresses where personId = :person_id AND dataTypeKey = :data_type_key AND endDate is NULL");
    query.setParameter(PERSON_ID, personId);
    query.setParameter(DATA_TYPE_KEY, addressType.index());

    boolean matchFound = false;

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        final Addresses bean = (Addresses) it.next();

        // Did we find a match using a match code?
        if (CprProperties.INSTANCE.getProperties()
                .getProperty(CprPropertyName.CPR_MATCHING_ALGORITHM.toString())
                .equals(MatchingAlgorithmType.PENN_STATE.toString())) {
            if (Utility.areStringFieldsEqual(addressStagingBean.getAddressMatchCode(),
                    bean.getAddressMatchCode())
                    && Utility.areStringFieldsEqual(addressStagingBean.getCityMatchCode(),
                            bean.getCityMatchCode())) {
                matchFound = true;
            }
            // No match, so expire existing address, because we are going to add a new one.
            else {
                expireAddress(db, bean, addressStagingBean.getImportFrom());
            }
        } else {
            if (Utility.areStringFieldsEqual(addressStagingBean.getAddress1(), bean.getAddress1())
                    && Utility.areStringFieldsEqual(addressStagingBean.getCity(), bean.getCity())) {
                matchFound = true;
            }
            // No match, so expire existing address, because we are going to add a new one.
            else {
                expireAddress(db, bean, addressStagingBean.getImportFrom());
            }
        }
    }
    // We did not find a match, so its time to add a new name.
    if (!matchFound) {
        addAddress(db, addressType, addressStagingBean);
    }
}

From source file:edu.psu.iam.cpr.core.database.postprocess.AddressPostProcessor.java

License:Creative Commons License

/**
 * This method is used to add a new permanent address to the database.
 * @param db contains the database session.
 * @param addressType contains the type of address to be added.
 * @param addressStagingBean contains the address bean.
 *///from w ww.  j  av a  2 s  .c  o  m
public void addAddress(final StatelessSession db, final AddressType addressType,
        final AddressStaging addressStagingBean) {

    final Addresses bean = new Addresses();
    final Date d = new Date();

    bean.setPersonId(addressStagingBean.getPersonId());

    bean.setDataTypeKey(addressType.index());
    bean.setDocumentTypeKey(null);

    // get the last group id for this address type for this person so can set the correct group id
    Long groupId = null;
    final Query maxQuery = db.createQuery(
            "select max(groupId) from Addresses where personId = :person_id and dataTypeKey = :data_type_key");
    maxQuery.setParameter(PERSON_ID, bean.getPersonId());
    maxQuery.setParameter(DATA_TYPE_KEY, bean.getDataTypeKey());
    groupId = (Long) maxQuery.list().get(0);
    if (groupId == null) {
        groupId = 1L;
    } else {
        groupId++;
    }
    bean.setGroupId(groupId);
    bean.setAddressMatchCode(addressStagingBean.getAddressMatchCode());

    if (addressStagingBean.getCampusCode() != null) {
        bean.setCampusCodeKey(campusMap.get(addressStagingBean.getCampusCode()));
    }

    if (addressStagingBean.getCountryCodeTwo() != null) {
        bean.setCountryKey(countryTwoMap.get(addressStagingBean.getCountryCodeTwo()));
    } else if (addressStagingBean.getCountryCodeThree() != null) {
        bean.setCountryKey(countryThreeMap.get(addressStagingBean.getCountryCodeThree()));
    }

    bean.setProvince(addressStagingBean.getProvince());

    bean.setAddress1(addressStagingBean.getAddress1());
    bean.setAddress2(addressStagingBean.getAddress2());
    bean.setAddress3(addressStagingBean.getAddress3());

    bean.setCity(addressStagingBean.getCity());
    bean.setCityMatchCode(addressStagingBean.getCityMatchCode());

    bean.setState(addressStagingBean.getState());
    bean.setPostalCode(addressStagingBean.getPostalCode());

    bean.setPrimaryFlag(NO);
    bean.setVerifiedFlag(NO);
    bean.setDoNotVerifyFlag(NO);

    bean.setStartDate(d);
    bean.setEndDate(null);

    bean.setCreatedBy(addressStagingBean.getImportFrom());
    bean.setCreatedOn(d);

    bean.setLastUpdateBy(addressStagingBean.getImportFrom());
    bean.setLastUpdateOn(d);

    bean.setImportFrom(addressStagingBean.getImportFrom());
    bean.setImportDate(d);

    db.insert(bean);

    setNewAddressBean(bean);
}

From source file:edu.psu.iam.cpr.core.database.postprocess.AddressPostProcessor.java

License:Creative Commons License

/**
 * This helper method is used to obtains all of the affiliations for a person and determine whether they are 
 * either an employee or a student or both.
 * @param personId contains the person identifier parameter.
 *//*from  w ww  .j av a2  s .c  om*/
public void getAndEvaluateAffiliations(final long personId) {

    setStudent(false);
    setEmployee(false);

    final StatelessSession db = getStatelessSession();
    final Query query = db
            .createQuery("from PersonAffiliation where personId = :person_id and endDate is null");
    query.setParameter(PERSON_ID, personId);
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        final PersonAffiliation bean = (PersonAffiliation) it.next();
        final AffiliationsType affiliationsType = AffiliationsType.get(bean.getAffiliationKey());

        if (affiliationsType == AffiliationsType.STUDENT) {
            setStudent(true);
        } else if (affiliationsType == AffiliationsType.STAFF || affiliationsType == AffiliationsType.FACULTY
                || affiliationsType == AffiliationsType.EMPLOYEE
                || affiliationsType == AffiliationsType.EMERITUS
                || affiliationsType == AffiliationsType.RETIREE) {
            setEmployee(true);
        }
    }

}