Package org.datanucleus.state

Examples of org.datanucleus.state.ObjectProvider


            return;
        }
        else
        {
            ObjectProvider otherSM = sm.getExecutionContext().findObjectProvider(pc);
            if (otherSM == null)
            {
                if (relationType == RelationType.ONE_TO_ONE_BI || relationType == RelationType.MANY_TO_ONE_BI ||
                    relationType == RelationType.MANY_TO_ONE_UNI)
                {
View Full Code Here


            {
                // We're deleting the FK at this side so shouldnt be an issue
                AbstractMemberMetaData relatedMmd = mmd.getRelatedMemberMetaDataForObject(clr, sm.getObject(), pc);
                if (relatedMmd != null)
                {
                    ObjectProvider otherSM = ec.findObjectProvider(pc);
                    if (otherSM != null)
                    {
                        // Managed Relations : 1-1 bidir, so null out the object at the other
                        Object currentValue = otherSM.provideField(relatedMmd.getAbsoluteFieldNumber());
                        if (currentValue != null)
                        {
                            if (NucleusLogger.PERSISTENCE.isDebugEnabled())
                            {
                                NucleusLogger.PERSISTENCE.debug(LOCALISER.msg("041019",
                                    StringUtils.toJVMIDString(pc), relatedMmd.getFullFieldName(),
                                    sm.getObjectAsPrintable()));
                            }
                            otherSM.replaceFieldMakeDirty(relatedMmd.getAbsoluteFieldNumber(), null);

                            if (ec.getManageRelations())
                            {
                                otherSM.getExecutionContext().getRelationshipManager(otherSM).relationChange(
                                    relatedMmd.getAbsoluteFieldNumber(), sm.getObject(), null);
                            }
                        }
                    }
                }
            }
        }
        else if (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() != null)
        {
            // 1-1 with FK at other side
            DatastoreClass relatedTable = storeMgr.getDatastoreClass(relatedMmds[0].getClassName(), clr);
            JavaTypeMapping relatedMapping = relatedTable.getMemberMapping(relatedMmds[0]);
            boolean isNullable = relatedMapping.isNullable();
            ObjectProvider otherSM = ec.findObjectProvider(pc);
            if (dependent)
            {
                if (isNullable)
                {
                    // Null out the FK in the datastore using a direct update (since we are deleting)
                    otherSM.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), null);
                    storeMgr.getPersistenceHandler().updateObject(
                        otherSM, new int[]{relatedMmds[0].getAbsoluteFieldNumber()});
                }
                // Mark the other object for deletion
                ec.deleteObjectInternal(pc);
            }
            else if (!hasFK)
            {
                if (isNullable())
                {
                    Object currentRelatedValue = otherSM.provideField(relatedMmds[0].getAbsoluteFieldNumber());
                    if (currentRelatedValue != null)
                    {
                        // Null out the FK in the datastore using a direct update (since we are deleting)
                        otherSM.replaceFieldMakeDirty(relatedMmds[0].getAbsoluteFieldNumber(), null);
                        storeMgr.getPersistenceHandler().updateObject(
                            otherSM, new int[]{relatedMmds[0].getAbsoluteFieldNumber()});

                        // Managed Relations : 1-1 bidir, so null out the object at the other
                        if (ec.getManageRelations())
                        {
                            otherSM.getExecutionContext().getRelationshipManager(otherSM).relationChange(
                                relatedMmds[0].getAbsoluteFieldNumber(), sm.getObject(), null);
                        }
                    }
                }
                else
                {
                    // TODO Remove it
                }
            }
            else
            {
                // User has a FK defined (in MetaData) so let the datastore take care of it
            }
        }
        else if (relationType == RelationType.MANY_TO_ONE_BI)
        {
            ObjectProvider otherSM = ec.findObjectProvider(pc);
            if (relatedMmds[0].getJoinMetaData() == null)
            {
                // N-1 with FK at this side
                if (otherSM.isDeleting())
                {
                    // Other object is being deleted too but this side has the FK so just delete this object
                }
                else
                {
                    // Other object is not being deleted so delete it if necessary
                    if (dependent)
                    {
                        if (isNullable())
                        {
                            // TODO Datastore nullability info can be unreliable so try to avoid this call
                            // Null out the FK in the datastore using a direct update (since we are deleting)
                            sm.replaceFieldMakeDirty(fieldNumber, null);
                            storeMgr.getPersistenceHandler().updateObject(sm, new int[]{fieldNumber});
                        }

                        if (ec.getApiAdapter().isDeleted(pc))
                        {
                            // Object is already tagged for deletion but we're deleting the FK so leave til flush()
                        }
                        else
                        {
                            // Mark the other object for deletion
                            ec.deleteObjectInternal(pc);
                        }
                    }
                    else
                    {
                        // Managed Relations : remove element from collection/map
                        if (relatedMmds[0].hasCollection())
                        {
                            // Only update the other side if not already being deleted
                            if (!ec.getApiAdapter().isDeleted(otherSM.getObject()) && !otherSM.isDeleting())
                            {
                                // Make sure the other object is updated in any caches
                                ec.markDirty(otherSM, false);

                                // Make sure collection field is loaded
                                otherSM.isLoaded(relatedMmds[0].getAbsoluteFieldNumber());
                                Collection otherColl = (Collection)otherSM.provideField(relatedMmds[0].getAbsoluteFieldNumber());
                                if (otherColl != null)
                                {
                                    if (ec.getManageRelations())
                                    {
                                        otherSM.getExecutionContext().getRelationshipManager(otherSM).relationRemove(
                                            relatedMmds[0].getAbsoluteFieldNumber(), sm.getObject());
                                    }
                                    // TODO Localise this message
                                    NucleusLogger.PERSISTENCE.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
                                    otherColl.remove(sm.getObject());
                                }
                            }
                        }
                        else if (relatedMmds[0].hasMap())
                        {
                            // TODO Cater for maps, but what is the key/value pair ?
                        }
                    }
                }
            }
            else
            {
                // N-1 with join table so no FK here so need to remove from Collection/Map first? (managed relations)
                if (dependent)
                {
                    // Mark the other object for deletion
                    ec.deleteObjectInternal(pc);
                }
                else
                {
                    // Managed Relations : remove element from collection/map
                    if (relatedMmds[0].hasCollection())
                    {
                        // Only update the other side if not already being deleted
                        if (!ec.getApiAdapter().isDeleted(otherSM.getObject()) && !otherSM.isDeleting())
                        {
                            // Make sure the other object is updated in any caches
                            ec.markDirty(otherSM, false);

                            // Make sure the other object has the collection loaded so does this change
                            otherSM.isLoaded(relatedMmds[0].getAbsoluteFieldNumber());
                            Collection otherColl = (Collection)otherSM.provideField(relatedMmds[0].getAbsoluteFieldNumber());
                            if (otherColl != null)
                            {
                                // TODO Localise this
                                NucleusLogger.PERSISTENCE.debug("ManagedRelationships : delete of object causes removal from collection at " + relatedMmds[0].getFullFieldName());
                                otherColl.remove(sm.getObject());
View Full Code Here

    AbstractMemberMetaData mmd = getMetaData(fieldNumber);
    ClassLoaderResolver clr = getClassLoaderResolver();
    RelationType relationType = mmd.getRelationType(clr);
    if (mmd.getEmbeddedMetaData() != null && RelationType.isRelationSingleValued(relationType)) {
      // Embedded persistable object
      ObjectProvider embeddedOP = getEmbeddedObjectProvider(mmd.getType(), fieldNumber, null);

      fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, mmd.getEmbeddedMetaData()));
      try {
        embeddedOP.replaceFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);

        // Checks for whether the member values imply a null object
        if (mmd.getEmbeddedMetaData() != null && mmd.getEmbeddedMetaData().getNullIndicatorColumn() != null) {
          String nullColumn = mmd.getEmbeddedMetaData().getNullIndicatorColumn();
          String nullValue = mmd.getEmbeddedMetaData().getNullIndicatorValue();
          AbstractMemberMetaData[] embMmds = mmd.getEmbeddedMetaData().getMemberMetaData();
          AbstractMemberMetaData nullMmd = null;
          for (int i=0;i<embMmds.length;i++) {
            ColumnMetaData[] colmds = embMmds[i].getColumnMetaData();
            if (colmds != null && colmds.length > 0 && colmds[0].getName() != null && colmds[0].getName().equals(nullColumn)) {
              nullMmd = embMmds[i];
              break;
            }
          }
          if (nullMmd != null) {
            int nullFieldPos = embeddedOP.getClassMetaData().getAbsolutePositionOfMember(nullMmd.getName());
            Object val = embeddedOP.provideField(nullFieldPos);
            if (val == null && nullValue == null) {
              return null;
            } else if (val != null && nullValue != null && val.equals(nullValue)) {
              return null;
            }
          }
          return embeddedOP.getObject();
        }
        else {
          return embeddedOP.getObject();
        }
      } finally {
        fieldManagerStateStack.removeFirst();
      }
    } else if (RelationType.isRelationMultiValued(relationType) && mmd.isEmbedded()) {
      // Embedded container
      if (mmd.hasCollection()) {
        // Embedded collections
        String collPropName = getPropertyNameForMember(mmd) + ".size";
        Long collSize = (Long)datastoreEntity.getProperty(collPropName);
        if (collSize == null || collSize == -1) {
          // Size of collection not stored or stored as -1, so null on persist
          return null;
        }

        Class elementType = clr.classForName(mmd.getCollection().getElementType());
        AbstractClassMetaData elemCmd = mmd.getCollection().getElementClassMetaData(clr, ec.getMetaDataManager());
        EmbeddedMetaData embmd =
          mmd.getElementMetaData() != null ? mmd.getElementMetaData().getEmbeddedMetaData() : null;
        Collection<Object> coll;
        try {
          Class instanceType = SCOUtils.getContainerInstanceType(mmd.getType(), mmd.getOrderMetaData() != null);
          coll = (Collection<Object>) instanceType.newInstance();
        } catch (Exception e) {
          throw new NucleusDataStoreException(e.getMessage(), e);
        }

        // Use discriminator for elements if available
        String collDiscName = null;
        if (elemCmd.hasDiscriminatorStrategy()) {
          collDiscName = elemCmd.getDiscriminatorColumnName();
          if (embmd != null && embmd.getDiscriminatorMetaData() != null) {
            // Override if specified under <embedded>
            DiscriminatorMetaData dismd = embmd.getDiscriminatorMetaData();
            ColumnMetaData discolmd = dismd.getColumnMetaData();
            if (discolmd != null && discolmd.getName() != null) {
              collDiscName = discolmd.getName();
            }
          }
          if (collDiscName == null) {
            collDiscName = getPropertyNameForMember(mmd) + ".discrim";
          }
        }

        for (int i=0;i<collSize;i++) {
          Class elementCls = elementType;
          if (collDiscName != null) {
            Object discVal = datastoreEntity.getProperty(collDiscName + "." + i);
            String className =
              org.datanucleus.metadata.MetaDataUtils.getClassNameFromDiscriminatorValue((String)discVal,
                  elemCmd.getDiscriminatorMetaDataRoot(), ec);
            elementCls = clr.classForName(className);
          }

          ObjectProvider embeddedOP = getEmbeddedObjectProvider(elementCls, fieldNumber, null);
          fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, embmd, i));
          try {
            embeddedOP.replaceFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);
          } finally {
            fieldManagerStateStack.removeFirst();
          }
          coll.add(embeddedOP.getObject());
        }
        return getObjectProvider().wrapSCOField(fieldNumber, coll, false, false, true);
      } else if (mmd.hasArray()) {
        // Embedded arrays
        String arrPropName = getPropertyNameForMember(mmd) + ".size";
        Long arrSize = (Long)datastoreEntity.getProperty(arrPropName);
        if (arrSize == null || arrSize == -1) {
          // Size of array not stored or stored as -1, so null on persist
          return null;
        }

        Class elementType = clr.classForName(mmd.getArray().getElementType());
        AbstractClassMetaData elemCmd = mmd.getArray().getElementClassMetaData(clr, ec.getMetaDataManager());
        EmbeddedMetaData embmd =
          mmd.getElementMetaData() != null ? mmd.getElementMetaData().getEmbeddedMetaData() : null;
        Object value = Array.newInstance(elementType, arrSize.intValue());

        // Use discriminator for elements if available
        String arrDiscName = null;
        if (elemCmd.hasDiscriminatorStrategy()) {
          arrDiscName = elemCmd.getDiscriminatorColumnName();
          if (embmd != null && embmd.getDiscriminatorMetaData() != null) {
            // Override if specified under <embedded>
            DiscriminatorMetaData dismd = embmd.getDiscriminatorMetaData();
            ColumnMetaData discolmd = dismd.getColumnMetaData();
            if (discolmd != null && discolmd.getName() != null) {
              arrDiscName = discolmd.getName();
            }
          }
          if (arrDiscName == null) {
            arrDiscName = getPropertyNameForMember(mmd) + ".discrim";
          }
        }

        for (int i=0;i<arrSize;i++) {
          Class elementCls = elementType;
          if (arrDiscName != null) {
            Object discVal = datastoreEntity.getProperty(arrDiscName + "." + i);
            String className =
              org.datanucleus.metadata.MetaDataUtils.getClassNameFromDiscriminatorValue((String)discVal,
                  elemCmd.getDiscriminatorMetaDataRoot(), ec);
            elementCls = clr.classForName(className);
          }

          ObjectProvider embeddedOP = getEmbeddedObjectProvider(elementCls, fieldNumber, null);
          fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, embmd, i));
          try {
            embeddedOP.replaceFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);
          } finally {
            fieldManagerStateStack.removeFirst();
          }
          Array.set(value, i, embeddedOP.getObject());
        }
        return value;
      } else if (mmd.hasMap()) {
        // TODO Support embedded maps
        throw new NucleusUserException("Don't currently support embedded maps at " + mmd.getFullFieldName());
View Full Code Here

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

            AbstractClassMetaData embCmd = ec.getMetaDataManager().getMetaDataForClass(value.getClass(), ec.getClassLoaderResolver());
            ObjectProvider embSM = ec.findObjectProvider(value);
            if (embSM == null || api.getExecutionContext(value) == null)
            {
                // Assign a StateManager to manage our embedded object
                embSM = ec.newObjectProviderForEmbedded(value, false, ownerSM, ownerFieldNumber);
                embSM.setPcObjectType(objectType);
            }

            int n = 0;

            if (discrimMapping != null)
            {
                if (discrimMetaData.getStrategy() == DiscriminatorStrategy.CLASS_NAME)
                {
                    discrimMapping.setObject(ec, ps, new int[]{param[n]}, value.getClass().getName());
                }
                else if (discrimMetaData.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
                {
                    DiscriminatorMetaData valueDismd = embCmd.getInheritanceMetaData().getDiscriminatorMetaData();
                    discrimMapping.setObject(ec, ps, new int[]{param[n]}, valueDismd.getValue());
                }
                n++;
            }

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

                // Retrieve value of member from Embedded StateManager
                int embAbsFieldNum = embCmd.getAbsolutePositionOfMember(mapping.getMemberMetaData().getName());
                if (embAbsFieldNum >= 0)
                {
                    // Member is present in this embedded type
                    Object fieldValue = embSM.provideField(embAbsFieldNum);
                    if (mapping instanceof EmbeddedPCMapping)
                    {
                        mapping.setObject(ec, ps, posMapping, fieldValue, embSM, embAbsFieldNum);
                    }
                    else
View Full Code Here

        if (mmd.getFieldTypes() != null && mmd.getFieldTypes().length > 0)
        {
            // Embedded type has field-type defined so use that as our embedded type
            embeddedType = ec.getClassLoaderResolver().classForName(mmd.getFieldTypes()[0]);
        }
        ObjectProvider embSM = ec.newObjectProviderForHollow(embeddedType, (Object)null);
        embSM.setPcObjectType(objectType);
        value = embSM.getObject();

        String nullColumn = null;
        String nullValue = null;
        if (emd != null)
        {
            nullColumn = emd.getNullIndicatorColumn();
            nullValue = emd.getNullIndicatorValue();
        }

        // Populate the field values
        for (int i=0; i<javaTypeMappings.size(); i++)
        {
            JavaTypeMapping mapping = javaTypeMappings.get(i);
            int embAbsFieldNum = embCmd.getAbsolutePositionOfMember(mapping.getMemberMetaData().getName());
            if (embAbsFieldNum >= 0)
            {
                // Mapping for field that is present in this embedded type, so set the field
                if (mapping instanceof EmbeddedPCMapping)
                {
                    // We have a nested embedded
                    int numSubParams = mapping.getNumberOfDatastoreMappings();
                    int[] subParam = new int[numSubParams];
                    int k = 0;
                    for (int j=n;j<n+numSubParams;j++)
                    {
                        subParam[k++] = param[j];
                    }
                    n += numSubParams;

                    // Use the sub-object mapping to extract the value for that object
                    Object subValue = mapping.getObject(ec, rs, subParam, embSM, embAbsFieldNum);
                    if (subValue != null)
                    {
                        embSM.replaceField(embAbsFieldNum, subValue);
                    }

                    // TODO Check the null column and its value in the sub-embedded ?
                }
                else
                {
                    // Extract the value(s) for this field and update the PC if it is not null
                    int[] posMapping = new int[mapping.getNumberOfDatastoreMappings()];
                    for (int j=0; j<posMapping.length; j++)
                    {
                        posMapping[j] = param[n++];
                    }
                    Object fieldValue = mapping.getObject(ec, rs, posMapping);

                    // Check for the null column and its value and break if this matches the null check
                    if (nullColumn != null &&
                        mapping.getMemberMetaData().getColumnMetaData()[0].getName().equals(nullColumn))
                    {
                        if ((nullValue == null && fieldValue == null) ||
                            (nullValue != null && fieldValue.toString().equals(nullValue)))
                        {
                            value = null;
                            break;
                        }
                    }

                    // Set the field value
                    if (fieldValue != null)
                    {
                        embSM.replaceField(embAbsFieldNum, fieldValue);
                    }
                    else
                    {
                        // If the value is null, but the field is not a primitive update it
                        AbstractMemberMetaData embFmd = embCmd.getMetaDataForManagedMemberAtAbsolutePosition(embAbsFieldNum);
                        if (!embFmd.getType().isPrimitive())
                        {
                            embSM.replaceField(embAbsFieldNum, fieldValue);
                        }
                    }
                }
            }
            else
            {
                // Mapping not present in this embedded type so maybe subclass, so just omit the positions
                int numSubParams = mapping.getNumberOfDatastoreMappings();
                n += numSubParams;
            }
        }

        // Update owner field in the element (if present)
        if (emd != null)
        {
            String ownerField = emd.getOwnerMember();
            if (ownerField != null)
            {
                int ownerFieldNumberInElement = embCmd.getAbsolutePositionOfMember(ownerField);
                if (ownerFieldNumberInElement >= 0)
                {
                    embSM.replaceField(ownerFieldNumberInElement, ownerSM.getObject());
                }
            }
        }
       
        // Register our owner now that we have our values set
View Full Code Here

            ObjectProvider ownerSM, int fieldNumber)
    {
        if (value != null)
        {
            // Assign a StateManager to the serialised object if none present
            ObjectProvider embSM = ec.findObjectProvider(value);
            if (embSM == null || ec.getApiAdapter().getExecutionContext(value) == null)
            {
                embSM = ec.newObjectProviderForEmbedded(value, false, ownerSM, fieldNumber);
            }
        }

        ObjectProvider sm = null;
        if (value != null)
        {
            // Find SM for serialised object
            sm = ec.findObjectProvider(value);
        }

        if (sm != null)
        {
            sm.setStoringPC();
        }
        getDatastoreMapping(0).setObject(preparedStatement, exprIndex[0], value);
        if (sm != null)
        {
            sm.unsetStoringPC();
        }
    }
View Full Code Here

    {
        Object obj = getDatastoreMapping(0).getObject(resultSet, exprIndex[0]);
        if (obj != null)
        {
            // Assign a StateManager to the serialised object if none present
            ObjectProvider embSM = ec.findObjectProvider(obj);
            if (embSM == null || ec.getApiAdapter().getExecutionContext(obj) == null)
            {
                ec.newObjectProviderForEmbedded(obj, false, ownerSM, fieldNumber);
            }
        }
View Full Code Here

    if (value == null) {
      value = JDOImplHelper.getInstance().newInstance(type,
          (javax.jdo.spi.StateManager)getObjectProvider());
    }

    ObjectProvider embeddedOP = ec.findObjectProvider(value);
    if (embeddedOP == null) {
        embeddedOP = ec.newObjectProviderForEmbedded(value, false, getObjectProvider(), fieldNumber);
        embeddedOP.setPcObjectType(ObjectProvider.EMBEDDED_PC);
    }
    return embeddedOP;
  }
View Full Code Here

   * @param pm The Persistence Manager
   * @return The Entity (if accessible)
   */
  public Entity getEntityFromJPA(Object pc, EntityManager em) {
    ExecutionContext ec = ((JPAEntityManager)em).getExecutionContext();
    ObjectProvider op = ec.findObjectProvider(pc);
    if (op != null) {
      DatastoreManager storeMgr = (DatastoreManager) ec.getStoreManager();
      DatastoreTransaction txn = storeMgr.getDatastoreTransaction(ec);
      if (txn != null) {
        Entity entity = (Entity)op.getAssociatedValue(txn);
        if (entity != null) {
          return entity;
        } else {
          Key key = EntityUtils.getPkAsKey(op);
          return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
View Full Code Here

   * @param pm The Persistence Manager
   * @return The Entity (if accessible)
   */
  public Entity getEntityFromJDO(Object pc, PersistenceManager pm) {
    ExecutionContext ec = ((JDOPersistenceManager)pm).getExecutionContext();
    ObjectProvider op = ec.findObjectProvider(pc);
    if (op != null) {
      DatastoreManager storeMgr = (DatastoreManager) ec.getStoreManager();
      DatastoreTransaction txn = storeMgr.getDatastoreTransaction(ec);
      if (txn != null) {
        Entity entity = (Entity)op.getAssociatedValue(txn);
        if (entity != null) {
          return entity;
        } else {
          Key key = EntityUtils.getPkAsKey(op);
          return EntityUtils.getEntityFromDatastore(storeMgr.getDatastoreServiceForReads(ec), op, key);
View Full Code Here

TOP

Related Classes of org.datanucleus.state.ObjectProvider

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.