Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.s2dao.extension;
 
 import java.util.List;
 import java.util.Map;
 

Author(s):
jflute
 
 public class TnRowCreatorExtension extends TnRowCreatorImpl {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
The logger instance for this class. (NotNull)
 
     private static final Logger _log = LoggerFactory.getLogger(TnRowCreatorExtension.class);

    
The key of DBMeta cache.
 
     protected static final String DBMETA_CACHE_KEY = "df:DBMetaCache";

    
The prefix mark for derived mapping alias.
 
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected DBMeta _fixedDBMeta;
     protected boolean _creatableByDBMeta;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     protected TnRowCreatorExtension() {
     }

    

Parameters:
beanClass The class of target bean to find DB-meta. (NullAllowed)
Returns:
The instance of internal row creator. (NotNull)
 
     public static TnRowCreatorExtension createRowCreator(Class<?> beanClass) {
         final TnRowCreatorExtension rowCreator = new TnRowCreatorExtension();
         if (beanClass != null) {
             final DBMeta dbmeta = findDBMetaByClass(beanClass);
             if (dbmeta != null) {
                 rowCreator.setFixedDBMeta(dbmeta);
                 rowCreator.setCreatableByDBMeta(isCreatableByDBMeta(beanClassdbmeta.getEntityType()));
             }
         }
         return rowCreator;
     }
 
     protected static DBMeta findDBMetaByClass(Class<?> beanClass) {
         if (!Entity.class.isAssignableFrom(beanClass)) {
             return null;
         }
         // getting from entity because the bean may be customize entity.
        final Object instance = newInstance(beanClass); // only when initialization
        return ((Entityinstance).asDBMeta();
    }
    protected static Object newInstance(Class<?> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalStateException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    protected static boolean isCreatableByDBMeta(Class<?> beanClassClass<?> entityType) {
        // Returns false when the bean is not related to the entity or is a sub class of the entity.
        return beanClass.isAssignableFrom(entityType);
    }
    // ===================================================================================
    //                                                                                Main
    //                                                                                ====
    
    public Object createRow(ResultSet rsMap<StringMap<StringInteger>> selectIndexMapMap<StringTnPropertyMappingpropertyCache,
            Class<?> beanClassConditionBean cbthrows SQLException {
        if (propertyCache.isEmpty()) {
            String msg = "The propertyCache should not be empty: bean=" + beanClass.getName();
            throw new IllegalStateException(msg);
        }
        // temporary variable, for exception message, debug message
        String columnName = null;
        TnPropertyMapping mapping = null;
        String propertyName = null;
        Object selectedValue = null;
        ColumnInfo columnInfo = null;
        final Object row;
        final DBMeta dbmeta;
        if ( != null) {
            if () { // mainly here
                final Entity entity = .newEntity();
                reflectConditionBeanOptionToEntity(cbentity);
                row = entity;
            } else { // e.g. manual-extended entity
                row = newBean(beanClass);
            }
            dbmeta = ;
        } else { // e.g. manual-created bean of outsideSql
            row = newBean(beanClass);
            dbmeta = findCachedDBMeta(row); // find just in case
        }
        try {
            if (dbmeta != null) { // mainly here
                final boolean isEntity = row instanceof Entity// almost always true
                final Entity entityRow = isEntity ? (Entityrow : null;
                for (Entry<StringTnPropertyMappingentry : propertyCache.entrySet()) {
                    columnName = entry.getKey();
                    mapping = entry.getValue();
                    propertyName = mapping.getPropertyName();
                    selectedValue = getValue(rscolumnNamemapping.getValueType(), selectIndexMap);
                    columnInfo = mapping.getEntityColumnInfo();
                    if (columnInfo != null && isEntity) {
                        columnInfo.write(entityRowselectedValue);
                    } else {
                        mapping.getPropertyAccessor().setValue(rowselectedValue);
                    }
                }
                if (canHandleDerivedMap(row)) {
                    processDerivedMap(rsselectIndexMappropertyCacherow);
                }
            } else { // not DBFlute entity
                for (Entry<StringTnPropertyMappingentry : propertyCache.entrySet()) {
                    columnName = entry.getKey();
                    mapping = entry.getValue();
                    propertyName = mapping.getPropertyName();
                    selectedValue = getValue(rscolumnNamemapping.getValueType(), selectIndexMap);
                    mapping.getPropertyAccessor().setValue(rowselectedValue);
                }
            }
            return row;
        } catch (ClassCastException e) {
            throwMappingClassCastException(rowdbmetamappingselectedValuee);
            return null// unreachable
        } catch (SQLException e) {
            if (.isDebugEnabled()) {
                String msg = "Failed to get selected values while resultSet handling:";
                msg = msg + " target=" + DfTypeUtil.toClassTitle(beanClass) + "." + propertyName;
                .debug(msg);
            }
            throw e;
        }
    }
    protected void reflectConditionBeanOptionToEntity(ConditionBean cbEntity entity) {
        // unlock access to undefined classification if allowed in condition-bean
        // this should be set before mapping values (and also relation table's creator)
        if (cb != null && cb.isUndefinedClassificationSelectAllowed()) {
            entity.myunlockUndefinedClassificationAccess();
        }
    }
    protected boolean canHandleDerivedMap(final Object row) {
        return row instanceof DerivedMappable && ConditionBeanContext.isExistConditionBeanOnThread();
    }
    protected void processDerivedMap(ResultSet rsMap<StringMap<StringInteger>> selectIndexMap,
            Map<StringTnPropertyMappingpropertyCacheObject rowthrows SQLException {
        final ConditionBean cb = ConditionBeanContext.getConditionBeanOnThread();
        final SqlClause sqlClause = cb.getSqlClause();
        if (!sqlClause.hasSpecifiedDerivingSubQuery()) {
            return;
        }
        final DerivedMappable mappable = (DerivedMappablerow;
        final List<StringderivingAliasList = sqlClause.getSpecifiedDerivingAliasList();
        DerivedTypeHandler typeHandler = null;
        for (String derivingAlias : derivingAliasList) {
            // propertyCache has alias name when derived-referrer as case-insensitive
            if (propertyCache.containsKey(derivingAlias)) { // already handled
                continue;
            }
            if (!derivingAlias.startsWith()) { // basically no way (just in case)
                continue// might be exception but no need to be strict here
            }
            if (typeHandler == null) {
                typeHandler = cb.xgetDerivedTypeHandler(); // basically fixed instance returned
                if (typeHandler == null) { // no way, just in case
                    String msg = "Not found the type handler from condition-bean: " + cb.asTableDbName();
                    throw new IllegalStateException(msg);
                }
            }
            final HpDerivingSubQueryInfo derivingInfo = sqlClause.getSpecifiedDerivingInfo(derivingAlias);
            final ValueType valueType = TnValueTypes.getValueType(typeHandler.findMappingType(derivingInfo));
            final String onQueryAlias = Srl.substringFirstRear(derivingAlias);
            Object selectedValue = getValue(rsonQueryAliasvalueTypeselectIndexMap);
            selectedValue = typeHandler.convertToMapValue(derivingInfoselectedValue);
            mappable.registerDerivedValue(derivingAliasselectedValue);
        }
    }
    protected Object getValue(ResultSet rsString columnNameValueType valueTypeMap<StringMap<StringInteger>> selectIndexMap)
            throws SQLException {
        final Object value;
        if (selectIndexMap != null) {
            value = ResourceContext.getLocalValue(rscolumnNamevalueTypeselectIndexMap);
        } else {
            value = valueType.getValue(rscolumnName);
        }
        return value;
    }
    protected void throwMappingClassCastException(Object entityDBMeta dbmetaTnPropertyMapping mappingObject selectedValue,
            ClassCastException e) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Failed to cast a class while data mapping.");
        br.addItem("Advice");
        br.addElement("If you use Seasar(S2Container), this exception may be");
        br.addElement("from ClassLoader Headache about HotDeploy.");
        br.addElement("Add the ignore-package setting to convention.dicon like this:");
        br.addElement("    <initMethod name=”addIgnorePackageName”>");
        br.addElement("        <arg>”com.example.xxx.dbflute”</arg>");
        br.addElement("    </initMethod>");
        br.addElement("If you use an other DI container, this exception may be");
        br.addElement("from illegal state about your settings of DBFlute.");
        br.addElement("Confirm your settings: for example, typeMappingMap.dfprop.");
        br.addItem("Exception Message");
        br.addElement(e.getMessage());
        br.addItem("Target Entity");
        br.addElement(entity);
        br.addElement("classLoader: " + entity.getClass().getClassLoader());
        br.addItem("Target DBMeta");
        br.addElement(dbmeta);
        br.addElement("classLoader: " + dbmeta.getClass().getClassLoader());
        br.addItem("Property Mapping");
        br.addElement(mapping);
        br.addElement("type: " + (mapping != null ? mapping.getClass() : null));
        br.addItem("Selected Value");
        br.addElement(selectedValue);
        br.addElement("type: " + (selectedValue != null ? selectedValue.getClass() : null));
        final String msg = br.buildExceptionMessage();
        throw new MappingClassCastException(msge);
    }
    // ===================================================================================
    //                                                                        DBMeta Cache
    //                                                                        ============
    // share with relation row
    

Parameters:
row The instance of row. (NotNull)
Returns:
The interface of DBMeta. (NullAllowed: If it's null, it means NotFound.)
    public static DBMeta findCachedDBMeta(Object row) {
        return DBMetaCacheHandler.findDBMeta(row);
    }

    

Parameters:
rowType The type of row. (NotNull)
tableName The name of table. (NotNull)
Returns:
The interface of DBMeta. (NullAllowed: If it's null, it means NotFound.)
    public static DBMeta findCachedDBMeta(Class<?> rowTypeString tableName) {
        return DBMetaCacheHandler.findDBMeta(rowTypetableName);
    }
    protected static class DBMetaCacheHandler {

        
The key of DBMeta cache.
        protected static final String DBMETA_CACHE_KEY = "df:DBMetaCache";
        public static DBMeta findDBMeta(Object row) {
            if (!(row instanceof Entity)) {
                return null;
            }
            final Entity entity = (Entityrow;
            DBMeta dbmeta = getCachedDBMeta(entity.getClass());
            if (dbmeta != null) {
                return dbmeta;
            }
            dbmeta = entity.asDBMeta();
            cacheDBMeta(entitydbmeta);
            return dbmeta;
        }
        public static DBMeta findDBMeta(Class<?> rowTypeString tableName) {
            DBMeta dbmeta = getCachedDBMeta(rowType);
            if (dbmeta != null) {
                return dbmeta;
            }
            // No check because the table name is not always for domain.
            dbmeta = ResourceContext.provideDBMeta(tableName);
            cacheDBMeta(rowTypedbmeta);
            return dbmeta;
        }
        protected static DBMeta getCachedDBMeta(Class<?> rowType) {
            Map<Class<?>, DBMetacontextCacheMap = getDBMetaContextCacheMap();
            if (contextCacheMap == null) {
                contextCacheMap = new HashMap<Class<?>, DBMeta>();
                InternalMapContext.setObject(contextCacheMap);
            }
            return contextCacheMap.get(rowType);
        }
        protected static void cacheDBMeta(Entity entityDBMeta dbmeta) {
            cacheDBMeta(entity.getClass(), dbmeta);
        }
        protected static void cacheDBMeta(Class<?> typeDBMeta dbmeta) {
            final Map<Class<?>, DBMetadbmetaCache = getDBMetaContextCacheMap();
            dbmetaCache.put(typedbmeta);
        }
        @SuppressWarnings("unchecked")
        protected static Map<Class<?>, DBMetagetDBMetaContextCacheMap() {
            return (Map<Class<?>, DBMeta>) InternalMapContext.getObject();
        }
    }
    // ===================================================================================
    //                                                                             Fix Row
    //                                                                             =======
    
Adjust created row. (clearing modified info, ...)

Parameters:
row The row of result list. (NotNull)
checkNonSp Does is use the check of access to non-specified column?
colNullObj Does is use the handling of column null object?
basePointBmd The bean meta data of the row for base-point table. (NotNull)
cb The condition-bean for the select. (NullAllowed: when not condition-bean select)
    public static void adjustCreatedRow(final Object rowboolean checkNonSpboolean colNullObjTnBeanMetaData basePointBmd,
            ConditionBean cb) {
        // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
        // static for handler calling
        // however no other callers now so unnecessary, exists once...
        // only for uniformity with relation
        //
        // *similar implementation for relation row exists
        // no refactoring because it needs high performance here,
        // comment only to avoid wasted calculation and determination
        // _/_/_/_/_/_/_/_/_/_/
        if (row instanceof Entity) {
            final Entity entity = (Entityrow;
            // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
            // check access to non-specified-column
            // modified properties matches specified properties so copy it
            // so should be before clearing modified info
            // _/_/_/_/_/_/_/_/_/_/
            if (checkNonSp) { // contains enabled by CB and using SpecifyColumn
                entity.modifiedToSpecified();
                // _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
                // adjust specification for column null object handling
                // null object target is not specified but it should be able to be called
                // so add dummy to specified properties
                // e.g. A, B and cached C
                // when specifyA, specifyC => no specified-property so *set dummy property here
                // when specifyA, specifyB => C should be checked, actually checked so no action here
                // when non-SpecifyColumm  => can get so no action here
                // when specifyA, specifyC but allowed => can get so no action here
                // when specifyA, specifyB but allowed => should be no cache logically, but rare case so no action here
                // _/_/_/_/_/_/_/_/_/_/
                if (colNullObj && cb != null) { // check 'cb' just in case
                    final SqlClause sqlClause = cb.getSqlClause();
                    for (ColumnInfo columnInfo : sqlClause.getLocalSpecifiedNullObjectColumnSet()) {
                        entity.myspecifyProperty(columnInfo.getPropertyName());
                    }
                }
            }
            // enable the handling of column null object if allowed and object-able
            // only table that has null object target column is object-able (generated as it)
            if (colNullObj && entity instanceof ColumnNullObjectable) {
                ((ColumnNullObjectableentity).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();
        }
    }
    // ===================================================================================
    //                                                                      General Helper
    //                                                                      ==============
    protected String ln() {
        return DBFluteSystem.ln();
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    public void setFixedDBMeta(DBMeta fixedDBMeta) {
        this. = fixedDBMeta;
    }
    public void setCreatableByDBMeta(boolean creatableByDBMeta) {
        this. = creatableByDBMeta;
    }
New to GrepCode? Check out our FAQ X