Package org.datanucleus.state

Examples of org.datanucleus.state.ObjectProvider


    else {
      // Ordered List - no index so null the FK (if nullable) or delete the element
      ExecutionContext ec = ownerOP.getExecutionContext();
      if (ownerMapping.isNullable()) {
        // Nullify the FK
        ObjectProvider elementSM = ec.findObjectProvider(element);
        if (relationType == RelationType.ONE_TO_MANY_BI) {
          // TODO This is ManagedRelations - move into RelationshipManager
          elementSM.replaceFieldMakeDirty(ownerMemberMetaData.getRelatedMemberMetaData(clr)[0].getAbsoluteFieldNumber(),
              null);
          if (ec.isFlushing()) {
            elementSM.flush();
          }
        } else {
          updateElementFk(ownerOP, element, null, -1);
          if (deleteElementsOnRemoveOrClear()) {
            // TODO If present elsewhere in List then don't delete the element from persistence
View Full Code Here


    try {
      removing.set(true);

      if (!deleteElement) {
        // Nullify the index of the element
        ObjectProvider elementOP = ec.findObjectProvider(element);
        if (elementOP != null && !ec.getApiAdapter().isDeleted(element)) {
          Entity elementEntity = getOwnerEntity(elementOP);
          if (!storeMgr.storageVersionAtLeast(StorageVersion.READ_OWNED_CHILD_KEYS_FROM_PARENTS)) {
            // Remove the external index property from the element
            elementEntity.removeProperty(getIndexPropertyName());
View Full Code Here

   */
  public int indexOf(ObjectProvider op, Object element) {
    ExecutionContext ec = op.getExecutionContext();
    validateElementForReading(ec, element);

    ObjectProvider elementOP = ec.findObjectProvider(element);
    Key elementKey = EntityUtils.getPrimaryKeyAsKey(ec.getApiAdapter(), elementOP);
    if (elementKey == null) {
      // Not persistent
      return -1;
    }
View Full Code Here

   * @see org.datanucleus.store.scostore.ListStore#lastIndexOf(org.datanucleus.store.ObjectProvider, java.lang.Object)
   */
  public int lastIndexOf(ObjectProvider op, Object element) {
    ExecutionContext ec = op.getExecutionContext();
    validateElementForReading(ec, element);
    ObjectProvider elementOP = ec.findObjectProvider(element);
    Key elementKey = EntityUtils.getPrimaryKeyAsKey(ec.getApiAdapter(), elementOP);
    if (elementKey == null) {
      // Not persistent
      return -1;
    }
View Full Code Here

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

        ObjectProvider sm = null;
        if (api.isPersistable(value))
        {
            // Find SM for serialised PC 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

     */
    protected String getReferenceStringForObject(ExecutionContext ec, Object value)
    {
        if (ec.getApiAdapter().isPersistable(value))
        {
            ObjectProvider sm = ec.findObjectProvider(value);
            if (sm == null)
            {
                // Referenced object is not yet persistent, so persist it
                ec.persistObjectInternal(value, null, -1, ObjectProvider.PC);
                sm = ec.findObjectProvider(value);
                sm.flush(); // Make sure the object is in the datastore so the id is set
            }

            String refString = null;
            if (mappingStrategy == ID_MAPPING)
            {
                refString = value.getClass().getName() + ":" + sm.getInternalObjectId();
            }
            else if (mappingStrategy == XCALIA_MAPPING)
            {
                AbstractClassMetaData cmd = sm.getClassMetaData();
                DiscriminatorMetaData dismd = cmd.getDiscriminatorMetaData();
                String definer = null;
                if (dismd != null && dismd.getValue() != null)
                {
                    definer = dismd.getValue();
                }
                else
                {
                    definer = cmd.getFullClassName();
                }
                if (cmd.getIdentityType() == IdentityType.DATASTORE)
                {
                    refString = definer + ":" + ((OID)sm.getInternalObjectId()).getKeyValue();
                }
                else
                {
                    refString = definer + ":" + sm.getInternalObjectId().toString();
                }
            }
            return refString;
        }
        else
View Full Code Here

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

    ClassLoaderResolver clr = getClassLoaderResolver();
    RelationType relationType = mmd.getRelationType(clr);

    if (RelationType.isRelationSingleValued(relationType) && mmd.getEmbeddedMetaData() != null) {
      // Embedded persistable object
      ObjectProvider embeddedOP = getEmbeddedObjectProvider(mmd.getType(), fieldNumber, value);

      fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, mmd.getEmbeddedMetaData()));
      embeddedOP.provideFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);
      fieldManagerStateStack.removeFirst();

      return;
    } else if (RelationType.isRelationMultiValued(relationType) && mmd.isEmbedded()) {
      // Embedded container field
      if (mmd.hasCollection()) {
        // Embedded collections
        // This is stored flat with all property names for the element class gaining a suffix ".{index}"
        // so we have properties like "NAME.0", "PRICE.0", "NAME.1", "PRICE.1" etc.
        Class elementType = clr.classForName(mmd.getCollection().getElementType());
        Collection valueColl = (Collection) value;
        AbstractClassMetaData elemCmd = mmd.getCollection().getElementClassMetaData(clr, ec.getMetaDataManager());
        EmbeddedMetaData embmd =
          mmd.getElementMetaData() != null ? mmd.getElementMetaData().getEmbeddedMetaData() : null;

        // Add property for size of collection
        String collPropName = getPropertyNameForMember(mmd) + ".size";
        EntityUtils.setEntityProperty(datastoreEntity, mmd, collPropName,
            (valueColl != null ? valueColl.size() : -1));

        // Add discriminator for elements if defined
        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";
          }
        }

        if (valueColl != null) {
          Iterator collIter = valueColl.iterator();
          int index = 0;
          while (collIter.hasNext()) {
            Object element = collIter.next();

            ObjectProvider embeddedOP = getEmbeddedObjectProvider(elementType, fieldNumber, element);
            fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, embmd, index));
            embeddedOP.provideFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);

            // Add discriminator for elements if defined
            if (collDiscName != null) {
              EntityUtils.setEntityProperty(datastoreEntity, elemCmd.getDiscriminatorMetaDataRoot(),
                  collDiscName + "." + index, embeddedOP.getClassMetaData().getDiscriminatorValue());
            }

            fieldManagerStateStack.removeFirst();
            index++;
          }
        }
        return;
      } else if (mmd.hasArray()) {
        // Embedded arrays
        // This is stored flat with all property names for the element class gaining a suffix ".{index}"
        // so we have properties like "NAME.0", "PRICE.0", "NAME.1", "PRICE.1" etc.
        Class elementType = clr.classForName(mmd.getArray().getElementType());
        AbstractClassMetaData elemCmd = mmd.getArray().getElementClassMetaData(clr, ec.getMetaDataManager());
        EmbeddedMetaData embmd =
          mmd.getElementMetaData() != null ? mmd.getElementMetaData().getEmbeddedMetaData() : null;

        // Add property for size of array
        String arrBaseName = getPropertyNameForMember(mmd);
        EntityUtils.setEntityProperty(datastoreEntity, mmd, arrBaseName + ".size",
            (value != null ? Array.getLength(value) : -1));

        // Add discriminator for elements if defined
        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";
          }
        }

        if (value != null) {
          for (int i=0;i<Array.getLength(value);i++) {
            Object element = Array.get(value, i);

            ObjectProvider embeddedOP = getEmbeddedObjectProvider(elementType, fieldNumber, element);
            fieldManagerStateStack.addFirst(new FieldManagerState(embeddedOP, embmd, i));
            embeddedOP.provideFields(embeddedOP.getClassMetaData().getAllMemberPositions(), this);

            // Add discriminator for elements if defined
            if (arrDiscName != null) {
              EntityUtils.setEntityProperty(datastoreEntity, elemCmd.getDiscriminatorMetaDataRoot(),
                  arrDiscName + "." + i, embeddedOP.getClassMetaData().getDiscriminatorValue());
            }

            fieldManagerStateStack.removeFirst();
          }
        }
View Full Code Here

    Object childPC = pc;
    if (ec.getApiAdapter().isDetached(pc)) {
      // Child is detached, so attach it
      childPC = ec.persistObjectInternal(pc, null, -1, ObjectProvider.PC);
    }
    ObjectProvider childOP = ec.findObjectProvider(childPC);
    if (childOP == null) {
      // Not yet persistent, so persist it
      childPC = ec.persistObjectInternal(childPC, null, -1, ObjectProvider.PC);
      childOP = ec.findObjectProvider(childPC);
    }

    if (mmd.getAbstractClassMetaData().getIdentityType() == IdentityType.DATASTORE) {
      OID oid = (OID)childOP.getInternalObjectId();
      if (oid == null) {
        // Object has not yet flushed to the datastore
        childOP.flush();
      }
    } else {
      Object primaryKey = ec.getApiAdapter().getTargetKeyForSingleFieldIdentity(childOP.getInternalObjectId());
      if (primaryKey == null) {
        // Object has not yet flushed to the datastore
        childOP.flush();
      }
    }
    return childPC;
  }
View Full Code Here

    if (relationStoreInfos.isEmpty()) {
      // No relations waiting to be persisted
      return false;
    }

    ObjectProvider op = getObjectProvider();
    DatastoreTable table = getDatastoreTable();
    if (datastoreEntity.getKey() != null) {
      Key key = datastoreEntity.getKey();
      AbstractClassMetaData acmd = op.getClassMetaData();
      int[] relationFieldNums = acmd.getRelationMemberPositions(ec.getClassLoaderResolver(), ec.getMetaDataManager());
      if (relationFieldNums != null) {
        for (int i=0;i<relationFieldNums.length;i++) {
          AbstractMemberMetaData mmd = acmd.getMetaDataForManagedMemberAtAbsolutePosition(relationFieldNums[i]);
          boolean owned = MetaDataUtils.isOwnedRelation(mmd, getStoreManager());
          if (owned) {
            // Register parent key for all owned related objects
            Object childValue = op.provideField(mmd.getAbsoluteFieldNumber());
            if (childValue != null) {
              if (childValue instanceof Object[]) {
                childValue = Arrays.asList((Object[]) childValue);
              }

              String expectedType = mmd.getTypeName();
              if (mmd.getCollection() != null) {
                CollectionMetaData cmd = mmd.getCollection();
                expectedType = cmd.getElementType();
              } else if (mmd.getArray() != null) {
                ArrayMetaData amd = mmd.getArray();
                expectedType = amd.getElementType();
              }

              if (childValue instanceof Iterable) {
                // TODO(maxr): Make sure we're not pulling back unnecessary data when we iterate over the values.
                for (Object element : (Iterable) childValue) {
                  addToParentKeyMap(keyRegistry, element, key, ec, expectedType, true);
                }
              } else if (childValue.getClass().isArray()) {
                for (int j=0;j<Array.getLength(childValue);i++) {
                  addToParentKeyMap(keyRegistry, Array.get(childValue, i), key, ec, expectedType, true);
                }
              } else if (childValue instanceof Map) {
                boolean persistableKey = (mmd.getMap().getKeyClassMetaData(ec.getClassLoaderResolver(), ec.getMetaDataManager()) != null);
                boolean persistableVal = (mmd.getMap().getValueClassMetaData(ec.getClassLoaderResolver(), ec.getMetaDataManager()) != null);
                Iterator entryIter = ((Map)childValue).entrySet().iterator();
                while (entryIter.hasNext()) {
                  Map.Entry entry = (Map.Entry)entryIter.next();
                  if (persistableKey) {
                    addToParentKeyMap(keyRegistry, entry.getKey(), key, ec, expectedType, true);
                  }
                  if (persistableVal) {
                    addToParentKeyMap(keyRegistry, entry.getValue(), key, ec, expectedType, true);
                  }
                }
              } else {
                addToParentKeyMap(keyRegistry, childValue, key, ec, expectedType,
                    !table.isParentKeyProvider(mmd));
              }
            }
          } else {
            // Register that related object(s) is unowned
            Object childValue = op.provideField(mmd.getAbsoluteFieldNumber());
            if (childValue != null) {
              if (childValue instanceof Object[]) {
                childValue = Arrays.asList((Object[]) childValue);
              }

              if (childValue instanceof Iterable) {
                // TODO(maxr): Make sure we're not pulling back unnecessary data when we iterate over the values.
                for (Object element : (Iterable) childValue) {
                  keyRegistry.registerUnownedObject(element);
                }
              } else {
                keyRegistry.registerUnownedObject(childValue);
              }
            }
          }
        }
      }
    }

    boolean modifiedEntity = false;

    // Stage 1 : process FKs
    for (RelationStoreInformation relInfo : relationStoreInfos) {
      AbstractMemberMetaData mmd = relInfo.mmd;
      try {
        JavaTypeMapping mapping = table.getMemberMappingInDatastoreClass(relInfo.mmd);
        if (mapping instanceof EmbeddedPCMapping ||
            mapping instanceof SerialisedPCMapping ||
            mapping instanceof SerialisedReferenceMapping ||
            mapping instanceof PersistableMapping ||
            mapping instanceof InterfaceMapping) {
          boolean owned = MetaDataUtils.isOwnedRelation(mmd, getStoreManager());
          RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
          if (owned) {
            // Owned relation
            boolean owner = false;
            if (relationType == RelationType.ONE_TO_ONE_UNI || relationType == RelationType.ONE_TO_MANY_UNI ||
                relationType == RelationType.ONE_TO_MANY_BI) {
              owner = true;
            } else if (relationType == RelationType.ONE_TO_ONE_BI && mmd.getMappedBy() == null) {
              owner = true;
            }

            if (!table.isParentKeyProvider(mmd)) {
              // Make sure the parent key is set properly between parent and child objects
              if (!owner) {
                ObjectProvider parentOP = ec.findObjectProvider(relInfo.value);
                EntityUtils.checkParentage(op.getObject(), parentOP);
              } else {
                EntityUtils.checkParentage(relInfo.value, op);
              }
              mapping.setObject(ec, datastoreEntity, IS_FK_VALUE_ARR, relInfo.value, op, mmd.getAbsoluteFieldNumber());
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.