Java Code Examples for org.apache.olingo.commons.api.data.Entity#getEditLink()

The following examples show how to use org.apache.olingo.commons.api.data.Entity#getEditLink() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EdmAssistedJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void doSerialize(final EdmEntityType entityType, final Entity entity,
    final String contextURLString, final String metadataETag, JsonGenerator json)
    throws IOException, SerializerException {

  json.writeStartObject();

  final String typeName = entity.getType() == null ? null : new EdmTypeInfo.Builder().setTypeExpression(entity
      .getType()).build().external();
  metadata(contextURLString, metadataETag, entity.getETag(), typeName, entity.getId(), true, json);

  for (final Annotation annotation : entity.getAnnotations()) {
    valuable(json, annotation, '@' + annotation.getTerm(), null, null);
  }

  for (final Property property : entity.getProperties()) {
    final String name = property.getName();
    final EdmProperty edmProperty = entityType == null || entityType.getStructuralProperty(name) == null ? null
        : entityType.getStructuralProperty(name);
    valuable(json, property, name, edmProperty == null ? null : edmProperty.getType(), edmProperty);
  }

  if (!isODataMetadataNone &&
      entity.getEditLink() != null && entity.getEditLink().getHref() != null) {
    json.writeStringField(Constants.JSON_EDIT_LINK, entity.getEditLink().getHref());

    if (entity.isMediaEntity()) {
      json.writeStringField(Constants.JSON_MEDIA_READ_LINK, entity.getEditLink().getHref() + "/$value");
    }
  }

  links(entity, entityType, json);

  json.writeEndObject();
}
 
Example 2
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void writeEntity(final ServiceMetadata metadata, final EdmEntityType entityType, final Entity entity,
    final ContextURL contextURL, final ExpandOption expand, Integer toDepth, 
    final SelectOption select, final boolean onlyReference, Set<String> ancestors, 
    String name, final JsonGenerator json)
    throws IOException, SerializerException, DecoderException {
  boolean cycle = false;
  if (expand != null) {
    if (ancestors == null) {
      ancestors = new HashSet<>();
    }
    cycle = !ancestors.add(getEntityId(entity, entityType, name));
  }
  try {
    json.writeStartObject();
    if (!isODataMetadataNone) {
      // top-level entity
      if (contextURL != null) {
        writeContextURL(contextURL, json);
        writeMetadataETag(metadata, json);
      }
      if (entity.getETag() != null) {
        json.writeStringField(constants.getEtag(), entity.getETag());
      }
      if (entityType.hasStream()) {
        if (entity.getMediaETag() != null) {
          json.writeStringField(constants.getMediaEtag(), entity.getMediaETag());
        }
        if (entity.getMediaContentType() != null) {
          json.writeStringField(constants.getMediaContentType(), entity.getMediaContentType());
        }
        if (entity.getMediaContentSource() != null) {
          json.writeStringField(constants.getMediaReadLink(), entity.getMediaContentSource().toString());
        }
        if (entity.getMediaEditLinks() != null && !entity.getMediaEditLinks().isEmpty()) {
          json.writeStringField(constants.getMediaEditLink(), entity.getMediaEditLinks().get(0).getHref());
        }
      }
    }
    if (cycle || onlyReference) {
      json.writeStringField(constants.getId(), getEntityId(entity, entityType, name));
    } else {
      final EdmEntityType resolvedType = resolveEntityType(metadata, entityType, entity.getType());
      if ((!isODataMetadataNone && !resolvedType.equals(entityType)) || isODataMetadataFull) {
        json.writeStringField(constants.getType(), "#" + entity.getType());
      }
      if ((!isODataMetadataNone && !areKeyPredicateNamesSelected(select, resolvedType)) || isODataMetadataFull) {
        json.writeStringField(constants.getId(), getEntityId(entity, resolvedType, name));
      }
      
      if (isODataMetadataFull) {
        if (entity.getSelfLink() != null) {
          json.writeStringField(constants.getReadLink(), entity.getSelfLink().getHref());
        }
        if (entity.getEditLink() != null) {
          json.writeStringField(constants.getEditLink(), entity.getEditLink().getHref());
        }
      }
      
      writeProperties(metadata, resolvedType, entity.getProperties(), select, json, entity, expand);
      writeNavigationProperties(metadata, resolvedType, entity, expand, toDepth, ancestors, name, json);
      writeOperations(entity.getOperations(), json);      
    }
    json.writeEndObject();
  } finally {
    if (expand != null && !cycle && ancestors != null) {
      ancestors.remove(getEntityId(entity, entityType, name));
    }
  }
}
 
Example 3
Source File: JsonEntitySerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void doContainerSerialize(final ResWrap<Entity> container, final JsonGenerator jgen)
    throws IOException, EdmPrimitiveTypeException {

  final Entity entity = container.getPayload();

  jgen.writeStartObject();

  if (serverMode && !isODataMetadataNone) {
    if (container.getContextURL() != null) {
      jgen.writeStringField(Constants.JSON_CONTEXT, container.getContextURL().toASCIIString());
    }
    if (container.getMetadataETag() != null) {
      jgen.writeStringField(Constants.JSON_METADATA_ETAG, container.getMetadataETag());
    }

    if (entity.getETag() != null) {
      jgen.writeStringField(Constants.JSON_ETAG, entity.getETag());
    }
  }

  if (entity.getType() != null && isODataMetadataFull) {
    jgen.writeStringField(Constants.JSON_TYPE,
        new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external());
  }

  if (entity.getId() != null && isODataMetadataFull) {
    jgen.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString());
  }

  for (Annotation annotation : entity.getAnnotations()) {
    valuable(jgen, annotation, "@" + annotation.getTerm());
  }

  for (Property property : entity.getProperties()) {
    valuable(jgen, property, property.getName());
  }

  if (serverMode && entity.getEditLink() != null && 
      entity.getEditLink().getHref() != null && isODataMetadataFull) {
    jgen.writeStringField(Constants.JSON_EDIT_LINK, entity.getEditLink().getHref());

    if (entity.isMediaEntity() && isODataMetadataFull) {
      jgen.writeStringField(Constants.JSON_MEDIA_READ_LINK,
          entity.getEditLink().getHref() + "/$value");
    }
  }

  if (!isODataMetadataNone) {
    links(entity, jgen);
  }

  if (isODataMetadataFull) {
    for (Link link : entity.getMediaEditLinks()) {
      if (link.getTitle() == null) {
        jgen.writeStringField(Constants.JSON_MEDIA_EDIT_LINK, link.getHref());
      }

      if (link.getInlineEntity() != null) {
        jgen.writeObjectField(link.getTitle(), link.getInlineEntity());
      }
      if (link.getInlineEntitySet() != null) {
        jgen.writeArrayFieldStart(link.getTitle());
        for (Entity subEntry : link.getInlineEntitySet().getEntities()) {
          jgen.writeObject(subEntry);
        }
        jgen.writeEndArray();
      }
    }
  }

  if (serverMode && isODataMetadataFull) {
    for (Operation operation : entity.getOperations()) {
      final String anchor = operation.getMetadataAnchor();
      final int index = anchor.lastIndexOf('#');
      jgen.writeObjectFieldStart('#' + anchor.substring(index < 0 ? 0 : (index + 1)));
      jgen.writeStringField(Constants.ATTR_TITLE, operation.getTitle());
      jgen.writeStringField(Constants.ATTR_TARGET, operation.getTarget().toASCIIString());
      jgen.writeEndObject();
    }
  }

  jgen.writeEndObject();
}