Search in sources :

Example 1 with AndQuery

use of org.dbflute.cbean.scoping.AndQuery 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

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConditionBean (org.dbflute.cbean.ConditionBean)1 AndQuery (org.dbflute.cbean.scoping.AndQuery)1 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)1 RelationInfo (org.dbflute.dbmeta.info.RelationInfo)1