Search in sources :

Example 16 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class AbstractBehaviorWritable method helpInsertOrUpdateInternally.

protected <RESULT extends ENTITY> void helpInsertOrUpdateInternally(RESULT entity, InsertOption<CB> insOption, UpdateOption<CB> updOption) {
    assertEntityNotNull(entity);
    if (helpDetermineInsertOrUpdateDirectInsert(entity)) {
        doCreate(entity, insOption);
        return;
    }
    RuntimeException updateException = null;
    try {
        doModify(entity, updOption);
    } catch (EntityAlreadyUpdatedException e) {
        // already updated (or means not found)
        updateException = e;
    } catch (EntityAlreadyDeletedException e) {
        // means not found
        updateException = e;
    } catch (OptimisticLockColumnValueNullException e) {
        // means insert?
        updateException = e;
    }
    if (updateException == null) {
        return;
    }
    final CB cb = newConditionBean();
    final Set<String> uniqueDrivenProperties = entity.myuniqueDrivenProperties();
    if (uniqueDrivenProperties != null && !uniqueDrivenProperties.isEmpty()) {
        for (String prop : uniqueDrivenProperties) {
            final DBMeta dbmeta = entity.asDBMeta();
            final ColumnInfo columnInfo = dbmeta.findColumnInfo(prop);
            // already checked in update process
            final Object value = columnInfo.read(entity);
            cb.localCQ().invokeQueryEqual(columnInfo.getColumnDbName(), value);
        }
    } else {
        cb.acceptPrimaryKeyMap(asDBMeta().extractPrimaryKeyMap(entity));
    }
    if (readCount(cb) == 0) {
        // anyway if not found, insert
        doCreate(entity, insOption);
    } else {
        throw updateException;
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) EntityAlreadyUpdatedException(org.dbflute.exception.EntityAlreadyUpdatedException) EntityAlreadyDeletedException(org.dbflute.exception.EntityAlreadyDeletedException) OptimisticLockColumnValueNullException(org.dbflute.exception.OptimisticLockColumnValueNullException)

Example 17 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class AbstractBehaviorWritable method setupExclusiveControlColumnOfQueryInsert.

protected void setupExclusiveControlColumnOfQueryInsert(Entity entity) {
    final DBMeta dbmeta = asDBMeta();
    if (dbmeta.hasVersionNo()) {
        final ColumnInfo columnInfo = dbmeta.getVersionNoColumnInfo();
        columnInfo.write(entity, InsertOption.VERSION_NO_FIRST_VALUE);
    }
    if (dbmeta.hasUpdateDate()) {
        final ColumnInfo columnInfo = dbmeta.getUpdateDateColumnInfo();
        columnInfo.write(entity, ResourceContext.getAccessTimestamp());
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 18 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class TnRowCreatorExtension method adjustCreatedRow.

// ===================================================================================
// Fix Row
// =======
/**
 * Adjust created row. (clearing modified info, ...)
 * @param row The row of result list. (NotNull)
 * @param checkNonSp Does is use the check of access to non-specified column?
 * @param colNullObj Does is use the handling of column null object?
 * @param basePointBmd The bean meta data of the row for base-point table. (NotNull)
 * @param cb The condition-bean for the select. (NullAllowed: when not condition-bean select)
 */
public static void adjustCreatedRow(final Object row, boolean checkNonSp, boolean colNullObj, TnBeanMetaData basePointBmd, ConditionBean cb) {
    // _/_/_/_/_/_/_/_/_/_/
    if (row instanceof Entity) {
        final Entity entity = (Entity) row;
        // _/_/_/_/_/_/_/_/_/_/
        if (checkNonSp) {
            // contains enabled by CB and using SpecifyColumn
            entity.modifiedToSpecified();
            // _/_/_/_/_/_/_/_/_/_/
            if (colNullObj && cb != null) {
                // check 'cb' just in case
                final SqlClause sqlClause = cb.getSqlClause();
                for (ColumnInfo columnInfo : sqlClause.getLocalSpecifiedNullObjectColumnSet()) {
                    entity.myspecifyProperty(columnInfo.getPropertyName());
                }
            }
        }
        // only table that has null object target column is object-able (generated as it)
        if (colNullObj && entity instanceof ColumnNullObjectable) {
            ((ColumnNullObjectable) entity).enableColumnNullObject();
        }
        // clear modified properties for update process using selected entity
        entity.clearModifiedInfo();
        // mark as select to determine the entity is selected or user-created
        // basically for e.g. determine columns of batch insert
        entity.markAsSelect();
    } else {
        // not DBFlute entity
        // actually any bean meta data can be accepted
        // because only it gets modified properties
        basePointBmd.getModifiedPropertyNames(row).clear();
    }
}
Also used : Entity(org.dbflute.Entity) SqlClause(org.dbflute.cbean.sqlclause.SqlClause) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ColumnNullObjectable(org.dbflute.dbmeta.accessory.ColumnNullObjectable)

Example 19 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method doHelpLoadReferrerInternally.

protected <// generic
LOCAL_ENTITY extends Entity, // generic
KEY, // generic
REFERRER_CB extends ConditionBean, // generic
REFERRER_ENTITY extends Entity> NestedReferrerListGateway<REFERRER_ENTITY> doHelpLoadReferrerInternally(List<LOCAL_ENTITY> localEntityList, LoadReferrerOption<REFERRER_CB, REFERRER_ENTITY> loadReferrerOption, final String referrerProperty) {
    final DBMeta dbmeta = asDBMeta();
    final ReferrerInfo referrerInfo = dbmeta.findReferrerInfo(referrerProperty);
    final BehaviorReadable referrerBhv = xfindReferrerBehavior(referrerInfo);
    // might be unique key
    final Set<ColumnInfo> pkColSet = referrerInfo.getLocalReferrerColumnInfoMap().keySet();
    // key is referrer's
    final Map<ColumnInfo, ColumnInfo> mappingColMap = referrerInfo.getReferrerLocalColumnInfoMap();
    final InternalLoadReferrerCallback<LOCAL_ENTITY, KEY, REFERRER_CB, REFERRER_ENTITY> callback;
    if (pkColSet.size() == 1) {
        // simple key
        final ColumnInfo pkCol = pkColSet.iterator().next();
        final ColumnInfo fkCol = mappingColMap.keySet().iterator().next();
        callback = xcreateLoadReferrerCallback(referrerProperty, dbmeta, referrerInfo, referrerBhv, pkCol, fkCol);
    } else {
        // compound key
        final Set<ColumnInfo> fkColSet = mappingColMap.keySet();
        callback = xcreateLoadReferrerCallback(referrerProperty, dbmeta, referrerInfo, referrerBhv, pkColSet, fkColSet, mappingColMap);
    }
    return helpLoadReferrerInternally(localEntityList, loadReferrerOption, callback);
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ReferrerInfo(org.dbflute.dbmeta.info.ReferrerInfo)

Example 20 with ColumnInfo

use of org.dbflute.dbmeta.info.ColumnInfo in project dbflute-core by dbflute.

the class AbstractBehaviorReadable method xcreateLoadReferrerCallback.

protected <// generic
LOCAL_ENTITY extends Entity, // generic
KEY, // generic
REFERRER_CB extends ConditionBean, // generic
REFERRER_ENTITY extends Entity> // return
InternalLoadReferrerCallback<LOCAL_ENTITY, KEY, REFERRER_CB, REFERRER_ENTITY> xcreateLoadReferrerCallback(final String referrerProperty, final DBMeta dbmeta, final ReferrerInfo referrerInfo, final BehaviorReadable referrerBhv, final Set<ColumnInfo> pkColSet, final Set<ColumnInfo> fkColSet, final Map<ColumnInfo, ColumnInfo> mappingColMap) {
    return new // for compound key
    InternalLoadReferrerCallback<LOCAL_ENTITY, KEY, REFERRER_CB, REFERRER_ENTITY>() {

        @SuppressWarnings("unchecked")
        public KEY getPKVal(LOCAL_ENTITY entity) {
            final Map<String, Object> keyMap = xnewLoadReferrerCompoundKeyMap();
            for (ColumnInfo pkCol : pkColSet) {
                // key is DB name
                keyMap.put(pkCol.getColumnDbName(), pkCol.read(entity));
            }
            return (KEY) keyMap;
        // cannot use because it might be unique key
        // return (KEY) dbmeta.extractPrimaryKeyMap(entity);
        }

        public void setRfLs(LOCAL_ENTITY entity, List<REFERRER_ENTITY> referrerList) {
            referrerInfo.write(entity, referrerList);
        }

        @SuppressWarnings("unchecked")
        public REFERRER_CB newMyCB() {
            return (REFERRER_CB) referrerBhv.newConditionBean();
        }

        public void qyFKIn(REFERRER_CB cb, final Collection<KEY> pkList) {
            // compound key doesn't use InScope so OrScopeQuery
            cb.invokeOrScopeQuery(new OrQuery<ConditionBean>() {

                public void query(ConditionBean orCB) {
                    for (final KEY pkKey : pkList) {
                        @SuppressWarnings("unchecked") final Map<String, Object> pkMap = (Map<String, Object>) pkKey;
                        orCB.invokeOrScopeQueryAndPart(new AndQuery<ConditionBean>() {

                            public void query(ConditionBean andCB) {
                                for (ColumnInfo fkCol : fkColSet) {
                                    final ColumnInfo pkCol = mappingColMap.get(fkCol);
                                    // key is DB name
                                    final Object pkValue = pkMap.get(pkCol.getColumnDbName());
                                    andCB.localCQ().invokeQueryEqual(fkCol.getColumnDbName(), pkValue);
                                }
                            }
                        });
                    }
                }
            });
        }

        public void qyOdFKAsc(REFERRER_CB cb) {
            for (ColumnInfo fkCol : fkColSet) {
                cb.localCQ().invokeOrderBy(fkCol.getColumnDbName(), true);
            }
        }

        public void spFKCol(REFERRER_CB cb) {
            for (ColumnInfo fkCol : fkColSet) {
                cb.localSp().xspecifyColumn(fkCol.getColumnDbName());
            }
        }

        public List<REFERRER_ENTITY> selRfLs(REFERRER_CB cb) {
            return referrerBhv.readList(cb);
        }

        @SuppressWarnings("unchecked")
        public KEY getFKVal(REFERRER_ENTITY entity) {
            final Map<String, Object> fkMap = xnewLoadReferrerCompoundKeyMap();
            for (ColumnInfo fkCol : fkColSet) {
                final Object fkValue = fkCol.read(entity);
                final ColumnInfo pkCol = mappingColMap.get(fkCol);
                // key is DB name
                final String mapKey = pkCol.getColumnDbName();
                final Class<?> fkType = fkCol.getObjectNativeType();
                final Class<?> pkType = pkCol.getObjectNativeType();
                final Object realValue;
                if (fkType.equals(pkType)) {
                    // basically true
                    realValue = fkValue;
                } else {
                    // different type (needs implicit conversion)
                    realValue = xconvertFK2PKImplicitly(referrerProperty, fkType, pkType, fkValue);
                }
                fkMap.put(mapKey, realValue);
            }
            return (KEY) fkMap;
        }

        public void setlcEt(REFERRER_ENTITY referrerEntity, LOCAL_ENTITY localEntity) {
            // always exists
            final RelationInfo reverseInfo = referrerInfo.getReverseRelation();
            final Object written = xconvertToRelationOptionalEntityIfNeeds(localEntity, reverseInfo);
            reverseInfo.write(referrerEntity, written);
        }

        public String getRfPrNm() {
            return referrerProperty;
        }
    };
}
Also used : ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ConditionBean(org.dbflute.cbean.ConditionBean) RelationInfo(org.dbflute.dbmeta.info.RelationInfo) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) AndQuery(org.dbflute.cbean.scoping.AndQuery) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)73 DBMeta (org.dbflute.dbmeta.DBMeta)27 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)14 Entity (org.dbflute.Entity)8 SpecifiedColumn (org.dbflute.cbean.dream.SpecifiedColumn)7 SqlClause (org.dbflute.cbean.sqlclause.SqlClause)7 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)5 PrimaryInfo (org.dbflute.dbmeta.info.PrimaryInfo)5 ColumnRealName (org.dbflute.dbmeta.name.ColumnRealName)5 List (java.util.List)4 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)4 HashSet (java.util.HashSet)3 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)3 IllegalConditionBeanOperationException (org.dbflute.exception.IllegalConditionBeanOperationException)3 Method (java.lang.reflect.Method)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 Collection (java.util.Collection)2 Date (java.util.Date)2