Search in sources :

Example 1 with PersistentObject

use of gov.ca.cwds.data.persistence.PersistentObject in project cals-api by ca-cwds.

the class StateReferentialIntegrityForEachValidator method getPersistentObject.

@Override
protected <T extends PersistentObject> PersistentObject getPersistentObject(Session currentSession, T obj, Serializable primaryKey) {
    PersistentObject found;
    Query<StateType> query = currentSession.createNamedQuery(NAMED_QUERY_FIND_STATE_BY_CODE, StateType.class);
    query.setParameter("stateCode", primaryKey);
    found = query.uniqueResult();
    return found;
}
Also used : StateType(gov.ca.cwds.cals.persistence.model.calsns.dictionaries.StateType) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject)

Example 2 with PersistentObject

use of gov.ca.cwds.data.persistence.PersistentObject in project cals-api by ca-cwds.

the class AbstractReferentialIntegrityValidator method getPersistentObject.

protected <T extends PersistentObject> PersistentObject getPersistentObject(Session currentSession, T obj, Serializable primaryKey) {
    PersistentObject found;
    found = currentSession.get(obj.getClass(), primaryKey);
    return found;
}
Also used : PersistentObject(gov.ca.cwds.data.persistence.PersistentObject)

Example 3 with PersistentObject

use of gov.ca.cwds.data.persistence.PersistentObject in project api-core by ca-cwds.

the class ApiHibernateInterceptor method preFlush.

/**
 * Called <strong>before</strong> the transaction commits.
 *
 * <p>
 * The underlying iterator's container may share records with other threads, though those records
 * are not visible here. To avoid concurrent modification exceptions, this method synchronizes
 * briefly, converts the iterator to a list, and iterates the list.
 * </p>
 */
@Override
@SuppressWarnings("rawtypes")
public void preFlush(Iterator iter) {
    final List list = iterToList(iter);
    for (Object obj : list) {
        if (obj instanceof PersistentObject) {
            // our object type
            final PersistentObject entity = (PersistentObject) obj;
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("preFlush -> id={}, entity={}", entity.getPrimaryKey(), entity);
            } else {
                LOGGER.info("preFlush -> id={}, entityClass={}", entity.getPrimaryKey(), entity.getClass().getName());
            }
            logLimitedAccessRecord(obj, "preFlush");
            final Class<?> klazz = entity.getClass();
            if (handlers.containsKey(klazz)) {
                LOGGER.info("handler for class {}", klazz);
                handlers.get(klazz).accept(entity);
            }
        }
    }
}
Also used : PersistentObject(gov.ca.cwds.data.persistence.PersistentObject) List(java.util.List) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject)

Example 4 with PersistentObject

use of gov.ca.cwds.data.persistence.PersistentObject in project cals-api by ca-cwds.

the class StateReferentialIntegrityValidator method getPersistentObject.

@Override
protected <T extends PersistentObject> PersistentObject getPersistentObject(Session currentSession, T obj, Serializable primaryKey) {
    PersistentObject found;
    Query<StateType> query = currentSession.createNamedQuery(NAMED_QUERY_FIND_STATE_BY_CODE, StateType.class);
    query.setParameter("stateCode", primaryKey);
    found = query.uniqueResult();
    return found;
}
Also used : StateType(gov.ca.cwds.cals.persistence.model.calsns.dictionaries.StateType) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject)

Example 5 with PersistentObject

use of gov.ca.cwds.data.persistence.PersistentObject in project cals-api by ca-cwds.

the class AbstractReferentialIntegrityValidator method checkReferentialIntegrity.

protected <T extends PersistentObject> boolean checkReferentialIntegrity(Session currentSession, T obj) {
    PersistentObject found = null;
    Serializable primaryKey = obj.getPrimaryKey();
    if (primaryKey != null) {
        found = getPersistentObject(currentSession, obj, primaryKey);
    }
    boolean valid = found != null;
    if (valid && isCheckEqualityRequired()) {
        valid = found.equals(obj);
    }
    if (valid && isEnrichmentRequired()) {
        List<Field> fields = getAllFields(new LinkedList<>(), obj.getClass());
        T finalFound = (T) found;
        fields.stream().filter(field -> !Modifier.isStatic(field.getModifiers())).forEach(field -> {
            try {
                field.setAccessible(true);
                field.set(obj, field.get(finalFound));
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException(e);
            }
        });
    }
    return valid;
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Modifier(java.lang.reflect.Modifier) Session(org.hibernate.Session) Field(java.lang.reflect.Field) LinkedList(java.util.LinkedList) NotNull(javax.validation.constraints.NotNull) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject) Serializable(java.io.Serializable) Field(java.lang.reflect.Field) Serializable(java.io.Serializable) PersistentObject(gov.ca.cwds.data.persistence.PersistentObject)

Aggregations

PersistentObject (gov.ca.cwds.data.persistence.PersistentObject)5 StateType (gov.ca.cwds.cals.persistence.model.calsns.dictionaries.StateType)2 List (java.util.List)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 NotNull (javax.validation.constraints.NotNull)1 Session (org.hibernate.Session)1