Package org.datanucleus.metadata

Examples of org.datanucleus.metadata.VersionMetaData


                    // Version field
                    ClassMetaData cmd = (ClassMetaData)getStack();
                    AbstractMemberMetaData mmd = newFieldObject(cmd, attrs);

                    // Tag this field as the version field
                    VersionMetaData vermd = cmd.newVersionMetadata();
                    vermd.setStrategy(VersionStrategy.VERSION_NUMBER).setFieldName(mmd.getName());

                    pushStack(mmd);
                }
            }
            else if (localName.equals("discriminator-value"))
View Full Code Here


        fmd.setMappedBy(mappedBy);

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

        cmd.addMember(fmd);

        if (orphanRemoval)
View Full Code Here

            mappingDefinition = stmtMapping;
            mappedFieldNumbers = fieldNumbers;
        }

        // Extract any surrogate version
        VersionMetaData vermd = cmd.getVersionMetaDataForClass();
        Object surrogateVersion = null;
        StatementMappingIndex versionMapping = null;
        if (vermd != null)
        {
            if (vermd.getFieldName() == null)
            {
                versionMapping = stmtMapping.getMappingForMemberPosition(StatementClassMapping.MEMBER_VERSION);
            }
            else
            {
                AbstractMemberMetaData vermmd = cmd.getMetaDataForMember(vermd.getFieldName());
                versionMapping = stmtMapping.getMappingForMemberPosition(vermmd.getAbsoluteFieldNumber());
            }
        }
        if (versionMapping != null)
        {
            // Surrogate version column returned by query
            JavaTypeMapping mapping = versionMapping.getMapping();
            surrogateVersion = mapping.getObject(ec, rs, versionMapping.getColumnPositions());
        }

        // Extract the object from the ResultSet
        Object obj = null;
        boolean needToSetVersion = false;
        if (persistentClass.isInterface() && !cmd.isImplementationOfPersistentDefinition())
        {
            // Querying by interface, and not a generated implementation so use the metadata for the interface
            cmd = ec.getMetaDataManager().getMetaDataForInterface(persistentClass, clr);
        }

        if (cmd.getIdentityType() == IdentityType.APPLICATION)
        {
            // Check if the PK field(s) are all null (implies null object, when using OUTER JOIN)
            int[] pkNumbers = cmd.getPKMemberPositions();
            ResultSet resultSet = (ResultSet)rs;
            boolean nullObject = true;
            for (int i=0;i<pkNumbers.length;i++)
            {
                StatementMappingIndex pkIdx = mappingDefinition.getMappingForMemberPosition(pkNumbers[i]);
                if (pkIdx == null)
                {
                    throw new NucleusException("You have just executed an SQL statement yet the information " +
                        "for the primary key column(s) is not available! " +
                        "Please generate a testcase and report this issue");
                }
                int[] colPositions = pkIdx.getColumnPositions();
                for (int j=0;j<colPositions.length;j++)
                {
                    try
                    {
                        Object pkObj = resultSet.getObject(colPositions[j]);
                        if (pkObj != null)
                        {
                            nullObject = false;
                            break;
                        }
                    }
                    catch (SQLException sqle)
                    {
                        NucleusLogger.DATASTORE_RETRIEVE.warn("Exception thrown while retrieving results ", sqle);
                    }
                    if (!nullObject)
                    {
                        break;
                    }
                }
            }

            if (nullObject)
            {
                obj = null;
            }
            else
            {
                // Retrieve the object with this application-identity
                if (warnMsg != null)
                {
                    NucleusLogger.DATASTORE_RETRIEVE.warn(warnMsg);
                }
                obj = getObjectForApplicationId(ec, rs, mappingDefinition, mappedFieldNumbers,
                    pcClassForObject, cmd, requiresInheritanceCheck, surrogateVersion);
            }
        }
        else if (cmd.getIdentityType() == IdentityType.DATASTORE)
        {
            // Generate the "oid" for this object (of type pcClassForObject), and find the object for that
            StatementMappingIndex datastoreIdMapping =
                stmtMapping.getMappingForMemberPosition(StatementClassMapping.MEMBER_DATASTORE_ID);
            JavaTypeMapping mapping = datastoreIdMapping.getMapping();
            OID oid = (OID)mapping.getObject(ec, rs, datastoreIdMapping.getColumnPositions());
            if (oid != null)
            {
                Object id = oid;
                if (!pcClassForObject.getName().equals(oid.getPcClass()))
                {
                    // Get an OID for the right inheritance level
                    id = OIDFactory.getInstance(ec.getNucleusContext(), pcClassForObject.getName(), oid.getKeyValue());
                }

                if (warnMsg != null)
                {
                    NucleusLogger.DATASTORE_RETRIEVE.warn(warnMsg);
                }
                if (mappedFieldNumbers == null)
                {
                    obj = ec.findObject(id, false, requiresInheritanceCheck, null);
                    needToSetVersion = true;
                }
                else
                {
                    obj = getObjectForDatastoreId(ec, rs, mappingDefinition, mappedFieldNumbers,
                        id, requiresInheritanceCheck ? null : pcClassForObject, cmd, surrogateVersion);
                }
            }
            else
            {
                obj = null;
            }
        }
        else if (cmd.getIdentityType() == IdentityType.NONDURABLE)
        {
            Object id = ec.newObjectId(className, null);
            if (mappedFieldNumbers == null)
            {
                obj = ec.findObject(id, false, requiresInheritanceCheck, null);
                needToSetVersion = true;
            }
            else
            {
                obj = getObjectForDatastoreId(ec, rs, mappingDefinition, mappedFieldNumbers,
                    id, pcClassForObject, cmd, surrogateVersion);
            }
        }

        if (obj != null && needToSetVersion)
        {
            // Set the version of the object where possible
            if (surrogateVersion != null)
            {
                ObjectProvider objSM = ec.findObjectProvider(obj);
                objSM.setVersion(surrogateVersion);
            }
            else
            {
                if (vermd != null && vermd.getFieldName() != null)
                {
                    // Version stored in a normal field
                    int versionFieldNumber = acmd.getMetaDataForMember(vermd.getFieldName()).getAbsoluteFieldNumber();
                    if (stmtMapping.getMappingForMemberPosition(versionFieldNumber) != null)
                    {
                        ObjectProvider objSM = ec.findObjectProvider(obj);
                        Object verFieldValue = objSM.provideField(versionFieldNumber);
                        if (verFieldValue != null)
View Full Code Here

                    // Surrogate version field
                    sm.setVersion(surrogateVersion);
                }
                else if (cmd.getVersionMetaData() != null && cmd.getVersionMetaData().getFieldName() != null)
                {
                    VersionMetaData vermd = cmd.getVersionMetaData();
                    // Version stored in a normal field
                    int versionFieldNumber = acmd.getMetaDataForMember(vermd.getFieldName()).getAbsoluteFieldNumber();
                    if (stmtMapping.getMappingForMemberPosition(versionFieldNumber) != null)
                    {
                        Object verFieldValue = sm.provideField(versionFieldNumber);
                        if (verFieldValue != null)
                        {
View Full Code Here

                    // Surrogate version field
                    sm.setVersion(surrogateVersion);
                }
                else if (cmd.getVersionMetaData() != null && cmd.getVersionMetaData().getFieldName() != null)
                {
                    VersionMetaData vermd = cmd.getVersionMetaData();
                    // Version stored in a normal field
                    int versionFieldNumber = acmd.getMetaDataForMember(vermd.getFieldName()).getAbsoluteFieldNumber();
                    if (stmtMapping.getMappingForMemberPosition(versionFieldNumber) != null)
                    {
                        Object verFieldValue = sm.provideField(versionFieldNumber);
                        if (verFieldValue != null)
                        {
View Full Code Here

                SQLExpression updateSqlExpr = null;

                ClassTable table = (ClassTable)stmt.getPrimaryTable().getTable();
                JavaTypeMapping verMapping = table.getVersionMapping(true);
                ClassTable verTable = table.getTableManagingMapping(verMapping);
                VersionMetaData vermd = candidateCmd.getVersionMetaData();
                if (vermd.getVersionStrategy() == VersionStrategy.VERSION_NUMBER)
                {
                    // Increment the version
                    SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
                    SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
                    SQLExpression incrExpr = verExpr.add(new IntegerLiteral(stmt,
                        stmt.getSQLExpressionFactory().getMappingForType(Integer.class, false), new Integer(1), null));

                    updateSqlExpr = verExpr.eq(incrExpr);

                    SQLExpression[] oldArray = updateSqlExprs;
                    updateSqlExprs = new SQLExpression[oldArray.length+1];
                    System.arraycopy(oldArray, 0, updateSqlExprs, 0, oldArray.length);
                    updateSqlExprs[oldArray.length] = updateSqlExpr;
                }
                else if (vermd.getVersionStrategy() == VersionStrategy.DATE_TIME)
                {
                    // Set version to the time of update
                    SQLTable verSqlTbl = stmt.getTable(verTable, stmt.getPrimaryTable().getGroupName());
                    SQLExpression verExpr = new NumericExpression(stmt, verSqlTbl, verMapping);
                    Object newVersion = VersionHelper.getNextVersion(vermd.getVersionStrategy(), null);
                    JavaTypeMapping valMapping = stmt.getSQLExpressionFactory().getMappingForType(newVersion.getClass(), false);
                    SQLExpression valExpr = new TemporalLiteral(stmt, valMapping, newVersion, null);

                    updateSqlExpr = verExpr.eq(valExpr);
View Full Code Here

            NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("052207", sm.toPrintableID(), table));
        }

        try
        {
            VersionMetaData vermd = table.getVersionMetaData();
            ExecutionContext ec = sm.getExecutionContext();
            RDBMSStoreManager storeMgr = (RDBMSStoreManager)ec.getStoreManager();
            if (vermd != null && vermd.getFieldName() != null)
            {
                // Version field - Update the version in the object
                AbstractMemberMetaData verfmd =
                    ((AbstractClassMetaData)vermd.getParent()).getMetaDataForMember(vermd.getFieldName());
                Object currentVersion = ec.getApiAdapter().getVersion(sm);
                if (currentVersion instanceof Number)
                {
                    // Cater for Integer based versions
                    currentVersion = Long.valueOf(((Number)currentVersion).longValue());
                }

                Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                if (verfmd.getType() == Integer.class || verfmd.getType() == int.class)
                {
                    // Cater for Integer based versions TODO Generalise this
                    nextOptimisticVersion = Integer.valueOf(((Long)nextOptimisticVersion).intValue());
                }
                sm.replaceField(verfmd.getAbsoluteFieldNumber(), nextOptimisticVersion);
            }

            // Set the state to "inserting" (may already be at this state if multiple inheritance level INSERT)
            sm.changeActivityState(ActivityState.INSERTING);

            SQLController sqlControl = storeMgr.getSQLController();
            ManagedConnection mconn = storeMgr.getConnection(ec);
            try
            {
                PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, insertStmt, batch);

                try
                {
                    StatementClassMapping mappingDefinition = new StatementClassMapping();
                    StatementMappingIndex[] idxs = stmtMappings;
                    for (int i=0;i<idxs.length;i++)
                    {
                        if (idxs[i] != null)
                        {
                            mappingDefinition.addMappingForMember(i, idxs[i]);
                        }
                    }

                    // Provide the primary key field(s)
                    if (table.getIdentityType() == IdentityType.DATASTORE)
                    {
                        if (!table.isObjectIdDatastoreAttributed() || !table.isBaseDatastoreClass())
                        {
                            int[] paramNumber = {IDPARAMNUMBER};
                            table.getDatastoreObjectIdMapping().setObject(ec, ps, paramNumber, sm.getInternalObjectId());
                        }
                    }
                    else if (table.getIdentityType() == IdentityType.APPLICATION)
                    {
                        sm.provideFields(pkFieldNumbers,
                            storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));
                    }

                    // Provide all non-key fields needed for the insert.
                    // This provides "persistence-by-reachability" for these fields
                    int numberOfFieldsToProvide = 0;
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getClassMetaData().getMemberCount())
                        {
                            numberOfFieldsToProvide++;
                        }
                    }
                    int j = 0;
                    int[] classFieldNumbers = new int[numberOfFieldsToProvide];
                    for (int i = 0; i < insertFieldNumbers.length; i++)
                    {
                        if (insertFieldNumbers[i] < sm.getClassMetaData().getMemberCount())
                        {
                            classFieldNumbers[j++] = insertFieldNumbers[i];
                        }
                    }
                    sm.provideFields(classFieldNumbers,
                        storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, true));

                    if (table.getVersionMapping(false) != null)
                    {
                        // Surrogate version - set the new version for the object
                        Object currentVersion = ec.getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                        for (int k=0;k<versionStmtMapping.getNumberOfParameterOccurrences();k++)
                        {
                            table.getVersionMapping(false).setObject(ec, ps,
                                versionStmtMapping.getParameterPositionsForOccurrence(k), nextOptimisticVersion);
                        }
                        sm.setTransactionalVersion(nextOptimisticVersion);
                    }
                    else if (vermd != null && vermd.getFieldName() != null)
                    {
                        // Version field - set the new version for the object
                        Object currentVersion = ec.getApiAdapter().getVersion(sm);
                        Object nextOptimisticVersion = VersionHelper.getNextVersion(table.getVersionMetaData().getVersionStrategy(), currentVersion);
                        sm.setTransactionalVersion(nextOptimisticVersion);
View Full Code Here

        // Extract version information, from this table and any super-tables
        DatastoreClass currentTable = table;
        while (currentTable != null)
        {
            VersionMetaData currentVermd = currentTable.getVersionMetaData();
            if (currentVermd != null)
            {
                if (currentVermd.getFieldName() == null)
                {
                    // Surrogate version stored in this table
                    fetchingSurrogateVersion = true;
                }
                else
                {
                    // Version field
                    versionFieldName = currentVermd.getFieldName();
                }
            }

            currentTable = currentTable.getSuperDatastoreClass();
        }
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.length == 0)
        {
View Full Code Here

    /* (non-Javadoc)
     * @see javax.persistence.metamodel.SingularAttribute#isVersion()
     */
    public boolean isVersion()
    {
        VersionMetaData vermd = mmd.getAbstractClassMetaData().getVersionMetaData();
        if (vermd.getFieldName() != null && vermd.getFieldName().equals(mmd.getName()))
        {
            return true;
        }
        return 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.