Package org.datanucleus.metadata

Examples of org.datanucleus.metadata.VersionMetaData


                    DiscriminatorMetaData dismd = (DiscriminatorMetaData)md;
                    dismd.setColumnMetaData(colmd);
                }
                else if (md instanceof VersionMetaData)
                {
                    VersionMetaData vermd = (VersionMetaData)md;
                    vermd.addColumn(colmd);
                }
                else if (md instanceof AbstractClassMetaData)
                {
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    cmd.addUnmappedColumn(colmd);
                }
                else if (md instanceof PrimaryKeyMetaData)
                {
                    PrimaryKeyMetaData pkmd = (PrimaryKeyMetaData)md;
                    pkmd.addColumn(colmd);
                }
                pushStack(colmd);
            }
            else if (localName.equals("element"))
            {
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                ElementMetaData elemmd = new ElementMetaData();
                elemmd.setColumnName(getAttr(attrs, "column"));
                elemmd.setDeleteAction(getAttr(attrs, "delete-action"));
                elemmd.setUpdateAction(getAttr(attrs, "update-action"));
                elemmd.setIndexed(IndexedValue.getIndexedValue(getAttr(attrs, "indexed")));
                elemmd.setUnique(getAttr(attrs, "unique"));
                String mappedBy = getAttr(attrs, "mapped-by");
                elemmd.setMappedBy(mappedBy);
                if (!StringUtils.isWhitespace(mappedBy) && fmd.getMappedBy() == null)
                {
                    // With collection/array this is the same as mapped-by on the field
                    fmd.setMappedBy(mappedBy);
                }
                fmd.setElementMetaData(elemmd);
                pushStack(elemmd);
            }
            else if (localName.equals("key"))
            {
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                KeyMetaData keymd = new KeyMetaData();
                keymd.setColumnName(getAttr(attrs, "column"));
                keymd.setDeleteAction(getAttr(attrs, "delete-action"));
                keymd.setUpdateAction(getAttr(attrs, "update-action"));
                keymd.setIndexed(IndexedValue.getIndexedValue(getAttr(attrs, "indexed")));
                keymd.setUnique(getAttr(attrs, "unique"));
                keymd.setMappedBy(getAttr(attrs, "mapped-by"));
                fmd.setKeyMetaData(keymd);
                pushStack(keymd);
            }
            // New value
            else if (localName.equals("value"))
            {
                AbstractMemberMetaData fmd = (AbstractMemberMetaData)getStack();
                ValueMetaData valuemd = new ValueMetaData();
                valuemd.setColumnName(getAttr(attrs, "column"));
                valuemd.setDeleteAction(getAttr(attrs, "delete-action"));
                valuemd.setUpdateAction(getAttr(attrs, "update-action"));
                valuemd.setIndexed(IndexedValue.getIndexedValue(getAttr(attrs, "indexed")));
                valuemd.setUnique(getAttr(attrs, "unique"));
                valuemd.setMappedBy(getAttr(attrs, "mapped-by"));
                fmd.setValueMetaData(valuemd);
                pushStack(valuemd);
            }
            // New fetch-group
            else if (localName.equals("fetch-group"))
            {
                MetaData md = getStack();
                FetchGroupMetaData fgmd = new FetchGroupMetaData(getAttr(attrs,"name"));
                fgmd.setPostLoad(getAttr(attrs,"post-load"));
                if (md instanceof FetchGroupMetaData)
                {
                    FetchGroupMetaData fgmdParent = (FetchGroupMetaData)md;
                    fgmdParent.addFetchGroup(fgmd);
                }
                else if (md instanceof AbstractClassMetaData)
                {
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    cmd.addFetchGroup(fgmd);
                }
                else if (md instanceof FetchPlanMetaData)
                {
                    FetchPlanMetaData fpmd = (FetchPlanMetaData)md;
                    fpmd.addFetchGroup(fgmd);
                }
                pushStack(fgmd);
            }
            else if (localName.equals("extension"))
            {
                MetaData md = getStack();
                md.addExtension(getAttr(attrs,"vendor-name"), getAttr(attrs,"key"), getAttr(attrs,"value"));
            }
            else if (localName.equals("version"))
            {
                AbstractClassMetaData cmd = (AbstractClassMetaData)getStack();
                VersionMetaData vermd = cmd.newVersionMetadata();
                vermd.setStrategy(getAttr(attrs,"strategy")).setColumnName(getAttr(attrs,"column"));
                vermd.setIndexed(IndexedValue.getIndexedValue(getAttr(attrs,"indexed")));
                pushStack(vermd);
            }
            else if (localName.equals("index"))
            {
                MetaData md = getStack();
                IndexMetaData idxmd = new IndexMetaData();
                idxmd.setName(getAttr(attrs,"name"));
                idxmd.setTable(getAttr(attrs,"table"));
                idxmd.setUnique(getAttr(attrs,"unique"));
                if (md instanceof AbstractClassMetaData)
                {
                    AbstractClassMetaData cmd = (AbstractClassMetaData)md;
                    cmd.addIndex(idxmd);
                }
                else if (md instanceof AbstractMemberMetaData)
                {
                    AbstractMemberMetaData fmd = (AbstractMemberMetaData)md;
                    fmd.setIndexMetaData(idxmd);
                }
                else if (md instanceof JoinMetaData)
                {
                    JoinMetaData jmd = (JoinMetaData)md;
                    jmd.setIndexMetaData(idxmd);
                }
                else if (md instanceof AbstractElementMetaData)
                {
                    AbstractElementMetaData elmd = (AbstractElementMetaData)md;
                    elmd.setIndexMetaData(idxmd);
                }
                else if (md instanceof OrderMetaData)
                {
                    OrderMetaData omd = (OrderMetaData)md;
                    omd.setIndexMetaData(idxmd);
                }
                else if (md instanceof VersionMetaData)
                {
                    VersionMetaData vermd = (VersionMetaData)md;
                    vermd.setIndexMetaData(idxmd);
                }
                else if (md instanceof DiscriminatorMetaData)
                {
                    DiscriminatorMetaData dismd = (DiscriminatorMetaData)md;
                    dismd.setIndexMetaData(idxmd);
View Full Code Here


                // Class is persistable so process remaining annotations
                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 cacheable = 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++)
                {
                    HashMap<String, Object> annotationValues = annotations[i].getNameValueMap();
                    String annName = annotations[i].getName();
                    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();
                        vermd.setStrategy(strategy);
                        vermd.setColumnName(column);
                        vermd.setIndexed(IndexedValue.getIndexedValue(indexed));
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd =
                                JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(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 an 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();
                        idmd.setColumnName(column);
                        idmd.setValueStrategy(IdentityStrategy.getIdentityStrategy(strategy));
                        idmd.setSequence(sequence);
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd =
                                JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(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();
                        pkmd.setName(pkName);
                        pkmd.setColumnName(pkColumn);
                        if (columns != null && columns.length > 0)
                        {
                            for (int j=0;j<columns.length;j++)
                            {
                                pkmd.addColumn(JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(columns[j]));
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.JOINS))
                    {
                        if (joins != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044210", 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++)
                            {
                                joins[j] = new JoinMetaData();
                                joins[j].setTable(js[j].table());
                                joins[j].setColumnName(js[j].column());
                                joins[j].setIndexed(IndexedValue.getIndexedValue(js[j].indexed()));
                                joins[j].setOuter(js[j].outer());
                                joins[j].setUnique(js[j].unique());
                                joins[j].setDeleteAction(JDOAnnotationUtils.getForeignKeyActionString(js[j].deleteAction()));
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.JOIN))
                    {
                        if (joins != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044210", cmd.getFullClassName()));
                        }
                        joins = new JoinMetaData[1];
                        joins[0] = new JoinMetaData();
                        joins[0].setTable((String)annotationValues.get("table"));
                        joins[0].setColumnName((String)annotationValues.get("column"));
                        joins[0].setIndexed(IndexedValue.getIndexedValue((String)annotationValues.get("indexed")));
                        joins[0].setOuter((String)annotationValues.get("outer"));
                        joins[0].setUnique((String)annotationValues.get("unique"));
                        joins[0].setDeleteAction(((ForeignKeyAction)annotationValues.get("deleteAction")).toString());
                        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 an extension strategy
                            strategy = customStrategy;
                        }
                        inhmd = new InheritanceMetaData();
                        inhmd.setStrategy(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();
                        dismd.setColumnName(column);
                        dismd.setValue(value);
                        dismd.setStrategy(strategy);
                        dismd.setIndexed(indexed);
                        if (columns != null && columns.length > 0)
                        {
                            // Only use the first column
                            ColumnMetaData colmd =
                                JDOAnnotationUtils.getColumnMetaDataForColumnAnnotation(columns[0]);
                            dismd.setColumnMetaData(colmd);
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.QUERIES))
                    {
                        if (queries != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044209", 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(qs[j].name());
                            queries[j].setScope(cls.getName());
                            queries[j].setLanguage(lang);
                            queries[j].setUnmodifiable(qs[j].unmodifiable());
                            queries[j].setResultClass(resultClassName);
                            queries[j].setUnique(qs[j].unique());
                            queries[j].setFetchPlanName(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)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044209", 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((String)annotationValues.get("name"));
                        queries[0].setScope(cls.getName());
                        queries[0].setLanguage(lang);
                        queries[0].setUnmodifiable(unmodifiable);
                        queries[0].setResultClass(resultClassName);
                        queries[0].setUnique((String)annotationValues.get("unique"));
                        queries[0].setFetchPlanName((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)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044207", 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(plans[j].name());
                            fetchPlans[j].setMaxFetchDepth(plans[j].maxFetchDepth());
                            fetchPlans[j].setFetchSize(plans[j].fetchSize());
                            int numGroups = plans[j].fetchGroups().length;
                            for (int k=0;k<numGroups;k++)
                            {
                                FetchGroupMetaData fgmd = new FetchGroupMetaData(plans[j].fetchGroups()[k]);
                                fetchPlans[j].addFetchGroup(fgmd);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHPLAN))
                    {
                        if (fetchPlans != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044207", cmd.getFullClassName()));
                        }
                        fetchPlans = new FetchPlanMetaData[1];
                        int maxFetchDepth = ((Integer)annotationValues.get("maxFetchDepth")).intValue();
                        int fetchSize = ((Integer)annotationValues.get("fetchSize")).intValue();
                        fetchPlans[0] = new FetchPlanMetaData((String)annotationValues.get("name"));
                        fetchPlans[0].setMaxFetchDepth(maxFetchDepth);
                        fetchPlans[0].setFetchSize(fetchSize);
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHGROUPS))
                    {
                        if (fetchGroups != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044208", 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(groups[j].name());
                            fetchGroups[j].setPostLoad(groups[j].postLoad());
                            int numFields = groups[j].members().length;
                            for (int k=0;k<numFields;k++)
                            {
                                FieldMetaData fmd = new FieldMetaData(fetchGroups[j],
                                    groups[j].members()[k].name());
                                fmd.setRecursionDepth(groups[j].members()[k].recursionDepth());
                                fetchGroups[j].addMember(fmd);
                            }
                            int numGroups = groups[j].fetchGroups().length;
                            for (int k=0;k<numGroups;k++)
                            {
                                FetchGroupMetaData subgrp = new FetchGroupMetaData(groups[j].fetchGroups()[k]);
                                fetchGroups[j].addFetchGroup(subgrp);
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.FETCHGROUP))
                    {
                        if (fetchGroups != null)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044208", cmd.getFullClassName()));
                        }
                        fetchGroups = new FetchGroupMetaData[1];
                        fetchGroups[0] = new FetchGroupMetaData((String)annotationValues.get("name"));
                        fetchGroups[0].setPostLoad((String)annotationValues.get("postLoad"));
                        Persistent[] fields = (Persistent[])annotationValues.get("members");
                        if (fields != null)
                        {
                            for (int j=0;j<fields.length;j++)
                            {
                                FieldMetaData fmd = new FieldMetaData(fetchGroups[0],
                                    fields[j].name());
                                fmd.setRecursionDepth(fields[j].recursionDepth());
                                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();
                        }
                        Integer seqSize = (Integer)annotationValues.get("allocationSize");
                        Integer seqStart = (Integer)annotationValues.get("initialValue");

                        seqmd = new SequenceMetaData(seqName, seqStrategy);
                        seqmd.setFactoryClass(seqFactoryClassName);
                        seqmd.setDatastoreSequence(seqSeq);
                        if (seqSize != null)
                        {
                            seqmd.setAllocationSize(seqSize);
                        }
                        if (seqStart != null)
                        {
                            seqmd.setInitialValue(seqStart);
                        }
                        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)
                                {
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("044204", 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)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044204", 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)
                                {
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("044205", 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)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044205", 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].updateAction());
                                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)
                                {
                                    NucleusLogger.METADATA.warn(LOCALISER.msg("044206", 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)
                        {
                            NucleusLogger.METADATA.warn(LOCALISER.msg("044206", 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.getColumnMetaDataForColumnAnnotation(cols[j]);
                                JDOAnnotationUtils.addExtensionsToMetaData(unmappedColumns[j], cols[j].extensions());
                            }
                        }
                    }
                    else if (annName.equals(JDOAnnotationUtils.CACHEABLE))
                    {
                        String cache = (String)annotationValues.get("value");
                        if (cache != null)
                        {
                            cacheable = cache;
                        }
                    }
                    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
                    {
                        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)
                {
View Full Code Here

            JavaTypeMapping delegate)
    {
        initialize(datastoreContainer.getStoreManager(), delegate.getType());
        this.delegate = delegate;
        this.datastoreContainer = datastoreContainer;
        VersionMetaData vermd = datastoreContainer.getVersionMetaData();

        // Currently we only use a single column mapping for versioning.
        // The MetaData supports multiple columns and so we could extend this in the future
        // to use all MetaData information.
        ColumnMetaData versionColumnMetaData = vermd.getColumnMetaData();
        ColumnMetaData colmd;
        IdentifierFactory idFactory = datastoreContainer.getStoreManager().getIdentifierFactory();
        DatastoreIdentifier id = null;
        if (versionColumnMetaData == null)
        {
View Full Code Here

    }

    op.replaceFields(fieldNumbers, new FetchFieldManager(op, entity, fieldNumbers));

    // Refresh version in case not yet set (e.g created HOLLOW object, and this is first fetch)
    VersionMetaData vmd = cmd.getVersionMetaDataForClass();
    if (cmd.isVersioned()) {
      Object versionValue = entity.getProperty(EntityUtils.getVersionPropertyName(datastoreMgr.getIdentifierFactory(), vmd));
      if (vmd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
        versionValue = new Timestamp((Long)versionValue);
      }
      op.setVersion(versionValue);
    }
View Full Code Here

   * @param versionBehavior Behaviour required for versioning here
   * @param operation Convenience string for messages
   */
  private void handleVersioningBeforeWrite(ObjectProvider op, Entity entity, boolean increment, String operation) {
    AbstractClassMetaData cmd = op.getClassMetaData();
    VersionMetaData vmd = cmd.getVersionMetaDataForClass();
    if (cmd.isVersioned()) {
      ExecutionContext ec = op.getExecutionContext();
      String versionPropertyName = EntityUtils.getVersionPropertyName(datastoreMgr.getIdentifierFactory(), vmd);
      Object curVersion = op.getVersion();
      if (curVersion != null) {
        // Fetch the latest and greatest version of the entity from the datastore
        // to see if anyone has made a change underneath us.  We need to execute
        // the fetch outside a txn to guarantee that we see the latest version.
        if (NucleusLogger.DATASTORE_NATIVE.isDebugEnabled()) {
          NucleusLogger.DATASTORE_NATIVE.debug("Getting entity with key " + entity.getKey());
        }
        Entity refreshedEntity;
        try {
          if (ec.getStatistics() != null) {
            ec.getStatistics().incrementNumReads();
          }
          refreshedEntity = datastoreMgr.getDatastoreServiceForReads(op.getExecutionContext()).get(entity.getKey());
        } catch (EntityNotFoundException e) {
          // someone deleted out from under us
          throw new NucleusOptimisticException(GAE_LOCALISER.msg("AppEngine.OptimisticError.EntityHasBeenDeleted", operation,
              cmd.getFullClassName(), entity.getKey()));
        }

        Object datastoreVersion = refreshedEntity.getProperty(versionPropertyName);
        if (vmd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
          datastoreVersion = new Timestamp((Long) datastoreVersion);
        }

        if (!datastoreVersion.equals(curVersion)) {
          throw new NucleusOptimisticException(GAE_LOCALISER.msg("AppEngine.OptimisticError.EntityHasBeenUpdated", operation,
              cmd.getFullClassName(), entity.getKey()));
        }
      }

      Object nextVersion = VersionHelper.getNextVersion(vmd.getVersionStrategy(), curVersion);
      op.setTransactionalVersion(nextVersion);
      if (vmd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
        EntityUtils.setEntityProperty(entity, vmd, versionPropertyName, ((Timestamp)nextVersion).getTime());
      } else {
        EntityUtils.setEntityProperty(entity, vmd, versionPropertyName, nextVersion);
      }

      // Version field - update the version on the object
      if (increment && vmd.getFieldName() != null) {
        AbstractMemberMetaData verfmd =
            ((AbstractClassMetaData)vmd.getParent()).getMetaDataForMember(vmd.getFieldName());
        if (nextVersion instanceof Number) {
          // Version can be long, Long, int, Integer, short, Short (or Timestamp).
          Number nextNumber = (Number) nextVersion;
          if (verfmd.getType().equals(Long.class) || verfmd.getType().equals(Long.TYPE)) {
            nextVersion = nextNumber.longValue();
View Full Code Here

   * @param ammd Metadata for the field/property
   * @return The property name to use in the datastore
   */
  public static String getPropertyName(IdentifierFactory idFactory, AbstractMemberMetaData ammd) {
    AbstractClassMetaData acmd = ammd.getAbstractClassMetaData();
    VersionMetaData vermd = acmd.getVersionMetaDataForClass();
    if (acmd.isVersioned() && ammd.getName().equals(vermd.getFieldName())) {
      return getVersionPropertyName(idFactory, acmd.getVersionMetaData());
    }

    // If a column name was explicitly provided, use that as the property name.
    if (ammd.getColumn() != null) {
View Full Code Here

    String val = md.getValueForExtension(DatastoreManager.UNINDEXED_PROPERTY);
    if (val != null && val.equalsIgnoreCase("true")) {
      unindexed = true;
    } else if (md instanceof VersionMetaData && ((VersionMetaData)md).getFieldName() != null) {
      // Version : Check against the metadata of the field
      VersionMetaData vmd = (VersionMetaData)md;
      AbstractMemberMetaData vermmd = ((AbstractClassMetaData)vmd.getParent()).getMetaDataForMember(vmd.getFieldName());
      val = vermmd.getValueForExtension(DatastoreManager.UNINDEXED_PROPERTY);
      unindexed = (val != null && val.equalsIgnoreCase("true"));
    }

    if (unindexed) {
View Full Code Here

    // so that we can do a fetch without having to hide the entity in the state manager.
    op.setAssociatedValue(((DatastoreManager)ec.getStoreManager()).getDatastoreTransaction(ec), entity);

    // Make sure any version is set
    if (acmd.isVersioned()) {
      VersionMetaData vermd = acmd.getVersionMetaDataForClass();
      Object versionValue = entity.getProperty(EntityUtils.getVersionPropertyName(storeMgr.getIdentifierFactory(), vermd));
      if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME) {
        versionValue = new Timestamp((Long)versionValue);
      }
      op.setVersion(versionValue);
    }
View Full Code Here

  private void handleVersioningBeforeWrite(StateManager sm, Entity entity,
      VersionBehavior versionBehavior, String op) {
    AbstractClassMetaData cmd = sm.getClassMetaData();
    if (cmd.hasVersionStrategy()) {
      VersionMetaData vmd = cmd.getVersionMetaData();
      Object curVersion = sm.getObjectManager().getApiAdapter().getVersion(sm);
      if (curVersion != null) {
        NucleusLogger.DATASTORE.debug("Getting entity with key " + entity.getKey());
        // Fetch the latest and greatest version of the entity from the datastore
        // to see if anyone has made a change underneath us.  We need to execute
        // the fetch outside a txn to guarantee that we see the latest version.
        Entity refreshedEntity;
        try {
          refreshedEntity = datastoreServiceForReads.get(entity.getKey());
        } catch (EntityNotFoundException e) {
          // someone deleted out from under us
          throw newNucleusOptimisticException(
              cmd, entity, op, "The underlying entity had already been deleted.");
        }
        if (!EntityUtils.getVersionFromEntity(
            getIdentifierFactory(sm), vmd, refreshedEntity).equals(curVersion)) {
          throw newNucleusOptimisticException(
              cmd, entity, op, "The underlying entity had already been updated.");
        }
      }
      Object nextVersion = vmd.getNextVersion(curVersion);

      sm.setTransactionalVersion(nextVersion);
      String versionPropertyName =
          EntityUtils.getVersionPropertyName(getIdentifierFactory(sm), vmd);
      entity.setProperty(versionPropertyName, nextVersion);

      // Version field - update the version on the object
      if (versionBehavior == VersionBehavior.INCREMENT && vmd.getFieldName() != null) {
        AbstractMemberMetaData verfmd =
            ((AbstractClassMetaData)vmd.getParent()).getMetaDataForMember(vmd.getFieldName());
        sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextVersion, false);
      }
    }
  }
View Full Code Here

  private void handleVersioningBeforeWrite(StateManager sm, Entity entity,
      VersionBehavior versionBehavior, String op) {
    AbstractClassMetaData cmd = sm.getClassMetaData();
    if (cmd.hasVersionStrategy()) {
      VersionMetaData vmd = cmd.getVersionMetaData();
      Object curVersion = sm.getObjectManager().getApiAdapter().getVersion(sm);
      if (curVersion != null) {
        NucleusLogger.DATASTORE.debug("Getting entity with key " + entity.getKey());
        // Fetch the latest and greatest version of the entity from the datastore
        // to see if anyone has made a change underneath us.  We need to execute
        // the fetch outside a txn to guarantee that we see the latest version.
        Entity refreshedEntity;
        try {
          refreshedEntity = datastoreService.get(entity.getKey());
        } catch (EntityNotFoundException e) {
          // someone deleted out from under us
          throw newNucleusOptimisticException(
              cmd, entity, op, "The underlying entity had already been deleted.");
        }
        if (!EntityUtils.getVersionFromEntity(
            getIdentifierFactory(sm), vmd, refreshedEntity).equals(curVersion)) {
          throw newNucleusOptimisticException(
              cmd, entity, op, "The underlying entity had already been updated.");
        }
      }
      Object nextVersion = vmd.getNextVersion(curVersion);

      sm.setTransactionalVersion(nextVersion);
      String versionPropertyName =
          EntityUtils.getVersionPropertyName(getIdentifierFactory(sm), vmd);
      entity.setProperty(versionPropertyName, nextVersion);

      // Version field - update the version on the object
      if (versionBehavior == VersionBehavior.INCREMENT && vmd.getFieldName() != null) {
        AbstractMemberMetaData verfmd =
            ((AbstractClassMetaData)vmd.getParent()).getMetaDataForMember(vmd.getFieldName());
        sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextVersion, false);
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.datanucleus.metadata.VersionMetaData

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.