Exemple #1
0
  private void processDelayedUpdates(
      final List<EntityLinkDesc> delayedUpdates,
      int pos,
      final TransactionItems items,
      final ODataChangeset changeset) {

    for (EntityLinkDesc delayedUpdate : delayedUpdates) {
      pos++;
      items.put(delayedUpdate.getSource(), pos);

      final ODataEntity changes =
          ODataFactory.newEntity(delayedUpdate.getSource().getEntity().getName());

      AttachedEntityStatus status =
          EntityContainerFactory.getContext().entityContext().getStatus(delayedUpdate.getSource());

      final URI sourceURI;
      if (status == AttachedEntityStatus.CHANGED) {
        sourceURI =
            URIUtils.getURI(
                factory.getServiceRoot(),
                delayedUpdate.getSource().getEntity().getEditLink().toASCIIString());
      } else {
        int sourcePos = items.get(delayedUpdate.getSource());
        sourceURI = URI.create("$" + sourcePos);
      }

      for (EntityTypeInvocationHandler target : delayedUpdate.getTargets()) {
        status = EntityContainerFactory.getContext().entityContext().getStatus(target);

        final URI targetURI;
        if (status == AttachedEntityStatus.CHANGED) {
          targetURI =
              URIUtils.getURI(
                  factory.getServiceRoot(), target.getEntity().getEditLink().toASCIIString());
        } else {
          int targetPos = items.get(target);
          targetURI = URI.create("$" + targetPos);
        }

        changes.addLink(
            delayedUpdate.getType() == ODataLinkType.ENTITY_NAVIGATION
                ? ODataFactory.newEntityNavigationLink(delayedUpdate.getSourceName(), targetURI)
                : ODataFactory.newFeedNavigationLink(delayedUpdate.getSourceName(), targetURI));

        LOG.debug(
            "'{}' from {} to {}",
            new Object[] {delayedUpdate.getType().name(), sourceURI, targetURI});
      }

      batchUpdate(delayedUpdate.getSource(), sourceURI, changes, changeset);
    }
  }
Exemple #2
0
  private int processEntityContext(
      final EntityTypeInvocationHandler handler,
      int pos,
      final TransactionItems items,
      final List<EntityLinkDesc> delayedUpdates,
      final ODataChangeset changeset) {

    LOG.debug("Process '{}'", handler);

    items.put(handler, null);

    final ODataEntity entity = SerializationUtils.clone(handler.getEntity());
    entity.getNavigationLinks().clear();

    final AttachedEntityStatus currentStatus =
        EntityContainerFactory.getContext().entityContext().getStatus(handler);

    if (AttachedEntityStatus.DELETED != currentStatus) {
      entity.getProperties().clear();
      EngineUtils.addProperties(factory.getMetadata(), handler.getPropertyChanges(), entity);
    }

    for (Map.Entry<NavigationProperty, Object> property : handler.getLinkChanges().entrySet()) {
      final ODataLinkType type =
          Collection.class.isAssignableFrom(property.getValue().getClass())
              ? ODataLinkType.ENTITY_SET_NAVIGATION
              : ODataLinkType.ENTITY_NAVIGATION;

      final Set<EntityTypeInvocationHandler> toBeLinked =
          new HashSet<EntityTypeInvocationHandler>();
      final String serviceRoot = factory.getServiceRoot();

      for (Object proxy :
          type == ODataLinkType.ENTITY_SET_NAVIGATION
              ? (Collection) property.getValue()
              : Collections.singleton(property.getValue())) {

        final EntityTypeInvocationHandler target =
            (EntityTypeInvocationHandler) Proxy.getInvocationHandler(proxy);

        final AttachedEntityStatus status =
            EntityContainerFactory.getContext().entityContext().getStatus(target);

        final URI editLink = target.getEntity().getEditLink();

        if ((status == AttachedEntityStatus.ATTACHED || status == AttachedEntityStatus.LINKED)
            && !target.isChanged()) {
          entity.addLink(
              buildNavigationLink(
                  property.getKey().name(),
                  URIUtils.getURI(serviceRoot, editLink.toASCIIString()),
                  type));
        } else {
          if (!items.contains(target)) {
            pos = processEntityContext(target, pos, items, delayedUpdates, changeset);
            pos++;
          }

          final Integer targetPos = items.get(target);
          if (targetPos == null) {
            // schedule update for the current object
            LOG.debug("Schedule '{}' from '{}' to '{}'", type.name(), handler, target);
            toBeLinked.add(target);
          } else if (status == AttachedEntityStatus.CHANGED) {
            entity.addLink(
                buildNavigationLink(
                    property.getKey().name(),
                    URIUtils.getURI(serviceRoot, editLink.toASCIIString()),
                    type));
          } else {
            // create the link for the current object
            LOG.debug("'{}' from '{}' to (${}) '{}'", type.name(), handler, targetPos, target);

            entity.addLink(
                buildNavigationLink(property.getKey().name(), URI.create("$" + targetPos), type));
          }
        }
      }

      if (!toBeLinked.isEmpty()) {
        delayedUpdates.add(new EntityLinkDesc(property.getKey().name(), handler, toBeLinked, type));
      }
    }

    // insert into the batch
    LOG.debug("{}: Insert '{}' into the batch", pos, handler);
    batch(handler, entity, changeset);

    items.put(handler, pos);

    int startingPos = pos;

    if (handler.getEntity().isMediaEntity()) {

      // update media properties
      if (!handler.getPropertyChanges().isEmpty()) {
        final URI targetURI =
            currentStatus == AttachedEntityStatus.NEW
                ? URI.create("$" + startingPos)
                : URIUtils.getURI(
                    factory.getServiceRoot(), handler.getEntity().getEditLink().toASCIIString());
        batchUpdate(handler, targetURI, entity, changeset);
        pos++;
        items.put(handler, pos);
      }

      // update media content
      if (handler.getStreamChanges() != null) {
        final URI targetURI =
            currentStatus == AttachedEntityStatus.NEW
                ? URI.create("$" + startingPos + "/$value")
                : URIUtils.getURI(
                    factory.getServiceRoot(),
                    handler.getEntity().getEditLink().toASCIIString() + "/$value");

        batchUpdateMediaEntity(handler, targetURI, handler.getStreamChanges(), changeset);

        // update media info (use null key)
        pos++;
        items.put(null, pos);
      }
    }

    for (Map.Entry<String, InputStream> streamedChanges :
        handler.getStreamedPropertyChanges().entrySet()) {
      final URI targetURI =
          currentStatus == AttachedEntityStatus.NEW
              ? URI.create("$" + startingPos)
              : URIUtils.getURI(
                  factory.getServiceRoot(),
                  EngineUtils.getEditMediaLink(streamedChanges.getKey(), entity).toASCIIString());

      batchUpdateMediaResource(handler, targetURI, streamedChanges.getValue(), changeset);

      // update media info (use null key)
      pos++;
      items.put(handler, pos);
    }

    return pos;
  }
  public ODataEntity getNewCustomer(final int id, final String name, final boolean withInlineInfo) {

    final ODataEntity entity =
        ODataObjectFactory.newEntity(
            "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer");

    // add name attribute
    entity.addProperty(
        ODataObjectFactory.newPrimitiveProperty(
            "Name",
            client.getPrimitiveValueBuilder().setText(name).setType(EdmSimpleType.String).build()));

    // add key attribute
    if (id != 0) {
      entity.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "CustomerId",
              client
                  .getPrimitiveValueBuilder()
                  .setText(String.valueOf(id))
                  .setType(EdmSimpleType.Int32)
                  .build()));
    } else {
      entity.addProperty(
          ODataObjectFactory.newPrimitiveProperty(
              "CustomerId",
              client
                  .getPrimitiveValueBuilder()
                  .setText(String.valueOf(0))
                  .setType(EdmSimpleType.Int32)
                  .build()));
    }
    final ODataCollectionValue backupContactInfoValue =
        new ODataCollectionValue(
            "Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)");

    final ODataComplexValue contactDetails =
        new ODataComplexValue("Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails");

    final ODataCollectionValue altNamesValue = new ODataCollectionValue("Collection(Edm.String)");
    altNamesValue.add(
        client
            .getPrimitiveValueBuilder()
            .setText("My Alternative name")
            .setType(EdmSimpleType.String)
            .build());
    contactDetails.add(ODataObjectFactory.newCollectionProperty("AlternativeNames", altNamesValue));

    final ODataCollectionValue emailBagValue = new ODataCollectionValue("Collection(Edm.String)");
    emailBagValue.add(
        client
            .getPrimitiveValueBuilder()
            .setText("*****@*****.**")
            .setType(EdmSimpleType.String)
            .build());
    contactDetails.add(ODataObjectFactory.newCollectionProperty("EmailBag", emailBagValue));

    final ODataComplexValue contactAliasValue =
        new ODataComplexValue("Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases");
    contactDetails.add(ODataObjectFactory.newComplexProperty("ContactAlias", contactAliasValue));

    final ODataCollectionValue aliasAltNamesValue =
        new ODataCollectionValue("Collection(Edm.String)");
    aliasAltNamesValue.add(
        client
            .getPrimitiveValueBuilder()
            .setText("myAlternativeName")
            .setType(EdmSimpleType.String)
            .build());
    contactAliasValue.add(
        ODataObjectFactory.newCollectionProperty("AlternativeNames", aliasAltNamesValue));

    final ODataComplexValue homePhone =
        new ODataComplexValue("Microsoft.Test.OData.Services.AstoriaDefaultService.Phone");
    homePhone.add(
        ODataObjectFactory.newPrimitiveProperty(
            "PhoneNumber",
            client
                .getPrimitiveValueBuilder()
                .setText("8437568356834568")
                .setType(EdmSimpleType.String)
                .build()));
    homePhone.add(
        ODataObjectFactory.newPrimitiveProperty(
            "Extension",
            client
                .getPrimitiveValueBuilder()
                .setText("124365426534621534423ttrf")
                .setType(EdmSimpleType.String)
                .build()));
    contactDetails.add(ODataObjectFactory.newComplexProperty("HomePhone", homePhone));

    backupContactInfoValue.add(contactDetails);
    entity.addProperty(
        ODataObjectFactory.newCollectionProperty("BackupContactInfo", backupContactInfoValue));
    if (withInlineInfo) {
      final ODataInlineEntity inlineInfo =
          ODataObjectFactory.newInlineEntity(
              "Info", URI.create("Customer(" + id + ")/Info"), getInfo(id, name + "_Info"));
      inlineInfo.getEntity().setMediaEntity(true);
      entity.addLink(inlineInfo);
    }

    return entity;
  }