Search in sources :

Example 1 with ContainerHandler

use of org.datanucleus.store.types.ContainerHandler in project datanucleus-core by datanucleus.

the class DeleteFieldManager method processContainer.

private void processContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd, ExecutionContext ec, RelationType relationType) {
    TypeManager typeManager = op.getExecutionContext().getTypeManager();
    ContainerHandler containerHandler = typeManager.getContainerHandler(mmd.getType());
    if (mmd.hasMap()) {
        processMapContainer(fieldNumber, container, mmd, containerHandler);
    } else {
        processElementContainer(fieldNumber, container, mmd, containerHandler, ec, relationType);
    }
}
Also used : ContainerHandler(org.datanucleus.store.types.ContainerHandler) ElementContainerHandler(org.datanucleus.store.types.ElementContainerHandler) TypeManager(org.datanucleus.store.types.TypeManager)

Example 2 with ContainerHandler

use of org.datanucleus.store.types.ContainerHandler in project datanucleus-core by datanucleus.

the class LoadFieldManager method processContainer.

private Object processContainer(int fieldNumber, Object container, AbstractMemberMetaData mmd) {
    Object wrappedContainer = container;
    if (mmd.hasArray()) {
        wrappedContainer = container;
    } else {
        if (!(container instanceof SCO)) {
            // Replace with SCO
            wrappedContainer = SCOUtils.wrapSCOField(op, fieldNumber, container, true);
        }
    }
    // Process all persistable objects in the container: elements,values and keys
    ExecutionContext ec = op.getExecutionContext();
    TypeManager typeManager = ec.getTypeManager();
    ContainerHandler containerHandler = typeManager.getContainerHandler(mmd.getType());
    ApiAdapter api = ec.getApiAdapter();
    for (Object object : containerHandler.getAdapter(wrappedContainer)) {
        if (api.isPersistable(object)) {
            processPersistable(object);
        }
    }
    return wrappedContainer;
}
Also used : ExecutionContext(org.datanucleus.ExecutionContext) ApiAdapter(org.datanucleus.api.ApiAdapter) ContainerHandler(org.datanucleus.store.types.ContainerHandler) TypeManager(org.datanucleus.store.types.TypeManager) SCO(org.datanucleus.store.types.SCO)

Example 3 with ContainerHandler

use of org.datanucleus.store.types.ContainerHandler in project datanucleus-api-jdo by datanucleus.

the class JDOAnnotationReader method processMemberAnnotations.

/**
 * Convenience method to process the annotations for a field/property. The passed annotations may have been specified on the field or on the getter methods.
 * @param cmd The ClassMetaData/InterfaceMetaData to update
 * @param member The field/property
 * @param annotations Annotations for the field/property
 * @return The FieldMetaData/PropertyMetaData that was added (if any)
 */
protected AbstractMemberMetaData processMemberAnnotations(AbstractClassMetaData cmd, Member member, AnnotationObject[] annotations) {
    if (annotations == null || annotations.length == 0) {
        return null;
    }
    AbstractMemberMetaData mmd = null;
    boolean primaryKey = false;
    boolean serialised = false;
    boolean embeddedMember = false;
    boolean nonPersistentField = false;
    boolean transactionalField = false;
    String cacheable = null;
    Class[] elementTypes = null;
    String embeddedElement = null;
    String serializedElement = null;
    String dependentElement = null;
    Class[] keyTypes = null;
    String embeddedKey = null;
    String serializedKey = null;
    String dependentKey = null;
    Class[] valueTypes = null;
    String embeddedValue = null;
    String serializedValue = null;
    String dependentValue = null;
    String embeddedOwnerField = null;
    String embeddedNullIndicatorColumn = null;
    String embeddedNullIndicatorValue = null;
    Persistent[] embeddedMembers = null;
    Persistent[] embeddedElementMembers = null;
    Persistent[] embeddedKeyMembers = null;
    Persistent[] embeddedValueMembers = null;
    ColumnMetaData[] colmds = null;
    JoinMetaData joinmd = null;
    ElementMetaData elemmd = null;
    KeyMetaData keymd = null;
    ValueMetaData valuemd = null;
    OrderMetaData ordermd = null;
    IndexMetaData idxmd = null;
    UniqueMetaData unimd = null;
    ForeignKeyMetaData fkmd = null;
    Map<String, String> extensions = null;
    Class convertConverterCls = null;
    for (AnnotationObject annotation : annotations) {
        String annName = annotation.getName();
        Map<String, Object> annotationValues = annotation.getNameValueMap();
        if (annName.equals(JDOAnnotationUtils.PERSISTENT)) {
            String pkStr = "" + annotationValues.get("primaryKey");
            Boolean pk = null;
            if (!StringUtils.isWhitespace(pkStr)) {
                pk = Boolean.valueOf(pkStr);
            }
            String dfgStr = (String) annotationValues.get("defaultFetchGroup");
            Boolean dfg = null;
            if (!StringUtils.isWhitespace(dfgStr)) {
                dfg = Boolean.valueOf(dfgStr);
            }
            String nullValue = JDOAnnotationUtils.getNullValueString((NullValue) annotationValues.get("nullValue"));
            String embStr = (String) annotationValues.get("embedded");
            Boolean embedded = null;
            if (!StringUtils.isWhitespace(embStr)) {
                embedded = Boolean.valueOf(embStr);
            }
            String serStr = (String) annotationValues.get("serialized");
            Boolean serialized = null;
            if (!StringUtils.isWhitespace(serStr)) {
                serialized = Boolean.valueOf(serStr);
            }
            String depStr = (String) annotationValues.get("dependent");
            Boolean dependent = null;
            if (!StringUtils.isWhitespace(depStr)) {
                dependent = Boolean.valueOf(depStr);
            }
            String valueStrategy = JDOAnnotationUtils.getValueGenerationStrategyString((IdGeneratorStrategy) annotationValues.get("valueStrategy"));
            String customValueStrategy = (String) annotationValues.get("customValueStrategy");
            if (!StringUtils.isWhitespace(customValueStrategy)) {
                // User has provided an extension strategy
                valueStrategy = customValueStrategy;
            }
            FieldPersistenceModifier modifier = JDOAnnotationUtils.getFieldPersistenceModifier((PersistenceModifier) annotationValues.get("persistenceModifier"));
            if (modifier == null) {
                modifier = FieldPersistenceModifier.PERSISTENT;
            }
            String sequence = (String) annotationValues.get("sequence");
            String mappedBy = (String) annotationValues.get("mappedBy");
            String table = (String) annotationValues.get("table");
            String column = (String) annotationValues.get("column");
            String loadFetchGroup = (String) annotationValues.get("loadFetchGroup");
            String fieldTypeName = null;
            int recursionDepth = ((Integer) annotationValues.get("recursionDepth")).intValue();
            cacheable = (String) annotationValues.get("cacheable");
            Class[] fieldTypes = (Class[]) annotationValues.get("types");
            if (fieldTypes != null && fieldTypes.length > 0) {
                StringBuilder typeStr = new StringBuilder();
                for (Class fieldType : fieldTypes) {
                    if (typeStr.length() > 0) {
                        typeStr.append(',');
                    }
                    if (fieldType != null && fieldType != void.class) {
                        typeStr.append(fieldType.getName());
                    }
                }
                fieldTypeName = typeStr.toString();
            }
            dependentElement = (String) annotationValues.get("dependentElement");
            serializedElement = (String) annotationValues.get("serializedElement");
            embeddedElement = (String) annotationValues.get("embeddedElement");
            dependentKey = (String) annotationValues.get("dependentKey");
            serializedKey = (String) annotationValues.get("serializedKey");
            embeddedKey = (String) annotationValues.get("embeddedKey");
            dependentValue = (String) annotationValues.get("dependentValue");
            serializedValue = (String) annotationValues.get("serializedValue");
            embeddedValue = (String) annotationValues.get("embeddedValue");
            Class converterCls = (Class) annotationValues.get("converter");
            if (converterCls == UseDefault.class) {
                converterCls = null;
            }
            Boolean disableConversion = (Boolean) annotationValues.get("useDefaultConversion");
            mmd = member.isProperty() ? new PropertyMetaData(cmd, member.getName()) : new FieldMetaData(cmd, member.getName());
            if (isPersistenceContext()) {
                if (disableConversion != null && disableConversion) {
                    mmd.setTypeConverterDisabled();
                } else if (converterCls != null) {
                    TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
                    if (typeMgr.getTypeConverterForName(converterCls.getName()) == null) {
                        // Not yet cached an instance of this converter so create one
                        AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), converterCls);
                        Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(converterCls, member.getType());
                        Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(converterCls, attrType, null);
                        // Register the TypeConverter under the name of the AttributeConverter class
                        JDOTypeConverter typeConv = new JDOTypeConverter(conv);
                        typeMgr.registerConverter(converterCls.getName(), typeConv, attrType, dbType, false, null);
                    }
                    mmd.setTypeConverterName(converterCls.getName());
                }
            }
            if (modifier != null) {
                mmd.setPersistenceModifier(modifier);
            }
            if (dfg != null) {
                mmd.setDefaultFetchGroup(dfg);
            }
            if (pk != null) {
                mmd.setPrimaryKey(pk);
            }
            if (embedded != null) {
                mmd.setEmbedded(embedded);
            }
            if (serialized != null) {
                mmd.setSerialised(serialized);
            }
            if (dependent != null) {
                mmd.setDependent(dependent);
            }
            mmd.setNullValue(org.datanucleus.metadata.NullValue.getNullValue(nullValue));
            mmd.setMappedBy(mappedBy);
            mmd.setColumn(column);
            mmd.setTable(table);
            mmd.setRecursionDepth(recursionDepth);
            mmd.setLoadFetchGroup(loadFetchGroup);
            mmd.setValueStrategy(valueStrategy);
            mmd.setSequence(sequence);
            mmd.setFieldTypes(fieldTypeName);
            // Add any columns defined on the @Persistent
            Column[] columns = (Column[]) annotationValues.get("columns");
            if (columns != null && columns.length > 0) {
                for (Column col : columns) {
                    mmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(col));
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(mmd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.PRIMARY_KEY)) {
            primaryKey = true;
            if (cmd.getIdentityType() == IdentityType.DATASTORE) {
                // ClassMetaData was created as DATASTORE so change it to APPLICATION
                cmd.setIdentityType(IdentityType.APPLICATION);
            }
        } else if (annName.equals(JDOAnnotationUtils.SERIALIZED)) {
            serialised = true;
        } else if (annName.equals(JDOAnnotationUtils.NOTPERSISTENT)) {
            nonPersistentField = true;
        } else if (annName.equals(JDOAnnotationUtils.TRANSACTIONAL)) {
            transactionalField = true;
        } else if (annName.equals(JDOAnnotationUtils.COLUMNS)) {
            // Multiple column specification
            Column[] cols = (Column[]) annotationValues.get("value");
            if (cols != null && cols.length > 0) {
                colmds = new ColumnMetaData[cols.length];
                for (int j = 0; j < cols.length; j++) {
                    colmds[j] = JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(cols[j]);
                    JDOAnnotationUtils.addExtensionsToMetaData(colmds[j], cols[j].extensions());
                }
            }
        } else if (annName.equals(JDOAnnotationUtils.COLUMN)) {
            // Single column specification
            colmds = new ColumnMetaData[1];
            colmds[0] = JDOAnnotationUtils.getColumnMetaDataForAnnotations(annotationValues);
            JDOAnnotationUtils.addExtensionsToMetaData(colmds[0], (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.JOIN)) {
            String joinColumn = (String) annotationValues.get("column");
            String joinOuter = (String) annotationValues.get("outer");
            String deleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
            String pkName = (String) annotationValues.get("primaryKey");
            String fkName = (String) annotationValues.get("foreignKey");
            String generateFK = (String) annotationValues.get("generateForeignKey");
            String indexed = (String) annotationValues.get("indexed");
            String indexName = (String) annotationValues.get("index");
            String unique = (String) annotationValues.get("unique");
            String uniqueName = (String) annotationValues.get("uniqueKey");
            String generatePK = (String) annotationValues.get("generatePrimaryKey");
            if (!StringUtils.isWhitespace(uniqueName)) {
                unique = "true";
            }
            if (!StringUtils.isWhitespace(indexName)) {
                indexed = "true";
            }
            Column[] joinColumns = (Column[]) annotationValues.get("columns");
            joinmd = new JoinMetaData();
            joinmd.setColumnName(joinColumn);
            joinmd.setOuter(MetaDataUtils.getBooleanForString(joinOuter, false));
            joinmd.setIndexed(IndexedValue.getIndexedValue(indexed));
            joinmd.setUnique(unique);
            joinmd.setDeleteAction(deleteAction);
            if (!StringUtils.isWhitespace(pkName)) {
                PrimaryKeyMetaData pkmd = new PrimaryKeyMetaData();
                pkmd.setName(pkName);
                joinmd.setPrimaryKeyMetaData(pkmd);
            } else if (generatePK != null && generatePK.equalsIgnoreCase("true")) {
                joinmd.setPrimaryKeyMetaData(new PrimaryKeyMetaData());
            }
            if (!StringUtils.isWhitespace(fkName)) {
                ForeignKeyMetaData joinFkmd = joinmd.getForeignKeyMetaData();
                if (joinFkmd == null) {
                    joinFkmd = new ForeignKeyMetaData();
                    joinFkmd.setName(fkName);
                    joinmd.setForeignKeyMetaData(joinFkmd);
                } else {
                    joinFkmd.setName(fkName);
                }
            } else if (generateFK != null && generateFK.equalsIgnoreCase("true")) {
                joinmd.setForeignKeyMetaData(new ForeignKeyMetaData());
            }
            if (!StringUtils.isWhitespace(indexName)) {
                IndexMetaData joinIdxmd = joinmd.getIndexMetaData();
                if (joinIdxmd == null) {
                    joinIdxmd = new IndexMetaData();
                    joinmd.setIndexMetaData(joinIdxmd);
                }
                joinIdxmd.setName(indexName);
            }
            if (!StringUtils.isWhitespace(uniqueName)) {
                UniqueMetaData joinUnimd = joinmd.getUniqueMetaData();
                if (joinUnimd == null) {
                    joinUnimd = new UniqueMetaData();
                    joinmd.setUniqueMetaData(joinUnimd);
                }
                joinUnimd.setName(uniqueName);
            }
            if (joinColumns != null && joinColumns.length > 0) {
                for (Column joinCol : joinColumns) {
                    joinmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(joinCol));
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(joinmd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.ELEMENT)) {
            // Element of a Collection/Array
            elementTypes = (Class[]) annotationValues.get("types");
            embeddedElement = (String) annotationValues.get("embedded");
            serializedElement = (String) annotationValues.get("serialized");
            dependentElement = (String) annotationValues.get("dependent");
            String elementDeleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
            String elementUpdateAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("updateAction"));
            String fkName = (String) annotationValues.get("foreignKey");
            String generateFK = (String) annotationValues.get("generateForeignKey");
            String indexed = (String) annotationValues.get("indexed");
            String indexName = (String) annotationValues.get("index");
            String unique = (String) annotationValues.get("unique");
            String uniqueName = (String) annotationValues.get("uniqueKey");
            Class converterCls = (Class) annotationValues.get("converter");
            if (converterCls == UseDefault.class) {
                converterCls = null;
            }
            Boolean disableConversion = (Boolean) annotationValues.get("useDefaultConversion");
            if (!StringUtils.isWhitespace(uniqueName)) {
                unique = "true";
            }
            if (!StringUtils.isWhitespace(indexName)) {
                indexed = "true";
            }
            elemmd = new ElementMetaData();
            elemmd.setTable((String) annotationValues.get("table"));
            elemmd.setColumnName((String) annotationValues.get("column"));
            elemmd.setDeleteAction(elementDeleteAction);
            elemmd.setUpdateAction(elementUpdateAction);
            elemmd.setIndexed(IndexedValue.getIndexedValue(indexed));
            elemmd.setUnique(MetaDataUtils.getBooleanForString(unique, false));
            elemmd.setMappedBy((String) annotationValues.get("mappedBy"));
            if (!StringUtils.isWhitespace(fkName)) {
                ForeignKeyMetaData elemFkmd = elemmd.getForeignKeyMetaData();
                if (elemFkmd == null) {
                    elemFkmd = new ForeignKeyMetaData();
                    elemFkmd.setName(fkName);
                    elemmd.setForeignKeyMetaData(elemFkmd);
                } else {
                    elemFkmd.setName(fkName);
                }
            } else if (generateFK != null && generateFK.equalsIgnoreCase("true")) {
                elemmd.setForeignKeyMetaData(new ForeignKeyMetaData());
            }
            if (!StringUtils.isWhitespace(indexName)) {
                IndexMetaData elemIdxmd = elemmd.getIndexMetaData();
                if (elemIdxmd == null) {
                    elemIdxmd = new IndexMetaData();
                    elemmd.setIndexMetaData(elemIdxmd);
                }
                elemIdxmd.setName(indexName);
            }
            if (!StringUtils.isWhitespace(uniqueName)) {
                UniqueMetaData elemUnimd = elemmd.getUniqueMetaData();
                if (elemUnimd == null) {
                    elemUnimd = new UniqueMetaData();
                    elemmd.setUniqueMetaData(elemUnimd);
                }
                elemUnimd.setName(uniqueName);
            }
            Column[] elementColumns = (Column[]) annotationValues.get("columns");
            if (elementColumns != null && elementColumns.length > 0) {
                for (Column elementCol : elementColumns) {
                    elemmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(elementCol));
                }
            }
            if (isPersistenceContext()) {
                if (disableConversion != null && disableConversion) {
                    elemmd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_DISABLED, "true");
                } else if (converterCls != null) {
                    TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
                    if (typeMgr.getTypeConverterForName(converterCls.getName()) == null) {
                        // Not yet cached an instance of this converter so create one
                        AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), converterCls);
                        Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(converterCls, ClassUtils.getCollectionElementType(member.getType(), member.getGenericType()));
                        Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(converterCls, attrType, null);
                        // Register the TypeConverter under the name of the AttributeConverter class
                        JDOTypeConverter typeConv = new JDOTypeConverter(conv);
                        typeMgr.registerConverter(converterCls.getName(), typeConv, attrType, dbType, false, null);
                    }
                    elemmd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME, converterCls.getName());
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(elemmd, (Extension[]) annotationValues.get("extensions"));
            Embedded[] embeddedMappings = (Embedded[]) annotationValues.get("embeddedMapping");
            if (embeddedMappings != null && embeddedMappings.length > 0) {
                // Embedded element
                EmbeddedMetaData embmd = new EmbeddedMetaData();
                embmd.setOwnerMember(embeddedMappings[0].ownerMember());
                embmd.setNullIndicatorColumn(embeddedMappings[0].nullIndicatorColumn());
                embmd.setNullIndicatorValue(embeddedMappings[0].nullIndicatorValue());
                try {
                    Discriminator disc = embeddedMappings[0].discriminatorColumnName();
                    if (disc != null) {
                        DiscriminatorMetaData dismd = embmd.newDiscriminatorMetadata();
                        dismd.setColumnName(disc.column());
                        dismd.setStrategy(JDOAnnotationUtils.getDiscriminatorStrategyString(disc.strategy()));
                    // TODO Support other attributes of discriminator?
                    }
                } catch (Throwable thr) {
                // Ignore this. Maybe not using JDO3.1 jar
                }
                elemmd.setEmbeddedMetaData(embmd);
                embeddedElementMembers = embeddedMappings[0].members();
            // Delay addition of embeddedElementMembers til completion of this loop so we have the
            // element type
            }
        } else if (annName.equals(JDOAnnotationUtils.KEY)) {
            // Key of a Map
            keyTypes = (Class[]) annotationValues.get("types");
            embeddedKey = (String) annotationValues.get("embedded");
            serializedKey = (String) annotationValues.get("serialized");
            dependentKey = (String) annotationValues.get("dependent");
            String keyDeleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
            String keyUpdateAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("updateAction"));
            String fkName = (String) annotationValues.get("foreignKey");
            String generateFK = (String) annotationValues.get("generateForeignKey");
            String indexed = (String) annotationValues.get("indexed");
            String indexName = (String) annotationValues.get("index");
            String unique = (String) annotationValues.get("unique");
            String uniqueName = (String) annotationValues.get("uniqueKey");
            Class converterCls = (Class) annotationValues.get("converter");
            if (converterCls == UseDefault.class) {
                converterCls = null;
            }
            Boolean disableConversion = (Boolean) annotationValues.get("useDefaultConversion");
            if (!StringUtils.isWhitespace(uniqueName)) {
                unique = "true";
            }
            if (!StringUtils.isWhitespace(indexName)) {
                indexed = "true";
            }
            keymd = new KeyMetaData();
            keymd.setTable((String) annotationValues.get("table"));
            keymd.setColumnName((String) annotationValues.get("column"));
            keymd.setDeleteAction(keyDeleteAction);
            keymd.setUpdateAction(keyUpdateAction);
            keymd.setIndexed(IndexedValue.getIndexedValue(indexed));
            keymd.setUnique(MetaDataUtils.getBooleanForString(unique, false));
            keymd.setMappedBy((String) annotationValues.get("mappedBy"));
            if (!StringUtils.isWhitespace(fkName)) {
                ForeignKeyMetaData keyFkmd = keymd.getForeignKeyMetaData();
                if (keyFkmd == null) {
                    keyFkmd = new ForeignKeyMetaData();
                    keyFkmd.setName(fkName);
                    keymd.setForeignKeyMetaData(keyFkmd);
                } else {
                    keyFkmd.setName(fkName);
                }
            } else if (generateFK != null && generateFK.equalsIgnoreCase("true")) {
                keymd.setForeignKeyMetaData(new ForeignKeyMetaData());
            }
            if (!StringUtils.isWhitespace(indexName)) {
                IndexMetaData keyIdxmd = keymd.getIndexMetaData();
                if (keyIdxmd == null) {
                    keyIdxmd = new IndexMetaData();
                    keymd.setIndexMetaData(keyIdxmd);
                }
                keyIdxmd.setName(indexName);
            }
            if (!StringUtils.isWhitespace(uniqueName)) {
                UniqueMetaData keyUnimd = keymd.getUniqueMetaData();
                if (keyUnimd == null) {
                    keyUnimd = new UniqueMetaData();
                    keymd.setUniqueMetaData(keyUnimd);
                }
                keyUnimd.setName(uniqueName);
            }
            Column[] keyColumns = (Column[]) annotationValues.get("columns");
            if (keyColumns != null && keyColumns.length > 0) {
                for (Column keyCol : keyColumns) {
                    keymd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(keyCol));
                }
            }
            if (isPersistenceContext()) {
                if (disableConversion != null && disableConversion) {
                    keymd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_DISABLED, "true");
                } else if (converterCls != null) {
                    TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
                    if (typeMgr.getTypeConverterForName(converterCls.getName()) == null) {
                        // Not yet cached an instance of this converter so create one
                        AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), converterCls);
                        Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(converterCls, ClassUtils.getMapKeyType(member.getType(), member.getGenericType()));
                        Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(converterCls, attrType, null);
                        // Register the TypeConverter under the name of the AttributeConverter class
                        JDOTypeConverter typeConv = new JDOTypeConverter(conv);
                        typeMgr.registerConverter(converterCls.getName(), typeConv, attrType, dbType, false, null);
                    }
                    keymd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME, converterCls.getName());
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(keymd, (Extension[]) annotationValues.get("extensions"));
            Embedded[] embeddedMappings = (Embedded[]) annotationValues.get("embeddedMapping");
            if (embeddedMappings != null && embeddedMappings.length > 0) {
                // Embedded key
                EmbeddedMetaData embmd = new EmbeddedMetaData();
                embmd.setOwnerMember(embeddedMappings[0].ownerMember());
                embmd.setNullIndicatorColumn(embeddedMappings[0].nullIndicatorColumn());
                embmd.setNullIndicatorValue(embeddedMappings[0].nullIndicatorValue());
                keymd.setEmbeddedMetaData(embmd);
                embeddedKeyMembers = embeddedMappings[0].members();
            // Delay addition of embeddedKeyMembers til completion of this loop so we have the key
            // type
            }
        } else if (annName.equals(JDOAnnotationUtils.VALUE)) {
            // Value of a Map
            valueTypes = (Class[]) annotationValues.get("types");
            embeddedValue = (String) annotationValues.get("embedded");
            serializedValue = (String) annotationValues.get("serialized");
            dependentValue = (String) annotationValues.get("dependent");
            String valueDeleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
            String valueUpdateAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("updateAction"));
            String fkName = (String) annotationValues.get("foreignKey");
            String generateFK = (String) annotationValues.get("generateForeignKey");
            String indexed = (String) annotationValues.get("indexed");
            String indexName = (String) annotationValues.get("index");
            String unique = (String) annotationValues.get("unique");
            String uniqueName = (String) annotationValues.get("uniqueKey");
            Class converterCls = (Class) annotationValues.get("converter");
            if (converterCls == UseDefault.class) {
                converterCls = null;
            }
            Boolean disableConversion = (Boolean) annotationValues.get("useDefaultConversion");
            if (!StringUtils.isWhitespace(uniqueName)) {
                unique = "true";
            }
            if (!StringUtils.isWhitespace(indexName)) {
                indexed = "true";
            }
            valuemd = new ValueMetaData();
            valuemd.setTable((String) annotationValues.get("table"));
            valuemd.setColumnName((String) annotationValues.get("column"));
            valuemd.setDeleteAction(valueDeleteAction);
            valuemd.setUpdateAction(valueUpdateAction);
            valuemd.setIndexed(IndexedValue.getIndexedValue(indexed));
            valuemd.setUnique(MetaDataUtils.getBooleanForString(unique, false));
            valuemd.setMappedBy((String) annotationValues.get("mappedBy"));
            if (!StringUtils.isWhitespace(fkName)) {
                ForeignKeyMetaData valueFkmd = valuemd.getForeignKeyMetaData();
                if (valueFkmd == null) {
                    valueFkmd = new ForeignKeyMetaData();
                    valueFkmd.setName(fkName);
                    valuemd.setForeignKeyMetaData(valueFkmd);
                } else {
                    valueFkmd.setName(fkName);
                }
            } else if (generateFK != null && generateFK.equalsIgnoreCase("true")) {
                valuemd.setForeignKeyMetaData(new ForeignKeyMetaData());
            }
            if (!StringUtils.isWhitespace(indexName)) {
                IndexMetaData valueIdxmd = valuemd.getIndexMetaData();
                if (valueIdxmd == null) {
                    valueIdxmd = new IndexMetaData();
                    valuemd.setIndexMetaData(valueIdxmd);
                }
                valueIdxmd.setName(indexName);
            }
            if (!StringUtils.isWhitespace(uniqueName)) {
                UniqueMetaData valueUnimd = valuemd.getUniqueMetaData();
                if (valueUnimd == null) {
                    valueUnimd = new UniqueMetaData();
                    valuemd.setUniqueMetaData(valueUnimd);
                }
                valueUnimd.setName(uniqueName);
            }
            Column[] valueColumns = (Column[]) annotationValues.get("columns");
            if (valueColumns != null && valueColumns.length > 0) {
                for (Column valueCol : valueColumns) {
                    valuemd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(valueCol));
                }
            }
            if (isPersistenceContext()) {
                if (disableConversion != null && disableConversion) {
                    valuemd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_DISABLED, "true");
                } else if (converterCls != null) {
                    TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
                    if (typeMgr.getTypeConverterForName(converterCls.getName()) == null) {
                        // Not yet cached an instance of this converter so create one
                        AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), converterCls);
                        Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(converterCls, ClassUtils.getMapValueType(member.getType(), member.getGenericType()));
                        Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(converterCls, attrType, null);
                        // Register the TypeConverter under the name of the AttributeConverter class
                        JDOTypeConverter typeConv = new JDOTypeConverter(conv);
                        typeMgr.registerConverter(converterCls.getName(), typeConv, attrType, dbType, false, null);
                    }
                    valuemd.addExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME, converterCls.getName());
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(valuemd, (Extension[]) annotationValues.get("extensions"));
            Embedded[] embeddedMappings = (Embedded[]) annotationValues.get("embeddedMapping");
            if (embeddedMappings != null && embeddedMappings.length > 0) {
                // Embedded value
                EmbeddedMetaData embmd = new EmbeddedMetaData();
                embmd.setOwnerMember(embeddedMappings[0].ownerMember());
                embmd.setNullIndicatorColumn(embeddedMappings[0].nullIndicatorColumn());
                embmd.setNullIndicatorValue(embeddedMappings[0].nullIndicatorValue());
                valuemd.setEmbeddedMetaData(embmd);
                embeddedValueMembers = embeddedMappings[0].members();
            // Delay addition of embeddedValueMembers til completion of this loop so we have the value type
            }
        } else if (annName.equals(JDOAnnotationUtils.ORDER)) {
            ordermd = new OrderMetaData();
            ordermd.setColumnName((String) annotationValues.get("column"));
            ordermd.setMappedBy((String) annotationValues.get("mappedBy"));
            Column[] orderColumns = (Column[]) annotationValues.get("columns");
            if (orderColumns != null && orderColumns.length > 0) {
                for (Column orderCol : orderColumns) {
                    ordermd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(orderCol));
                }
            }
            JDOAnnotationUtils.addExtensionsToMetaData(ordermd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.EMBEDDED)) {
            embeddedMember = true;
            embeddedOwnerField = (String) annotationValues.get("ownerMember");
            if (StringUtils.isWhitespace(embeddedOwnerField)) {
                embeddedOwnerField = null;
            }
            embeddedNullIndicatorColumn = (String) annotationValues.get("nullIndicatorColumn");
            if (StringUtils.isWhitespace(embeddedNullIndicatorColumn)) {
                embeddedNullIndicatorColumn = null;
            }
            embeddedNullIndicatorValue = (String) annotationValues.get("nullIndicatorValue");
            if (StringUtils.isWhitespace(embeddedNullIndicatorValue)) {
                embeddedNullIndicatorValue = null;
            }
            embeddedMembers = (Persistent[]) annotationValues.get("members");
            if (embeddedMembers != null && embeddedMembers.length == 0) {
                embeddedMembers = null;
            }
        // TODO Support discriminator
        } else if (annName.equals(JDOAnnotationUtils.INDEX)) {
            // Index for the field
            String name = (String) annotationValues.get("name");
            String table = (String) annotationValues.get("table");
            String unique = (String) annotationValues.get("unique");
            String[] members = (String[]) annotationValues.get("members");
            Column[] columns = (Column[]) annotationValues.get("columns");
            idxmd = JDOAnnotationUtils.getIndexMetaData(name, table, unique, members, columns);
            JDOAnnotationUtils.addExtensionsToMetaData(idxmd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.UNIQUE)) {
            // Unique for the field
            String name = (String) annotationValues.get("name");
            String table = (String) annotationValues.get("table");
            String deferred = (String) annotationValues.get("deferred");
            String[] members = (String[]) annotationValues.get("members");
            Column[] columns = (Column[]) annotationValues.get("columns");
            unimd = JDOAnnotationUtils.getUniqueMetaData(name, table, deferred, members, columns);
            JDOAnnotationUtils.addExtensionsToMetaData(unimd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.FOREIGNKEY)) {
            // ForeignKey for field
            String name = (String) annotationValues.get("name");
            String table = (String) annotationValues.get("table");
            String unique = (String) annotationValues.get("unique");
            String deferred = (String) annotationValues.get("deferred");
            String deleteAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("deleteAction"));
            String updateAction = JDOAnnotationUtils.getForeignKeyActionString((ForeignKeyAction) annotationValues.get("updateAction"));
            String[] members = (String[]) annotationValues.get("members");
            Column[] columns = (Column[]) annotationValues.get("columns");
            fkmd = JDOAnnotationUtils.getFKMetaData(name, table, unique, deferred, deleteAction, updateAction, members, columns);
            JDOAnnotationUtils.addExtensionsToMetaData(fkmd, (Extension[]) annotationValues.get("extensions"));
        } else if (annName.equals(JDOAnnotationUtils.CACHEABLE)) {
            String cache = (String) annotationValues.get("value");
            if (cache != null) {
                cacheable = cache;
            }
        } else if (annName.equals(JDOAnnotationUtils.CONVERT)) {
            convertConverterCls = (Class) annotationValues.get("value");
            if (convertConverterCls == UseDefault.class) {
                convertConverterCls = null;
            }
            Boolean enabled = (Boolean) annotationValues.get("enabled");
            if (!enabled) {
                convertConverterCls = null;
            }
        } else if (annName.equals(JDOAnnotationUtils.EXTENSIONS)) {
            Extension[] values = (Extension[]) annotationValues.get("value");
            if (values != null && values.length > 0) {
                extensions = new HashMap<String, String>(values.length);
                for (Extension ext : values) {
                    String vendorName = ext.vendorName();
                    if (StringUtils.isWhitespace(vendorName)) {
                        throw new InvalidMetaDataException("044160", vendorName, ext.key().toString(), ext.value().toString());
                    } else if (vendorName.equalsIgnoreCase(MetaData.VENDOR_NAME)) {
                        extensions.put(ext.key().toString(), ext.value().toString());
                    }
                }
            }
        } else if (annName.equals(JDOAnnotationUtils.EXTENSION)) {
            String vendorName = (String) annotationValues.get("vendorName");
            if (StringUtils.isWhitespace(vendorName)) {
                throw new InvalidMetaDataException("044160", vendorName, annotationValues.get("key"), annotationValues.get("value"));
            } else if (vendorName.equalsIgnoreCase(MetaData.VENDOR_NAME)) {
                extensions = new HashMap<String, String>(1);
                extensions.put((String) annotationValues.get("key"), (String) annotationValues.get("value"));
            }
        } else {
            NucleusLogger.METADATA.debug(Localiser.msg("044211", cmd.getFullClassName(), member.getName(), annotation.getName()));
        }
    }
    if (mmd == null && (transactionalField || nonPersistentField || primaryKey || colmds != null || serialised || embeddedOwnerField != null || embeddedNullIndicatorColumn != null || embeddedNullIndicatorValue != null || embeddedMembers != null || elemmd != null || keymd != null || valuemd != null || ordermd != null || idxmd != null || unimd != null || fkmd != null || joinmd != null || extensions != null || convertConverterCls != null)) {
        // @Persistent not supplied but other relevant annotations defined, so add default metadata element
        mmd = member.isProperty() ? new PropertyMetaData(cmd, member.getName()) : new FieldMetaData(cmd, member.getName());
        if (primaryKey) {
            mmd.setPersistenceModifier(FieldPersistenceModifier.PERSISTENT);
            mmd.setPrimaryKey(primaryKey);
        }
        if (serialised) {
            mmd.setPersistenceModifier(FieldPersistenceModifier.PERSISTENT);
        }
    }
    if (mmd != null) {
        cmd.addMember(mmd);
        if (primaryKey) {
            mmd.setPrimaryKey(true);
        }
        if (serialised) {
            mmd.setSerialised(true);
        }
        if (embeddedMember) {
            mmd.setEmbedded(true);
        }
        if (nonPersistentField) {
            mmd.setNotPersistent();
        }
        if (transactionalField) {
            mmd.setTransactional();
        }
        if (isPersistenceContext()) {
            if (convertConverterCls != null) {
                TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
                if (typeMgr.getTypeConverterForName(convertConverterCls.getName()) == null) {
                    // Not yet cached an instance of this converter so create one
                    AttributeConverter conv = JDOTypeConverterUtils.createAttributeConverter((PersistenceNucleusContext) mmgr.getNucleusContext(), convertConverterCls);
                    Class attrType = JDOTypeConverterUtils.getAttributeTypeForAttributeConverter(convertConverterCls, member.getType());
                    Class dbType = JDOTypeConverterUtils.getDatastoreTypeForAttributeConverter(convertConverterCls, attrType, null);
                    // Register the TypeConverter under the name of the AttributeConverter class
                    JDOTypeConverter typeConv = new JDOTypeConverter(conv);
                    typeMgr.registerConverter(convertConverterCls.getName(), typeConv, attrType, dbType, false, null);
                }
                mmd.setTypeConverterName(convertConverterCls.getName());
            }
        }
        // Add any embedded info
        if (embeddedMember) {
            if (embeddedOwnerField != null || embeddedNullIndicatorColumn != null || embeddedNullIndicatorValue != null || embeddedMembers != null) {
                EmbeddedMetaData embmd = new EmbeddedMetaData();
                embmd.setOwnerMember(embeddedOwnerField);
                embmd.setNullIndicatorColumn(embeddedNullIndicatorColumn);
                embmd.setNullIndicatorValue(embeddedNullIndicatorValue);
                mmd.setEmbeddedMetaData(embmd);
                if (embeddedMembers != null && embeddedMembers.length > 0) {
                    for (Persistent embMember : embeddedMembers) {
                        // Add the metadata for the embedded field/property to the embedded metadata
                        String memberName = embMember.name();
                        if (memberName.indexOf('.') > 0) {
                            memberName = memberName.substring(memberName.lastIndexOf('.') + 1);
                        }
                        AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embMember, isMemberOfClassAField(member.getType(), memberName));
                        embmd.addMember(embfmd);
                    }
                }
            }
        }
        TypeManager typeManager = mmgr.getNucleusContext().getTypeManager();
        ContainerHandler containerHandler = typeManager.getContainerHandler(member.getType());
        ContainerMetaData contmd = null;
        // If the field is a container then add its container element
        if (containerHandler != null) {
            contmd = containerHandler.newMetaData();
        }
        if (contmd instanceof CollectionMetaData) {
            Class collectionElementType = null;
            StringBuilder elementTypeStr = new StringBuilder();
            if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class) {
                // User-specified element type(s)
                for (Class elementType : elementTypes) {
                    if (elementTypeStr.length() > 0) {
                        elementTypeStr.append(',');
                    }
                    elementTypeStr.append(elementType.getName());
                }
                // Use the first only
                collectionElementType = elementTypes[0];
            } else {
                // Try to derive element type from generics info
                collectionElementType = ClassUtils.getCollectionElementType(member.getType(), member.getGenericType());
            }
            contmd = new CollectionMetaData();
            contmd.setParent(mmd);
            CollectionMetaData collmd = (CollectionMetaData) contmd;
            collmd.setElementType(elementTypeStr.toString());
            if (!StringUtils.isWhitespace(embeddedElement)) {
                collmd.setEmbeddedElement(Boolean.valueOf(embeddedElement));
            }
            if (!StringUtils.isWhitespace(serializedElement)) {
                collmd.setSerializedElement(Boolean.valueOf(serializedElement));
            }
            if (!StringUtils.isWhitespace(dependentElement)) {
                collmd.setDependentElement(Boolean.valueOf(dependentElement));
            }
            if ((embeddedElementMembers != null || "true".equalsIgnoreCase(embeddedElement)) && elemmd == null) {
                elemmd = new ElementMetaData();
                mmd.setElementMetaData(elemmd);
            }
            if (elemmd != null) {
                if (embeddedElementMembers != null) {
                    // Add any embedded element mappings
                    EmbeddedMetaData embmd = elemmd.getEmbeddedMetaData();
                    if ("true".equalsIgnoreCase(embeddedElement) && elemmd.getEmbeddedMetaData() == null) {
                        // Create EmbeddedMetaData for element since not existing
                        embmd = elemmd.newEmbeddedMetaData();
                    }
                    for (Persistent embeddedElementMember : embeddedElementMembers) {
                        // Add the metadata for the embedded element to the embedded metadata
                        String memberName = embeddedElementMember.name();
                        if (memberName.indexOf('.') > 0) {
                            memberName = memberName.substring(memberName.lastIndexOf('.') + 1);
                        }
                        AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedElementMember, isMemberOfClassAField(collectionElementType, memberName));
                        embmd.addMember(embfmd);
                    }
                }
            }
        } else if (contmd instanceof ArrayMetaData) {
            StringBuilder elementTypeStr = new StringBuilder();
            if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class) {
                // User-specified element type(s)
                for (Class elementType : elementTypes) {
                    if (elementTypeStr.length() > 0) {
                        elementTypeStr.append(',');
                    }
                    elementTypeStr.append(elementType.getName());
                }
            } else {
                // Derive from component type
                elementTypeStr.append(member.getType().getComponentType().getName());
            }
            contmd = new ArrayMetaData();
            contmd.setParent(mmd);
            ArrayMetaData arrmd = (ArrayMetaData) contmd;
            arrmd.setElementType(elementTypeStr.toString());
            if (!StringUtils.isWhitespace(embeddedElement)) {
                arrmd.setEmbeddedElement(Boolean.valueOf(embeddedElement));
            }
            if (!StringUtils.isWhitespace(serializedElement)) {
                arrmd.setSerializedElement(Boolean.valueOf(serializedElement));
            }
            if (!StringUtils.isWhitespace(dependentElement)) {
                arrmd.setDependentElement(Boolean.valueOf(dependentElement));
            }
        } else if (contmd instanceof MapMetaData) {
            Class mapKeyType = null;
            if (keyTypes != null && keyTypes.length > 0 && keyTypes[0] != void.class) {
                // User-specified key type TODO Support multiple keys (interface implementations)
                mapKeyType = keyTypes[0];
            } else {
                // Try to derive key type from generics info
                mapKeyType = ClassUtils.getMapKeyType(member.getType(), member.getGenericType());
            }
            Class mapValueType = null;
            if (valueTypes != null && valueTypes.length > 0 && valueTypes[0] != void.class) {
                // User-specified value type TODO Support multiple values (interface implementations)
                mapValueType = valueTypes[0];
            } else {
                // Try to derive value type from generics info
                mapValueType = ClassUtils.getMapValueType(member.getType(), member.getGenericType());
            }
            contmd = new MapMetaData();
            contmd.setParent(mmd);
            MapMetaData mapmd = (MapMetaData) contmd;
            mapmd.setKeyType((mapKeyType != null ? mapKeyType.getName() : null));
            if (!StringUtils.isWhitespace(embeddedKey)) {
                mapmd.setEmbeddedKey(Boolean.valueOf(embeddedKey));
            }
            if (!StringUtils.isWhitespace(serializedKey)) {
                mapmd.setSerializedKey(Boolean.valueOf(serializedKey));
            }
            if (!StringUtils.isWhitespace(dependentKey)) {
                mapmd.setDependentKey(Boolean.valueOf(dependentKey));
            }
            mapmd.setValueType((mapValueType != null ? mapValueType.getName() : null));
            if (!StringUtils.isWhitespace(embeddedValue)) {
                mapmd.setEmbeddedValue(Boolean.valueOf(embeddedValue));
            }
            if (!StringUtils.isWhitespace(serializedValue)) {
                mapmd.setSerializedValue(Boolean.valueOf(serializedValue));
            }
            if (!StringUtils.isWhitespace(dependentValue)) {
                mapmd.setDependentValue(Boolean.valueOf(dependentValue));
            }
            if ((embeddedKeyMembers != null || "true".equalsIgnoreCase(embeddedKey)) && keymd == null) {
                keymd = new KeyMetaData();
                mmd.setKeyMetaData(keymd);
            }
            if (keymd != null) {
                if (embeddedKeyMembers != null) {
                    // Add any embedded key mappings
                    EmbeddedMetaData embmd = keymd.getEmbeddedMetaData();
                    if ("true".equalsIgnoreCase(embeddedKey) && keymd.getEmbeddedMetaData() == null) {
                        // Create EmbeddedMetaData for key since not existing
                        embmd = keymd.newEmbeddedMetaData();
                    }
                    for (Persistent embeddedKeyMember : embeddedKeyMembers) {
                        // Add the metadata for the embedded key to the embedded metadata
                        String memberName = embeddedKeyMember.name();
                        if (memberName.indexOf('.') > 0) {
                            memberName = memberName.substring(memberName.lastIndexOf('.') + 1);
                        }
                        AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedKeyMember, isMemberOfClassAField(mapKeyType, memberName));
                        embmd.addMember(embfmd);
                    }
                }
            }
            if ((embeddedKeyMembers != null || "true".equalsIgnoreCase(embeddedKey)) && valuemd == null) {
                valuemd = new ValueMetaData();
                mmd.setValueMetaData(valuemd);
            }
            if (valuemd != null) {
                if (embeddedValueMembers != null) {
                    // Add any embedded value mappings
                    EmbeddedMetaData embmd = valuemd.getEmbeddedMetaData();
                    if ("true".equalsIgnoreCase(embeddedValue) && valuemd.getEmbeddedMetaData() == null) {
                        // Create EmbeddedMetaData for value since not existing
                        embmd = valuemd.newEmbeddedMetaData();
                    }
                    for (Persistent embeddedValueMember : embeddedValueMembers) {
                        // Add the metadata for the embedded value to the embedded metadata
                        String memberName = embeddedValueMember.name();
                        if (memberName.indexOf('.') > 0) {
                            memberName = memberName.substring(memberName.lastIndexOf('.') + 1);
                        }
                        AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedValueMember, isMemberOfClassAField(mapValueType, memberName));
                        embmd.addMember(embfmd);
                    }
                }
            }
        }
        if (contmd != null) {
            mmd.setContainer(contmd);
            if (elemmd != null) {
                elemmd.setParent(mmd);
                mmd.setElementMetaData(elemmd);
                if (elemmd.getMappedBy() != null && mmd.getMappedBy() == null) {
                    // With collection/array this is the same as mapped-by on the field
                    mmd.setMappedBy(elemmd.getMappedBy());
                }
            }
            if (keymd != null) {
                keymd.setParent(mmd);
                mmd.setKeyMetaData(keymd);
            }
            if (valuemd != null) {
                valuemd.setParent(mmd);
                mmd.setValueMetaData(valuemd);
            }
            if (ordermd != null) {
                ordermd.setParent(mmd);
                mmd.setOrderMetaData(ordermd);
            }
        }
        if (joinmd != null) {
            mmd.setJoinMetaData(joinmd);
        }
        if (colmds != null) {
            for (ColumnMetaData colmd : colmds) {
                mmd.addColumn(colmd);
            }
        }
        if (idxmd != null) {
            mmd.setIndexMetaData(idxmd);
        }
        if (unimd != null) {
            mmd.setUniqueMetaData(unimd);
        }
        if (fkmd != null) {
            mmd.setForeignKeyMetaData(fkmd);
        }
        if (cacheable != null && cacheable.equalsIgnoreCase("false")) {
            mmd.setCacheable(false);
        }
        if (extensions != null) {
            mmd.addExtensions(extensions);
        }
    }
    return mmd;
}
Also used : ElementMetaData(org.datanucleus.metadata.ElementMetaData) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) CollectionMetaData(org.datanucleus.metadata.CollectionMetaData) ForeignKeyAction(javax.jdo.annotations.ForeignKeyAction) ValueMetaData(org.datanucleus.metadata.ValueMetaData) UseDefault(javax.jdo.AttributeConverter.UseDefault) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) Embedded(javax.jdo.annotations.Embedded) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) EmbeddedMetaData(org.datanucleus.metadata.EmbeddedMetaData) UniqueMetaData(org.datanucleus.metadata.UniqueMetaData) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) MapMetaData(org.datanucleus.metadata.MapMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) IndexMetaData(org.datanucleus.metadata.IndexMetaData) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) JoinMetaData(org.datanucleus.metadata.JoinMetaData) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) InvalidMetaDataException(org.datanucleus.metadata.InvalidMetaDataException) JDOTypeConverter(org.datanucleus.api.jdo.JDOTypeConverter) OrderMetaData(org.datanucleus.metadata.OrderMetaData) AttributeConverter(javax.jdo.AttributeConverter) FieldMetaData(org.datanucleus.metadata.FieldMetaData) Column(javax.jdo.annotations.Column) ContainerHandler(org.datanucleus.store.types.ContainerHandler) KeyMetaData(org.datanucleus.metadata.KeyMetaData) PrimaryKeyMetaData(org.datanucleus.metadata.PrimaryKeyMetaData) ForeignKeyMetaData(org.datanucleus.metadata.ForeignKeyMetaData) Persistent(javax.jdo.annotations.Persistent) Discriminator(javax.jdo.annotations.Discriminator) Extension(javax.jdo.annotations.Extension) ArrayMetaData(org.datanucleus.metadata.ArrayMetaData) FieldPersistenceModifier(org.datanucleus.metadata.FieldPersistenceModifier) TypeManager(org.datanucleus.store.types.TypeManager) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ContainerMetaData(org.datanucleus.metadata.ContainerMetaData)

Example 4 with ContainerHandler

use of org.datanucleus.store.types.ContainerHandler in project datanucleus-core by datanucleus.

the class StateManagerImpl method refreshFieldsInFetchPlan.

/**
 * Refreshes from the database all fields in fetch plan.
 * Called by life-cycle transitions when the object undergoes a "transitionRefresh".
 */
public void refreshFieldsInFetchPlan() {
    int[] fieldNumbers = myFP.getMemberNumbers();
    if (fieldNumbers != null && fieldNumbers.length > 0) {
        clearDirtyFlags(fieldNumbers);
        ClassUtils.clearFlags(loadedFields, fieldNumbers);
        // Can't refresh PK fields!
        markPKFieldsAsLoaded();
        boolean callPostLoad = myFP.isToCallPostLoadFetchPlan(this.loadedFields);
        // Refresh the fetch plan fields in this object
        // Make sure that the version is reset upon fetch
        setTransactionalVersion(null);
        loadFieldsFromDatastore(fieldNumbers);
        if (cmd.hasRelations(myEC.getClassLoaderResolver())) {
            // Check for cascade refreshes to related objects
            for (int i = 0; i < fieldNumbers.length; i++) {
                AbstractMemberMetaData fmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumbers[i]);
                RelationType relationType = fmd.getRelationType(myEC.getClassLoaderResolver());
                if (relationType != RelationType.NONE && fmd.isCascadeRefresh()) {
                    // Need to refresh the related field object(s)
                    Object value = provideField(fieldNumbers[i]);
                    if (value != null) {
                        if (fmd.hasContainer()) {
                            // TODO This should replace the SCO wrapper with a new one, or reload the wrapper
                            ApiAdapter api = getExecutionContext().getApiAdapter();
                            ContainerHandler containerHandler = myEC.getTypeManager().getContainerHandler(fmd.getType());
                            for (Object object : containerHandler.getAdapter(value)) {
                                if (api.isPersistable(object)) {
                                    getExecutionContext().refreshObject(object);
                                }
                            }
                        } else if (value instanceof Persistable) {
                            // Refresh any PC fields
                            myEC.refreshObject(value);
                        }
                    }
                }
            }
        }
        updateLevel2CacheForFields(fieldNumbers);
        if (callPostLoad) {
            postLoad();
        }
        getCallbackHandler().postRefresh(myPC);
    }
}
Also used : ApiAdapter(org.datanucleus.api.ApiAdapter) Persistable(org.datanucleus.enhancement.Persistable) RelationType(org.datanucleus.metadata.RelationType) ContainerHandler(org.datanucleus.store.types.ContainerHandler) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 5 with ContainerHandler

use of org.datanucleus.store.types.ContainerHandler in project datanucleus-core by datanucleus.

the class AbstractMemberMetaData method populate.

/**
 * Method to provide the details of the field being represented by this MetaData hence populating
 * certain parts of the MetaData. This is used to firstly provide defaults for attributes that aren't
 * specified in the MetaData, and secondly to report any errors with attributes that have been specifed
 * that are inconsistent with the field being represented.
 * Either a field or a method should be passed in (one or the other) depending on what is being represented
 * by this "member".
 * @param clr ClassLoaderResolver to use for any class loading
 * @param field Field that we are representing (if it's a field)
 * @param method Method(property) that we are representing (if it's a method).
 * @param primary the primary ClassLoader to use (or null)
 * @param mmgr MetaData manager
 */
public synchronized void populate(ClassLoaderResolver clr, Field field, Method method, ClassLoader primary, MetaDataManager mmgr) {
    if (isPopulated() || isInitialised()) {
        return;
    }
    // Set defaults for cascading when not yet set
    ApiAdapter apiAdapter = mmgr.getNucleusContext().getApiAdapter();
    if (cascadePersist == null) {
        cascadePersist = apiAdapter.getDefaultCascadePersistForField();
    }
    if (cascadeUpdate == null) {
        cascadeUpdate = apiAdapter.getDefaultCascadeUpdateForField();
    }
    if (cascadeDelete == null) {
        cascadeDelete = apiAdapter.getDefaultCascadeDeleteForField();
    }
    if (cascadeDetach == null) {
        cascadeDetach = apiAdapter.getDefaultCascadeDetachForField();
    }
    if (cascadeRefresh == null) {
        cascadeRefresh = apiAdapter.getDefaultCascadeRefreshForField();
    }
    if (field == null && method == null) {
        NucleusLogger.METADATA.error(Localiser.msg("044106", getClassName(), getName()));
        throw new InvalidMemberMetaDataException("044106", getClassName(), getName());
    }
    // No class loader, so use System
    if (clr == null) {
        NucleusLogger.METADATA.warn(Localiser.msg("044067", name, getClassName(true)));
        clr = mmgr.getNucleusContext().getClassLoaderResolver(null);
    }
    memberRepresented = field != null ? field : method;
    if (type == null) {
        // Type not yet set so set from field/method (will only be set if we are imposing the type due to Java generics TypeVariable usage)
        if (field != null) {
            this.type = field.getType();
        } else if (method != null) {
            this.type = method.getReturnType();
        }
    }
    if (className != null) {
        // Property is overriding a superclass property, so check that it is valid
        Class thisClass = null;
        if (parent instanceof EmbeddedMetaData) {
            // <embedded> is contained in a <field>, <element>, <key>, <value>
            // but could be multiple levels deep so adopt a generic strategy for finding the parent class
            MetaData superMd = parent.getParent();
            thisClass = ((AbstractMemberMetaData) superMd).getType();
        } else {
            // Overriding field in a superclass of this class
            try {
                thisClass = clr.classForName(getAbstractClassMetaData().getPackageName() + "." + getAbstractClassMetaData().getName());
            } catch (ClassNotResolvedException cnre) {
            // Do nothing
            }
        }
        Class fieldClass = null;
        try {
            fieldClass = clr.classForName(className);
        } catch (ClassNotResolvedException cnre) {
            try {
                fieldClass = clr.classForName(getAbstractClassMetaData().getPackageName() + "." + className);
                className = getAbstractClassMetaData().getPackageName() + "." + className;
            } catch (ClassNotResolvedException cnre2) {
                NucleusLogger.METADATA.error(Localiser.msg("044113", getClassName(), getName(), className));
                NucleusException ne = new InvalidMemberMetaDataException("044113", getClassName(), getName(), className);
                ne.setNestedException(cnre);
                throw ne;
            }
        }
        if (fieldClass != null && !fieldClass.isAssignableFrom(thisClass)) {
            // TODO We could also check if persistable, but won't work when enhancing
            NucleusLogger.METADATA.error(Localiser.msg("044114", getClassName(), getName(), className));
            throw new InvalidMemberMetaDataException("044114", getClassName(), getName(), className);
        }
    }
    if (primaryKey == null) {
        // Primary key not set by user so initialise it to false
        primaryKey = Boolean.FALSE;
    }
    // Update "embedded" based on type
    if (primaryKey == Boolean.FALSE && embedded == null) {
        Class element_type = getType();
        if (element_type.isArray()) {
            element_type = element_type.getComponentType();
            if (mmgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(element_type)) {
                embedded = Boolean.TRUE;
            }
        } else if (mmgr.getNucleusContext().getTypeManager().isDefaultEmbeddedType(element_type)) {
            embedded = Boolean.TRUE;
        }
    }
    if (embedded == null) {
        embedded = Boolean.FALSE;
    }
    // Update "persistence-modifier" according to type etc
    if (FieldPersistenceModifier.DEFAULT.equals(persistenceModifier)) {
        if (getTypeConverterName() != null) {
            // Explicitly set a converter, so assume it is persistent
            persistenceModifier = FieldPersistenceModifier.PERSISTENT;
        } else {
            boolean isPcClass = getType().isArray() ? isFieldArrayTypePersistable(mmgr) : mmgr.isFieldTypePersistable(type);
            if (!isPcClass) {
                if (getType().isArray() && getType().getComponentType().isInterface()) {
                    isPcClass = mmgr.getMetaDataForClassInternal(getType().getComponentType(), clr) != null;
                } else if (getType().isInterface()) {
                    isPcClass = mmgr.getMetaDataForClassInternal(getType(), clr) != null;
                }
            }
            persistenceModifier = getDefaultFieldPersistenceModifier(getType(), memberRepresented.getModifiers(), isPcClass, mmgr);
        }
    }
    // TODO If this field is NONE in superclass, make it NONE here too
    // If type is a container, load create the metadata. The field will be handled as a container if it
    // has a ContainerHandler registered against it.
    TypeManager typeMgr = mmgr.getNucleusContext().getTypeManager();
    ContainerHandler containerHandler = typeMgr.getContainerHandler(type);
    if (containerHandler == null) {
        // No container handler registered for this type
        if (hasContainer()) {
            // Container metadata specified on a type that is not a valid or supported container
            NucleusLogger.METADATA.error(Localiser.msg("044212", getClassName(), getName(), type));
            NucleusException ne = new InvalidMemberMetaDataException("044212", getClassName(), getName(), type);
            throw ne;
        }
    } else {
        // Field is a container type
        if (!hasContainer()) {
            // No container metadata has not been specified yet, create a default empty one
            setContainer(containerHandler.newMetaData());
        }
        containerHandler.populateMetaData(clr, primary, this);
    }
    // Update "default-fetch-group" according to type
    if (defaultFetchGroup == null && persistenceModifier.equals(FieldPersistenceModifier.NONE)) {
        defaultFetchGroup = Boolean.FALSE;
    } else if (defaultFetchGroup == null && persistenceModifier.equals(FieldPersistenceModifier.TRANSACTIONAL)) {
        defaultFetchGroup = Boolean.FALSE;
    } else if (defaultFetchGroup == null) {
        defaultFetchGroup = Boolean.FALSE;
        if (!primaryKey.equals(Boolean.TRUE)) {
            if (hasContainer() && containerHandler != null) {
                defaultFetchGroup = containerHandler.isDefaultFetchGroup(clr, typeMgr, this);
            } else if (typeMgr.isDefaultFetchGroup(getType())) {
                // If still not determined rely on the type
                defaultFetchGroup = Boolean.TRUE;
            }
        }
    }
    // Field is not specified as "persistent" yet has DFG or primary-key !
    if (persistenceModifier.equals(FieldPersistenceModifier.TRANSACTIONAL) || persistenceModifier.equals(FieldPersistenceModifier.NONE)) {
        if (defaultFetchGroup == Boolean.TRUE || primaryKey == Boolean.TRUE) {
            throw new InvalidMemberMetaDataException("044109", getClassName(), name, this.getType().getName(), persistenceModifier.toString());
        }
    }
    if (storeInLob) {
        // Set up the jdbcType/serialized settings according to the field type in line with JPA
        boolean useClob = false;
        if (type == String.class || (type.isArray() && type.getComponentType() == Character.class) || (type.isArray() && type.getComponentType() == char.class)) {
            useClob = true;
            if (columns == null || columns.isEmpty()) {
                // Create a CLOB column. What if the RDBMS doesn't support CLOB ?
                ColumnMetaData colmd = new ColumnMetaData();
                colmd.setName(column);
                colmd.setJdbcType("CLOB");
                addColumn(colmd);
            } else {
                ColumnMetaData colmd = columns.get(0);
                colmd.setJdbcType("CLOB");
            }
        }
        if (!useClob) {
            serialized = Boolean.TRUE;
        }
    }
    if (!mmgr.isDefaultNullable() && !hasContainer()) {
        // Find column metadata definition, creating one if not specified
        ColumnMetaData colMmd;
        if (columns == null || columns.isEmpty()) {
            newColumnMetaData();
        }
        colMmd = getColumnMetaData()[0];
        // Set column not-null by default
        if (colMmd.getAllowsNull() == null) {
            colMmd.setAllowsNull(Boolean.FALSE);
        }
    }
    if (this.containerMetaData != null && this.dependent != null) {
        // Check for invalid dependent field specifications
        NucleusLogger.METADATA.error(Localiser.msg("044110", getClassName(), getName(), ((ClassMetaData) this.parent).getName()));
        throw new InvalidMemberMetaDataException("044110", getClassName(), getName(), ((ClassMetaData) this.parent).getName());
    }
    if (embedded == Boolean.TRUE && embeddedMetaData == null) {
        // User specified "embedded" on the member, yet no embedded definition so add one TODO Omit this, since we should only use when provided
        AbstractClassMetaData memberCmd = mmgr.getMetaDataForClassInternal(getType(), clr);
        if (memberCmd != null) {
            embeddedMetaData = new EmbeddedMetaData();
            embeddedMetaData.setParent(this);
        }
    }
    if (embeddedMetaData != null) {
        // Update with any extensions (for lack of features in JPA)
        if (hasExtension("null-indicator-column")) {
            embeddedMetaData.setNullIndicatorColumn(getValueForExtension("null-indicator-column"));
            if (hasExtension("null-indicator-value")) {
                embeddedMetaData.setNullIndicatorValue(getValueForExtension("null-indicator-value"));
            }
        }
        // Populate any embedded object
        embeddedMetaData.populate(clr, primary);
        embedded = Boolean.TRUE;
    }
    if (containerMetaData != null && persistenceModifier == FieldPersistenceModifier.PERSISTENT) {
        // Populate any container
        if (containerMetaData instanceof CollectionMetaData) {
        // if (cascadeDelete)
        // {
        // // User has set cascade-delete (JPA) so set the element as dependent
        // getCollection().element.dependent = Boolean.TRUE;
        // }
        // getCollection().populate(clr, primary, mmgr);
        } else if (containerMetaData instanceof MapMetaData) {
        // String keyCascadeVal = getValueForExtension("cascade-delete-key");
        // if (cascadeDelete)
        // {
        // // User has set cascade-delete (JPA) so set the value as dependent
        // getMap().key.dependent = Boolean.FALSE; // JPA spec doesn't define what this should be
        // getMap().value.dependent = Boolean.TRUE;
        // }
        // if (keyCascadeVal != null)
        // {
        // if (keyCascadeVal.equalsIgnoreCase("true"))
        // {
        // getMap().key.dependent = Boolean.TRUE;
        // }
        // else
        // {
        // getMap().key.dependent = Boolean.FALSE;
        // }
        // }
        // getMap().populate(clr, primary, mmgr);
        } else if (containerMetaData instanceof ArrayMetaData) {
        // if (cascadeDelete)
        // {
        // // User has set cascade-delete (JPA) so set the element as dependent
        // getArray().element.dependent = Boolean.TRUE;
        // }
        // getArray().populate(clr, primary, mmgr);
        }
    }
    if (mmgr.isFieldTypePersistable(type) && cascadeDelete) {
        setDependent(true);
    }
    if (hasExtension(MetaData.EXTENSION_MEMBER_IMPLEMENTATION_CLASSES)) {
        // Check the validity of the implementation-classes and qualify them where required.
        StringBuilder str = new StringBuilder();
        String[] implTypes = getValuesForExtension(MetaData.EXTENSION_MEMBER_IMPLEMENTATION_CLASSES);
        for (int i = 0; i < implTypes.length; i++) {
            String implTypeName = ClassUtils.createFullClassName(getAbstractClassMetaData().getPackageName(), implTypes[i]);
            if (i > 0) {
                str.append(",");
            }
            try {
                clr.classForName(implTypeName);
                str.append(implTypeName);
            } catch (ClassNotResolvedException cnre) {
                try {
                    // Maybe the user specified a java.lang class without fully-qualifying it
                    // This is beyond the scope of the JDO spec which expects java.lang cases to be fully-qualified
                    String langClassName = ClassUtils.getJavaLangClassForType(implTypeName);
                    clr.classForName(langClassName);
                    str.append(langClassName);
                } catch (ClassNotResolvedException cnre2) {
                    // Implementation type not found
                    throw new InvalidMemberMetaDataException("044116", getClassName(), getName(), implTypes[i]);
                }
            }
        }
        // Replace with this new value
        addExtension(MetaData.EXTENSION_MEMBER_IMPLEMENTATION_CLASSES, str.toString());
    }
    // Set up persistence flags for enhancement process
    byte serializable = 0;
    if (Serializable.class.isAssignableFrom(getType()) || getType().isPrimitive()) {
        serializable = Persistable.SERIALIZABLE;
    }
    if (FieldPersistenceModifier.NONE.equals(persistenceModifier)) {
        persistenceFlags = 0;
    } else if (FieldPersistenceModifier.TRANSACTIONAL.equals(persistenceModifier) && Modifier.isTransient(memberRepresented.getModifiers())) {
        persistenceFlags = (byte) (Persistable.CHECK_WRITE | serializable);
    } else if (primaryKey.booleanValue()) {
        persistenceFlags = (byte) (Persistable.MEDIATE_WRITE | serializable);
    } else if (defaultFetchGroup.booleanValue()) {
        persistenceFlags = (byte) (Persistable.CHECK_READ | Persistable.CHECK_WRITE | serializable);
    } else if (!defaultFetchGroup.booleanValue()) {
        persistenceFlags = (byte) (Persistable.MEDIATE_READ | Persistable.MEDIATE_WRITE | serializable);
    } else {
        persistenceFlags = 0;
    }
    // Set fields that are not relations
    if (persistenceModifier != FieldPersistenceModifier.PERSISTENT) {
        // Not a relation field so set relation information
        relationType = RelationType.NONE;
    } else if (containerMetaData == null && !mmgr.isFieldTypePersistable(type)) {
        if (!type.getName().equals(ClassNameConstants.Object) && !type.isInterface()) {
            // Not a container field, not a persistable type, nor a reference type so not a relation field
            relationType = RelationType.NONE;
        }
    }
    if (serialized == Boolean.TRUE && hasExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME)) {
        NucleusLogger.METADATA.warn(Localiser.msg("044127", getClassName(), getName(), getValueForExtension(MetaData.EXTENSION_MEMBER_TYPE_CONVERTER_NAME)));
        serialized = Boolean.FALSE;
    }
    setPopulated();
}
Also used : Serializable(java.io.Serializable) ApiAdapter(org.datanucleus.api.ApiAdapter) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) ContainerHandler(org.datanucleus.store.types.ContainerHandler) TypeManager(org.datanucleus.store.types.TypeManager) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

ContainerHandler (org.datanucleus.store.types.ContainerHandler)7 TypeManager (org.datanucleus.store.types.TypeManager)5 ApiAdapter (org.datanucleus.api.ApiAdapter)3 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)3 ElementContainerHandler (org.datanucleus.store.types.ElementContainerHandler)2 SCO (org.datanucleus.store.types.SCO)2 Serializable (java.io.Serializable)1 AttributeConverter (javax.jdo.AttributeConverter)1 UseDefault (javax.jdo.AttributeConverter.UseDefault)1 Column (javax.jdo.annotations.Column)1 Discriminator (javax.jdo.annotations.Discriminator)1 Embedded (javax.jdo.annotations.Embedded)1 Extension (javax.jdo.annotations.Extension)1 ForeignKeyAction (javax.jdo.annotations.ForeignKeyAction)1 Persistent (javax.jdo.annotations.Persistent)1 ExecutionContext (org.datanucleus.ExecutionContext)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JDOTypeConverter (org.datanucleus.api.jdo.JDOTypeConverter)1 Persistable (org.datanucleus.enhancement.Persistable)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1