Package org.jpox.metadata

Examples of org.jpox.metadata.AbstractClassMetaData


        {
            // TODO Localise this message
            throw new JPOXException("No PackageMetaData specified to use for annotations");
        }

        AbstractClassMetaData cmd = null;

        if (annotations != null && annotations.length > 0)
        {
            InheritanceMetaData inhmd = null;
            DiscriminatorMetaData dismd = null;
            IdentityMetaData idmd = null;
            PrimaryKeyMetaData pkmd = null;
            VersionMetaData vermd = null;
            JoinMetaData[] joins = null;
            QueryMetaData[] queries = null;
            FetchPlanMetaData[] fetchPlans = null;
            FetchGroupMetaData[] fetchGroups = null;
            SequenceMetaData seqmd = null;
            String tableName = null;
            String catalogName = null;
            String schemaName = null;
            boolean embeddedOnly = false;
            ColumnMetaData[] unmappedColumns = null;
            HashSet<IndexMetaData> indices = null;
            HashSet<UniqueMetaData> uniqueKeys = null;
            HashSet<ForeignKeyMetaData> fks = null;
            HashSet<ExtensionMetaData> extensions = null;

            for (int i=0;i<annotations.length;i++)
            {
                if (isSupportedAnnotation(annotations[i].getName()))
                {
                    HashMap<String, Object> annotationValues = annotations[i].getNameValueMap();
                    String annName = annotations[i].getName();
                    if (annName.equals(JDOAnnotationUtils.PERSISTENCE_CAPABLE))
                    {
                        // PersistenceCapable class
                        String embeddedOnlyString = (String)annotationValues.get("embeddedOnly");
                        String requiresExtent = (String)annotationValues.get("requiresExtent");
                        String detachable = (String)annotationValues.get("detachable");
                        tableName = (String)annotationValues.get("table");
                        catalogName = (String)annotationValues.get("catalog");
                        schemaName = (String)annotationValues.get("schema");

                        javax.jdo.annotations.IdentityType idTypeVal = (javax.jdo.annotations.IdentityType)annotationValues.get("identityType");
                        String identityType = JDOAnnotationUtils.getIdentityTypeString(idTypeVal);

                        String idClassName = null;
                        Class idClass = (Class)annotationValues.get("objectIdClass");
                        if (idClass != null && idClass != void.class)
                        {
                            idClassName = idClass.getName();
                        }

                        if (cls.isInterface())
                        {
                            cmd = mgr.getMetaDataFactory().newInterfaceObject(pmd, ClassUtils.getClassNameForClass(cls),
                                identityType, idClassName, requiresExtent, detachable,
                                embeddedOnlyString, catalogName, schemaName, tableName, null);
                        }
                        else
                        {
                            cmd = mgr.getMetaDataFactory().newClassObject(pmd, ClassUtils.getClassNameForClass(cls),
                                identityType, idClassName, requiresExtent, detachable,
                                embeddedOnlyString, "persistence-capable", null, catalogName, schemaName, tableName, null);
                        }
                        JDOAnnotationUtils.addExtensionsToMetaData(cmd, (Extension[])annotationValues.get("extensions"));

                        // Members typically providing specification of overridden fields/properties
                        Persistent[] members = (Persistent[])annotationValues.get("members");
                        if (members != null)
                        {
                            // Add on the fields/properties direct to the metadata for the class/interface
                            for (int j=0;j<members.length;j++)
                            {
                                String memberName = members[j].name();
                                if (memberName.indexOf('.') > 0)
                                {
                                    memberName = memberName.substring(memberName.lastIndexOf('.')+1);
                                }
                                boolean isField = isMemberOfClassAField(cls, memberName);
                                AbstractMemberMetaData fmd = getFieldMetaDataForPersistent(cmd, members[j], isField);
                                cmd.addMember(fmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.PERSISTENCE_AWARE))
                    {
                        // PersistenceAware class
                        cmd = mgr.getMetaDataFactory().newClassObject(pmd, ClassUtils.getClassNameForClass(cls), null,
                            null, null, null, null, "persistence-aware",
                            null, null, null, null, null);
                    }
                    else if (annName.equals(JDOAnnotationUtils.EMBEDDED_ONLY))
                    {
                        embeddedOnly = true;
                    }
                    else if (annName.equals(JDOAnnotationUtils.VERSION))
                    {
                        VersionStrategy versionStrategy = (VersionStrategy)annotationValues.get("strategy");
                        String strategy = JDOAnnotationUtils.getVersionStrategyString(versionStrategy);
                        String indexed = (String)annotationValues.get("indexed");
                        String column = (String)annotationValues.get("column");
                        Column[] columns = (Column[])annotationValues.get("columns");
                        vermd = new VersionMetaData(strategy, column, indexed);
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumn(vermd, columns[0]);
                            vermd.addColumn(colmd);
                        }
                        JDOAnnotationUtils.addExtensionsToMetaData(vermd, (Extension[])annotationValues.get("extensions"));
                    }
                    else if (annName.equals(JDOAnnotationUtils.DATASTORE_IDENTITY))
                    {
                        String strategy = JDOAnnotationUtils.getIdentityStrategyString(
                            (IdGeneratorStrategy)annotationValues.get("strategy"));
                        String customStrategy = (String)annotationValues.get("customStrategy");
                        if (!StringUtils.isWhitespace(customStrategy))
                        {
                            // User has provided a JPOX extension strategy
                            strategy = customStrategy;
                        }
                        String sequence = (String)annotationValues.get("sequence");
                        String column = (String)annotationValues.get("column");
                        Column[] columns = (Column[])annotationValues.get("columns");
                        idmd = new IdentityMetaData(cmd, column, strategy, sequence);
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumn(idmd, columns[0]);
                            idmd.addColumn(colmd);
                        }
                        JDOAnnotationUtils.addExtensionsToMetaData(idmd, (Extension[])annotationValues.get("extensions"));
                    }
                    else if (annName.equals(JDOAnnotationUtils.PRIMARY_KEY))
                    {
                        String pkName = (String)annotationValues.get("name");
                        String pkColumn = (String)annotationValues.get("column");
                        Column[] columns = (Column[])annotationValues.get("columns");
                        pkmd = new PrimaryKeyMetaData(null, pkName, pkColumn);
                        if (columns != null && columns.length > 0)
                        {
                            for (int j=0;j<columns.length;j++)
                            {
                                pkmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumn(pkmd, columns[j]));
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.JOINS))
                    {
                        if (joins != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.JoinSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        Join[] js = (Join[])annotationValues.get("value");
                        if (js != null && js.length > 0)
                        {
                            joins = new JoinMetaData[js.length];
                            for (int j=0;j<js.length;j++)
                            {
                                String deleteAction = JDOAnnotationUtils.getForeignKeyActionString(js[j].deleteAction());
                                joins[j] = new JoinMetaData(cmd, js[j].table(), null, null,
                                    js[j].column(), js[j].outer(), deleteAction, js[i].indexed(), js[i].unique());
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.JOIN))
                    {
                        if (joins != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.JoinSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        joins = new JoinMetaData[1];
                        joins[0] = new JoinMetaData(cmd, (String)annotationValues.get("table"), null, null,
                            (String)annotationValues.get("column"), (String)annotationValues.get("outer"),
                            ((ForeignKeyAction)annotationValues.get("deleteAction")).toString(),
                            (String)annotationValues.get("indexed"),
                            (String)annotationValues.get("unique"));
                        JDOAnnotationUtils.addExtensionsToMetaData(joins[0], (Extension[])annotationValues.get("extensions"));
                    }
                    else if (annName.equals(JDOAnnotationUtils.INHERITANCE))
                    {
                        String strategy = JDOAnnotationUtils.getInheritanceStrategyString(
                            (InheritanceStrategy)annotationValues.get("strategy"));
                        String customStrategy = (String)annotationValues.get("customStrategy");
                        if (!StringUtils.isWhitespace(customStrategy))
                        {
                            // User has provided a JPOX extension strategy
                            strategy = customStrategy;
                        }
                        inhmd = new InheritanceMetaData(cmd, strategy);
                    }
                    else if (annName.equals(JDOAnnotationUtils.DISCRIMINATOR))
                    {
                        DiscriminatorStrategy discriminatorStrategy = (DiscriminatorStrategy)annotationValues.get("strategy");
                        String strategy = JDOAnnotationUtils.getDiscriminatorStrategyString(discriminatorStrategy);
                        String column = (String)annotationValues.get("column");
                        String indexed = (String)annotationValues.get("indexed");
                        String value = (String)annotationValues.get("value");
                        Column[] columns = (Column[])annotationValues.get("columns");
                        dismd = new DiscriminatorMetaData(null, column, value, strategy, indexed);
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd = JDOAnnotationUtils.getColumnMetaDataForColumn(dismd, columns[0]);
                            dismd.setColumnMetaData(colmd);
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.QUERIES))
                    {
                        if (queries != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.QuerySpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        Query[] qs = (Query[])annotationValues.get("value");
                        queries = new QueryMetaData[qs.length];
                        for (int j=0;j<queries.length;j++)
                        {
                            String lang = JDOAnnotationUtils.getQueryLanguageName(qs[j].language());
                            String resultClassName = (qs[j].resultClass() != null && qs[j].resultClass() != void.class ?
                                    qs[j].resultClass().getName() : null);
                            queries[j] = new QueryMetaData(cmd, cls.getName(), qs[j].name(), lang,
                                "" + qs[j].unmodifiable(), resultClassName, null, qs[j].unique(), qs[j].fetchPlan());
                            queries[j].setQuery(qs[j].value());
                            JDOAnnotationUtils.addExtensionsToMetaData(queries[j], qs[j].extensions());
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.QUERY))
                    {
                        if (queries != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.QuerySpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        queries = new QueryMetaData[1];
                        String unmodifiable = "" + annotationValues.get("unmodifiable");
                        Class resultClassValue = (Class)annotationValues.get("resultClass");
                        String resultClassName =
                            (resultClassValue != null && resultClassValue != void.class ? resultClassValue.getName() : null);
                        String lang = JDOAnnotationUtils.getQueryLanguageName((String)annotationValues.get("language"));
                        queries[0] = new QueryMetaData(cmd, cls.getName(), (String)annotationValues.get("name"),
                            lang.toString(), unmodifiable, resultClassName, null, (String)annotationValues.get("unique"),
                            (String)annotationValues.get("fetchPlan"));
                        queries[0].setQuery((String)annotationValues.get("value"));
                        JDOAnnotationUtils.addExtensionsToMetaData(queries[0], (Extension[])annotationValues.get("extensions"));
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHPLANS))
                    {
                        if (fetchPlans != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FetchPlanSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        FetchPlan[] plans = (FetchPlan[])annotationValues.get("value");
                        fetchPlans = new FetchPlanMetaData[plans.length];
                        for (int j=0;j<plans.length;j++)
                        {
                            fetchPlans[j] = new FetchPlanMetaData(cmd, plans[j].name(), "" + plans[j].maxFetchDepth(),
                                "" + plans[j].fetchSize());
                            int numGroups = plans[j].fetchGroups().length;
                            for (int k=0;k<numGroups;k++)
                            {
                                FetchGroupMetaData fgmd = new FetchGroupMetaData(fetchPlans[j], null,
                                    plans[j].fetchGroups()[k]);
                                fetchPlans[j].addFetchGroup(fgmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHPLAN))
                    {
                        if (fetchPlans != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FetchPlanSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        fetchPlans = new FetchPlanMetaData[1];
                        int maxFetchDepth = ((Integer)annotationValues.get("max-fetch-depth")).intValue();
                        int fetchSize = ((Integer)annotationValues.get("fetch-size")).intValue();
                        fetchPlans[0] = new FetchPlanMetaData(cmd, (String)annotationValues.get("name"),
                            "" + maxFetchDepth, "" + fetchSize);
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHGROUPS))
                    {
                        if (fetchGroups != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FetchGroupSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        FetchGroup[] groups = (FetchGroup[])annotationValues.get("value");
                        fetchGroups = new FetchGroupMetaData[groups.length];
                        for (int j=0;j<groups.length;j++)
                        {
                            fetchGroups[j] = new FetchGroupMetaData(cmd, groups[j].postLoad(), groups[j].name());
                            int numFields = groups[j].members().length;
                            for (int k=0;k<numFields;k++)
                            {
                                FieldMetaData fmd = mgr.getMetaDataFactory().newFieldObject(fetchGroups[j],
                                    groups[j].members()[k].name(),
                                    null, null, null, null, null, null, null, null, null, null, null,
                                    null, null, null, null, "" + groups[j].members()[k].recursionDepth(), null, null, null, null);
                                fetchGroups[j].addMember(fmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHGROUP))
                    {
                        if (fetchGroups != null)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FetchGroupSpecificationConflict",
                                cmd.getFullClassName()));
                        }
                        fetchGroups = new FetchGroupMetaData[1];
                        fetchGroups[0] = new FetchGroupMetaData(cmd, (String)annotationValues.get("postLoad"),
                            (String)annotationValues.get("name"));
                        Persistent[] fields = (Persistent[])annotationValues.get("members");
                        if (fields != null)
                        {
                            for (int j=0;j<fields.length;j++)
                            {
                                FieldMetaData fmd = mgr.getMetaDataFactory().newFieldObject(fetchGroups[0], fields[j].name(),
                                    null, null, null, null, null, null, null, null, null, null, null,
                                    null, null, null, null, "" + fields[j].recursionDepth(), null, null, null, null);
                                fetchGroups[0].addMember(fmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.SEQUENCE))
                    {
                        String seqName = (String)annotationValues.get("name");
                        String seqStrategy = JDOAnnotationUtils.getSequenceStrategyString(
                            (SequenceStrategy)annotationValues.get("strategy"));
                        String seqSeq = (String)annotationValues.get("datastoreSequence");
                        Class seqFactory = (Class)annotationValues.get("factoryClass");
                        String seqFactoryClassName = null;
                        if (seqFactory != null && seqFactory != void.class)
                        {
                            seqFactoryClassName = seqFactory.getName();
                        }
                        seqmd = new SequenceMetaData(null, seqName, seqSeq, seqFactoryClassName, seqStrategy, null, null);
                        JDOAnnotationUtils.addExtensionsToMetaData(seqmd, (Extension[])annotationValues.get("extensions"));
                    }
                    else if (annName.equals(JDOAnnotationUtils.INDICES))
                    {
                        // Multiple Indices for the class
                        Index[] values = (Index[])annotationValues.get("value");
                        if (values != null && values.length > 0)
                        {
                            indices = new HashSet<IndexMetaData>(values.length);
                            for (int j=0;j<values.length;j++)
                            {
                                IndexMetaData idxmd = JDOAnnotationUtils.getIndexMetaData(values[j].name(), values[j].table(),
                                    "" + values[j].unique(), values[j].members(), values[j].columns());
                                if (idxmd.getNumberOfColumns() == 0 && idxmd.getNumberOfMembers() == 0)
                                {
                                    JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.IndexAtClassWithoutFieldsColumns",
                                        cls.getName()));
                                }
                                else
                                {
                                    indices.add(idxmd);
                                }
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.INDEX))
                    {
                        // Single Index for the class
                        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");

                        IndexMetaData idxmd = JDOAnnotationUtils.getIndexMetaData(name, table, unique, members, columns);
                        if (idxmd.getNumberOfColumns() == 0 && idxmd.getNumberOfMembers() == 0)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.IndexAtClassWithoutFieldsColumns",
                                cls.getName()));
                        }
                        else
                        {
                            indices = new HashSet<IndexMetaData>(1);
                            indices.add(idxmd);
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.UNIQUES))
                    {
                        // Multiple Unique Constraints for the class
                        Unique[] values = (Unique[])annotationValues.get("value");
                        if (values != null && values.length > 0)
                        {
                            uniqueKeys = new HashSet<UniqueMetaData>(values.length);
                            for (int j=0;j<values.length;j++)
                            {
                                UniqueMetaData unimd = JDOAnnotationUtils.getUniqueMetaData(values[j].name(),
                                    values[j].table(), "" + values[j].deferred(), values[j].members(), values[j].columns());
                                if (unimd.getNumberOfColumns() == 0 && unimd.getNumberOfMembers() == 0)
                                {
                                    JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.UniqueAtClassWithoutFieldsColumns",
                                        cls.getName()));
                                }
                                else
                                {
                                    uniqueKeys.add(unimd);
                                }
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.UNIQUE))
                    {
                        // Single Unique constraint for the class
                        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");

                        UniqueMetaData unimd = JDOAnnotationUtils.getUniqueMetaData(name, table, deferred, members, columns);
                        if (unimd.getNumberOfColumns() == 0 && unimd.getNumberOfMembers() == 0)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.UniqueAtClassWithoutFieldsColumns",
                                cls.getName()));
                        }
                        else
                        {
                            uniqueKeys = new HashSet<UniqueMetaData>(1);
                            uniqueKeys.add(unimd);
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FOREIGNKEYS))
                    {
                        // Multiple FKs for the class
                        ForeignKey[] values = (ForeignKey[])annotationValues.get("value");
                        if (values != null && values.length > 0)
                        {
                            fks = new HashSet<ForeignKeyMetaData>(values.length);
                            for (int j=0;j<values.length;j++)
                            {
                                String deleteAction = JDOAnnotationUtils.getForeignKeyActionString(values[j].deleteAction());
                                String updateAction = JDOAnnotationUtils.getForeignKeyActionString(values[j].deleteAction());
                                ForeignKeyMetaData fkmd = JDOAnnotationUtils.getFKMetaData(values[j].name(),
                                    values[j].table(), values[j].unique(), "" + values[j].deferred(),
                                    deleteAction, updateAction, values[j].members(), values[j].columns());
                                if (fkmd.getNumberOfColumns() == 0 && fkmd.getNumberOfMembers() == 0)
                                {
                                    JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FKAtClassWithoutFieldsColumns",
                                        cls.getName()));
                                }
                                else
                                {
                                    fks.add(fkmd);
                                }
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FOREIGNKEY))
                    {
                        // Single FK constraint for the class
                        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");

                        ForeignKeyMetaData fkmd = JDOAnnotationUtils.getFKMetaData(name, table, unique, deferred,
                            deleteAction, updateAction, members, columns);
                        if (fkmd.getNumberOfColumns() == 0 && fkmd.getNumberOfMembers() == 0)
                        {
                            JPOXLogger.METADATA.warn(LOCALISER.msg("MetaData.Annotations.FKAtClassWithoutFieldsColumns",
                                cls.getName()));
                        }
                        else
                        {
                            fks = new HashSet<ForeignKeyMetaData>(1);
                            fks.add(fkmd);
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.COLUMNS))
                    {
                        // Unmapped column specification
                        Column[] cols = (Column[])annotationValues.get("value");
                        if (cols != null && cols.length > 0)
                        {
                            unmappedColumns = new ColumnMetaData[cols.length];
                            for (int j=0;j<cols.length;j++)
                            {
                                unmappedColumns[j] = JDOAnnotationUtils.getColumnMetaDataForColumn(cmd, cols[j]);
                                JDOAnnotationUtils.addExtensionsToMetaData(unmappedColumns[j], cols[j].extensions());
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.EXTENSIONS))
                    {
                        Extension[] values = (Extension[])annotationValues.get("value");
                        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
                    {
                        JPOXLogger.METADATA.error(LOCALISER.msg("MetaData.Annotations.AnnotationNotProcessed",
                            cls.getName(), annotations[i].getName()));
                    }
                }
            }

            if (cmd != null)
            {
                // Either PersistenceCapable, PersistenceAware or PersistentInterface so build up the metadata
                JPOXLogger.METADATA.info(LOCALISER.msg("MetaData.Annotations.ClassUsingAnnotations", cls.getName(), "JDO"));

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

                if (embeddedOnly)
                {
                    cmd.setEmbeddedOnly();
                }
                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);
                }

                if (joins != null && joins.length > 0)
                {
                    // Joins
                    for (int i=0;i<joins.length;i++)
                    {
                        joins[i].setParent(cmd);
                        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)
                    seqmd.setParent(cmd.getPackageMetaData());
                    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 (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


    private void invokeCallback(final Object pc, final Class callbackClass)
    {
        final ObjectManager om = ((AbstractPersistenceManager) ((PersistenceCapable) pc).jdoGetPersistenceManager()).getObjectManager();
        final ClassLoaderResolver clr = om.getClassLoaderResolver();

        AbstractClassMetaData acmd = om.getOMFContext().getMetaDataManager().getMetaDataForClass(pc.getClass(), clr);

        try
        {
            if (!acmd.isExcludeDefaultListeners())
            {
                // Global listeners for all classes
                List<EventListenerMetaData> listenerMetaData = om.getMetaDataManager().getListeners();
                if (listenerMetaData != null && listenerMetaData.size() > 0)
                {
                    // Files have listeners so go through them in the same order
                    Iterator<EventListenerMetaData> listenerIter = listenerMetaData.iterator();
                    while (listenerIter.hasNext())
                    {
                        EventListenerMetaData elmd = listenerIter.next();
                        Class listenerClass = clr.classForName(elmd.getClassName());
                        String methodName = elmd.getMethodNameForCallbackClass(callbackClass.getName());
                        if (methodName != null)
                        {
                            // Separate listener class taking the PC object as input
                            Object listener = listenerClass.newInstance();
                            invokeCallbackMethod(listener, methodName, pc, clr);
                        }
                    }
                }
            }

            // Class listeners for this class
            List<String> entityMethodsToInvoke = null;
            while (acmd != null)
            {
                List<EventListenerMetaData> listenerMetaData = acmd.getListeners();
                if (listenerMetaData != null && listenerMetaData.size() > 0)
                {
                    // Class has listeners so go through them in the same order
                    Iterator<EventListenerMetaData> listenerIter = listenerMetaData.iterator();
                    while (listenerIter.hasNext())
                    {
                        EventListenerMetaData elmd = listenerIter.next();
                        Class listenerClass = clr.classForName(elmd.getClassName());
                        String methodName = elmd.getMethodNameForCallbackClass(callbackClass.getName());
                        if (methodName != null)
                        {
                            if (elmd.getClassName().equals(acmd.getFullClassName()))
                            {
                                // Class itself is the listener
                                if (entityMethodsToInvoke == null)
                                {
                                    entityMethodsToInvoke = new ArrayList<String>();
                                }
                                if (!entityMethodsToInvoke.contains(methodName))
                                {
                                    // Only add the method if is not already present (allows for inherited listener methods)
                                    entityMethodsToInvoke.add(methodName);
                                }
                            }
                            else
                            {
                                // Separate listener class taking the PC object as input
                                Object listener = listenerClass.newInstance();
                                invokeCallbackMethod(listener, methodName, pc, clr);
                            }
                        }
                    }
                    if (acmd.isExcludeSuperClassListeners())
                    {
                        break;
                    }
                }

                // Move up to superclass
                acmd = acmd.getSuperAbstractClassMetaData();
            }

            if (entityMethodsToInvoke != null && entityMethodsToInvoke.size() > 0)
            {
                // Invoke all listener methods on the entity
View Full Code Here

    public Object find(Class entityClass, Object primaryKey)
    {
        assertIsOpen();
        assertEntity(entityClass);

        AbstractClassMetaData acmd = om.getMetaDataManager().getMetaDataForClass(entityClass, om.getClassLoaderResolver());
        if (acmd == null)
        {
            throw new EntityNotFoundException();
        }
        Object pc;
        try
        {
            if (acmd.getObjectidClass().equals(primaryKey.getClass().getName()))
            {
                pc = om.findObject(primaryKey, true, true, null);
            }
            else
            {
View Full Code Here

                pushStack(mmd);
            }
            else if (localName.equals("basic"))
            {
                // Basic field
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);

                pushStack(mmd);
            }
            else if (localName.equals("lob"))
            {
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                fmd.setStoreInLob(); // Just mark it as to be stored in a "lob" and let the MetaData sort it out
            }
            else if (localName.equals("enumerated"))
            {
                // Processed elsewhere
            }
            else if (localName.equals("temporal"))
            {
                // Processed elsewhere
            }
            else if (localName.equals("transient"))
            {
                // Transient field
                ClassMetaData cmd = (ClassMetaData)getStack();
                AbstractMemberMetaData mmd = newTransientFieldObject(cmd, getAttr(attrs, "name"));
                cmd.addMember(mmd);

                pushStack(mmd);
            }
            else if (localName.equals("one-to-many"))
            {
                // 1-N field
                ClassMetaData cmd = (ClassMetaData)getStack();
                AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);
                mmd.setTargetClassName(getAttr(attrs, "target-entity"));
                mmd.setOrdered();
                if (mmd.getMappedBy() == null && mmd.getJoinMetaData() == null &&
                    !mgr.getOMFContext().getPersistenceConfiguration().getBooleanProperty("org.jpox.jpa.oneToManyUniFkRelations"))
                {
                    // Strict JPA : 1-N uni with no join specified (yet) so add one (see JPA spec [9.1.24])
                    mmd.setJoinMetaData(new JoinMetaData(mmd, null, null, null, null, null, null, null, null));
                }

                pushStack(mmd);
            }
            else if (localName.equals("one-to-one"))
            {
                // 1-1 field
                ClassMetaData cmd = (ClassMetaData)getStack();
                AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);
                mmd.setTargetClassName(getAttr(attrs, "target-entity"));

                pushStack(mmd);
            }
            else if (localName.equals("many-to-one"))
            {
                // N-1 field
                ClassMetaData cmd = (ClassMetaData)getStack();
                AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);
                mmd.setTargetClassName(getAttr(attrs, "target-entity"));

                pushStack(mmd);
            }
            else if (localName.equals("many-to-many"))
            {
                // M-N field
                ClassMetaData cmd = (ClassMetaData)getStack();
                AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);
                mmd.setTargetClassName(getAttr(attrs, "target-entity"));
                mmd.setOrdered();
                if (mmd.getMappedBy() == null && mmd.getJoinMetaData() == null)
                {
                    // M-N and no join specified (yet) so add one
                    mmd.setJoinMetaData(new JoinMetaData(mmd, null, null, null, null, null, null, null, null));
                }

                pushStack(mmd);
            }
            else if (localName.equals("map-key"))
            {
                // Key of a Map (field/property of the value class)
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                String mappedByFieldName = getAttr(attrs, "name");
                if (StringUtils.isWhitespace(mappedByFieldName))
                {
                    mappedByFieldName = "#PK"; // Special value understood by MapMetaData.populate()
                }
                KeyMetaData keymd = new KeyMetaData(fmd, null, null, null, null, null, mappedByFieldName);
                fmd.setKeyMetaData(keymd);
            }
            else if (localName.equals("order-by"))
            {
                // Processed in endElement()
            }
            else if (localName.equals("cascade"))
            {
                // Do nothing
            }
            else if (localName.equals("cascade-type"))
            {
                // Handled in elements below
            }
            else if (localName.equals("cascade-all"))
            {
                AbstractMemberMetaData mmd = (AbstractMemberMetaData)getStack();
                mmd.setCascadePersist(true);
                mmd.setCascadeUpdate(true);
                mmd.setCascadeDelete(true);
                mmd.setCascadeRefresh(true);
            }
            else if (localName.equals("cascade-persist"))
            {
                MetaData md = getStack();
                if (md instanceof AbstractMemberMetaData)
                {
                    AbstractMemberMetaData mmd = (AbstractMemberMetaData)md;
                    mmd.setCascadePersist(true);
                }
                else if (md instanceof FileMetaData)
                {
                    // Specified at <persistence-unit-defaults>
                    defaultCascadePersist = true;
                }
            }
            else if (localName.equals("cascade-merge"))
            {
                AbstractMemberMetaData mmd = (AbstractMemberMetaData)getStack();
                mmd.setCascadeUpdate(true);
            }
            else if (localName.equals("cascade-remove"))
            {
                AbstractMemberMetaData mmd = (AbstractMemberMetaData)getStack();
                mmd.setCascadeDelete(true);
            }
            else if (localName.equals("cascade-refresh"))
            {
                AbstractMemberMetaData mmd = (AbstractMemberMetaData)getStack();
                mmd.setCascadeRefresh(true);
            }
            else if (localName.equals("version"))
            {
                if (getStack() instanceof ClassMetaData)
                {
                    // Version field
                    ClassMetaData cmd = (ClassMetaData)getStack();
                    AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);

                    // Tag this field as the version field
                    VersionMetaData vermd = new VersionMetaData(VersionStrategy.VERSION_NUMBER.toString(), mmd.getName());
                    cmd.setVersionMetaData(vermd);

                    pushStack(mmd);
                }
            }
            else if (localName.equals("discriminator-value"))
            {
                // Processed in endElement()
            }
            else if (localName.equals("discriminator-column"))
            {
                ClassMetaData cmd = (ClassMetaData)getStack();
                InheritanceMetaData inhmd = cmd.getInheritanceMetaData();
                if (inhmd == null)
                {
                    // Add an empty inheritance specification
                    inhmd = new InheritanceMetaData(cmd, null);
                    cmd.setInheritanceMetaData(inhmd);
                }
                DiscriminatorMetaData discmd = inhmd.getDiscriminatorMetaData();
                if (discmd == null)
                {
                    // User hasnt specified discriminator value so use "provider-specific function" (JPA spec 9.1.3.1) - what a joke spec
                    discmd = new DiscriminatorMetaData(inhmd, null, null, DiscriminatorStrategy.CLASS_NAME.toString(), "true");
                    inhmd.setDiscriminatorMetaData(discmd);
                }
                String jdbcType = null;
                String discType = getAttr(attrs, "discriminator-type");
                if (discType != null)
                {
                    if (discType.equalsIgnoreCase("STRING"))
                    {
                        jdbcType = "VARCHAR";
                    }
                    else if (discType.equalsIgnoreCase("CHAR"))
                    {
                        jdbcType = "CHAR";
                    }
                    else if (discType.equalsIgnoreCase("INTEGER"))
                    {
                        jdbcType = "INTEGER";
                    }
                }
                ColumnMetaData colmd = new ColumnMetaData(discmd, getAttr(attrs, "name"), null, null, jdbcType, null, getAttr(attrs, "length"),
                    null, null, null, null, null, null, null);
                discmd.setColumnMetaData(colmd);
            }
            else if (localName.equals("generated-value"))
            {
                // generated value for this field
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                IdentityStrategy idStrategy = IdentityStrategy.getIdentityStrategy(getAttr(attrs, "strategy"));
                fmd.setValueStrategy(idStrategy);
                fmd.setValueGeneratorName(getAttr(attrs, "generator"));
            }
            else if (localName.equals("join-table"))
            {
                // Join table for this field
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                JoinMetaData joinmd = new JoinMetaData(fmd, null, null, null, null, null, null, null, null);
                String tableName = getAttr(attrs, "name");
                String schemaName = getAttr(attrs, "schema");
                String catalogName = getAttr(attrs, "catalog");

                fmd.setJoinMetaData(joinmd);
                if (!StringUtils.isWhitespace(tableName))
                {
                    fmd.setTable(tableName);
                }
                if (!StringUtils.isWhitespace(schemaName))
                {
                    fmd.setSchema(schemaName);
                }
                if (!StringUtils.isWhitespace(catalogName))
                {
                    fmd.setSchema(catalogName);
                }
                pushStack(joinmd);
            }
            else if (localName.equals("column"))
            {
                // Column for the current field
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                ColumnMetaData colmd =
                    new ColumnMetaData(fmd,
                        getAttr(attrs, "name"),
                        null, // No "target" in JPA
                        null, // No "target-field" in JPA
                        null, // No "jdbc-type" in JPA (use Lob, Basic, Temporal to decide this)
                        null, // No "sql-type" in JPA
                        getAttr(attrs, "length"), // Should use precision if floating point type
                        getAttr(attrs, "scale"),
                        getAttr(attrs, "nullable"),
                        null, // No default-value in JPA
                        null, // No insert-value in JPA
                        getAttr(attrs, "insertable"),
                        getAttr(attrs, "updatable"),
                        getAttr(attrs, "unique"));
                fmd.addColumn(colmd);
                String table = getAttr(attrs, "table");
                if (!StringUtils.isWhitespace(table))
                {
                  // Using secondary table
                  fmd.setTable(table);
                }
            }
            else if (localName.equals("join-column"))
            {
              MetaData md = getStack();
              if (md instanceof JoinMetaData)
              {
                JoinMetaData joinmd = (JoinMetaData)md;
                    ColumnMetaData colmd =
                        new ColumnMetaData(joinmd,
                            getAttr(attrs, "name"),
                            getAttr(attrs, "referenced-column-name"),
                            null,
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            getAttr(attrs, "nullable"),
                            null, // No default-value in JPA
                            null, // No insert-value in JPA
                            getAttr(attrs, "insertable"),
                            getAttr(attrs, "updatable"),
                            getAttr(attrs, "unique"));
                joinmd.addColumn(colmd);
              }
              else if (md instanceof AbstractMemberMetaData)
              {
                // N-1, 1-1, 1-N (FK). Just set <column> for the field. Is this correct for 1-N FK ?
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)md;
                ColumnMetaData colmd =
                        new ColumnMetaData(fmd,
                            getAttr(attrs, "name"),
                            getAttr(attrs, "referenced-column-name"),
                            null,
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            getAttr(attrs, "nullable"),
                            null, // No default-value in JPA
                            null, // No insert-value in JPA
                            getAttr(attrs, "insertable"),
                            getAttr(attrs, "updatable"),
                            getAttr(attrs, "unique"));
                fmd.addColumn(colmd);
              }
            }
            else if (localName.equals("inverse-join-column"))
            {
              MetaData md = getStack();
              if (md instanceof JoinMetaData)
              {
                // Join table column that is FK to the element table
                JoinMetaData joinmd = (JoinMetaData)md;
                ElementMetaData elemmd = null;
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)joinmd.getParent();
                if (fmd.getElementMetaData() != null)
                {
                  elemmd = fmd.getElementMetaData();
                }
                else
                {
                  elemmd = new ElementMetaData(fmd, null, null, null, null, null, null);
                  fmd.setElementMetaData(elemmd);
                }
                ColumnMetaData colmd =
                        new ColumnMetaData(elemmd,
                            getAttr(attrs, "name"),
                            getAttr(attrs, "referenced-column-name"),
                            null,
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            null, // Copy referenced column
                            getAttr(attrs, "nullable"),
                            null, // No default-value in JPA
                            null, // No insert-value in JPA
                            getAttr(attrs, "insertable"),
                            getAttr(attrs, "updatable"),
                            getAttr(attrs, "unique"));
                elemmd.addColumn(colmd);
              }
            }
            else if (localName.equals("unique-constraint"))
            {
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Unique constraint on primary table
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    UniqueMetaData unimd = new UniqueMetaData(null, cmd.getTable(), null); // Columns are in subelement
                    cmd.addUniqueConstraint(unimd);
                    pushStack(unimd);
                }
                else if (md instanceof JoinMetaData)
                {
                    // Unique constraint on secondary table or join table
                    JoinMetaData joinmd = (JoinMetaData)md;
                    UniqueMetaData unimd = new UniqueMetaData(null, null, null);
                    joinmd.setUniqueMetaData(unimd);
                    pushStack(unimd);
                }
            }
            else if (localName.equals("entity-listeners"))
            {
                // Nothing to add at this point
            }
            else if (localName.equals("entity-listener"))
            {
                MetaData md = getStack();
                EventListenerMetaData elmd = new EventListenerMetaData(getAttr(attrs, "class"));
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    ((AbstractClassMetaData)md).addListener(elmd);
                }
                else if (md instanceof FileMetaData)
                {
                    // Specified at <persistence-unit-defaults>
                    ((FileMetaData)md).addListener(elmd);
                }

                pushStack(elmd);
            }
            else if (localName.equals("pre-persist"))
            {
                // Pre-create callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PrePersist", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PrePersist", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("post-persist"))
            {
                // Post-create callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PostPersist", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PostPersist", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("pre-remove"))
            {
                // Pre-delete callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PreRemove", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PreRemove", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("post-remove"))
            {
                // Post-delete callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PostRemove", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PostRemove", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("pre-update"))
            {
                // Pre-store callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PreUpdate", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PreUpdate", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("post-update"))
            {
                // Post-store callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PostUpdate", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PostUpdate", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("post-load"))
            {
                // Post-load callback
                MetaData md = getStack();
                if (md instanceof AbstractClassMetaData)
                {
                    // Specified at <entity> or <mapped-superclass>
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    EventListenerMetaData elmd = cmd.getListenerForClass(cmd.getFullClassName());
                    if (elmd == null)
                    {
                        elmd = new EventListenerMetaData(cmd.getFullClassName());
                        cmd.addListener(elmd);
                    }
                    elmd.addCallback("javax.persistence.PostLoad", getAttr(attrs, "method-name"));
                }
                else
                {
                    // Specified at <entity-listener>
                    EventListenerMetaData elmd = (EventListenerMetaData)md;
                    elmd.addCallback("javax.persistence.PostLoad", getAttr(attrs, "method-name"));
                }
            }
            else if (localName.equals("attribute-override"))
            {
                // Override columns for a superclass field
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                AbstractMemberMetaData fmd = newOverriddenFieldObject(cmd, attrs);
                cmd.addMember(fmd);
                pushStack(fmd);
            }
            else if (localName.equals("association-override"))
            {
                // Override columns for a superclass field
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                AbstractMemberMetaData fmd = newOverriddenFieldObject(cmd, attrs);
                cmd.addMember(fmd);
                pushStack(fmd);
            }
            else if (localName.equals("exclude-default-listeners"))
            {
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                cmd.excludeDefaultListeners();
            }
            else if (localName.equals("exclude-superclass-listeners"))
            {
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                cmd.excludeSuperClassListeners();
            }
            else
            {
                String message = LOCALISER.msg("044037", qName);
                JPOXLogger.METADATA.error(message);
View Full Code Here

        {
            return null;
        }

        // Check for MetaData loaded when we loaded the "persistence-unit"
        AbstractClassMetaData acmd = (AbstractClassMetaData)classMetaDataByClass.get(c.getName());
        if (acmd != null)
        {
            return acmd;
        }
View Full Code Here

        this.typeName = typeName;
        this.objectType = objectType;

        // Find the MetaData for the embedded PC class
        MetaDataManager mmgr = fmd.getAbstractClassMetaData().getMetaDataManager();
        AbstractClassMetaData pcCmd = mmgr.getMetaDataForClass(typeName, clr);
        if (pcCmd == null)
        {
            // Not found so must be an interface
            if (fmd != null)
            {
                // Try using the fieldTypes on the field/property - we support it if only 1 implementation
                String[] fieldTypes = fmd.getFieldTypes();
                if (fieldTypes != null && fieldTypes.length == 1)
                {
                    pcCmd = mmgr.getMetaDataForClass(fieldTypes[0], clr);
                }
                else if (fieldTypes != null && fieldTypes.length > 1)
                {
                    // TODO Cater for multiple implementations
                    throw new JPOXUserException("Field " + fmd.getFullFieldName() + " is a reference field that is embedded. " +
                        "JPOX doesnt support embedded reference fields that have more than 1 implementation");
                }
            }

            if (pcCmd == null)
            {
                // Try a persistent interface
                pcCmd = mmgr.getMetaDataForInterface(clr.classForName(typeName), clr);
                if (pcCmd == null && fmd.getFieldTypes() != null && fmd.getFieldTypes().length == 1)
                {
                    // No MetaData for the type so try "fieldType" specified on the field
                    pcCmd = mmgr.getMetaDataForInterface(clr.classForName(fmd.getFieldTypes()[0]), clr);
                }
            }
        }

        embCmd = pcCmd;

        AbstractMemberMetaData[] embFmds;
        if (emd == null && pcCmd.isEmbeddedOnly())
        {
            // No <embedded> block yet the class is defined as embedded-only so just use its own definition of fields
            embFmds = pcCmd.getManagedMembers();
        }
        else
        {
            // <embedded> block so use those field definitions
            embFmds = emd.getFieldMetaData();
        }

        // Add all fields of the embedded class (that are persistent)
        int[] pcFieldNumbers = pcCmd.getAllMemberPositions();
        for (int i=0;i<pcFieldNumbers.length;i++)
        {
            AbstractMemberMetaData pcFmd = pcCmd.getMetaDataForManagedMemberAtAbsolutePosition(pcFieldNumbers[i]);
            if (pcFmd.getPersistenceModifier() == FieldPersistenceModifier.PERSISTENT)
            {
                if (emd != null && emd.getOwnerMember() != null && emd.getOwnerMember().equals(pcFmd.getName()))
                {
                    // Do nothing since we dont map owner fields (since the owner is the containing object)
                }
                else
                {
                    AbstractMemberMetaData embeddedFmd = null;
                    for (int j=0;j<embFmds.length;j++)
                    {
                        // Why are these even possible ? Why are they here ? Why dont they use localised messages ?
                        if (embFmds[j] == null)
                        {
                            throw new RuntimeException("embFmds[j] is null"+pcCmd.toString()+" type "+ typeName);
                        }
                        if (pcCmd.getMetaDataForMember(embFmds[j].getName()) == null)
                        {
                            throw new RuntimeException("pcCmd.getField(embFmds[j].getName()) is null"+
                                pcCmd.toString()+" type "+ typeName+" embFmds[j].getName() "+embFmds[j].getName());
                        }

                        // Check if this field has a mapping in the embedded specification
                        if (pcCmd.getMetaDataForMember(embFmds[j].getName()).getAbsoluteFieldNumber() == pcFieldNumbers[i])
                        {
                            embeddedFmd = embFmds[j];
                            break;
                        }
                    }
View Full Code Here

            {
                throw new JPOXException(LOCALISER.msg("041016",
                    value.getClass(), value)).setFatal();
            }

            AbstractClassMetaData embCmd = om.getMetaDataManager().getMetaDataForClass(value.getClass().getName(), om.getClassLoaderResolver());
            StateManager embSM = om.findStateManager(value);
            if (embSM == null || ObjectManagerHelper.getObjectManager(value) == null)
            {
                // Assign a StateManager to manage our embedded object
                embSM = StateManagerFactory.newStateManagerForEmbedded(om, value, false);
                embSM.addEmbeddedOwner(ownerSM, ownerFieldNumber);
                embSM.setPcObjectType(objectType);
            }

            int n = 0;
            for (int i=0; i<javaTypeMappings.size(); i++)
            {
                JavaTypeMapping mapping = ((JavaTypeMapping)javaTypeMappings.get(i));
                int[] posMapping = new int[mapping.getNumberOfDatastoreFields()];
                for (int j=0; j<posMapping.length; j++)
                {
                    posMapping[j] = param[n++];
                }

                // Retrieve value of field from Embedded StateManager
                int embAbsFieldNum = embCmd.getAbsolutePositionOfMember(mapping.getFieldMetaData().getName());
                Object fieldValue = embSM.provideField(embAbsFieldNum);

                if (mapping instanceof EmbeddedPCMapping)
                {
                    mapping.setObject(om, ps, posMapping, fieldValue, embSM, embAbsFieldNum);
View Full Code Here

                    return te.newFieldExpression(fieldName + "." + subfieldName);
                }
            }
            else if (mapping instanceof PersistenceCapableMapping || mapping instanceof ReferenceMapping)
            {
                AbstractClassMetaData otherCmd = qs.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForClass(
                    mapping.getType(), clr);
                if (otherCmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.SUBCLASS_TABLE)
                {
                    // Field is a PC class that uses "subclass-table" inheritance strategy (and so has multiple possible tables to join to)
                    AbstractClassMetaData[] cmds = qs.getStoreManager().getClassesManagingTableForClass(otherCmd, clr);
                    if (cmds != null)
                    {
                        // Join to the first table
                        // TODO Allow for all possible tables. Can we do an OR of the tables ? How ?
                        if (cmds.length > 1)
                        {
                            JPOXLogger.QUERY.warn(LOCALISER.msg("037006",
                                mapping.getFieldMetaData().getFullFieldName(), cmds[0].getFullClassName()));
                        }
                        table = qs.getStoreManager().getDatastoreClass(cmds[0].getFullClassName(), clr);
                    }
                    else
                    {
                        // No subclasses with tables to join to, so throw a user error
                        throw new JPOXUserException(LOCALISER.msg("037005",
                            mapping.getFieldMetaData().getFullFieldName()));
                    }
                }
                else
                {
                    // Class of the field will have its own table
                    table = qs.getStoreManager().getDatastoreClass(mapping.getType(), clr);
                    ApiAdapter api = qs.getStoreManager().getApiAdapter();

                    if (fieldName != null && subfieldName != null)
                    {
                        AbstractMemberMetaData subfieldMetaData = otherCmd.getMetaDataForMember(subfieldName);
                        if (subfieldMetaData != null && subfieldMetaData.isPrimaryKey() &&
                            !api.isPersistable(subfieldMetaData.getType()))
                        {
                            // Selecting a non-PC field in the other class that is part of its PK mapping (so we have a column here for it)
                            // Avoids the extra join to the other table
View Full Code Here

                    return new BooleanLiteral(qs, mapping, true).eq(new BooleanLiteral(qs, mapping, true));
                }
                else if (mapping instanceof PersistenceCapableMapping || mapping instanceof ReferenceMapping)
                {
                    // Field has its own table, so join to it
                    AbstractClassMetaData fieldCmd = qs.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForClass(
                        mapping.getType(), clr);
                    if (fieldCmd.getInheritanceMetaData().getStrategyValue() == InheritanceStrategy.SUBCLASS_TABLE)
                    {
                        // Field is a PC class that uses "subclass-table" inheritance strategy (and so has multiple possible tables to join to)
                        AbstractClassMetaData[] cmds = qs.getStoreManager().getClassesManagingTableForClass(fieldCmd, clr);
                        if (cmds != null)
                        {
View Full Code Here

        // Default to the "class-name" discriminator strategy
        String discriminatorValue = className;
        if (dismd.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
        {
            // Get the MetaData for the target class since that holds the "value"
            AbstractClassMetaData targetCmd = stmt.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForClass(className, stmt.getClassLoaderResolver());
            discriminatorValue = targetCmd.getInheritanceMetaData().getDiscriminatorMetaData().getValue();
        }
        ScalarExpression discrExpr = discriminatorMapping.newScalarExpression(stmt, tableExpr);
        ScalarExpression discrVal = discriminatorMapping.newLiteral(stmt, discriminatorValue);
        return discrExpr.eq(discrVal);
    }
View Full Code Here

TOP

Related Classes of org.jpox.metadata.AbstractClassMetaData

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.