Package org.datanucleus.metadata

Examples of org.datanucleus.metadata.ExtensionMetaData


                        }
                    }
                    else if (annName.equals(JPAAnnotationUtils.EXTENSION))
                    {
                        // extension
                        ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                            (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                        if (extensions == null)
                        {
                            extensions = new HashSet<ExtensionMetaData>(1);
                        }
                        extensions.add(extmd);
                    }
                    else if (annName.equals(JPAAnnotationUtils.EXTENSIONS))
                    {
                        // extension
                        Extension[] values = (Extension[])annotationValues.get("value");
                        if (values != null && values.length > 0)
                        {
                            if (extensions == null)
                            {
                                extensions = new HashSet<ExtensionMetaData>(values.length);
                            }
                            for (int j=0;j<values.length;j++)
                            {
                                ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                    values[j].key().toString(), values[j].value().toString());
                                extensions.add(extmd);
                            }
                        }
                    }
                    else
                    {
                        NucleusLogger.METADATA.error(LOCALISER.msg("044203",
                            cls.getName(), annotations[i].getName()));
                    }
                }

                if (entityName == null || entityName.length() == 0)
                {
                    entityName = ClassUtils.getClassNameForClass(cls);
                }

                NucleusLogger.METADATA.info(LOCALISER.msg("044200", cls.getName(), "JPA"));

                cmd.setTable(table);
                cmd.setCatalog(catalog);
                cmd.setSchema(schema);
                cmd.setEntityName(entityName);
                cmd.setDetachable(detachable);
                cmd.setRequiresExtent(requiresExtent);
                cmd.setObjectIdClass(idClassName);
                cmd.setEmbeddedOnly(embeddedOnly);
                cmd.setCacheable(cacheable);
                cmd.setIdentityType(identityType);
                if (isClassPersistenceCapable(cls.getSuperclass()))
                {
                    cmd.setPersistenceCapableSuperclass(cls.getSuperclass().getName());
                }
                if (excludeSuperClassListeners)
                {
                    cmd.excludeSuperClassListeners();
                }
                if (excludeDefaultListeners)
                {
                    cmd.excludeDefaultListeners();
                }
                if (entityListeners != null)
                {
                    for (int i=0; i<entityListeners.length; i++)
                    {
                        // Any EventListener will not have their callback methods registered at this point
                        EventListenerMetaData elmd = new EventListenerMetaData(entityListeners[i].getName());
                        cmd.addListener(elmd);
                    }
                }

                // Inheritance
                InheritanceMetaData inhmd = null;
                if (inheritanceStrategy != null)
                {
                    // Strategy specified so create inheritance data
                    inhmd = cmd.newInheritanceMetadata().setStrategy(inheritanceStrategy);
                    inhmd.setStrategyForTree(inheritanceStrategyForTree);
                }
                else if (discriminatorValue != null || discriminatorColumnName != null ||
                        discriminatorColumnLength != null || discriminatorColumnType != null)
                {
                    // Discriminator specified so we need inheritance data
                    inhmd = cmd.newInheritanceMetadata().setStrategyForTree(inheritanceStrategyForTree);
                }

                if (discriminatorValue != null || discriminatorColumnName != null ||
                    discriminatorColumnLength != null || discriminatorColumnType != null)
                {
                    // Add discriminator information to the inheritance of this class
                    DiscriminatorMetaData dismd = inhmd.newDiscriminatorMetadata();
                    if (discriminatorValue != null)
                    {
                        // Value specified so assumed to be value-map
                        dismd.setColumnName(discriminatorColumnName);
                        dismd.setValue(discriminatorValue).setStrategy("value-map").setIndexed("false");
                    }
                    else
                    {
                        // No value so use class-name
                        discriminatorValue = cls.getName();
                        dismd.setColumnName(discriminatorColumnName);
                        dismd.setValue(discriminatorValue).setStrategy("value-map").setIndexed("false");
                    }

                    ColumnMetaData discolmd = null;
                    if (discriminatorColumnLength != null || discriminatorColumnName != null || discriminatorColumnType != null)
                    {
                        discolmd = new ColumnMetaData();
                        discolmd.setName(discriminatorColumnName);
                        if (discriminatorColumnType != null)
                        {
                            discolmd.setJdbcType(discriminatorColumnType);
                        }
                        if (discriminatorColumnLength != null)
                        {
                            discolmd.setLength(discriminatorColumnLength);
                        }
                        dismd.setColumnMetaData(discolmd);
                        if (discriminatorColumnDdl != null)
                        {
                            discolmd.setColumnDdl(discriminatorColumnDdl);
                        }
                    }
                }

                // extension - datastore-identity
                if (identityType == IdentityType.DATASTORE)
                {
                    IdentityMetaData idmd = cmd.newIdentityMetadata();
                    idmd.setColumnName(identityColumn);
                    idmd.setValueStrategy(IdentityStrategy.getIdentityStrategy(identityStrategy));
                    if (identityGenerator != null)
                    {
                        idmd.setSequence(identityGenerator);
                        idmd.setValueGeneratorName(identityGenerator);
                    }
                }

                if (pkColumnMetaData != null)
                {
                    // PK columns overriding those in the root class
                    PrimaryKeyMetaData pkmd = cmd.newPrimaryKeyMetadata();
                    for (int i=0;i<pkColumnMetaData.length;i++)
                    {
                        pkmd.addColumn(pkColumnMetaData[i]);
                    }
                }
                if (uniques != null && uniques.size() > 0)
                {
                    // Unique constraints for the primary/secondary tables
                    Iterator<UniqueMetaData> uniquesIter = uniques.iterator();
                    while (uniquesIter.hasNext())
                    {
                        cmd.addUniqueConstraint(uniquesIter.next());
                    }
                }

                if (overriddenFields != null)
                {
                    // Fields overridden from superclasses
                    Iterator<AbstractMemberMetaData> iter = overriddenFields.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addMember(iter.next());
                    }
                }
                if (namedQueries != null)
                {
                    Iterator<QueryMetaData> iter = namedQueries.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addQuery(iter.next());
                    }
                }
                if (resultMappings != null)
                {
                    Iterator<QueryResultMetaData> iter = resultMappings.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addQueryResultMetaData(iter.next());
                    }
                }
                if (extensions != null)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        cmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }

            // Process any secondary tables
View Full Code Here


                modifier = FieldPersistenceModifier.PERSISTENT.toString();
            }
            else if (annName.equals(JPAAnnotationUtils.EXTENSION) &&
                    jpaLevel.equalsIgnoreCase("DataNucleus"))
            {
                ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                    (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                if (extensions == null)
                {
                    extensions = new HashSet<ExtensionMetaData>(1);
                }
                extensions.add(extmd);
            }
            else if (annName.equals(JPAAnnotationUtils.EXTENSIONS) &&
                    jpaLevel.equalsIgnoreCase("DataNucleus"))
            {
                Extension[] values = (Extension[])annotationValues.get("value");
                if (values != null && values.length > 0)
                {
                    if (extensions == null)
                    {
                        extensions = new HashSet<ExtensionMetaData>(values.length);
                    }
                    for (int j=0;j<values.length;j++)
                    {
                        ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                            values[j].key().toString(), values[j].value().toString());
                        extensions.add(extmd);
                    }
                }
            }
            else if (annName.equals(JPAAnnotationUtils.SEQUENCE_GENERATOR))
            {
                // Sequence generator, so store it against the package that we are under
                processSequenceGeneratorAnnotation(cmd.getPackageMetaData(), annotationValues);
            }
            else if (annName.equals(JPAAnnotationUtils.TABLE_GENERATOR))
            {
                // Table generator, so store it against the package that we are under
                processTableGeneratorAnnotation(cmd.getPackageMetaData(), annotationValues);
            }
        }

        if (JPAAnnotationUtils.isBasicByDefault(field.getType()) && modifier == null)
        {
            modifier = FieldPersistenceModifier.PERSISTENT.toString();
        }

        // Create the field
        AbstractMemberMetaData fmd;
        if (field.isProperty())
        {
            fmd = new PropertyMetaData(cmd, field.getName());
        }
        else
        {
            fmd = new FieldMetaData(cmd, field.getName());
        }
        fmd.setPersistenceModifier(modifier);
        fmd.setPrimaryKey(pk);
        fmd.setDefaultFetchGroup(dfg);
        fmd.setEmbedded(embedded);
        fmd.setNullValue(NullValue.getNullValue(nullValue));
        fmd.setMappedBy(mappedBy);

        if (version != null)
        {
            // Tag this field as the version field
            VersionMetaData vermd = cmd.newVersionMetadata();
            vermd.setStrategy(VersionStrategy.VERSION_NUMBER).setFieldName(fmd.getName());
        }

        cmd.addMember(fmd);

        if (orphanRemoval)
        {
            fmd.setCascadeRemoveOrphans(true);
        }
        if (cascades != null)
        {
            for (int i = 0; i < cascades.length; i++)
            {
                if (cascades[i] == CascadeType.ALL)
                {
                    fmd.setCascadePersist(true);
                    fmd.setCascadeUpdate(true);
                    fmd.setCascadeDelete(true);
                    fmd.setCascadeRefresh(true);
                }
                else if (cascades[i] == CascadeType.PERSIST)
                {
                    fmd.setCascadePersist(true);
                }
                else if (cascades[i] == CascadeType.MERGE)
                {
                    fmd.setCascadeUpdate(true);
                }
                else if (cascades[i] == CascadeType.REMOVE)
                {
                    fmd.setCascadeDelete(true);
                }
                else if (cascades[i] == CascadeType.REFRESH)
                {
                    fmd.setCascadeRefresh(true);
                }
            }
        }

        // Value generation
        if (valueStrategy != null && valueGenerator != null)
        {
            fmd.setSequence(valueGenerator);
            fmd.setValueGeneratorName(valueGenerator);
        }
        if (valueStrategy != null)
        {
            fmd.setValueStrategy(valueStrategy);
        }

        // Type storage
        if (storeInLob)
        {
            fmd.setStoreInLob();
        }

        // Container fields : If the field is a container then add its container element
        ContainerMetaData contmd = null;
        if (Collection.class.isAssignableFrom(field.getType()))
        {
            String elementType = null;
            if (targetEntity != null && targetEntity != void.class)
            {
                elementType = targetEntity.getName();
            }
            if (elementType == null)
            {
                Class elType = ClassUtils.getCollectionElementType(field.getType(), field.getGenericType());
                elementType = (elType != null ? elType.getName() : null);
            }
            // No annotation for collections so cant specify the element type, dependent, embedded, serialized

            contmd = new CollectionMetaData();
            ((CollectionMetaData)contmd).setElementType(elementType);
        }
        else if (field.getType().isArray())
        {
            contmd = new ArrayMetaData();
        }
        else if (Map.class.isAssignableFrom(field.getType()))
        {
            Class keyCls = ClassUtils.getMapKeyType(field.getType(), field.getGenericType());
            String keyType = (keyCls != null ? keyCls.getName() : null);
            String valueType = null;
            if (targetEntity != null && targetEntity != void.class)
            {
                valueType = targetEntity.getName();
            }
            if (valueType == null)
            {
                Class valueCls = ClassUtils.getMapValueType(field.getType(), field.getGenericType());
                valueType = (valueCls != null ? valueCls.getName() : null);
            }

            // No annotation for maps so cant specify the key/value type, dependent, embedded, serialized
            contmd = new MapMetaData();
            MapMetaData mapmd = (MapMetaData)contmd;
            mapmd.setKeyType(keyType);
            mapmd.setValueType(valueType);
        }
        if (contmd != null)
        {
            fmd.setContainer(contmd);
        }

        if (addJoin)
        {
            if (fmd.getJoinMetaData() == null)
            {
                JoinMetaData joinmd = new JoinMetaData();
                fmd.setJoinMetaData(joinmd);
            }
        }

        // Extensions
        if (extensions != null)
        {
            Iterator<ExtensionMetaData> iter = extensions.iterator();
            while (iter.hasNext())
            {
                ExtensionMetaData extmd = iter.next();
                fmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
            }
        }

        return fmd;
    }
View Full Code Here

                        if (values != null && values.length > 0)
                        {
                            extensions = new HashSet<ExtensionMetaData>(values.length);
                            for (int j=0;j<values.length;j++)
                            {
                                ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                    values[j].key().toString(), values[j].value().toString());
                                extensions.add(extmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.EXTENSION))
                    {
                        ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                            (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                        extensions = new HashSet<ExtensionMetaData>(1);
                        extensions.add(extmd);
                    }
                    else
                    {
                        if (!annName.equals(JDOAnnotationUtils.PERSISTENCE_CAPABLE) &&
                                !annName.equals(JDOAnnotationUtils.PERSISTENCE_AWARE))
                        {
                            NucleusLogger.METADATA.error(LOCALISER.msg("044203", cls.getName(),
                                annotations[i].getName()));
                        }
                    }
                }

                // Either PersistenceCapable, PersistenceAware or PersistentInterface so build up the metadata
                NucleusLogger.METADATA.info(LOCALISER.msg("044200", cls.getName(), "JDO"));

                if (cmd instanceof ClassMetaData)
                {
                    pmd.addClass((ClassMetaData)cmd);
                }
                else if (cmd instanceof InterfaceMetaData)
                {
                    pmd.addInterface((InterfaceMetaData)cmd);
                }

                if (embeddedOnly)
                {
                    cmd.setEmbeddedOnly(true);
                }
                if (idmd != null)
                {
                    // Datastore identity
                    idmd.setParent(cmd);
                    cmd.setIdentityMetaData(idmd);
                }
                if (pkmd != null)
                {
                    // Primary Key
                    pkmd.setParent(cmd);
                    cmd.setPrimaryKeyMetaData(pkmd);
                }

                if (vermd != null)
                {
                    // Version
                    vermd.setParent(cmd);
                    cmd.setVersionMetaData(vermd);
                }

                if (inhmd != null)
                {
                    // Inheritance
                    if (dismd != null)
                    {
                        inhmd.setDiscriminatorMetaData(dismd);
                    }
                    inhmd.setParent(cmd);
                    cmd.setInheritanceMetaData(inhmd);
                }
                else if (dismd != null)
                {
                    inhmd = new InheritanceMetaData();
                    inhmd.setDiscriminatorMetaData(dismd);
                    cmd.setInheritanceMetaData(inhmd);
                }

                if (joins != null && joins.length > 0)
                {
                    // Joins
                    for (int i=0;i<joins.length;i++)
                    {
                        cmd.addJoin(joins[i]);
                    }
                }
                if (queries != null && queries.length > 0)
                {
                    // Named Queries
                    for (int i=0;i<queries.length;i++)
                    {
                        queries[i].setParent(cmd);
                        cmd.addQuery(queries[i]);
                    }
                }
                if (fetchGroups != null && fetchGroups.length > 0)
                {
                    // Fetch Groups
                    for (int i=0;i<fetchGroups.length;i++)
                    {
                        fetchGroups[i].setParent(cmd);
                        cmd.addFetchGroup(fetchGroups[i]);
                    }
                }

                if (seqmd != null)
                {
                    // Sequence - currently only allowing 1 per class (should really be on the package)
                    cmd.getPackageMetaData().addSequence(seqmd);
                }

                if (indices != null)
                {
                    Iterator iter = indices.iterator();
                    while (iter.hasNext())
                    {
                        IndexMetaData idxmd = (IndexMetaData)iter.next();
                        idxmd.setParent(cmd);
                        cmd.addIndex(idxmd);
                    }
                }
                if (uniqueKeys != null)
                {
                    Iterator iter = uniqueKeys.iterator();
                    while (iter.hasNext())
                    {
                        UniqueMetaData unimd = (UniqueMetaData)iter.next();
                        unimd.setParent(cmd);
                        cmd.addUniqueConstraint(unimd);
                    }
                }
                if (fks != null)
                {
                    Iterator iter = fks.iterator();
                    while (iter.hasNext())
                    {
                        ForeignKeyMetaData fkmd = (ForeignKeyMetaData)iter.next();
                        fkmd.setParent(cmd);
                        cmd.addForeignKey(fkmd);
                    }
                }
                if (unmappedColumns != null)
                {
                    for (int i=0;i<unmappedColumns.length;i++)
                    {
                        ColumnMetaData colmd = unmappedColumns[i];
                        colmd.setParent(cmd);
                        cmd.addUnmappedColumn(colmd);
                    }
                }
                if (cacheable != null && cacheable.equalsIgnoreCase("false"))
                {
                    cmd.setCacheable(false);
                }
                if (extensions != null)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        cmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }
        }
View Full Code Here

                    if (values != null && values.length > 0)
                    {
                        extensions = new HashSet<ExtensionMetaData>(values.length);
                        for (int j=0;j<values.length;j++)
                        {
                            ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                values[j].key().toString(), values[j].value().toString());
                            extensions.add(extmd);
                        }
                    }
                }
                else if (annName.equals(JDOAnnotationUtils.EXTENSION))
                {
                    ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                        (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                    extensions = new HashSet<ExtensionMetaData>(1);
                    extensions.add(extmd);
                }
                else
                {
                    NucleusLogger.METADATA.error(LOCALISER.msg("044203",
                        member.getName(), annotations[i].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))
            {
                // @Persistent not supplied but other relevant annotations defined, so add default metadata element
                if (member.isProperty())
                {
                    mmd = new PropertyMetaData(cmd, member.getName());
                }
                else
                {
                    mmd = new FieldMetaData(cmd, member.getName());
                }
                if (primaryKey)
                {
                    mmd.setPersistenceModifier(FieldPersistenceModifier.PERSISTENT.toString());
                    mmd.setPrimaryKey("" + primaryKey);
                }
                if (serialised)
                {
                    mmd.setPersistenceModifier(FieldPersistenceModifier.PERSISTENT.toString());
                }
            }

            if (mmd != null)
            {
                cmd.addMember(mmd);

                if (primaryKey)
                {
                    mmd.setPrimaryKey(true);
                }
                if (serialised)
                {
                    mmd.setSerialised(true);
                }
                if (nonPersistentField)
                {
                    mmd.setNotPersistent();
                }
                if (transactionalField)
                {
                    mmd.setTransactional();
                }

                // Add any embedded info
                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 (int j=0;j<embeddedMembers.length;j++)
                        {
                            // Add the metadata for the embedded field/property to the embedded metadata
                            String memberName = embeddedMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedMembers[j],
                                isMemberOfClassAField(member.getType(), memberName));
                            embmd.addMember(embfmd);
                        }
                    }
                }

                // If the field is a container then add its container element
                ContainerMetaData contmd = null;
                if (Collection.class.isAssignableFrom(member.getType()))
                {
                    Class collectionElementType = null;
                    StringBuilder elementTypeStr = new StringBuilder();
                    if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class)
                    {
                        // User-specified element type(s)
                        for (int j=0;j<elementTypes.length;j++)
                        {
                            if (elementTypeStr.length() > 0)
                            {
                                elementTypeStr.append(',');
                            }
                            elementTypeStr.append(elementTypes[j].getName());
                        }
                        collectionElementType = elementTypes[0]; // Use the first only
                    }
                    else
                    {
                        // Try to derive element type from generics info
                        collectionElementType =
                            ClassUtils.getCollectionElementType(member.getType(), member.getGenericType());
                    }

                    contmd = new CollectionMetaData();
                    CollectionMetaData collmd = (CollectionMetaData)contmd;
                    collmd.setElementType(elementTypeStr.toString());
                    collmd.setEmbeddedElement(embeddedElement);
                    collmd.setSerializedElement(serializedElement);
                    collmd.setDependentElement(dependentElement);

                    // Add any embedded element mappings
                    if (embeddedElementMembers != null)
                    {
                        EmbeddedMetaData embmd = elemmd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedElementMembers.length;j++)
                        {
                            // Add the metadata for the embedded element to the embedded metadata
                            String memberName = embeddedElementMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd,
                                embeddedElementMembers[j],
                                isMemberOfClassAField(collectionElementType, memberName));
                            embmd.addMember(embfmd);
                        }
                    }
                }
                else if (member.getType().isArray())
                {
                    StringBuilder elementTypeStr = new StringBuilder();
                    if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class)
                    {
                        // User-specified element type(s)
                        for (int j=0;j<elementTypes.length;j++)
                        {
                            if (elementTypeStr.length() > 0)
                            {
                                elementTypeStr.append(',');
                            }
                            elementTypeStr.append(elementTypes[j].getName());
                        }
                    }
                    else
                    {
                        // Derive from component type
                        elementTypeStr.append(member.getType().getComponentType().getName());
                    }

                    contmd = new ArrayMetaData();
                    ArrayMetaData arrmd = (ArrayMetaData)contmd;
                    arrmd.setElementType(elementTypeStr.toString());
                    arrmd.setEmbeddedElement(embeddedElement);
                    arrmd.setSerializedElement(serializedElement);
                    arrmd.setDependentElement(dependentElement);
                }
                else if (Map.class.isAssignableFrom(member.getType()))
                {
                    Class mapKeyType = null;
                    if (keyType != null && keyType != void.class)
                    {
                        // User-specified key type
                        mapKeyType = keyType;
                    }
                    else
                    {
                        // Try to derive key type from generics info
                        mapKeyType = ClassUtils.getMapKeyType(member.getType(), member.getGenericType());
                    }

                    Class mapValueType = null;
                    if (valueType != null && valueType != void.class)
                    {
                        // User-specified value type
                        mapValueType = valueType;
                    }
                    else
                    {
                        // Try to derive value type from generics info
                        mapValueType = ClassUtils.getMapValueType(member.getType(), member.getGenericType());
                    }

                    contmd = new MapMetaData();
                    MapMetaData mapmd = (MapMetaData)contmd;
                    mapmd.setKeyType((mapKeyType != null ? mapKeyType.getName() : null));
                    mapmd.setEmbeddedKey(embeddedKey);
                    mapmd.setSerializedKey(serializedKey);
                    mapmd.setDependentKey(dependentKey);
                    mapmd.setValueType((mapValueType != null ? mapValueType.getName() : null));
                    mapmd.setEmbeddedValue(embeddedValue);
                    mapmd.setSerializedValue(serializedValue);
                    mapmd.setDependentValue(dependentValue);

                    if (embeddedKeyMembers != null)
                    {
                        // Add any embedded key mappings
                        EmbeddedMetaData embmd = keymd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedKeyMembers.length;j++)
                        {
                            // Add the metadata for the embedded key to the embedded metadata
                            String memberName = embeddedKeyMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedKeyMembers[j],
                                isMemberOfClassAField(mapKeyType, memberName));
                            embmd.addMember(embfmd);
                        }
                    }

                    if (embeddedValueMembers != null)
                    {
                        // Add any embedded value mappings
                        EmbeddedMetaData embmd = valuemd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedValueMembers.length;j++)
                        {
                            // Add the metadata for the embedded value to the embedded metadata
                            String memberName = embeddedValueMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedValueMembers[j],
                                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 (int i=0;i<colmds.length;i++)
                    {
                        mmd.addColumn(colmds[i]);
                    }
                }
                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)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        mmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }
        }
View Full Code Here

  public void testGetCustomSerializer() {
    SerializationManager mgr = new SerializationManager();
    AbstractMemberMetaData ammd = new AbstractMemberMetaData(null, "yar") {
      @Override
      public ExtensionMetaData[] getExtensions() {
        ExtensionMetaData emd = new ExtensionMetaData(
            "datanucleus",
            SerializationManager.SERIALIZATION_STRATEGY_KEY,
            HasSerializableJDO.ProtocolBufferSerializationStrategy.class.getName());
        return new ExtensionMetaData[] {emd};
      }
View Full Code Here

  public void testGetCustomSerializer_BadClass() {
    SerializationManager mgr = new SerializationManager();
    AbstractMemberMetaData ammd = new AbstractMemberMetaData(null, "yar") {
      @Override
      public ExtensionMetaData[] getExtensions() {
        ExtensionMetaData emd = new ExtensionMetaData(
            "datanucleus",
            SerializationManager.SERIALIZATION_STRATEGY_KEY,
            getClass().getName());
        return new ExtensionMetaData[] {emd};
      }
View Full Code Here

                            if (values != null && values.length > 0)
                            {
                                extensions = new HashSet<ExtensionMetaData>(values.length);
                                for (int j=0;j<values.length;j++)
                                {
                                    ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                        values[j].key().toString(), values[j].value().toString());
                                    extensions.add(extmd);
                                }
                            }
                        }
                        else if (annName.equals(JDOAnnotationUtils.EXTENSION))
                        {
                            ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                                (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                            extensions = new HashSet<ExtensionMetaData>(1);
                            extensions.add(extmd);
                        }
                        else
                        {
                            if (!annName.equals(JDOAnnotationUtils.PERSISTENCE_CAPABLE) &&
                                !annName.equals(JDOAnnotationUtils.PERSISTENCE_AWARE))
                            {
                                NucleusLogger.METADATA.error(LOCALISER.msg("044203", cls.getName(),
                                    annotations[i].getName()));
                            }
                        }
                    }
                }

                // Either PersistenceCapable, PersistenceAware or PersistentInterface so build up the metadata
                NucleusLogger.METADATA.info(LOCALISER.msg("044200", cls.getName(), "JDO"));

                if (cmd instanceof ClassMetaData)
                {
                    pmd.addClass((ClassMetaData)cmd);
                }
                else if (cmd instanceof InterfaceMetaData)
                {
                    pmd.addInterface((InterfaceMetaData)cmd);
                }

                if (embeddedOnly)
                {
                    cmd.setEmbeddedOnly(true);
                }
                if (idmd != null)
                {
                    // Datastore identity
                    idmd.setParent(cmd);
                    cmd.setIdentityMetaData(idmd);
                }
                if (pkmd != null)
                {
                    // Primary Key
                    pkmd.setParent(cmd);
                    cmd.setPrimaryKeyMetaData(pkmd);
                }

                if (vermd != null)
                {
                    // Version
                    vermd.setParent(cmd);
                    cmd.setVersionMetaData(vermd);
                }

                if (inhmd != null)
                {
                    // Inheritance
                    if (dismd != null)
                    {
                        inhmd.setDiscriminatorMetaData(dismd);
                    }
                    inhmd.setParent(cmd);
                    cmd.setInheritanceMetaData(inhmd);
                }
                else if (dismd != null)
                {
                    inhmd = new InheritanceMetaData();
                    inhmd.setDiscriminatorMetaData(dismd);
                    cmd.setInheritanceMetaData(inhmd);
                }

                if (joins != null && joins.length > 0)
                {
                    // Joins
                    for (int i=0;i<joins.length;i++)
                    {
                        cmd.addJoin(joins[i]);
                    }
                }
                if (queries != null && queries.length > 0)
                {
                    // Named Queries
                    for (int i=0;i<queries.length;i++)
                    {
                        queries[i].setParent(cmd);
                        cmd.addQuery(queries[i]);
                    }
                }
                if (fetchGroups != null && fetchGroups.length > 0)
                {
                    // Fetch Groups
                    for (int i=0;i<fetchGroups.length;i++)
                    {
                        fetchGroups[i].setParent(cmd);
                        cmd.addFetchGroup(fetchGroups[i]);
                    }
                }

                if (seqmd != null)
                {
                    // Sequence - currently only allowing 1 per class (should really be on the package)
                    cmd.getPackageMetaData().addSequence(seqmd);
                }

                if (indices != null)
                {
                    Iterator iter = indices.iterator();
                    while (iter.hasNext())
                    {
                        IndexMetaData idxmd = (IndexMetaData)iter.next();
                        idxmd.setParent(cmd);
                        cmd.addIndex(idxmd);
                    }
                }
                if (uniqueKeys != null)
                {
                    Iterator iter = uniqueKeys.iterator();
                    while (iter.hasNext())
                    {
                        UniqueMetaData unimd = (UniqueMetaData)iter.next();
                        unimd.setParent(cmd);
                        cmd.addUniqueConstraint(unimd);
                    }
                }
                if (fks != null)
                {
                    Iterator iter = fks.iterator();
                    while (iter.hasNext())
                    {
                        ForeignKeyMetaData fkmd = (ForeignKeyMetaData)iter.next();
                        fkmd.setParent(cmd);
                        cmd.addForeignKey(fkmd);
                    }
                }
                if (unmappedColumns != null)
                {
                    Iterator iter = fks.iterator();
                    while (iter.hasNext())
                    {
                        ColumnMetaData colmd = (ColumnMetaData)iter.next();
                        colmd.setParent(cmd);
                        cmd.addUnmappedColumn(colmd);
                    }
                }
                if (cacheable != null && cacheable.equalsIgnoreCase("false"))
                {
                    cmd.setCacheable(false);
                }
                if (extensions != null)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        cmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }
        }
View Full Code Here

                            // processed below in newJoinMetaData
                        }
                        else if (annName.equals(JPAAnnotationUtils.EXTENSION))
                        {
                            // extension
                            ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                                (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                            if (extensions == null)
                            {
                                extensions = new HashSet<ExtensionMetaData>(1);
                            }
                            extensions.add(extmd);
                        }
                        else if (annName.equals(JPAAnnotationUtils.EXTENSIONS))
                        {
                            // extension
                            Extension[] values = (Extension[])annotationValues.get("value");
                            if (values != null && values.length > 0)
                            {
                                if (extensions == null)
                                {
                                    extensions = new HashSet<ExtensionMetaData>(values.length);
                                }
                                for (int j=0;j<values.length;j++)
                                {
                                    ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                        values[j].key().toString(), values[j].value().toString());
                                    extensions.add(extmd);
                                }
                            }
                        }
                        else
                        {
                            NucleusLogger.METADATA.error(LOCALISER.msg("044203",
                                cls.getName(), annotations[i].getName()));
                        }
                    }
                }

                if (entityName == null || entityName.length() == 0)
                {
                    entityName = ClassUtils.getClassNameForClass(cls);
                }

                NucleusLogger.METADATA.info(LOCALISER.msg("044200", cls.getName(), "JPA"));

                cmd.setTable(table);
                cmd.setCatalog(catalog);
                cmd.setSchema(schema);
                cmd.setEntityName(entityName);
                cmd.setDetachable(detachable);
                cmd.setRequiresExtent(requiresExtent);
                cmd.setObjectIdClass(idClassName);
                cmd.setEmbeddedOnly(embeddedOnly);
                cmd.setCacheable(cacheable);
                cmd.setIdentityType(identityType);
                if (isClassPersistenceCapable(cls.getSuperclass()))
                {
                    cmd.setPersistenceCapableSuperclass(cls.getSuperclass().getName());
                }
                if (excludeSuperClassListeners)
                {
                    cmd.excludeSuperClassListeners();
                }
                if (excludeDefaultListeners)
                {
                    cmd.excludeDefaultListeners();
                }
                if (entityListeners != null)
                {
                    for (int i=0; i<entityListeners.length; i++)
                    {
                        // Any EventListener will not have their callback methods registered at this point
                        EventListenerMetaData elmd = new EventListenerMetaData(entityListeners[i].getName());
                        cmd.addListener(elmd);
                    }
                }

                // Inheritance
                InheritanceMetaData inhmd = null;
                if (inheritanceStrategy != null)
                {
                    // Strategy specified so create inheritance data
                    inhmd = cmd.newInheritanceMetadata().setStrategy(inheritanceStrategy);
                    inhmd.setStrategyForTree(inheritanceStrategyForTree);
                }
                else if (discriminatorValue != null || discriminatorColumnName != null ||
                        discriminatorColumnLength != null || discriminatorColumnType != null)
                {
                    // Discriminator specified so we need inheritance data
                    inhmd = cmd.newInheritanceMetadata().setStrategyForTree(inheritanceStrategyForTree);
                }

                if (discriminatorValue != null || discriminatorColumnName != null ||
                    discriminatorColumnLength != null || discriminatorColumnType != null)
                {
                    // Add discriminator information to the inheritance of this class
                    DiscriminatorMetaData dismd = inhmd.newDiscriminatorMetadata();
                    if (discriminatorValue != null)
                    {
                        // Value specified so assumed to be value-map
                        dismd.setColumnName(discriminatorColumnName);
                        dismd.setValue(discriminatorValue).setStrategy("value-map").setIndexed("false");
                    }
                    else
                    {
                        // No value so use class-name
                        discriminatorValue = cls.getName();
                        dismd.setColumnName(discriminatorColumnName);
                        dismd.setValue(discriminatorValue).setStrategy("value-map").setIndexed("false");
                    }

                    ColumnMetaData discolmd = null;
                    if (discriminatorColumnLength != null || discriminatorColumnName != null || discriminatorColumnType != null)
                    {
                        discolmd = new ColumnMetaData();
                        discolmd.setName(discriminatorColumnName);
                        if (discriminatorColumnType != null)
                        {
                            discolmd.setJdbcType(discriminatorColumnType);
                        }
                        if (discriminatorColumnLength != null)
                        {
                            discolmd.setLength(discriminatorColumnLength);
                        }
                        dismd.setColumnMetaData(discolmd);
                        if (discriminatorColumnDdl != null)
                        {
                            discolmd.setColumnDdl(discriminatorColumnDdl);
                        }
                    }
                }

                // extension - datastore-identity
                if (identityType == IdentityType.DATASTORE)
                {
                    IdentityMetaData idmd = cmd.newIdentityMetadata();
                    idmd.setColumnName(identityColumn);
                    idmd.setValueStrategy(IdentityStrategy.getIdentityStrategy(identityStrategy));
                    idmd.setSequence(identityGenerator);
                    if (identityGenerator != null)
                    {
                        idmd.setValueGeneratorName(identityGenerator);
                    }
                }

                if (pkColumnMetaData != null)
                {
                    // PK columns overriding those in the root class
                    PrimaryKeyMetaData pkmd = cmd.newPrimaryKeyMetadata();
                    for (int i=0;i<pkColumnMetaData.length;i++)
                    {
                        pkmd.addColumn(pkColumnMetaData[i]);
                    }
                }
                if (uniques != null && uniques.size() > 0)
                {
                    // Unique constraints for the primary/secondary tables
                    Iterator<UniqueMetaData> uniquesIter = uniques.iterator();
                    while (uniquesIter.hasNext())
                    {
                        cmd.addUniqueConstraint(uniquesIter.next());
                    }
                }

                if (overriddenFields != null)
                {
                    // Fields overridden from superclasses
                    Iterator<AbstractMemberMetaData> iter = overriddenFields.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addMember(iter.next());
                    }
                }
                if (namedQueries != null)
                {
                    Iterator<QueryMetaData> iter = namedQueries.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addQuery(iter.next());
                    }
                }
                if (resultMappings != null)
                {
                    Iterator<QueryResultMetaData> iter = resultMappings.iterator();
                    while (iter.hasNext())
                    {
                        cmd.addQueryResultMetaData(iter.next());
                    }
                }
                if (extensions != null)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        cmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }

            // Process any secondary tables
View Full Code Here

                    modifier = FieldPersistenceModifier.PERSISTENT.toString();
                }
                else if (annName.equals(JPAAnnotationUtils.EXTENSION) &&
                    jpaLevel.equalsIgnoreCase("DataNucleus"))
                {
                    ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                        (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                    if (extensions == null)
                    {
                        extensions = new HashSet<ExtensionMetaData>(1);
                    }
                    extensions.add(extmd);
                }
                else if (annName.equals(JPAAnnotationUtils.EXTENSIONS) &&
                    jpaLevel.equalsIgnoreCase("DataNucleus"))
                {
                    Extension[] values = (Extension[])annotationValues.get("value");
                    if (values != null && values.length > 0)
                    {
                        if (extensions == null)
                        {
                            extensions = new HashSet<ExtensionMetaData>(values.length);
                        }
                        for (int j=0;j<values.length;j++)
                        {
                            ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                values[j].key().toString(), values[j].value().toString());
                            extensions.add(extmd);
                        }
                    }
                }
                else if (annName.equals(JPAAnnotationUtils.SEQUENCE_GENERATOR))
                {
                    // Sequence generator, so store it against the package that we are under
                    processSequenceGeneratorAnnotation(cmd.getPackageMetaData(), annotationValues);
                }
                else if (annName.equals(JPAAnnotationUtils.TABLE_GENERATOR))
                {
                    // Table generator, so store it against the package that we are under
                    processTableGeneratorAnnotation(cmd.getPackageMetaData(), annotationValues);
                }
            }
        }

        if (JPAAnnotationUtils.isBasicByDefault(field.getType()) && modifier == null)
        {
            modifier = FieldPersistenceModifier.PERSISTENT.toString();
        }

        // Create the field
        AbstractMemberMetaData fmd;
        if (field.isProperty())
        {
            fmd = new PropertyMetaData(cmd, field.getName());
        }
        else
        {
            fmd = new FieldMetaData(cmd, field.getName());
        }
        fmd.setPersistenceModifier(modifier);
        fmd.setPrimaryKey(pk);
        fmd.setDefaultFetchGroup(dfg);
        fmd.setEmbedded(embedded);
        fmd.setNullValue(NullValue.getNullValue(nullValue));
        fmd.setMappedBy(mappedBy);

        if (version != null)
        {
            // Tag this field as the version field
            VersionMetaData vermd = cmd.newVersionMetadata();
            vermd.setStrategy(VersionStrategy.VERSION_NUMBER).setFieldName(fmd.getName());
        }

        cmd.addMember(fmd);

        if (cascades != null)
        {
            for (int i = 0; i < cascades.length; i++)
            {
                if (cascades[i] == CascadeType.ALL)
                {
                    fmd.setCascadePersist(true);
                    fmd.setCascadeUpdate(true);
                    fmd.setCascadeDelete(true);
                    fmd.setCascadeRefresh(true);
                }
                else if (cascades[i] == CascadeType.PERSIST)
                {
                    fmd.setCascadePersist(true);
                }
                else if (cascades[i] == CascadeType.MERGE)
                {
                    fmd.setCascadeUpdate(true);
                }
                else if (cascades[i] == CascadeType.REMOVE)
                {
                    fmd.setCascadeDelete(true);
                }
                else if (cascades[i] == CascadeType.REFRESH)
                {
                    fmd.setCascadeRefresh(true);
                }
            }
        }

        // Value generation
        if (valueStrategy != null && valueGenerator != null)
        {
            fmd.setValueGeneratorName(valueGenerator);
        }
        if (valueStrategy != null)
        {
            fmd.setValueStrategy(valueStrategy);
        }

        // Type storage
        if (storeInLob)
        {
            fmd.setStoreInLob();
        }

        // Container fields : If the field is a container then add its container element
        ContainerMetaData contmd = null;
        if (Collection.class.isAssignableFrom(field.getType()))
        {
            String elementType = null;
            if (targetEntity != null && targetEntity != void.class)
            {
                elementType = targetEntity.getName();
            }
            if (elementType == null)
            {
                elementType = ClassUtils.getCollectionElementType(field.getType(),field.getGenericType());
            }
            // No annotation for collections so cant specify the element type, dependent, embedded, serialized

            contmd = new CollectionMetaData();
            ((CollectionMetaData)contmd).setElementType(elementType);
        }
        else if (field.getType().isArray())
        {
            contmd = new ArrayMetaData();
        }
        else if (Map.class.isAssignableFrom(field.getType()))
        {
            String keyType = ClassUtils.getMapKeyType(field.getType(),field.getGenericType());
            String valueType = null;
            if (targetEntity != null && targetEntity != void.class)
            {
                valueType = targetEntity.getName();
            }
            if (valueType == null)
            {
                valueType = ClassUtils.getMapValueType(field.getType(),field.getGenericType());
            }

            // No annotation for maps so cant specify the key/value type, dependent, embedded, serialized
            contmd = new MapMetaData();
            MapMetaData mapmd = (MapMetaData)contmd;
            mapmd.setKeyType(keyType);
            mapmd.setValueType(valueType);
        }
        if (contmd != null)
        {
            fmd.setContainer(contmd);
        }

        if (addJoin)
        {
            if (fmd.getJoinMetaData() == null)
            {
                JoinMetaData joinmd = new JoinMetaData();
                fmd.setJoinMetaData(joinmd);
            }
        }

        // Extensions
        if (extensions != null)
        {
            Iterator<ExtensionMetaData> iter = extensions.iterator();
            while (iter.hasNext())
            {
                ExtensionMetaData extmd = iter.next();
                fmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
            }
        }

        return fmd;
    }
View Full Code Here

                        if (values != null && values.length > 0)
                        {
                            extensions = new HashSet<ExtensionMetaData>(values.length);
                            for (int j=0;j<values.length;j++)
                            {
                                ExtensionMetaData extmd = new ExtensionMetaData(values[j].vendorName(),
                                    values[j].key().toString(), values[j].value().toString());
                                extensions.add(extmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.EXTENSION))
                    {
                        ExtensionMetaData extmd = new ExtensionMetaData((String)annotationValues.get("vendorName"),
                            (String)annotationValues.get("key"), (String)annotationValues.get("value"));
                        extensions = new HashSet<ExtensionMetaData>(1);
                        extensions.add(extmd);
                    }
                    else
                    {
                        NucleusLogger.METADATA.error(LOCALISER.msg("044203",
                            member.getName(), annotations[i].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))
            {
                // @Persistent not supplied but other relevant annotations defined, so add default metadata element
                if (member.isProperty())
                {
                    mmd = new PropertyMetaData(cmd, member.getName());
                }
                else
                {
                    mmd = new FieldMetaData(cmd, member.getName());
                }
                mmd.setPrimaryKey("" + primaryKey);
            }

            if (mmd != null)
            {
                cmd.addMember(mmd);

                if (primaryKey)
                {
                    mmd.setPrimaryKey(true);
                }
                if (serialised)
                {
                    mmd.setSerialised(true);
                }
                if (nonPersistentField)
                {
                    mmd.setNotPersistent();
                }
                if (transactionalField)
                {
                    mmd.setTransactional();
                }

                // Add any embedded info
                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 (int j=0;j<embeddedMembers.length;j++)
                        {
                            // Add the metadata for the embedded field/property to the embedded metadata
                            String memberName = embeddedMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedMembers[j],
                                isMemberOfClassAField(member.getType(), memberName));
                            embmd.addMember(embfmd);
                        }
                    }
                }

                // If the field is a container then add its container element
                ContainerMetaData contmd = null;
                if (Collection.class.isAssignableFrom(member.getType()))
                {
                    Class collectionElementType = null;
                    StringBuffer elementTypeStr = new StringBuffer();
                    if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class)
                    {
                        // User-specified element type(s)
                        for (int j=0;j<elementTypes.length;j++)
                        {
                            if (elementTypeStr.length() > 0)
                            {
                                elementTypeStr.append(',');
                            }
                            elementTypeStr.append(elementTypes[j].getName());
                        }
                        collectionElementType = elementTypes[0]; // Use the first only
                    }
                    else
                    {
                        // Try to derive element type from generics info
                        if (member.getGenericType() instanceof ParameterizedType)
                        {
                            ParameterizedType paramtype = (ParameterizedType)member.getGenericType();
                            if (paramtype.getActualTypeArguments().length == 1)
                            {
                                Type type = paramtype.getActualTypeArguments()[0];
                                if (type instanceof Class)
                                {
                                    collectionElementType = (Class) type;
                                }
                                else if (type instanceof ParameterizedType)
                                {
                                    collectionElementType = (Class) ((ParameterizedType)type).getRawType();
                                }
                                else
                                {
                                    throw new IllegalStateException("Unknown type: " + type);
                                }

                                elementTypeStr.append(collectionElementType.getName());
                            }
                        }
                    }

                    contmd = new CollectionMetaData();
                    CollectionMetaData collmd = (CollectionMetaData)contmd;
                    collmd.setElementType(elementTypeStr.toString());
                    collmd.setEmbeddedElement(embeddedElement);
                    collmd.setSerializedElement(serializedElement);
                    collmd.setDependentElement(dependentElement);

                    // Add any embedded element mappings
                    if (embeddedElementMembers != null)
                    {
                        EmbeddedMetaData embmd = elemmd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedElementMembers.length;j++)
                        {
                            // Add the metadata for the embedded element to the embedded metadata
                            String memberName = embeddedElementMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd,
                                embeddedElementMembers[j],
                                isMemberOfClassAField(collectionElementType, memberName));
                            embmd.addMember(embfmd);
                        }
                    }
                }
                else if (member.getType().isArray())
                {
                    StringBuffer elementTypeStr = new StringBuffer();
                    if (elementTypes != null && elementTypes.length > 0 && elementTypes[0] != void.class)
                    {
                        // User-specified element type(s)
                        for (int j=0;j<elementTypes.length;j++)
                        {
                            if (elementTypeStr.length() > 0)
                            {
                                elementTypeStr.append(',');
                            }
                            elementTypeStr.append(elementTypes[j].getName());
                        }
                    }
                    else
                    {
                        // Derive from component type
                        elementTypeStr.append(member.getType().getComponentType().getName());
                    }

                    contmd = new ArrayMetaData();
                    ArrayMetaData arrmd = (ArrayMetaData)contmd;
                    arrmd.setElementType(elementTypeStr.toString());
                    arrmd.setEmbeddedElement(embeddedElement);
                    arrmd.setSerializedElement(serializedElement);
                    arrmd.setDependentElement(dependentElement);
                }
                else if (Map.class.isAssignableFrom(member.getType()))
                {
                    Class mapKeyType = null;
                    if (keyType != null && keyType != void.class)
                    {
                        // User-specified key type
                        mapKeyType = keyType;
                    }
                    else
                    {
                        // Try to derive key type from generics info
                        if (member.getGenericType() instanceof ParameterizedType)
                        {
                            ParameterizedType paramtype = (ParameterizedType)member.getGenericType();
                            if (paramtype.getActualTypeArguments().length == 2)
                            {
                                Type type = paramtype.getActualTypeArguments()[0];
                                if (type instanceof Class)
                                {
                                    mapKeyType = (Class) type;
                                }
                                else if (type instanceof ParameterizedType)
                                {
                                    mapKeyType = (Class) ((ParameterizedType)type).getRawType();
                                }
                                else
                                {
                                    throw new IllegalStateException("Unknown type: " + type);
                                }
                            }
                        }
                    }

                    Class mapValueType = null;
                    if (valueType != null && valueType != void.class)
                    {
                        // User-specified value type
                        mapValueType = valueType;
                    }
                    else
                    {
                        // Try to derive value type from generics info
                        if (member.getGenericType() instanceof ParameterizedType)
                        {
                            ParameterizedType paramtype = (ParameterizedType)member.getGenericType();
                            if (paramtype.getActualTypeArguments().length == 2)
                            {
                                Type type = paramtype.getActualTypeArguments()[1];
                                if (type instanceof Class)
                                {
                                    mapValueType = (Class) type;
                                }
                                else if (type instanceof ParameterizedType)
                                {
                                    mapValueType = (Class) ((ParameterizedType)type).getRawType();
                                }
                                else
                                {
                                    throw new IllegalStateException("Unknown type: " + type);
                                }
                            }
                        }
                    }

                    contmd = new MapMetaData();
                    MapMetaData mapmd = (MapMetaData)contmd;
                    mapmd.setKeyType((mapKeyType != null ? mapKeyType.getName() : null));
                    mapmd.setEmbeddedKey(embeddedKey);
                    mapmd.setSerializedKey(serializedKey);
                    mapmd.setDependentKey(dependentKey);
                    mapmd.setValueType((mapValueType != null ? mapValueType.getName() : null));
                    mapmd.setEmbeddedValue(embeddedValue);
                    mapmd.setSerializedValue(serializedValue);
                    mapmd.setDependentValue(dependentValue);

                    if (embeddedKeyMembers != null)
                    {
                        // Add any embedded key mappings
                        EmbeddedMetaData embmd = keymd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedKeyMembers.length;j++)
                        {
                            // Add the metadata for the embedded key to the embedded metadata
                            String memberName = embeddedKeyMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedKeyMembers[j],
                                isMemberOfClassAField(mapKeyType, memberName));
                            embmd.addMember(embfmd);
                        }
                    }

                    if (embeddedValueMembers != null)
                    {
                        // Add any embedded value mappings
                        EmbeddedMetaData embmd = valuemd.getEmbeddedMetaData();
                        for (int j=0;j<embeddedValueMembers.length;j++)
                        {
                            // Add the metadata for the embedded value to the embedded metadata
                            String memberName = embeddedValueMembers[j].name();
                            if (memberName.indexOf('.') > 0)
                            {
                                memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                            }
                            AbstractMemberMetaData embfmd = getFieldMetaDataForPersistent(embmd, embeddedValueMembers[j],
                                isMemberOfClassAField(mapValueType, memberName));
                            embmd.addMember(embfmd);
                        }
                    }
                }
                if (contmd != null)
                {
                    mmd.setContainer(contmd);
                   
                    if (elemmd != null)
                    {
                        elemmd.setParent(mmd);
                        mmd.setElementMetaData(elemmd);
                    }
                    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 (int i=0;i<colmds.length;i++)
                    {
                        mmd.addColumn(colmds[i]);
                    }
                }
                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)
                {
                    Iterator<ExtensionMetaData> iter = extensions.iterator();
                    while (iter.hasNext())
                    {
                        ExtensionMetaData extmd = iter.next();
                        mmd.addExtension(extmd.getVendorName(), extmd.getKey(), extmd.getValue());
                    }
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.datanucleus.metadata.ExtensionMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.