Example usage for org.hibernate.type StandardBasicTypes STRING

List of usage examples for org.hibernate.type StandardBasicTypes STRING

Introduction

In this page you can find the example usage for org.hibernate.type StandardBasicTypes STRING.

Prototype

StringType STRING

To view the source code for org.hibernate.type StandardBasicTypes STRING.

Click Source Link

Document

The standard Hibernate type for mapping String to JDBC java.sql.Types#VARCHAR VARCHAR .

Usage

From source file:edu.psu.iam.cpr.core.database.tables.PersonUseridIapTable.java

License:Apache License

/**
 * Retrieve all active PSU IAPS for a userid.
 * @param db An active database handle//from   w  w  w.  j a v  a  2 s.  c om
 * @param personId contains the person_id
 * @param userid The userid to query
 * 
 * @return A list of IAPs
 * @throws CprException 
 */
public IAPReturn[] getPSUIAP(final Database db, final long personId, final String userid) throws CprException {

    boolean useridValid = false;
    final Session session = db.getSession();
    useridValid = db.isValidUserid(personId, userid);

    if (!useridValid) {
        throw new CprException(ReturnType.INVALID_PARAMETERS_EXCEPTION, USERID_ARG);
    }

    final List<IAPReturn> results = new ArrayList<IAPReturn>();
    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT iap.iap, ");
    sb.append("person_userid_iap.start_date, ");
    sb.append("person_userid_iap.end_date, ");
    sb.append("person_userid_iap.last_update_by, ");
    sb.append("person_userid_iap.last_update_on, ");
    sb.append("person_userid_iap.created_by, ");
    sb.append("person_userid_iap.created_on ");
    sb.append("FROM {h-schema}iap ");
    sb.append("LEFT JOIN {h-schema}person_userid_iap ON iap.iap_key = person_userid_iap.iap_key ");
    sb.append("WHERE person_userid_iap.person_id = :person_id_in ");
    sb.append("AND person_userid_iap.userid=:userid_in ");

    // If we are not returning all records, we need to just return the active ones.
    if (!isReturnHistoryFlag()) {
        sb.append("AND person_userid_iap.end_date IS NULL ");
    }
    sb.append("ORDER BY iap.iap_key ASC, person_userid_iap.start_date ASC ");
    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id_in", personId);

    query.setParameter("userid_in", userid);
    query.addScalar("iap", StandardBasicTypes.STRING);
    query.addScalar("start_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("end_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("last_update_by", StandardBasicTypes.STRING);
    query.addScalar("last_update_on", StandardBasicTypes.TIMESTAMP);
    query.addScalar("created_by", StandardBasicTypes.STRING);
    query.addScalar("created_on", StandardBasicTypes.TIMESTAMP);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Object[] res = (Object[]) it.next();
        IAPReturn anIAP = new IAPReturn();
        anIAP.setIap((String) res[IAP]);
        anIAP.setStartDate(Utility.formatDateToISO8601((Date) res[START_DATE]));
        anIAP.setEndDate(Utility.formatDateToISO8601((Date) res[END_DATE]));
        anIAP.setLastUpdateBy((String) res[LAST_UPDATE_BY]);
        anIAP.setLastUpdateOn(Utility.formatDateToISO8601((Date) res[LAST_UPDATE_ON]));
        anIAP.setCreatedBy((String) res[CREATED_BY]);
        anIAP.setCreatedOn(Utility.formatDateToISO8601((Date) res[CREATED_ON]));
        results.add(anIAP);
    }

    return results.toArray(new IAPReturn[results.size()]);
}

From source file:edu.psu.iam.cpr.core.database.tables.PhonesTable.java

License:Apache License

/**
 * This routine will obtain a list of phone numbers for a person id
 * @param db //from ww  w  .j a  v  a 2 s.  c  o m
 * @param personId contains the personID
 * @return list of phone numbers
 */
public PhoneReturn[] getPhones(final Database db, final long personId) {

    final List<PhoneReturn> results = new ArrayList<PhoneReturn>();

    final Session session = db.getSession();
    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append(
            "SELECT phone_key, data_type_key, group_id, primary_flag, phone_number, extension, international_number_flag,  ");
    sb.append("start_date, ");
    sb.append("end_date, ");
    sb.append("last_update_by, ");
    sb.append("last_update_on, ");
    sb.append("created_by, ");
    sb.append("created_on ");
    sb.append("FROM {h-schema}phones ");
    sb.append("WHERE person_id = :person_id_in ");

    if (getPhoneType() != null) {
        sb.append("AND data_type_key = :data_type_key_in ");
    }

    if (!isReturnHistoryFlag()) {
        sb.append("AND end_date IS NULL");
    }

    if (getPhoneKey() > 0L) {
        sb.append("AND phone_key = :phone_key ");
    }

    sb.append(" ORDER BY data_type_key ASC, start_date ASC ");

    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id_in", personId);

    if (getPhoneType() != null) {
        query.setParameter("data_type_key_in", getPhoneType().index());
    }

    if (getPhoneKey() > 0L) {
        query.setParameter(PHONE_KEY_STRING, getPhoneKey());
    }

    query.addScalar(PHONE_KEY_STRING, StandardBasicTypes.LONG);
    query.addScalar(DATA_TYPE_KEY_STRING, StandardBasicTypes.LONG);
    query.addScalar(GROUP_ID_STRING, StandardBasicTypes.LONG);
    query.addScalar("primary_flag", StandardBasicTypes.STRING);
    query.addScalar("phone_number", StandardBasicTypes.STRING);
    query.addScalar("extension", StandardBasicTypes.STRING);
    query.addScalar("international_number_flag", StandardBasicTypes.STRING);
    query.addScalar("start_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("end_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("last_update_by", StandardBasicTypes.STRING);
    query.addScalar("last_update_on", StandardBasicTypes.TIMESTAMP);
    query.addScalar("created_by", StandardBasicTypes.STRING);
    query.addScalar("created_on", StandardBasicTypes.TIMESTAMP);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Object[] res = (Object[]) it.next();
        PhoneReturn aPhone = new PhoneReturn();
        aPhone.setPhoneKey(((Long) res[PHONE_KEY]).toString());
        aPhone.setPhoneType(PhoneType.get((Long) res[PHONE_TYPE]).toString());
        aPhone.setGroupId((Long) res[GROUP_ID]);
        aPhone.setPrimaryFlag((String) res[PRIMARY_FLAG]);
        aPhone.setPhoneNumber((String) res[PHONE_NUMBER]);
        aPhone.setExtension((String) res[EXTENSION]);
        aPhone.setInternationalNumber((String) res[INTERNATIONAL_NUMBER]);
        aPhone.setStartDate(Utility.formatDateToISO8601((Date) res[START_DATE]));
        aPhone.setEndDate(Utility.formatDateToISO8601((Date) res[END_DATE]));
        aPhone.setLastUpdateBy((String) res[LAST_UPDATE_BY]);
        aPhone.setLastUpdateOn(Utility.formatDateToISO8601((Date) res[LAST_UPDATE_ON]));
        aPhone.setCreatedBy((String) res[CREATED_BY]);
        aPhone.setCreatedOn(Utility.formatDateToISO8601((Date) res[CREATED_ON]));
        results.add(aPhone);
    }

    return results.toArray(new PhoneReturn[results.size()]);
}

From source file:edu.psu.iam.cpr.core.database.tables.PhonesTable.java

License:Apache License

/**
 * This routine will set a primary phone number within a phonetype for a person id
 * @param db //  w w w  .j a  va  2  s  .co  m
 * @throws CprException 
 * 
 */
public void setPrimaryByType(final Database db) throws CprException {

    boolean notFound = false;
    boolean alreadyPrimary = false;

    final Session session = db.getSession();
    final Phones bean = getPhonesBean();

    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT  primary_flag ");
    sb.append("FROM {h-schema}phones ");
    sb.append("WHERE person_id = :person_id_in ");
    sb.append("AND data_type_key = :data_type_key ");
    sb.append("AND group_id = :group_id ");
    sb.append("AND end_date IS NULL ");
    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id_in", bean.getPersonId());
    query.setParameter(DATA_TYPE_KEY_STRING, bean.getDataTypeKey());
    query.setParameter(GROUP_ID_STRING, bean.getGroupId());
    query.addScalar("primary_flag", StandardBasicTypes.STRING);
    Iterator<?> it = query.list().iterator();
    if (!it.hasNext()) {
        notFound = true;
    } else {

        final String primaryFlag = (String) it.next();
        if (Utility.isOptionYes(primaryFlag)) {
            alreadyPrimary = true;
        } else {

            String sqlQuery = "from Phones where personId = :person_id and dataTypeKey = :data_type_key and primaryFlag = 'Y' and endDate is null";
            Query query1 = session.createQuery(sqlQuery);
            query1.setParameter(PERSON_ID_STRING, bean.getPersonId());
            query1.setParameter(DATA_TYPE_KEY_STRING, bean.getDataTypeKey());
            for (it = query1.list().iterator(); it.hasNext();) {
                Phones dbBean = (Phones) it.next();
                dbBean.setPrimaryFlag("N");
                dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                session.update(dbBean);
                session.flush();
            }

            sqlQuery = "from Phones where personId = :person_id and dataTypeKey = :data_type_key and groupId = :group_id and endDate IS NULL";
            query1 = session.createQuery(sqlQuery);
            query1.setParameter(PERSON_ID_STRING, bean.getPersonId());
            query1.setParameter(DATA_TYPE_KEY_STRING, bean.getDataTypeKey());
            query1.setParameter(GROUP_ID_STRING, bean.getGroupId());
            it = query1.list().iterator();
            if (it.hasNext()) {
                Phones dbBean = (Phones) it.next();
                dbBean.setPrimaryFlag("Y");
                dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                session.update(dbBean);
                session.flush();
            }
        }
    }

    if (notFound) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, TABLE_NAME);
    }

    if (alreadyPrimary) {
        throw new CprException(ReturnType.SET_PRIMARY_FAILED_EXCEPTION, TABLE_NAME);
    }
}

From source file:edu.psu.iam.cpr.core.database.tables.PsuDirectoryTable.java

License:Apache License

/**
 * This routine is used to populate the directory table class with information from the PSU_DIRECTORY table for a specific person identifier.
 * @param db contains the database connection
 * @param personId contains the person identifier.
 * @throws CprException /*www.j  a  v a2  s. c om*/
 */
public void getPsuDirectoryTable(final Database db, final Long personId) throws CprException {

    boolean found = false;
    final Session session = db.getSession();

    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT userid, psu_directory_key ");
    sb.append("FROM {h-schema}psu_directory ");
    sb.append("WHERE person_id = :person_id_in ");
    sb.append("AND end_date IS NULL ");

    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id_in", personId);
    query.addScalar("userid", StandardBasicTypes.STRING);
    query.addScalar("psu_directory_key", StandardBasicTypes.LONG);

    final Iterator<?> it = query.list().iterator();

    if (it.hasNext()) {
        Object[] res = (Object[]) it.next();

        final PsuDirectory bean = new PsuDirectory();
        bean.setPersonId(personId);
        bean.setUserid((String) res[USERID]);
        bean.setPsuDirectoryKey((Long) res[PSU_DIRECTORY_KEY]);
        setPsuDirectoryBean(bean);
        found = true;
    }

    if (!found) {
        throw new CprException(ReturnType.PERSON_NOT_FOUND_EXCEPTION);
    }
}

From source file:edu.psu.iam.cpr.core.database.tables.PsuIdTable.java

License:Apache License

/**
 * This routine is used to obtain the PSU ID for a specific person.
 * @param db contains the open database connection.
 * @param personId contains the person identifier used to retrieve the PSU ID for.
 * @return will return a PsuIdReturn object if success.
 *//*  w w w . j  ava  2 s  .c  o m*/
public PsuIdReturn[] getPsuIdForPersonId(final Database db, final long personId) {

    final Session session = db.getSession();
    final List<PsuIdReturn> results = new ArrayList<PsuIdReturn>();
    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);

    sb.append("SELECT psu_id, ");
    sb.append("start_date, ");
    sb.append("end_date, ");
    sb.append("last_update_by, ");
    sb.append("last_update_on, ");
    sb.append("created_by, ");
    sb.append("created_on ");
    sb.append("FROM {h-schema}psu_id WHERE person_id=:person_id ");

    // If we are not returning all records, we need to just return the active ones.
    if (!isReturnHistoryFlag()) {
        sb.append("AND end_date IS NULL ");
    }
    sb.append("ORDER BY start_date ASC ");

    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id", personId);
    query.addScalar("psu_id", StandardBasicTypes.STRING);
    query.addScalar("start_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("end_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("last_update_by", StandardBasicTypes.STRING);
    query.addScalar("last_update_on", StandardBasicTypes.TIMESTAMP);
    query.addScalar("created_by", StandardBasicTypes.STRING);
    query.addScalar("created_on", StandardBasicTypes.TIMESTAMP);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Object[] res = (Object[]) it.next();
        results.add(new PsuIdReturn((String) res[PSU_ID], Utility.formatDateToISO8601((Date) res[START_DATE]),
                Utility.formatDateToISO8601((Date) res[END_DATE]), (String) res[LAST_UPDATE_BY],
                Utility.formatDateToISO8601((Date) res[LAST_UPDATE_ON]), (String) res[CREATED_BY],
                Utility.formatDateToISO8601((Date) res[CREATED_ON])));
    }
    return results.toArray(new PsuIdReturn[results.size()]);
}

From source file:edu.psu.iam.cpr.core.database.tables.UserCommentTable.java

License:Apache License

/**
 * Obtain user comments.//w  ww.j  av  a2  s . c  o  m
 * @param db
 * @param userId contains the user id of person.
 * @return an array of user comments.
 */
public UserCommentReturn[] getUserComments(final Database db, final String userId) {

    final List<UserCommentReturn> results = new ArrayList<UserCommentReturn>();

    final Session session = db.getSession();

    // Build the query string.
    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT user_comment_key, data_type_key, comments, ");
    sb.append("start_date, ");
    sb.append("end_date, ");
    sb.append("last_update_by, ");
    sb.append("last_update_on, ");
    sb.append("created_by ");
    sb.append("FROM {h-schema}user_comments ");
    sb.append("WHERE userid = :userid_in ");

    if (getUserCommentType() != null) {
        sb.append("AND data_type_key = :data_type_key_in ");
    }

    // If we are not returning all records, we need to just return the active ones.
    if (!isReturnHistoryFlag()) {
        sb.append("AND end_date IS NULL ");
    }

    if (getCommentKey() > 0L) {
        sb.append("AND user_comment_key = :user_comment_key ");
    }

    sb.append("ORDER BY data_type_key ASC, start_date ASC ");

    // Init the hibernate query.
    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("userid_in", userId);

    if (getUserCommentType() != null) {
        query.setParameter("data_type_key_in", getUserCommentType().index());
    }

    if (getCommentKey() > 0L) {
        query.setParameter("user_comment_key", getCommentKey());
    }

    query.addScalar("user_comment_key", StandardBasicTypes.LONG);
    query.addScalar("data_type_key", StandardBasicTypes.LONG);
    query.addScalar("comments", StandardBasicTypes.STRING);
    query.addScalar("start_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("end_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("last_update_by", StandardBasicTypes.STRING);
    query.addScalar("last_update_on", StandardBasicTypes.TIMESTAMP);
    query.addScalar("created_by", StandardBasicTypes.STRING);

    // Perform the query.
    final Iterator<?> it = query.list().iterator();

    // Process the results.
    while (it.hasNext()) {
        Object[] res = (Object[]) it.next();
        UserCommentReturn ucr = new UserCommentReturn();
        ucr.setCommentKey(((Long) res[COMMENT_KEY]).toString());
        ucr.setUserCommentType(UserCommentType.get((Long) res[USER_COMMENT_TYPE]).toString());
        ucr.setComment((String) res[COMMENT]);
        ucr.setCommentDateString(Utility.formatDateToISO8601((Date) res[COMMENT_DATE_STRING]));
        ucr.setEndDate(Utility.formatDateToISO8601((Date) res[END_DATE]));
        ucr.setLastUpdatedBy((String) res[LAST_UPDATE_BY]);
        ucr.setLastUpdateOn(Utility.formatDateToISO8601((Date) res[LAST_UPDATE_ON]));
        ucr.setCommenter((String) res[COMMENTER]);
        results.add(ucr);
    }

    return results.toArray(new UserCommentReturn[results.size()]);
}

From source file:edu.psu.iam.cpr.core.database.tables.UseridTable.java

License:Apache License

/**
 * This routine is to call a stored procedure to set the primary userid for a user.
 * @param db contains a reference to a open database connection.
 * @throws CprException  will be thrown if there are any CPR related errors.
 * //w w  w .  j a  va 2 s  .co m
 */
public void setPrimaryUserid(final Database db) throws CprException {

    boolean recordExpired = false;
    boolean alreadyPrimary = false;
    boolean recordNotFound = false;

    final Session session = db.getSession();
    final Userid bean = getUseridBean();

    // For the selected userid, obtain the end date and their primary flag.
    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT end_date, primary_flag ");
    sb.append("FROM {h-schema}userid ");
    sb.append("WHERE person_id = :person_id_in ");
    sb.append("AND userid = :userid_in ");
    final SQLQuery query = session.createSQLQuery(sb.toString());
    query.setParameter("person_id_in", bean.getPersonId());
    query.setParameter("userid_in", bean.getUserid());
    query.addScalar("end_date", StandardBasicTypes.DATE);
    query.addScalar("primary_flag", StandardBasicTypes.STRING);
    Iterator<?> it = query.list().iterator();

    if (it.hasNext()) {
        Object[] res = (Object[]) it.next();
        bean.setEndDate((Date) res[0]);
        bean.setPrimaryFlag((String) res[1]);

        // Expired, we have an error.
        if (bean.getEndDate() != null) {
            recordExpired = true;
        }

        // Already primary, we have an error.
        else if (Utility.isOptionYes(bean.getPrimaryFlag())) {
            alreadyPrimary = true;
        } else {

            // Switch the current primary record.
            String sqlQuery = "from Userid where personId = :person_id_in AND primaryFlag = 'Y' AND endDate IS NULL";
            Query query1 = session.createQuery(sqlQuery);
            query1.setParameter("person_id_in", bean.getPersonId());
            for (it = query1.list().iterator(); it.hasNext();) {
                Userid dbBean = (Userid) it.next();

                dbBean.setPrimaryFlag("N");
                dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                session.update(dbBean);
                session.flush();
            }

            // Make the new record primary.
            sqlQuery = "from Userid where personId = :person_id_in AND userid = :userid_in AND endDate IS NULL";
            query1 = session.createQuery(sqlQuery);
            query1.setParameter("person_id_in", bean.getPersonId());
            query1.setParameter("userid_in", bean.getUserid());
            for (it = query1.list().iterator(); it.hasNext();) {
                Userid dbBean = (Userid) it.next();
                dbBean.setPrimaryFlag("Y");
                dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                session.update(dbBean);
                session.flush();
            }
        }
    } else {
        recordNotFound = true;
    }

    // Handle other errors.
    if (recordExpired) {
        throw new CprException(ReturnType.ALREADY_DELETED_EXCEPTION, TABLE_NAME);
    }
    if (alreadyPrimary) {
        throw new CprException(ReturnType.SET_PRIMARY_FAILED_EXCEPTION, TABLE_NAME);
    }
    if (recordNotFound) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, TABLE_NAME);
    }
}

From source file:edu.psu.iam.cpr.core.database.tables.UseridTable.java

License:Apache License

/**
 * This routine is used to archive a userid.  It is called by the ArchiveUserid service.
 * @param db contains a reference to an open database connection.
 * @throws CprException will be thrown for any CPR specific problems.
 *///from  w w  w .j  a  v  a 2s .  com
public void archiveUserid(final Database db) throws CprException {

    boolean noneActive = false;
    boolean notFound = false;
    boolean alreadyArchived = false;
    boolean cannotArchive = false;

    final Session session = db.getSession();
    final Userid bean = getUseridBean();

    // Determine how many userids are active for the current user.
    String sqlQuery = "SELECT person_id FROM {h-schema}userid WHERE person_id = :person_id_in AND end_date IS NULL";
    SQLQuery query = session.createSQLQuery(sqlQuery);
    query.setParameter("person_id_in", bean.getPersonId());
    query.addScalar("person_id", StandardBasicTypes.LONG);
    final int activeCount = query.list().size();
    if (activeCount == 0) {
        noneActive = true;
    } else {

        // For the selected userid, obtain the end date and their primary flag.
        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
        sb.append("SELECT end_date, primary_flag ");
        sb.append("FROM {h-schema}userid ");
        sb.append("WHERE person_id = :person_id_in ");
        sb.append("AND userid = :userid_in ");
        query = session.createSQLQuery(sb.toString());
        query.setParameter("person_id_in", bean.getPersonId());
        query.setParameter("userid_in", bean.getUserid());
        query.addScalar("end_date", StandardBasicTypes.DATE);
        query.addScalar("primary_flag", StandardBasicTypes.STRING);
        Iterator<?> it = query.list().iterator();

        if (it.hasNext()) {
            Object[] res = (Object[]) it.next();
            bean.setEndDate((Date) res[0]);
            bean.setPrimaryFlag((String) res[1]);

            // Error if the record already has an end date.
            if (bean.getEndDate() != null) {
                alreadyArchived = true;
            }

            // If there are more than one record and this one is primary, do not all the archival.
            else if (activeCount > 1 && Utility.isOptionYes(bean.getPrimaryFlag())) {
                cannotArchive = true;
            }

            // Otherwise we can do the archive.
            else {
                sqlQuery = "from Userid where personId = :person_id_in AND userid = :userid_in AND endDate IS NULL";
                final Query query1 = session.createQuery(sqlQuery);
                query1.setParameter("person_id_in", bean.getPersonId());
                query1.setParameter("userid_in", bean.getUserid());
                for (it = query1.list().iterator(); it.hasNext();) {
                    Userid dbBean = (Userid) it.next();
                    dbBean.setPrimaryFlag("N");
                    dbBean.setEndDate(bean.getLastUpdateOn());
                    dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                    dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                    session.update(dbBean);
                    session.flush();
                }
            }
        } else {
            notFound = true;
        }

    }

    if (notFound) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, TABLE_NAME);
    }
    if (noneActive) {
        throw new CprException(ReturnType.GENERAL_EXCEPTION,
                "Cannot archive userid, because there are no active userids.");
    }
    if (alreadyArchived) {
        throw new CprException(ReturnType.ALREADY_DELETED_EXCEPTION, TABLE_NAME);
    }
    if (cannotArchive) {
        throw new CprException(ReturnType.GENERAL_EXCEPTION,
                "Cannot archive userid, because its the primary userid.");
    }

}

From source file:edu.psu.iam.cpr.core.database.tables.UseridTable.java

License:Apache License

/**
 * This routine is used to unarchive a userid.  It is called by the UnarchiveUserid service.
 * @param db contains a reference to an open database connection.
 * @throws CprException will be thrown for any CPR specific problems.
 *//*from ww  w  .  j a  v  a 2s.  c o  m*/
public void unarchiveUserid(final Database db) throws CprException {

    boolean alreadyUnarchived = false;
    boolean noArchivedRecords = false;
    boolean recordNotFound = false;

    final Session session = db.getSession();
    final Userid bean = getUseridBean();

    // See how any userids are archived for the user, if there are none that are archived, we have an error.
    String sqlQuery = "SELECT person_id FROM {h-schema}userid WHERE person_id = :person_id_in AND end_date IS NOT NULL";
    SQLQuery query = session.createSQLQuery(sqlQuery);
    query.setParameter("person_id_in", bean.getPersonId());
    query.addScalar("person_id", StandardBasicTypes.LONG);
    final int archivedCount = query.list().size();

    if (archivedCount == 0) {
        noArchivedRecords = true;
    } else {

        // For the selected userid, obtain the end date and their primary flag.
        final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
        sb.append("SELECT end_date, primary_flag ");
        sb.append("FROM {h-schema}userid ");
        sb.append("WHERE person_id = :person_id_in ");
        sb.append("AND userid = :userid_in ");
        query = session.createSQLQuery(sb.toString());
        query.setParameter("person_id_in", bean.getPersonId());
        query.setParameter("userid_in", bean.getUserid());
        query.addScalar("end_date", StandardBasicTypes.DATE);
        query.addScalar("primary_flag", StandardBasicTypes.STRING);
        Iterator<?> it = query.list().iterator();

        if (it.hasNext()) {
            Object[] res = (Object[]) it.next();
            bean.setEndDate((Date) res[0]);
            bean.setPrimaryFlag((String) res[1]);

            if (bean.getEndDate() == null) {
                alreadyUnarchived = true;
            } else {
                // Determine how many userids are active for the current user.
                sqlQuery = "SELECT person_id FROM {h-schema}userid WHERE person_id = :person_id_in AND end_date IS NULL";
                query = session.createSQLQuery(sqlQuery);
                query.setParameter("person_id_in", bean.getPersonId());
                query.addScalar("person_id", StandardBasicTypes.LONG);
                final int activeCount = query.list().size();

                if (activeCount == 0) {
                    bean.setPrimaryFlag("Y");
                } else {
                    bean.setPrimaryFlag("N");
                }

                // Do the unarchive.
                sqlQuery = "from Userid where personId = :person_id AND userid = :userid_in AND endDate IS NOT NULL";
                final Query query1 = session.createQuery(sqlQuery);
                query1.setParameter("person_id", bean.getPersonId());
                query1.setParameter("userid_in", bean.getUserid());
                for (it = query1.list().iterator(); it.hasNext();) {
                    Userid dbBean = (Userid) it.next();
                    dbBean.setPrimaryFlag(bean.getPrimaryFlag());
                    dbBean.setEndDate(null);
                    dbBean.setLastUpdateBy(bean.getLastUpdateBy());
                    dbBean.setLastUpdateOn(bean.getLastUpdateOn());
                    session.update(dbBean);
                    session.flush();
                }
            }

        } else {
            recordNotFound = true;
        }
    }

    if (alreadyUnarchived) {
        throw new CprException(ReturnType.UNARCHIVE_FAILED_EXCEPTION, "userid");
    }

    if (noArchivedRecords) {
        throw new CprException(ReturnType.GENERAL_EXCEPTION, "There are no records that can be unarchived.");
    }

    if (recordNotFound) {
        throw new CprException(ReturnType.RECORD_NOT_FOUND_EXCEPTION, "userid");
    }
}

From source file:edu.psu.iam.cpr.core.database.tables.UseridTable.java

License:Apache License

/**
 * This routine will obtain a list of userids for a person id.
 * @param db contains an open database connection.
 * @param personId contains the person id.
 * @return an array of userids./* ww  w .  j  a  v  a 2s  . c  o  m*/
 */
public UseridReturn[] getUseridsForPersonId(final Database db, final long personId) {

    final Session session = db.getSession();
    final List<UseridReturn> results = new ArrayList<UseridReturn>();

    final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
    sb.append("SELECT userid, primary_flag, ");
    sb.append("start_date, ");
    sb.append("end_date, ");
    sb.append("last_update_by, ");
    sb.append("last_update_on, ");
    sb.append("created_by, ");
    sb.append("created_on ");
    sb.append("FROM {h-schema}userid ");
    sb.append("WHERE person_id = :person_id_in ");
    if (!isReturnHistoryFlag()) {
        sb.append("AND end_date IS NULL ");
    }
    if (getUserid() != null) {
        sb.append("AND userid = :userid ");
    }
    sb.append("ORDER BY start_date");

    final SQLQuery query = session.createSQLQuery(sb.toString());

    query.setParameter("person_id_in", personId);

    if (getUserid() != null) {
        query.setParameter("userid", getUserid());
    }

    query.addScalar("userid", StandardBasicTypes.STRING);
    query.addScalar("primary_flag", StandardBasicTypes.STRING);
    query.addScalar("start_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("end_date", StandardBasicTypes.TIMESTAMP);
    query.addScalar("last_update_by", StandardBasicTypes.STRING);
    query.addScalar("last_update_on", StandardBasicTypes.TIMESTAMP);
    query.addScalar("created_by", StandardBasicTypes.STRING);
    query.addScalar("created_on", StandardBasicTypes.TIMESTAMP);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Object[] res = (Object[]) it.next();
        results.add(new UseridReturn((String) res[USERID], (String) res[PRIMARY_FLAG],
                Utility.formatDateToISO8601((Date) res[START_DATE]),
                Utility.formatDateToISO8601((Date) res[END_DATE]), (String) res[LAST_UPDATE_BY],
                Utility.formatDateToISO8601((Date) res[LAST_UPDATE_ON]), (String) res[CREATED_BY],
                Utility.formatDateToISO8601((Date) res[CREATED_ON])));
    }

    return results.toArray(new UseridReturn[results.size()]);
}