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;
 import java.util.Set;
 
The DBFlute extension of factory of bean meta data.

Author(s):
jflute
 
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
Log instance for internal debug. (XLog should be used instead for execute-status log)
 
     private static final Logger _log = LoggerFactory.getLogger(TnBeanMetaDataFactoryExtension.class);
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     
The cached instance of relation optional factory. (NotNull)
 
     protected final TnRelationRowOptionalHandler _relationRowOptionalHandler;

    
The map of bean meta data for cache. (NotNull)
 
     protected final Map<Class<?>, TnBeanMetaData_metaMap = newConcurrentHashMap();

    
Is internal debug enabled?
 
     protected boolean _internalDebug;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     

Parameters:
relationOptionalFactory The factory of relation optional. (NotNull)
 
     public TnBeanMetaDataFactoryExtension(RelationOptionalFactory relationOptionalFactory) {
          = createRelationRowOptionalHandler(relationOptionalFactory);
     }
 
     // ===================================================================================
     //                                                                  Override for Cache
     //                                                                  ==================
     @Override
     public TnBeanMetaData createBeanMetaData(Class<?> beanClass) {
         final TnBeanMetaData cachedMeta = findCachedMeta(beanClass);
         if (cachedMeta != null) {
             return cachedMeta;
         } else {
             return super.createBeanMetaData(beanClass);
         }
     }
    @Override
    public TnBeanMetaData createBeanMetaData(Class<?> beanClassint relationNestLevel) {
        final TnBeanMetaData cachedMeta = findCachedMeta(beanClass);
        if (cachedMeta != null) {
            return cachedMeta;
        } else {
            return super.createBeanMetaData(beanClassrelationNestLevel);
        }
    }
    @Override
        final MetaDataConnectionProvider connectionProvider = createMetaDataConnectionProvider();
        final LazyDatabaseMetaDataWrapper metaDataWrapper = new LazyDatabaseMetaDataWrapper(connectionProvider);
        metaDataWrapper.restrictMetaData(); // because DBFlute completely does not use dynamic meta data
        return metaDataWrapper;
    }

    
Create the provider of connection for database meta data.
The provider might provide connection from manual thread.

Returns:
The instance of the connection provider. (NotNull)
        return new MetaDataConnectionProvider() {
            public Connection getConnection() throws SQLException {
                final ManualThreadDataSourceHandler handler = getManualThreadDataSourceHandler();
                if (handler != null) {
                    return handler.getConnection();
                }
                return .getConnection();
            }
        };
    }

    
Get the data source handler of manual thread.

Returns:
The instance of the data source handler. (NullAllowed: if null, no manual thread handling)
        return ManualThreadDataSourceHandler.getDataSourceHandler();
    }
    @Override
    public TnBeanMetaData createBeanMetaData(DatabaseMetaData dbMetaDataClass<?> beanClassint relationNestLevel) {
        final TnBeanMetaData cachedMeta = findOrCreateCachedMetaIfNeeds(dbMetaDatabeanClassrelationNestLevel);
        if (cachedMeta != null) {
            return cachedMeta;
        } else {
            return super.createBeanMetaData(dbMetaDatabeanClassrelationNestLevel);
        }
    }
    protected TnBeanMetaData findCachedMeta(Class<?> beanClass) {
        if (isDBFluteEntity(beanClass)) {
            final TnBeanMetaData cachedMeta = getMetaFromCache(beanClass);
            if (cachedMeta != null) {
                return cachedMeta;
            }
        }
        return null;
    }
    protected TnBeanMetaData findOrCreateCachedMetaIfNeeds(DatabaseMetaData dbMetaDataClass<?> beanClassint relationNestLevel) {
        if (isDBFluteEntity(beanClass)) {
            final TnBeanMetaData cachedMeta = getMetaFromCache(beanClass);
            if (cachedMeta != null) {
                return cachedMeta;
            } else {
                return super.createBeanMetaData(dbMetaDatabeanClass, 0);
            }
        }
        return null;
    }
    @Override
    protected TnBeanMetaDataImpl createBeanMetaDataImpl(Class<?> beanClass) {
        // /= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // for ConditionBean and insert() and update() and delete() and so on...
        // = = = = = = = = = =/
        final DBMeta dbmeta = provideDBMeta(beanClass);
        return new TnBeanMetaDataImpl(beanClassdbmeta) {
            
The internal list of identifier generator. Elements of this list should be added when initializing.
            protected final List<TnIdentifierGenerator_internalIdentifierGeneratorList = new ArrayList<TnIdentifierGenerator>();

            
The internal map of identifier generator by property name.
            // /= = = = = = =
            // for cache
            // = = = = =/
            @Override
            public void initialize() { // non thread safe so this is called immediately after creation 
                final Class<?> myBeanClass = getBeanClass();
                if (isDBFluteEntity(myBeanClass)) {
                    final TnBeanMetaData cachedMeta = getMetaFromCache(myBeanClass);
                    if (cachedMeta == null) {
                        if (isInternalDebugEnabled()) {
                            .debug("...Caching the bean: " + DfTypeUtil.toClassTitle(myBeanClass));
                        }
                        .put(myBeanClassthis);
                    }
                }
                super.initialize();
            }
            // /= = = = = = =
            // for insert()
            // = = = = =/
            // The attributes 'identifierGenerators' and 'identifierGeneratorsByPropertyName'
            // of super class are unused. It prepares original attributes here.
            @Override
            protected void setupIdentifierGenerator(TnPropertyType propertyType) { // only called in the initialize() process
                final DfPropertyDesc pd = propertyType.getPropertyDesc();
                final String propertyName = propertyType.getPropertyName();
                final String idType = .getId(pd);
                final TnIdentifierGenerator generator = createInternalIdentifierGenerator(propertyTypeidType);
                .add(generator);
                .put(propertyNamegenerator);
            }
            protected TnIdentifierGenerator createInternalIdentifierGenerator(TnPropertyType propertyTypeString idType) {
                return TnIdentifierGeneratorFactory.createIdentifierGenerator(propertyTypeidType);
            }
            @Override
            public TnIdentifierGenerator getIdentifierGenerator(int index) {
                return .get(index);
            }
            @Override
            public int getIdentifierGeneratorSize() {
                return .size();
            }
            @Override
            public TnIdentifierGenerator getIdentifierGenerator(String propertyName) {
                return .get(propertyName);
            }
        };
    }
    // ===================================================================================
    //                                                       Override for ModifiedProperty
    //                                                       =============================
    @Override
        return new TnModifiedPropertySupport() {
            public Set<StringgetModifiedPropertyNames(Object bean) {
                if (bean instanceof Entity) { // all entities of DBFlute are here
                    return ((Entitybean).mymodifiedProperties();
                } else { // basically no way on DBFlute (S2Dao's route)
                    final DfBeanDesc beanDesc = DfBeanDescFactory.getBeanDesc(bean.getClass());
                    final String propertyName = ;
                    if (!beanDesc.hasPropertyDesc(propertyName)) {
                        return DfCollectionUtil.emptySet();
                    } else {
                        final DfPropertyDesc propertyDesc = beanDesc.getPropertyDesc(propertyName);
                        final Object value = propertyDesc.getValue(bean);
                        if (value != null) {
                            @SuppressWarnings("unchecked")
                            final Set<StringextractedSet = (Set<String>) value;
                            return extractedSet;
                        } else {
                            return DfCollectionUtil.emptySet();
                        }
                    }
                }
            }
        };
    }
    // ===================================================================================
    //                                                                   Annotation Reader
    //                                                                   =================
    @Override
    protected TnBeanAnnotationReader createBeanAnnotationReader(Class<?> beanClass) {
        // the DBMeta annotation reader also has field annotation reader's functions
        // so fixedly creates and returns
        return new TnDBMetaBeanAnnotationReader(beanClass);
    }
    // ===================================================================================
    //                                                                       Property Type
    //                                                                       =============
    protected TnRelationPropertyTypeFactory createRelationPropertyTypeFactory(Class<?> beanClassTnBeanMetaDataImpl localBeanMetaData,
            TnBeanAnnotationReader beanAnnotationReaderDatabaseMetaData dbMetaDataint relationNestLevelboolean stopRelationCreation) {
        // DBFlute needs local BeanMetaData for relation property type
        return builder.build(beanClasslocalBeanMetaDatabeanAnnotationReaderdbMetaDatarelationNestLevelstopRelationCreation,
                getRelationOptionalEntityType());
    }
        return new TnRelationPropertyTypeFactoryBuilderImpl(this); // is already customized for DBFlute
    }
    protected Class<?> getRelationOptionalEntityType() {
    }
    // ===================================================================================
    //                                                                 Relation Next Level
    //                                                                 ===================
    
Get the limit nest level of relation.

Returns:
The limit nest level of relation.
    @Override
    protected int getLimitRelationNestLevel() {
        // for Compatible to old version DBFlute
        // and this is actually unused on ConditionBean for now
        // CB covers an infinity nest level scope by its own original way
        // this method is used only when you use runtime classes as plain S2Dao
        return 2;
    }
    // ===================================================================================
    //                                                                   Optional Handling
    //                                                                   =================
        return ;
    }
        return new TnRelationRowOptionalHandler(factory);
    }
    // ===================================================================================
    //                                                                       Assist Helper
    //                                                                       =============
    protected boolean isDBFluteEntity(Class<?> beanClass) {
        return Entity.class.isAssignableFrom(beanClass);
    }
    protected DBMeta provideDBMeta(Class<?> entityType) {
        return ResourceContext.provideDBMeta(entityType);
    }
    protected TnBeanMetaData getMetaFromCache(Class<?> beanClass) {
        return .get(beanClass);
    }
    // ===================================================================================
    //                                                                      Internal Debug
    //                                                                      ==============
    private boolean isInternalDebugEnabled() { // because log instance is private
        return ResourceContext.isInternalDebug() && .isDebugEnabled();
    }
    // ===================================================================================
    //                                                                      General Helper
    //                                                                      ==============
    protected <KEY, VALUE> ConcurrentHashMap<KEY, VALUE> newConcurrentHashMap() {
        return new ConcurrentHashMap<KEY, VALUE>();
    }
    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    public void setInternalDebug(boolean internalDebug) {
         = internalDebug;
    }
New to GrepCode? Check out our FAQ X