Example usage for org.hibernate Query setFetchSize

List of usage examples for org.hibernate Query setFetchSize

Introduction

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

Prototype

Query<R> setFetchSize(int fetchSize);

Source Link

Document

Sets a JDBC fetch size hint for the query.

Usage

From source file:de.tudarmstadt.ukp.lmf.transform.DBToXMLTransformer.java

License:Apache License

protected void doTransform(boolean includeAxes, final Lexicon... includeLexicons) throws SAXException {
    final int bufferSize = 100;
    commitCounter = 1;//from   ww w  .ja  va  2s  .c  o m

    writeStartElement(lexicalResource);

    // Iterate over all lexicons
    if (includeLexicons == null || includeLexicons.length > 0) {
        for (Lexicon lexicon : lexicalResource.getLexicons()) {
            String lexiconName = lexicon.getName();

            // Check if we want to include this lexicon.
            if (includeLexicons != null) {
                boolean found = false;
                for (Lexicon l : includeLexicons) {
                    if (lexiconName.equals(l.getName())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    continue;
                }
            }

            logger.info("Processing lexicon: " + lexiconName);
            writeStartElement(lexicon);

            // Iterate over all possible sub-elements of this Lexicon and
            // write them to the XML
            Class<?>[] lexiconClassesToSave = { LexicalEntry.class, SubcategorizationFrame.class,
                    SubcategorizationFrameSet.class, SemanticPredicate.class, Synset.class,
                    SynSemCorrespondence.class,
                    //ConstraintSet.class
            };

            //  "Unfortunately, MySQL does not treat large offset values efficiently by default and will still read all the rows prior to an offset value. It is common to see a query with an offset above 100,000 take over 20 times longer than an offset of zero!"
            // http://www.numerati.com/2012/06/26/reading-large-result-sets-with-hibernate-and-mysql/
            for (Class<?> clazz : lexiconClassesToSave) {
                /*DetachedCriteria criteria = DetachedCriteria.forClass(clazz)
                      .add(Restrictions.sqlRestriction("lexiconId = '" + lexicon.getId() + "'"));
                CriteriaIterator<Object> iter = new CriteriaIterator<Object>(criteria, sessionFactory, bufferSize);
                while (iter.hasNext()) {
                   Object obj = iter.next();
                   writeElement(obj);
                   session.evict(obj);
                   commitCounter++;
                   if (commitCounter % 1000 == 0)
                      logger.info("progress: " + commitCounter  + " class instances written to file");
                }*/
                Session lookupSession = sessionFactory.openSession();
                Query query = lookupSession.createQuery("FROM " + clazz.getSimpleName() + " WHERE lexiconId = '"
                        + lexicon.getId() + "' ORDER BY id");
                query.setReadOnly(true);
                if (DBConfig.MYSQL.equals(dbConfig.getDBType())) {
                    query.setFetchSize(Integer.MIN_VALUE); // MIN_VALUE gives hint to JDBC driver to stream results
                } else {
                    query.setFetchSize(1000);
                }
                ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
                while (results.next()) {
                    // For streamed query results, no further queries are allowed (incl. lazy proxy queries!)
                    // Detach the object from the lookup session and reload it using the "official" session.
                    Object[] rows = results.get();
                    Object row = rows[0];
                    lookupSession.evict(row);
                    lookupSession.evict(rows);
                    rows = null;
                    row = session.get(row.getClass(), ((IHasID) row).getId());
                    writeElement(row);
                    session.evict(row);
                    row = null;
                    commitCounter++;
                    if (commitCounter % 1000 == 0) {
                        logger.info("progress: " + commitCounter + " class instances written to file");
                    }
                    if (commitCounter % 10000 == 0) {
                        closeSession();
                        openSession();
                    }
                }
                results.close();
                lookupSession.close();
            }
            writeEndElement(lexicon);
        }
    }

    // Iterate over SenseAxes and write them to XMLX when not only
    // lexicons should be converted
    if (includeAxes) {
        logger.info("Processing sense axes");
        DetachedCriteria criteria = DetachedCriteria.forClass(SenseAxis.class)
                .add(Restrictions.sqlRestriction("lexicalResourceId = '" + lexicalResource.getName() + "'"));
        CriteriaIterator<Object> iter = new CriteriaIterator<Object>(criteria, sessionFactory, bufferSize);
        while (iter.hasNext()) {
            Object obj = iter.next();
            writeElement(obj);
            session.evict(obj);
            commitCounter++;
            if (commitCounter % 1000 == 0) {
                logger.info("progress: " + commitCounter + " class instances written to file");
            }
        }

        logger.info("Processing predicateargument axes");
        DetachedCriteria criteria2 = DetachedCriteria.forClass(PredicateArgumentAxis.class)
                .add(Restrictions.sqlRestriction("lexicalResourceId = '" + lexicalResource.getName() + "'"));
        CriteriaIterator<Object> iter2 = new CriteriaIterator<Object>(criteria2, sessionFactory, bufferSize);
        while (iter2.hasNext()) {
            Object obj = iter2.next();
            writeElement(obj);
            session.evict(obj);
            commitCounter++;
            if (commitCounter % 1000 == 0) {
                logger.info("progress: " + commitCounter + " class instances written to file");
            }
        }

    }
    writeEndElement(lexicalResource);

    writeEndDocument();
}

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  ww w  .  ja v a2 s  .c  o m
@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);/*from  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 . ja  v  a2 s. c om*/
@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.
 */// www.ja  va  2 s  .c om
@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 w  w.ja va2s  .  co 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 ww  w .j  a va 2 s.c om*/
@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.ur.hibernate.file.mime.HbSubTypeDAO.java

License:Apache License

@SuppressWarnings("unchecked")
public List<SubType> getSubTypes(final Long topMediaTypeId, final int rowStart, final int numberOfResultsToShow,
        final String sortType) {
    List<SubType> subTypes = (List<SubType>) hbCrudDAO.getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query q = null;
            if (sortType.equalsIgnoreCase("asc")) {
                q = session.getNamedQuery("getSubTypesOrderByNameAsc");
            } else {
                q = session.getNamedQuery("getSubTypesOrderByNameDesc");
            }/*ww  w .j av a  2  s. c o m*/
            q.setParameter("topMediaTypeId", topMediaTypeId);
            q.setFirstResult(rowStart);
            q.setMaxResults(numberOfResultsToShow);
            q.setReadOnly(true);
            q.setFetchSize(numberOfResultsToShow);
            return q.list();

        }
    });

    return subTypes;
}

From source file:edu.ur.hibernate.file.mime.HbSubTypeExtensionDAO.java

License:Apache License

/**
 * @see edu.ur.file.mime.SubTypeExtensionDAO#getSubTypeExtensions(Long, int, int, String)
 */// w w  w .ja va  2  s.c o  m
@SuppressWarnings("unchecked")
public List<SubTypeExtension> getSubTypeExtensions(final Long subTypeId, final int rowStart,
        final int numberOfResultsToShow, final String sortType) {
    List<SubTypeExtension> subTypeExtensions = (List<SubTypeExtension>) hbCrudDAO.getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {

                    Query q = null;
                    if (sortType.equalsIgnoreCase("asc")) {
                        q = session.getNamedQuery("getSubTypeExtensionsOrderByNameAsc");
                    } else {
                        q = session.getNamedQuery("getSubTypeExtensionsOrderByNameDesc");
                    }
                    q.setParameter("subTypeId", subTypeId);
                    q.setFirstResult(rowStart);
                    q.setMaxResults(numberOfResultsToShow);
                    q.setReadOnly(true);
                    q.setFetchSize(numberOfResultsToShow);
                    return q.list();
                }
            });

    return subTypeExtensions;
}

From source file:edu.ur.hibernate.file.mime.HbTopMediaTypeDAO.java

License:Apache License

/**
 * @see edu.ur.file.mime.TopMediaTypeDAO#getTopMediaTypes(int, int, String)
 *//*  w  ww . j  av  a 2s  . c  o  m*/
@SuppressWarnings("unchecked")
public List<TopMediaType> getTopMediaTypes(final int rowStart, final int numberOfResultsToShow,
        final String sortType) {
    List<TopMediaType> topMediaTypes = (List<TopMediaType>) hbCrudDAO.getHibernateTemplate()
            .execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    Query q = null;
                    if (sortType.equalsIgnoreCase("asc")) {
                        q = session.getNamedQuery("getTopMediaTypesOrderByNameAsc");
                    } else {
                        q = session.getNamedQuery("getTopMediaTypesOrderByNameDesc");
                    }

                    q.setFirstResult(rowStart);
                    q.setMaxResults(numberOfResultsToShow);
                    q.setReadOnly(true);
                    q.setFetchSize(numberOfResultsToShow);
                    return q.list();
                }
            });

    return topMediaTypes;
}