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

The following examples show how to use org.apache.olingo.commons.api.data.Entity#getId() . 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: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void writeAddedUpdatedEntity(final ServiceMetadata metadata, final EdmEntityType entityType,
    final Entity entity, final ExpandOption expand, final SelectOption select, final ContextURL url,
    final boolean onlyReference, String name, final JsonGenerator json, boolean isFullRepresentation)
    throws IOException, SerializerException {
  json.writeStartObject();
  if (entity.getId() != null && url != null) {
    name = url.getEntitySetOrSingletonOrType();
    String entityId = entity.getId().toString();
    if (!entityId.contains(name)) {
      String entityName = entityId.substring(0, entityId.indexOf("("));
      if (!entityName.equals(name)) {
        json.writeStringField(Constants.AT + Constants.CONTEXT, Constants.HASH + entityName
            + Constants.ENTITY);
      }
    }
  }
  String id = getEntityId(entity, entityType, name);
  json.writeStringField(Constants.AT + Constants.ATOM_ATTR_ID, id);
  writeProperties(metadata, entityType, entity.getProperties(), select, json);
  writeNavigationProperties(metadata, entityType, entity, expand, id, json, isFullRepresentation);
  json.writeEndObject();

}
 
Example 2
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Get the ascii representation of the entity id
 * or thrown an {@link SerializerException} if id is <code>null</code>.
 *
 * @param entity the entity
 * @return ascii representation of the entity id
 */
private String getEntityId(Entity entity, EdmEntityType entityType, String name) throws SerializerException {
  try {
    if (entity != null) {
      if (entity.getId() == null) {
        if (entityType == null || entityType.getKeyPredicateNames() == null
            || name == null) {
          throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
        } else {
          final UriHelper uriHelper = new UriHelperImpl();
          entity.setId(URI.create(name + '(' + uriHelper.buildKeyPredicate(entityType, entity) + ')'));
          return entity.getId().toASCIIString();
        }
      } else {
        return entity.getId().toASCIIString();
      }
    }
    return null;
  } catch (Exception e) {
    throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
  }
}
 
Example 3
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void writeAddedUpdatedEntity(final ServiceMetadata metadata, final EdmEntityType entityType,
    final Entity entity, final ExpandOption expand, final SelectOption select, final ContextURL url,
    final boolean onlyReference, String name, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartObject();
  if (entity.getId() != null && url != null) {
    String entityId = entity.getId().toString();
    name = url.getEntitySetOrSingletonOrType();
    if (!entityId.contains(name)) {
      String entityName = entityId.substring(0, entityId.indexOf("("));
      if (!entityName.equals(name)) {
        json.writeStringField(Constants.JSON_CONTEXT, HASH + entityName + ENTITY);
      }
    }
  }
  json.writeStringField(Constants.JSON_ID, getEntityId(entity, entityType, name));
  writeProperties(metadata, entityType, entity.getProperties(), select, json);
  json.writeEndObject();

}
 
Example 4
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Get the ascii representation of the entity id
 * or thrown an {@link SerializerException} if id is <code>null</code>.
 *
 * @param entity the entity
 * @return ascii representation of the entity id
 */
private String getEntityId(Entity entity, EdmEntityType entityType, String name) throws SerializerException {
  try {
    if (entity != null) {
      if (entity.getId() == null) {
        if (entityType == null || entityType.getKeyPredicateNames() == null
            || name == null) {
          throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
        } else {
          final UriHelper uriHelper = new UriHelperImpl();
          entity.setId(URI.create(name + '(' + uriHelper.buildKeyPredicate(entityType, entity) + ')'));
          return entity.getId().toASCIIString();
        }
      } else {
        return entity.getId().toASCIIString();
      }
    } 
    return null;
  } catch (Exception e) {
    throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
  }
}
 
Example 5
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Get the ascii representation of the entity id
 * or thrown an {@link SerializerException} if id is <code>null</code>.
 *
 * @param entity the entity
 * @param entityType 
 * @param name 
 * @return ascii representation of the entity id
 */
private String getEntityId(Entity entity, EdmEntityType entityType, String name) throws SerializerException {
  if(entity != null && entity.getId() == null) {
    if(entityType == null || entityType.getKeyPredicateNames() == null 
        || name == null) {
      throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
    }else{
      final UriHelper uriHelper = new UriHelperImpl(); 
      entity.setId(URI.create(name + '(' + uriHelper.buildKeyPredicate(entityType, entity) + ')'));
    }
  }
  return entity.getId().toASCIIString();
}
 
Example 6
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 7
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
/**
 * Get the ascii representation of the entity id
 * or thrown an {@link SerializerException} if id is <code>null</code>.
 *
 * @param entity the entity
 * @param entityType the entity Type
 * @param name the entity name
 * @return ascii representation of the entity id
 */
private String getEntityId(Entity entity, EdmEntityType entityType, String name) throws SerializerException {
  if(entity.getId() == null) {
    if((entity == null || entityType == null || entityType.getKeyPredicateNames() == null 
        || name == null)) {
      throw new SerializerException("Entity id is null.", SerializerException.MessageKeys.MISSING_ID);
    }else{
      final UriHelper uriHelper = new UriHelperImpl(); 
      entity.setId(URI.create(name + '(' + uriHelper.buildKeyPredicate(entityType, entity) + ')'));
    }
  }
  return entity.getId().toASCIIString();
}
 
Example 8
Source File: ODataXmlSerializer.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, final Integer toDepth,
    final SelectOption select, final String xml10InvalidCharReplacement,
    final XMLStreamWriter writer, final boolean top, final boolean writeOnlyRef,String name,  Set<String> ancestors)
    throws XMLStreamException, SerializerException {
  boolean cycle = false;
  if (expand != null) {
    if (ancestors == null) {
      ancestors = new HashSet<String>();
    }
    cycle = !ancestors.add(getEntityId(entity, entityType, name));
  }

  if (cycle || writeOnlyRef) {
    writeReference(entity, contextURL, writer, top);
    return;
  }
  try {
    writer.writeStartElement(ATOM, Constants.ATOM_ELEM_ENTRY, NS_ATOM);
    if (top) {
      writer.writeNamespace(ATOM, NS_ATOM);
      writer.writeNamespace(METADATA, NS_METADATA);
      writer.writeNamespace(DATA, NS_DATA);

      if (contextURL != null) { // top-level entity
        writer.writeAttribute(METADATA, NS_METADATA, Constants.CONTEXT,
            ContextURLBuilder.create(contextURL).toASCIIString());
        writeMetadataETag(metadata, writer);
      }
    }
    if (entity.getETag() != null) {
      writer.writeAttribute(METADATA, NS_METADATA, Constants.ATOM_ATTR_ETAG, entity.getETag());
    }

    if (entity.getId() != null) {
      writer.writeStartElement(NS_ATOM, Constants.ATOM_ELEM_ID);
      writer.writeCharacters(entity.getId().toASCIIString());
      writer.writeEndElement();
    }

    writerAuthorInfo(entity.getTitle(), writer);

    if (entity.getId() != null) {
      writer.writeStartElement(NS_ATOM, Constants.ATOM_ELEM_LINK);
      writer.writeAttribute(Constants.ATTR_REL, Constants.EDIT_LINK_REL);
      writer.writeAttribute(Constants.ATTR_HREF, entity.getId().toASCIIString());
      writer.writeEndElement();
    }

    if (entityType.hasStream()) {
      writer.writeStartElement(NS_ATOM, Constants.ATOM_ELEM_CONTENT);
      writer.writeAttribute(Constants.ATTR_TYPE, entity.getMediaContentType());
      if (entity.getMediaContentSource() != null) {
        writer.writeAttribute(Constants.ATOM_ATTR_SRC, entity.getMediaContentSource().toString());
      } else {
        String id = entity.getId().toASCIIString();
        writer.writeAttribute(Constants.ATOM_ATTR_SRC,
            id + (id.endsWith("/") ? "" : "/") + "$value");
      }
      writer.writeEndElement();
    }

    // write media links
    for (Link link : entity.getMediaEditLinks()) {
      writeLink(writer, link);
    }

    EdmEntityType resolvedType = resolveEntityType(metadata, entityType, entity.getType());
    writeNavigationProperties(metadata, resolvedType, entity, expand,
      toDepth, xml10InvalidCharReplacement, ancestors, name, writer);

    writer.writeStartElement(ATOM, Constants.ATOM_ELEM_CATEGORY, NS_ATOM);
    writer.writeAttribute(Constants.ATOM_ATTR_SCHEME, Constants.NS_SCHEME);
    writer.writeAttribute(Constants.ATOM_ATTR_TERM,
        "#" + resolvedType.getFullQualifiedName().getFullQualifiedNameAsString());
    writer.writeEndElement();

    // In the case media, content is sibiling
    if (!entityType.hasStream()) {
      writer.writeStartElement(NS_ATOM, Constants.ATOM_ELEM_CONTENT);
      writer.writeAttribute(Constants.ATTR_TYPE, "application/xml");
    }

    writer.writeStartElement(METADATA, Constants.PROPERTIES, NS_METADATA);
    writeProperties(metadata, resolvedType, entity.getProperties(), select, 
        xml10InvalidCharReplacement, writer, entity, expand);
    writer.writeEndElement(); // properties

    if (!entityType.hasStream()) { // content
      writer.writeEndElement();
    }
    
    writeOperations(entity.getOperations(), writer);
    
    writer.writeEndElement(); // entry
  } finally {
    if (!cycle && ancestors != null) {
      ancestors.remove(getEntityId(entity, entityType, name));
    }
  }
}
 
Example 9
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();
}