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

The following examples show how to use org.apache.olingo.commons.api.data.Entity#getAnnotations() . 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: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writeDeletedEntity(Entity deletedEntity,
    JsonGenerator json) throws IOException, SerializerException {
  if (deletedEntity.getId() == null) {
    throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
  }
  json.writeStartObject();
  if (isODataMetadataFull) {

    json.writeStringField(Constants.AT + Constants.CONTEXT, Constants.HASH + deletedEntity.getId().toASCIIString()
        + Constants.DELETEDENTITY);
  }
  if (((DeletedEntity) deletedEntity).getReason() == null) {
    throw new SerializerException("DeletedEntity reason is null.",
        SerializerException.MessageKeys.MISSING_DELTA_PROPERTY, Constants.REASON);
  }
  json.writeFieldName(Constants.AT + Constants.REMOVED);
  json.writeStartObject();
  json.writeStringField(Constants.ELEM_REASON,
      ((DeletedEntity) deletedEntity).getReason().name());
  List<Annotation> annotations = deletedEntity.getAnnotations();
  if (annotations != null && !annotations.isEmpty()) {
    for (Annotation annotation : annotations) {
      json.writeStringField(Constants.AT + annotation.getTerm(), annotation.getValue().toString());
    }
  }
  json.writeEndObject();
  List<Property> properties = deletedEntity.getProperties();
  if (properties != null && !properties.isEmpty()) {
    for (Property property : properties) {
      json.writeStringField(property.getName(), property.getValue().toString());
    }
  }
  json.writeStringField(Constants.ID,  deletedEntity.getId().toASCIIString());
  json.writeEndObject();

}
 
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();
}