Example usage for org.hibernate StatelessSession beginTransaction

List of usage examples for org.hibernate StatelessSession beginTransaction

Introduction

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

Prototype

Transaction beginTransaction();

Source Link

Document

Begin a unit of work and return the associated Transaction object.

Usage

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateGenericVulnerabilityDao.java

License:Mozilla Public License

@Override
public void insertAll(List<GenericVulnerability> vulnerabilityList) {

    StatelessSession statelessSession = sessionFactory.openStatelessSession();
    Transaction transaction = statelessSession.beginTransaction();

    try {/*ww w.  j ava2  s.co m*/
        for (GenericVulnerability vulnerability : vulnerabilityList) {
            statelessSession.insert(vulnerability);
        }

        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
    } finally {
        statelessSession.close();
    }
}

From source file:com.romeikat.datamessie.core.base.util.execute.ExecuteWithTransactionAndResult.java

License:Open Source License

private Transaction beginTransaction(final StatelessSession statelessSession) {
    return statelessSession.beginTransaction();
}

From source file:com.trifork.stamdata.persistence.StatelessPersistenceFilter.java

License:Mozilla Public License

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    StatelessSession session = null;

    try {/*  ww  w . ja  va2s. c  o  m*/
        session = sessionProvider.get();
        session.beginTransaction();
        Transaction transaction = session.beginTransaction();
        transaction.setTimeout(5);

        chain.doFilter(request, response);

        session.getTransaction().commit();
    } catch (Exception e) {
        try {
            session.getTransaction().rollback();
        } catch (Exception ex) {
        }

        throw new ServletException(e);
    }
}

From source file:edu.psu.iam.cpr.batch.processor.impl.AddressBatchProcessor.java

License:Creative Commons License

/**
 * This method implements the core logic of the batch processor.
 * @param databaseSession contains the database session from the abstract class that will be used to iterator over the individual records.
 * @param messagingCore contains a reference to the messaging infrastructure.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *//*from   w w  w  .j  a va  2s.  c om*/
@Override
public void implementBatchProcessor(StatelessSession databaseSession, MessagingCore messagingCore,
        DataQualityService dataQualityService)
        throws CprException, ParseException, JSONException, JMSException {

    final Date d = new Date();

    // Calculate the start and end date/times based on the current date.
    setStartDateTime(Utility.makeStartDate(d));
    setEndDateTime(Utility.makeEndDate(d));

    // Do a select to find all of the people who have had an address changed for the current date.
    final Query query = databaseSession
            .createQuery("from AddressStaging where importDate BETWEEN :start_date AND :end_date");
    query.setFetchSize(RECORD_FETCH_SIZE);
    query.setParameter("start_date", startDateTime);
    query.setParameter("end_date", endDateTime);

    // Init some objects.
    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final AddressPostProcessor addressPostProcessor = new AddressPostProcessor(recordSession, messagingCore);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    final PersonBio personBio = new PersonBio(recordSession, BatchDataSource.ADDRESS_POSTPROCESS,
            dataQualityService);
    Transaction tx = null;

    // Loop through the results.
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        AddressStaging bean = (AddressStaging) it.next();
        final Long personId = bean.getPersonId();

        try {

            tx = recordSession.beginTransaction();

            // Process an address change for a person.
            addressPostProcessor.resetHistoryBeans();
            addressPostProcessor.processAddressChange(personId, AddressType.get(bean.getDataTypeKey()));

            // Only check for messaging if there was a change.
            if (addressPostProcessor.getOldAddressBean() != null
                    || addressPostProcessor.getNewAddressBean() != null) {

                // Find the person using their person identifier.
                Long pid = personBio.findPersonUsingPersonId(personId);
                if (pid != null && pid.equals(personId)) {

                    // Set the required information for a json message.
                    if (addressPostProcessor.getNewAddressBean() != null) {

                        changeNotification.setRequiredInfo(personId, personBio.getPsuIdNumber(),
                                personBio.getPrimaryUserid(),
                                addressPostProcessor.getNewAddressBean().getImportFrom());

                        // Address change.
                        changeNotification.addressChange(addressPostProcessor.getOldAddressBean(),
                                addressPostProcessor.getNewAddressBean());
                    } else {
                        LOG.info(
                                "Address Post Process Batch: expired old address, but did not add a new one for person identifier : "
                                        + personId);
                    }
                }
            }

            tx.commit();
        } catch (HibernateException e) { // $codepro.audit.disable logExceptions
            LOG.error("Address Post Processor Batch: error encountered person identifier : " + personId);
            tx.rollback();
            recordSession.close();
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            addressPostProcessor.setStatelessSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            personBio.setDatabaseSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (Exception e) { // $codepro.audit.disable emptyCatchClause
    }

}

From source file:edu.psu.iam.cpr.batch.processor.impl.EmployeeBatchProcessor.java

License:Creative Commons License

@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService) throws CprException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Perform a query for all of the trans empl records.
    final Query query = databaseSession.createQuery("FROM TransEmpl ORDER BY codeApptType ASC");
    query.setFetchSize(RECORD_FETCH_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final EmployeeInfo employeeInfo = new EmployeeInfo(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    affiliationCalculator = new EduPersonAffiliationCalculator(recordSession, BATCH_DATA_SOURCE,
            dataQualityService);// w  ww. j a  v  a 2 s.co  m

    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransEmpl transEmpl = (TransEmpl) it.next();
        recordsProcessed++;

        fixBogusData(transEmpl);

        try {
            // Begin transaction.
            tx = recordSession.beginTransaction();

            employeeInfo.resetHistoryBeans();
            affiliationCalculator.resetHistoryBeans();

            employeeInfo.setRecordNumber(transEmpl.getTransEmplKey().longValue());
            employeeInfo.findPerson(transEmpl.getPsuId());

            final Long personId = employeeInfo.getPersonId();
            if (personId == null) {
                addEmployee(transEmpl, employeeInfo, changeNotification);
            } else {
                updateEmployee(transEmpl, employeeInfo, changeNotification);
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException ex) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transEmpl.getTransEmplKey(), ex);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            employeeInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            affiliationCalculator.setDatabaseSession(recordSession);
        } catch (final CprException ex) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transEmpl.getTransEmplKey(), ex);
            throw ex;
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) {

        // Rollback the transaction, close the session.
        tx.rollback();
        recordSession.close();

        // We need to create a new session and update the helper classes with the new session.
        recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
        employeeInfo.setDatabaseSession(recordSession);
        changeNotification.setStatelessSession(recordSession);
        affiliationCalculator.setDatabaseSession(recordSession);
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.psu.iam.cpr.batch.processor.impl.HMCBatchProcessor.java

License:Creative Commons License

/**
 * This method provides the implementation of the Hershey medical center processor.
 * @param databaseSession contains an instance of the database session.
 * @param messagingCore contains the messaging core instance.
 * @param dataQualityService contains an instance to the data flux server.
 * @throws CprException will be thrown if there are any Cpr relate problems.
 *//*from   w  w  w. j a v  a  2s  .c o  m*/
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService) throws CprException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Perform a query for all of the trans hershey records.
    final Query query = databaseSession
            .createQuery("FROM " + TRANS_DATABASE_TABLE + " ORDER BY transHersheyKey ASC");
    query.setFetchSize(RECORD_FETCH_SIZE);
    // Below line should be uncommented during manual testing to only allow a certain number of records through at a time.
    //      query.setMaxResults(TEST_RECORD_MAX_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final EmployeeInfo employeeInfo = new EmployeeInfo(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    affiliationCalculator = new EduPersonAffiliationCalculator(recordSession, BATCH_DATA_SOURCE,
            dataQualityService);

    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransHershey transHershey = (TransHershey) it.next();
        recordsProcessed++;

        try {
            // Begin transaction.
            tx = recordSession.beginTransaction();

            employeeInfo.resetHistoryBeans();
            affiliationCalculator.resetHistoryBeans();

            employeeInfo.setRecordNumber(transHershey.getTransHersheyKey().longValue());
            employeeInfo.findPerson(transHershey.getPsuId());
            final Long personId = employeeInfo.getPersonId();
            if (personId == null) {

                // The psu id might have been merged or reassigned.
                // Call CIDR to check. If so, do an update.

                addEmployee(transHershey, employeeInfo, changeNotification);
            } else {
                updateEmployee(transHershey, employeeInfo, changeNotification);
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transHershey.getTransHersheyKey(), e);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the helper classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            employeeInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            affiliationCalculator.setDatabaseSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

        // Rollback the transaction, close the session.
        tx.rollback();
        recordSession.close();

        // We need to create a new session and update the helper classes with the new session.
        recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
        employeeInfo.setDatabaseSession(recordSession);
        changeNotification.setStatelessSession(recordSession);
        affiliationCalculator.setDatabaseSession(recordSession);
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.psu.iam.cpr.batch.processor.impl.NamesBatchProcessor.java

License:Creative Commons License

/**
 * This method implements the core logic of the batch processor.
 * @param databaseSession contains the database session from the abstract class that will be used to iterator over the individual records.
 * @param messagingCore contains a reference to the messaging infrastructure.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *///from w  w  w .jav  a 2s .c o m
@Override
public void implementBatchProcessor(StatelessSession databaseSession, MessagingCore messagingCore,
        DataQualityService dataQualityService)
        throws CprException, ParseException, JSONException, JMSException {

    final Date d = new Date();
    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Calculate the start and end date/times based on the current date.
    setStartDateTime(Utility.makeStartDate(d));
    setEndDateTime(Utility.makeEndDate(d));

    // Do a select to find all of the people who have had a name changed for the current date.
    final Query query = databaseSession.createQuery(
            "select distinct personId from NamesStaging where importDate BETWEEN :start_date AND :end_date");
    query.setFetchSize(RECORD_FETCH_SIZE);
    query.setParameter("start_date", startDateTime);
    query.setParameter("end_date", endDateTime);
    totalRecords = query.list().size();

    // Init some objects.
    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final NamesPostProcessor namesPostProcessor = new NamesPostProcessor(recordSession, messagingCore);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    final PersonBio personBio = new PersonBio(recordSession, BatchDataSource.NAME_POSTPROCESS,
            dataQualityService);
    Transaction tx = null;

    // Loop through the results.
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Long personId = (Long) it.next();

        try {

            tx = recordSession.beginTransaction();

            // Process a name change for a person.
            namesPostProcessor.resetHistoryBeans();
            namesPostProcessor.processNameChange(personId);

            // Only check for messaging if there was a change.
            if (namesPostProcessor.getOldNamesBean() != null || namesPostProcessor.getNewNamesBean() != null) {

                // Find the person using their person identifier.
                Long pid = personBio.findPersonUsingPersonId(personId);
                if (pid != null && pid.equals(personId)) {

                    // Set the required information for a json message.
                    changeNotification.setRequiredInfo(personId, personBio.getPsuIdNumber(),
                            personBio.getPrimaryUserid(), namesPostProcessor.getNewNamesBean().getImportFrom());

                    // Name change.
                    changeNotification.nameChange(namesPostProcessor.getOldNamesBean(),
                            namesPostProcessor.getNewNamesBean());
                }
            }

            tx.commit();
            recordsProcessed++;

        } catch (HibernateException e) {
            LOG.info("Names Post Processor Batch: error encountered person identifier : " + personId);
            tx.rollback();
            recordSession.close();
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            namesPostProcessor.setStatelessSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            personBio.setDatabaseSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (Exception e) { // $codepro.audit.disable emptyCatchClause
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BatchDataSource.NAME_POSTPROCESS.toString() + " Batch: processed " + recordsProcessed
            + " records out of " + totalRecords + " in " + elapsedTime + " seconds");

}

From source file:edu.psu.iam.cpr.batch.processor.impl.OasisBatchProcessor.java

License:Creative Commons License

/**
 * This method implements the core logic of the batch processor.
 * @param databaseSession contains the database session from the abstract class that will be used to iterator over the individual records.
 * @param messagingCore contains a reference to the messaging infrastructure.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *///from  w ww  .  ja  va 2 s .  c o m
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService) throws CprException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Perform a query for all of the trans oasis records.
    final Query query = databaseSession.createQuery("from " + TRANS_DATABASE_TABLE);
    query.setFetchSize(RECORD_FETCH_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final PersonBio personBio = new PersonBio(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final AccessAccountStatus accessAccountStatus = new AccessAccountStatus(recordSession, messagingCore);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransOasis transOasis = (TransOasis) it.next();
        recordsProcessed++;
        try {
            // Begin transaction.
            tx = recordSession.beginTransaction();

            // Using person bio, find the person and their associated userid.  NOTE: if the userid is not in the userid table, person bio will add it.
            personBio.resetHistoryBeans();
            personBio.findPerson(transOasis.getPsuId());
            final Long personId = personBio.getPersonId();
            if (personId == null) {
                LOG.info(BATCH_DATA_SOURCE.toString()
                        + " Batch: psu id number not found error encountered on record #: "
                        + transOasis.getTransOasisKey());
            } else {
                final String userid = transOasis.getUserid().toLowerCase();
                personBio.updateUserid(userid);

                final String status = transOasis.getStatus();

                // Update the status.
                accessAccountStatus.processStatusChange(personId, userid, AccessAccountStatusType.get(status));

                if (personBio.getPrimaryUserid() == null && personBio.getNewUserid() != null) {
                    personBio.setPrimaryUserid(personBio.getNewUserid().getUserid());
                }

                changeNotification.setRequiredInfo(personId, personBio.getPsuIdNumber(),
                        personBio.getPrimaryUserid(), BATCH_DATA_SOURCE.toString());

                changeNotification.useridChange(personBio.getOldUserid(), personBio.getNewUserid());

                if (accessAccountStatus.getOldAccessAccountStatus() != accessAccountStatus
                        .getNewAccessAccountStatus()) {
                    changeNotification.accountStatusChange(accessAccountStatus.getOldAccessAccountStatus(),
                            accessAccountStatus.getNewAccessAccountStatus());
                }
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transOasis.getTransOasisKey());

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio and access account status classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            personBio.setDatabaseSession(recordSession);
            accessAccountStatus.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        } catch (final CprException ex) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transOasis.getTransOasisKey(), ex);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio and access account status classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            personBio.setDatabaseSession(recordSession);
            accessAccountStatus.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable emptyCatchClause, logExceptions
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.psu.iam.cpr.batch.processor.impl.StudentBatchProcessor.java

License:Creative Commons License

/**
 * This method provides the implementation of the student processor.
 * @param databaseSession contains an instance of the database session.
 * @param messagingCore contains the messaging core instance.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *//*from w w w .  j  ava2s.co  m*/
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService)
        throws CprException, ParseException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    String currentSemester = null;
    // Get the current semester
    final Date d = new Date();
    Query query = databaseSession
            .createQuery("from Semesters where :now >= semStartDate AND :now < semEndDate");
    query.setParameter(NOW, d);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        final Semesters bean = (Semesters) it.next();
        currentSemester = bean.getSemesterCode();
    }

    // Perform a query for all of the trans stubio records.
    query = databaseSession
            .createQuery("from " + TRANS_DATABASE_TABLE + " order by sem asc, codeStudStat desc");
    query.setFetchSize(RECORD_FETCH_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final StudentInfo studentInfo = new StudentInfo(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final EduPersonAffiliationCalculator eduPersonAffiliationCalculator = new EduPersonAffiliationCalculator(
            recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransStubio transStubio = (TransStubio) it.next();
        recordsProcessed++;
        try {
            final String semesterCode = transStubio.getSem();
            // Begin transaction.
            tx = recordSession.beginTransaction();

            studentInfo.resetHistoryBeans();

            Long personId = studentInfo.findPerson(transStubio.getPsuId());
            if (personId == null && transStubio.getPsuIdPrev() != null) {
                personId = studentInfo.findPerson(transStubio.getPsuIdPrev());

                // Update PSU ID
                if (personId != null) {
                    studentInfo.updatePsuId(transStubio.getPsuId());
                }
            }

            // User was not found, so we need to add them.
            if (personId == null) {

                // Create a new person
                studentInfo.addPerson();

                // Add their PSU ID
                studentInfo.addPsuId(transStubio.getPsuId());

                // Add Date of birth
                studentInfo.addDateOfBirth(parseDateString(transStubio.getDatePersBirth()));

                // Add Gender if not null
                if (!ValidateHelper.isFieldEmpty(transStubio.getCodePersSex())) {
                    GenderType genderType;
                    try {
                        genderType = Utility.genderStringToType(transStubio.getCodePersSex());
                        studentInfo.addGender(genderType);
                    } catch (final IllegalArgumentException e) {
                        LOG.error("Invalid gender for record " + transStubio.getTransStubioKey());
                    }

                }

                // Update phones
                studentInfo.updatePhone(PhoneType.LOCAL_PHONE, transStubio.getPhoneLocal(), null);
                final Phones newPhone = studentInfo.getNewPhone();
                studentInfo.updatePhone(PhoneType.PERMANENT_PHONE, transStubio.getPhoneHome(), null);

                // Add name
                studentInfo.addName(transStubio.getNamePersFirst(), transStubio.getNamePersMid(),
                        transStubio.getNamePersLast(), transStubio.getNamePersSfx());

                // Update addresses
                studentInfo.updateAddress(AddressType.LOCAL_ADDRESS, transStubio.getAddrLoclSt1(),
                        transStubio.getAddrLoclSt2(), transStubio.getAddrLoclSt3(),
                        transStubio.getAddrLoclCity(), transStubio.getAddrLoclState(),
                        transStubio.getAddrLoclZip(), transStubio.getAddrLoclCtry(), transStubio.getCodeCamp());
                final Addresses newAddress = studentInfo.getNewAddress();

                studentInfo.updateAddress(AddressType.PERMANENT_ADDRESS, transStubio.getAddrHomeSt1(),
                        transStubio.getAddrHomeSt2(), transStubio.getAddrHomeSt3(),
                        transStubio.getAddrHomeCity(), transStubio.getAddrHomeState(),
                        transStubio.getAddrHomeZip(), transStubio.getAddrHomeCtry(), null);

                // Add the student record
                if (transStubio.getCodeStudStat() != null && transStubio.getCodeStudLvl() != null) {
                    studentInfo.addStudent(semesterCode, transStubio.getCodeCamp(),
                            transStubio.getCodeStudStat(), transStubio.getCodeStudLvl(),
                            transStubio.getCodeStudClsfctnYrtm(), transStubio.getIndcStudGraduation(),
                            transStubio.getLoaStart(), transStubio.getLoaReturnSt1(), transStubio.getCodeHotl(),
                            transStubio.getClassLoad(), transStubio.getStudentAid());

                    // Add the student's academic colleges
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl1(),
                            LONG_ONE);
                    final StudentAcademicCollege newStudentAcademicCollege = studentInfo
                            .getNewStudentAcademicCollege();
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl2(),
                            LONG_TWO);
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl3(),
                            LONG_THREE);
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl4(),
                            LONG_FOUR);

                    // Update the student's academic departments
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt1(),
                            LONG_ONE);
                    final StudentAcademicDepartment newStudentAcademicDepartment = studentInfo
                            .getNewStudentAcademicDepartment();
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt2(),
                            LONG_TWO);
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt3(),
                            LONG_THREE);
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt4(),
                            LONG_FOUR);

                    // Update the student's majors
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr1(), LONG_ONE);
                    final StudentMajor newStudentMajor = studentInfo.getNewStudentMajor();
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr2(), LONG_TWO);
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr3(), LONG_THREE);
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr4(), LONG_FOUR);

                    changeNotification.setRequiredInfo(studentInfo.getPersonId(), studentInfo.getPsuIdNumber(),
                            null, BATCH_DATA_SOURCE.toString());
                    if (Utility.areStringFieldsEqual(currentSemester, semesterCode)) {
                        eduPersonAffiliationCalculator.setPersonId(studentInfo.getPersonId());
                        eduPersonAffiliationCalculator.setStudentAffiliation(transStubio.getCodeStudStat());
                        changeNotification.primaryAffiliationChange(
                                eduPersonAffiliationCalculator.getOldPersonAffiliation(),
                                eduPersonAffiliationCalculator.getNewPersonAffiliation());
                    }
                    changeNotification.newStudent(studentInfo.getNewName(), newAddress, newPhone, null, // email address.
                            eduPersonAffiliationCalculator.getNewPersonAffiliation(), // affiliation.
                            studentInfo.getNewPersonGender(), null, // confidentiality
                            studentInfo.getNewStudent(), newStudentAcademicCollege,
                            newStudentAcademicDepartment, newStudentMajor);
                } else {
                    LOG.error("Skipping student record: " + transStubio.getTransStubioKey()
                            + " due to a lack of data.");
                }

            }

            // User was found.
            else {

                changeNotification.setRequiredInfo(studentInfo.getPersonId(), studentInfo.getPsuIdNumber(),
                        studentInfo.getPrimaryUserid(), BATCH_DATA_SOURCE.toString());

                // Update name - change name for name will be done in the names post processor.
                studentInfo.updateName(transStubio.getNamePersFirst(), transStubio.getNamePersMid(),
                        transStubio.getNamePersLast(), transStubio.getNamePersSfx());

                // Update addresses
                studentInfo.updateAddress(AddressType.LOCAL_ADDRESS, transStubio.getAddrLoclSt1(),
                        transStubio.getAddrLoclSt2(), transStubio.getAddrLoclSt3(),
                        transStubio.getAddrLoclCity(), transStubio.getAddrLoclState(),
                        transStubio.getAddrLoclZip(), transStubio.getAddrLoclCtry(), transStubio.getCodeCamp());
                changeNotification.addressChange(studentInfo.getOldAddress(), studentInfo.getNewAddress());

                studentInfo.updateAddress(AddressType.PERMANENT_ADDRESS, transStubio.getAddrHomeSt1(),
                        transStubio.getAddrHomeSt2(), transStubio.getAddrHomeSt3(),
                        transStubio.getAddrHomeCity(), transStubio.getAddrHomeState(),
                        transStubio.getAddrHomeZip(), transStubio.getAddrHomeCtry(), null);

                // Update Date of birth
                studentInfo.updateDateOfBirth(parseDateString(transStubio.getDatePersBirth()));
                changeNotification.dateOfBirthChange(studentInfo.getOldDateOfBirth(),
                        studentInfo.getNewDateOfBirth());
                //TODO Add change notifications for date of birth.

                // Update Gender if not null
                if (!ValidateHelper.isFieldEmpty(transStubio.getCodePersSex())) {
                    try {
                        final GenderType genderType = Utility.genderStringToType(transStubio.getCodePersSex());
                        studentInfo.updateGender(genderType);
                        changeNotification.genderChange(studentInfo.getOldPersonGender(),
                                studentInfo.getNewPersonGender());
                    } catch (final IllegalArgumentException e) {
                        LOG.error("Invalid gender for record " + transStubio.getTransStubioKey());
                    }
                }

                // Update phones
                studentInfo.updatePhone(PhoneType.LOCAL_PHONE, transStubio.getPhoneLocal(), null);
                changeNotification.phoneChange(studentInfo.getOldPhone(), studentInfo.getNewPhone());

                studentInfo.updatePhone(PhoneType.PERMANENT_PHONE, transStubio.getPhoneHome(), null);

                boolean processRecord = true;

                // If the student status and academic level are null, we need to do an additional check.
                if (transStubio.getCodeStudStat() == null && transStubio.getCodeStudLvl() == null) {

                    // Pull the number of records for the current user and their semester code.
                    final String sqlQuery = "from TransStubio where psuId = :psu_id AND sem = :sem";
                    final Query countQuery = recordSession.createQuery(sqlQuery);
                    countQuery.setParameter("psu_id", transStubio.getPsuId());
                    countQuery.setParameter("sem", transStubio.getSem());

                    // If there are more than one record, we need to skip this one because it contains null information.  Based
                    // on analysis there will be another record in the file that will contain real data.  This null record must
                    // be skipping.
                    if (countQuery.list().size() > 1) {
                        processRecord = false;
                    }
                }

                // Check to see if we are going to process the record...
                if (processRecord) {
                    studentInfo.updateStudent(semesterCode, transStubio.getCodeCamp(),
                            transStubio.getCodeStudStat(), transStubio.getCodeStudLvl(),
                            transStubio.getCodeStudClsfctnYrtm(), transStubio.getIndcStudGraduation(),
                            transStubio.getLoaStart(), transStubio.getLoaReturnSt1(), transStubio.getCodeHotl(),
                            transStubio.getClassLoad(), transStubio.getStudentAid());

                    // Update the student's academic colleges
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl1(),
                            LONG_ONE);
                    final StudentAcademicCollege oldStudentAcademicCollege = studentInfo
                            .getOldStudentAcademicCollege();
                    final StudentAcademicCollege newStudentAcademicCollege = studentInfo
                            .getNewStudentAcademicCollege();
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl2(),
                            LONG_TWO);
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl3(),
                            LONG_THREE);
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl4(),
                            LONG_FOUR);

                    // Update the student's academic departments
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt1(),
                            LONG_ONE);
                    final StudentAcademicDepartment oldStudentAcademicDepartment = studentInfo
                            .getOldStudentAcademicDepartment();
                    final StudentAcademicDepartment newStudentAcademicDepartment = studentInfo
                            .getNewStudentAcademicDepartment();
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt2(),
                            LONG_TWO);
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt3(),
                            LONG_THREE);
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt4(),
                            LONG_FOUR);

                    // Update the student's majors
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr1(), LONG_ONE);
                    final StudentMajor oldStudentMajor = studentInfo.getOldStudentMajor();
                    final StudentMajor newStudentMajor = studentInfo.getNewStudentMajor();
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr2(), LONG_TWO);
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr3(), LONG_THREE);
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr4(), LONG_FOUR);

                    // Change notification for student data.
                    changeNotification.studentChange(studentInfo.getOldStudent(), oldStudentAcademicCollege,
                            oldStudentAcademicDepartment, oldStudentMajor, studentInfo.getNewStudent(),
                            newStudentAcademicCollege, newStudentAcademicDepartment, newStudentMajor);

                    // If we are looking at the current semester
                    // process the Affiliation Change.
                    if (Utility.areStringFieldsEqual(currentSemester, semesterCode)) {
                        eduPersonAffiliationCalculator.setPersonId(studentInfo.getPersonId());
                        eduPersonAffiliationCalculator.setStudentAffiliation(transStubio.getCodeStudStat());
                        changeNotification.primaryAffiliationChange(
                                eduPersonAffiliationCalculator.getOldPersonAffiliation(),
                                eduPersonAffiliationCalculator.getNewPersonAffiliation());
                    }

                } else {
                    LOG.error("Skipping student record: " + transStubio.getTransStubioKey()
                            + " due to a lack of data.");
                }
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), e);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            studentInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            eduPersonAffiliationCalculator.setDatabaseSession(recordSession);
        } catch (final CprException ex) {
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), ex);
            throw ex;
        } catch (final RuntimeException ex) {
            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), ex);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            studentInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable logExceptions, emptyCatchClause
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.scripps.fl.pipeline.StatelessCommitStage.java

License:Apache License

@Override
public void process(Object obj) throws StageException {
    try {/*ww w .  ja  v a2 s .  c o  m*/
        StatelessSession session = getStatelessSession();
        int count = counter.incrementAndGet();
        doSave(obj);
        if (count % getCommitFrequency() == 0) {
            getTransaction().commit();
            setTransaction(session.beginTransaction());
        }
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}