Search in sources :

Example 1 with TnIdentifierGenerator

use of org.dbflute.s2dao.identity.TnIdentifierGenerator in project dbflute-core by dbflute.

the class TnBeanMetaDataFactoryExtension method createBeanMetaDataImpl.

@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(beanClass, dbmeta) {

        /**
         * 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.
         */
        protected final Map<String, TnIdentifierGenerator> _internalIdentifierGeneratorsByPropertyName = newConcurrentHashMap();

        // /= = = = = = =
        // 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()) {
                        _log.debug("...Caching the bean: " + DfTypeUtil.toClassTitle(myBeanClass));
                    }
                    _metaMap.put(myBeanClass, this);
                }
            }
            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 = _beanAnnotationReader.getId(pd);
            final TnIdentifierGenerator generator = createInternalIdentifierGenerator(propertyType, idType);
            _internalIdentifierGeneratorList.add(generator);
            _internalIdentifierGeneratorsByPropertyName.put(propertyName, generator);
        }

        protected TnIdentifierGenerator createInternalIdentifierGenerator(TnPropertyType propertyType, String idType) {
            return TnIdentifierGeneratorFactory.createIdentifierGenerator(propertyType, idType);
        }

        @Override
        public TnIdentifierGenerator getIdentifierGenerator(int index) {
            return _internalIdentifierGeneratorList.get(index);
        }

        @Override
        public int getIdentifierGeneratorSize() {
            return _internalIdentifierGeneratorList.size();
        }

        @Override
        public TnIdentifierGenerator getIdentifierGenerator(String propertyName) {
            return _internalIdentifierGeneratorsByPropertyName.get(propertyName);
        }
    };
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) DBMeta(org.dbflute.dbmeta.DBMeta) TnBeanMetaData(org.dbflute.s2dao.metadata.TnBeanMetaData) ArrayList(java.util.ArrayList) List(java.util.List) TnBeanMetaDataImpl(org.dbflute.s2dao.metadata.impl.TnBeanMetaDataImpl) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType) TnIdentifierGenerator(org.dbflute.s2dao.identity.TnIdentifierGenerator)

Example 2 with TnIdentifierGenerator

use of org.dbflute.s2dao.identity.TnIdentifierGenerator in project dbflute-core by dbflute.

the class TnInsertEntityDynamicCommand method createInsertPropertyTypes.

// ===================================================================================
// Insert Column
// =============
protected TnPropertyType[] createInsertPropertyTypes(TnBeanMetaData bmd, Object bean, String[] propertyNames, InsertOption<ConditionBean> option) {
    if (0 == propertyNames.length) {
        String msg = "The property name was not found in the bean: " + bean;
        throw new IllegalStateException(msg);
    }
    final List<TnPropertyType> typeList = new ArrayList<TnPropertyType>();
    final Set<?> modifiedSet = getModifiedPropertyNames(bean);
    final String timestampProp = bmd.getTimestampPropertyName();
    final String versionNoProp = bmd.getVersionNoPropertyName();
    for (int i = 0; i < propertyNames.length; ++i) {
        final TnPropertyType pt = bmd.getPropertyType(propertyNames[i]);
        if (pt.isPrimaryKey()) {
            if (option == null || !option.isPrimaryKeyIdentityDisabled()) {
                final TnIdentifierGenerator generator = bmd.getIdentifierGenerator(pt.getPropertyName());
                if (!generator.isSelfGenerate()) {
                    continue;
                }
            }
            typeList.add(pt);
        } else {
            if (// OptimisticLock
            isOptimisticLockProperty(timestampProp, versionNoProp, pt) || isSpecifiedProperty(bean, option, modifiedSet, pt)) {
                // Specified
                typeList.add(pt);
            }
        }
    }
    if (typeList.isEmpty()) {
        throwEntityInsertPropertyNotFoundException(bmd, bean);
    }
    return (TnPropertyType[]) typeList.toArray(new TnPropertyType[typeList.size()]);
}
Also used : ArrayList(java.util.ArrayList) TnPropertyType(org.dbflute.s2dao.metadata.TnPropertyType) TnIdentifierGenerator(org.dbflute.s2dao.identity.TnIdentifierGenerator)

Example 3 with TnIdentifierGenerator

use of org.dbflute.s2dao.identity.TnIdentifierGenerator in project dbflute-core by dbflute.

the class TnBeanMetaDataImpl method setupIdentifierGenerator.

protected void setupIdentifierGenerator(TnPropertyType pt) {
    // only called in the initialize() process
    final DfPropertyDesc pd = pt.getPropertyDesc();
    final String propertyName = pt.getPropertyName();
    final String idType = _beanAnnotationReader.getId(pd);
    final TnIdentifierGenerator generator = TnIdentifierGeneratorFactory.createIdentifierGenerator(pt, idType);
    _identifierGeneratorList.add(generator);
    _identifierGeneratorsByPropertyName.put(propertyName, generator);
}
Also used : DfPropertyDesc(org.dbflute.helper.beans.DfPropertyDesc) TnIdentifierGenerator(org.dbflute.s2dao.identity.TnIdentifierGenerator)

Aggregations

TnIdentifierGenerator (org.dbflute.s2dao.identity.TnIdentifierGenerator)3 ArrayList (java.util.ArrayList)2 DfPropertyDesc (org.dbflute.helper.beans.DfPropertyDesc)2 TnPropertyType (org.dbflute.s2dao.metadata.TnPropertyType)2 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DBMeta (org.dbflute.dbmeta.DBMeta)1 TnBeanMetaData (org.dbflute.s2dao.metadata.TnBeanMetaData)1 TnBeanMetaDataImpl (org.dbflute.s2dao.metadata.impl.TnBeanMetaDataImpl)1