Search in sources :

Example 1 with RelationOptionalFactory

use of org.dbflute.optional.RelationOptionalFactory in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method toRelationOptional.

// ===================================================================================
// Optional Handling
// =================
/**
 * Create present or null entity as relation optional.
 * @param relationTitle The title of relation for exception message. (NotNull)
 * @param relationRow The entity instance of relation row. (NullAllowed)
 * @return The optional object for the entity, which has present or null entity. (NotNull)
 */
protected Object toRelationOptional(final String relationTitle, Object relationRow) {
    assertObjectNotNull("relationTitle", relationTitle);
    final RelationOptionalFactory factory = xgetROpFactory();
    final Object result;
    if (relationRow != null) {
        result = factory.createOptionalPresentEntity(relationRow);
    } else {
        result = factory.createOptionalNullEntity(new OptionalThingExceptionThrower() {

            public void throwNotFoundException() {
                String msg = "Not found the relation row for: " + relationTitle;
                throw new EntityAlreadyDeletedException(msg);
            }
        });
    }
    return result;
}
Also used : OptionalThingExceptionThrower(org.dbflute.optional.OptionalThingExceptionThrower) RelationOptionalFactory(org.dbflute.optional.RelationOptionalFactory) EntityAlreadyDeletedException(org.dbflute.exception.EntityAlreadyDeletedException)

Example 2 with RelationOptionalFactory

use of org.dbflute.optional.RelationOptionalFactory in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method isRelationOptional.

/**
 * Is the property type optional for relation?
 * @param relationPropertyType The type of relation property. (NotNull)
 * @return The determination, true or false.
 */
protected boolean isRelationOptional(Class<?> relationPropertyType) {
    assertObjectNotNull("relationPropertyType", relationPropertyType);
    final RelationOptionalFactory factory = xgetROpFactory();
    return factory.isOptionalType(relationPropertyType);
}
Also used : RelationOptionalFactory(org.dbflute.optional.RelationOptionalFactory)

Example 3 with RelationOptionalFactory

use of org.dbflute.optional.RelationOptionalFactory in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method helpPulloutInternally.

// ===================================================================================
// Pull out Relation
// =================
protected <LOCAL_ENTITY extends Entity, FOREIGN_ENTITY extends Entity> List<FOREIGN_ENTITY> helpPulloutInternally(List<LOCAL_ENTITY> localEntityList, String foreignPropertyName) {
    assertObjectNotNull("localEntityList", localEntityList);
    assertObjectNotNull("foreignPropertyName", foreignPropertyName);
    final DBMeta dbmeta = asDBMeta();
    final ForeignInfo foreignInfo = dbmeta.findForeignInfo(foreignPropertyName);
    final RelationInfo reverseInfo = foreignInfo.getReverseRelation();
    final boolean existsReferrer = reverseInfo != null;
    final RelationOptionalFactory optionalFactory = xgetROpFactory();
    final Map<Integer, FOREIGN_ENTITY> foreignBasicMap = new LinkedHashMap<Integer, FOREIGN_ENTITY>();
    // lazy-loaded
    Map<Integer, List<FOREIGN_ENTITY>> foreignCollisionMap = null;
    final Map<FOREIGN_ENTITY, List<LOCAL_ENTITY>> foreignReferrerMap = new LinkedHashMap<FOREIGN_ENTITY, List<LOCAL_ENTITY>>();
    for (LOCAL_ENTITY localEntity : localEntityList) {
        final FOREIGN_ENTITY foreignEntity = xextractPulloutForeignEntity(foreignInfo, reverseInfo, optionalFactory, localEntity);
        if (foreignEntity == null) {
            continue;
        }
        // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
        // basically mapped foreign entities are unique instances
        // but according to circumstances, same PK different instance (rare case)
        // 
        // e.g. normal pattern (no problem)
        // A - M - X
        // B - M - X
        // C - N - X
        // D - O - Y
        // 
        // e.g. when OverRelation, might be following pattern
        // A - M - X
        // B - M - Y *same relation but different nested relation
        // C - N - X
        // D - O - Y
        // 
        // when the latter pattern, the instance of A's M is different from the one of B's M
        // so it need to handle the unique as instance (don't use overridden equals() of entity)
        // _/_/_/_/_/_/_/_/_/_/
        foreignCollisionMap = xsavePulloutForeignEntity(foreignBasicMap, foreignCollisionMap, foreignEntity);
        if (existsReferrer) {
            if (!foreignReferrerMap.containsKey(foreignEntity)) {
                foreignReferrerMap.put(foreignEntity, new ArrayList<LOCAL_ENTITY>());
            }
            foreignReferrerMap.get(foreignEntity).add(localEntity);
        }
    }
    if (existsReferrer) {
        for (Entry<FOREIGN_ENTITY, List<LOCAL_ENTITY>> entry : foreignReferrerMap.entrySet()) {
            final FOREIGN_ENTITY foreignEntity = entry.getKey();
            final List<LOCAL_ENTITY> mappedLocalList = entry.getValue();
            final Object writtenObj = xextractPulloutReverseWrittenObject(foreignInfo, reverseInfo, optionalFactory, mappedLocalList);
            reverseInfo.write(foreignEntity, writtenObj);
        }
    }
    return xpreparePulloutResultList(foreignBasicMap, foreignCollisionMap);
}
Also used : RelationOptionalFactory(org.dbflute.optional.RelationOptionalFactory) LinkedHashMap(java.util.LinkedHashMap) ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) RelationInfo(org.dbflute.dbmeta.info.RelationInfo) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

RelationOptionalFactory (org.dbflute.optional.RelationOptionalFactory)3 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 DBMeta (org.dbflute.dbmeta.DBMeta)1 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)1 RelationInfo (org.dbflute.dbmeta.info.RelationInfo)1 EntityAlreadyDeletedException (org.dbflute.exception.EntityAlreadyDeletedException)1 OptionalThingExceptionThrower (org.dbflute.optional.OptionalThingExceptionThrower)1