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

The following examples show how to use org.apache.olingo.commons.api.data.Entity#setType() . 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: Services.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@GET
@Path("/StoredPIs(1000)")
public Response getStoredPI(@Context final UriInfo uriInfo) {
  final Entity entity = new Entity();
  entity.setType("Microsoft.Test.OData.Services.ODataWCFService.StoredPI");
  final Property id = new Property();
  id.setType("Edm.Int32");
  id.setName("StoredPIID");
  id.setValue(ValueType.PRIMITIVE, 1000);
  entity.getProperties().add(id);
  final Link edit = new Link();
  edit.setHref(uriInfo.getRequestUri().toASCIIString());
  edit.setRel("edit");
  edit.setTitle("StoredPI");
  entity.setEditLink(edit);

  final ByteArrayOutputStream content = new ByteArrayOutputStream();
  final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
  try {
    jsonSerializer.write(writer, new ResWrap<Entity>((URI) null, null, entity));
    return xml.createResponse(new ByteArrayInputStream(content.toByteArray()), null, Accept.JSON_FULLMETA);
  } catch (Exception e) {
    LOG.error("While creating StoredPI", e);
    return xml.createFaultResponse(Accept.JSON_FULLMETA.toString(), e);
  }
}
 
Example 2
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public Entity create(final EdmEntitySet edmEntitySet) throws DataProviderException {
  final EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  EntityCollection entitySet = readAll(edmEntitySet);
  final List<Entity> entities = entitySet.getEntities();
  final Map<String, Object> newKey = findFreeComposedKey(entities, edmEntityType);
  Entity newEntity = new Entity();
  newEntity.setType(edmEntityType.getFullQualifiedName().getFullQualifiedNameAsString());
  for (final String keyName : edmEntityType.getKeyPredicateNames()) {
    newEntity.addProperty(DataCreator.createPrimitive(keyName, newKey.get(keyName)));
  }

  createProperties(edmEntityType, newEntity.getProperties());
  try {
    newEntity.setId(URI.create(odata.createUriHelper().buildCanonicalURL(edmEntitySet, newEntity)));
  } catch (final SerializerException e) {
    throw new DataProviderException("Unable to set entity ID!", HttpStatusCode.INTERNAL_SERVER_ERROR, e);
  }
  entities.add(newEntity);

  return newEntity;
}
 
Example 3
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void metadataMin() throws Exception {
  final ServiceMetadata metadata = oData.createServiceMetadata(null, Collections.<EdmxReference> emptyList(),
      new MetadataETagSupport("W/\"42\""));
  Entity entity = new Entity();
  entity.setType("Namespace.EntityType");
  entity.setId(URI.create("ID"));
  entity.setETag("W/\"1000\"");
  Link link = new Link();
  link.setHref("editLink");
  entity.setEditLink(link);
  entity.setMediaContentSource(URI.create("media"));
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE,
      UUID.fromString("12345678-ABCD-1234-CDEF-123456789012")));
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1)\","
      + "\"@odata.metadataEtag\":\"W/\\\"42\\\"\",\"value\":[{"
      + "\"@odata.etag\":\"W/\\\"1000\\\"\","
      + "\"Property1\":\"12345678-abcd-1234-cdef-123456789012\","
      + "\"@odata.editLink\":\"editLink\","
      + "\"@odata.mediaReadLink\":\"editLink/$value\"}]}",
      serialize(serializerMin, metadata, null, entityCollection, null));
}
 
Example 4
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity consumeEntityNode(final EdmEntityType edmEntityType, final ObjectNode tree,
    final ExpandTreeBuilder expandBuilder) throws DeserializerException {
  Entity entity = new Entity();
  entity.setType(edmEntityType.getFullQualifiedName().getFullQualifiedNameAsString());
  
  // Check and consume @id for v4.01
  consumeId(tree, entity);

  // Check and consume all Properties
  consumeEntityProperties(edmEntityType, tree, entity);

  // Check and consume all expanded Navigation Properties
  consumeExpandedNavigationProperties(edmEntityType, tree, entity, expandBuilder);

  // consume delta json node fields for v4.01
  consumeDeltaJsonNodeFields(edmEntityType, tree, entity, expandBuilder);
  
  // consume remaining json node fields
  consumeRemainingJsonNodeFields(edmEntityType, tree, entity);

  assertJsonNodeIsEmpty(tree);

  return entity;
}
 
Example 5
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity createEntity(EdmEntitySet edmEntitySet, EdmEntityType edmEntityType, Entity entity, 
    List<Entity> entityList, final String rawServiceUri) throws ODataApplicationException {
  
  // 1.) Create the entity
  final Entity newEntity = new Entity();
  newEntity.setType(entity.getType());
  
  // Create the new key of the entity
  int newId = 1;
  while (entityIdExists(newId, entityList)) {
    newId++;
  }
  
  // Add all provided properties
  newEntity.getProperties().addAll(entity.getProperties());
  
  // Add the key property
  newEntity.getProperties().add(new Property(null, "ID", ValueType.PRIMITIVE, newId));
  newEntity.setId(createId(newEntity, "ID"));
  
  // --> Implement Deep Insert handling here <--
  
  entityList.add(newEntity);

  return newEntity;
}
 
Example 6
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private EntityCollection createManufacturers() {
  EntityCollection entitySet = new EntityCollection();

  Entity el = new Entity()
      .addProperty(createPrimitive("Id", 1))
      .addProperty(createPrimitive("Name", "Star Powered Racing"))
      .addProperty(createAddress("Star Street 137", "Stuttgart", "70173", "Germany"));
  el.setId(createId(CarsEdmProvider.ES_MANUFACTURER_NAME, 1));
  entitySet.getEntities().add(el);

  el = new Entity()
      .addProperty(createPrimitive("Id", 2))
      .addProperty(createPrimitive("Name", "Horse Powered Racing"))
      .addProperty(createAddress("Horse Street 1", "Maranello", "41053", "Italy"));
  el.setId(createId(CarsEdmProvider.ES_MANUFACTURER_NAME, 2));
  entitySet.getEntities().add(el);

  for (Entity entity:entitySet.getEntities()) {
    entity.setType(CarsEdmProvider.ET_MANUFACTURER.getFullQualifiedNameAsString());
  }
  return entitySet;
}
 
Example 7
Source File: DataCreator.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EntityCollection createESTwoPrimDerived(final Edm edm, final OData odata) {
  EntityCollection entityCollection = new EntityCollection();

  entityCollection.getEntities().add(new Entity()
      .addProperty(createPrimitive("PropertyInt16", (short) -365))
      .addProperty(createPrimitive("PropertyString", "Test String2")));

  entityCollection.getEntities().add(new Entity()
      .addProperty(createPrimitive("PropertyInt16", (short) -32766))
      .addProperty(createPrimitive("PropertyString", null)));

  entityCollection.getEntities().add(new Entity()
      .addProperty(createPrimitive("PropertyInt16", Short.MAX_VALUE))
      .addProperty(createPrimitive("PropertyString", "Test String4")));

  setEntityType(entityCollection, edm.getEntityType(EntityTypeProvider.nameETTwoPrim));
  
  Entity derivedEntity = new Entity()
      .addProperty(createPrimitive("PropertyInt16", (short) 32766))
      .addProperty(createPrimitive("PropertyString", "Test String1"))
      .addProperty(createPrimitive("AdditionalPropertyString_5", "Additional String1"));
      derivedEntity.setType(EntityTypeProvider.nameETBase.getFullQualifiedNameAsString());
      entityCollection.getEntities().add(derivedEntity);
      
  createEntityId(edm, odata, "ESTwoPrimDerived", entityCollection);
  createOperations("ESTwoPrimDerived", entityCollection, EntityTypeProvider.nameETTwoPrim);
  return entityCollection;
}
 
Example 8
Source File: ODataBinderImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void copyAnnotations(Entity inlineEntity, ComplexValue complex) {
  for (Annotation annotation:complex.getAnnotations()) {
    if (annotation.getTerm().equals(Constants.JSON_TYPE.substring(1))){
      inlineEntity.setType((String)annotation.asPrimitive());
    } else if (annotation.getTerm().equals(Constants.JSON_ID.substring(1))){
      inlineEntity.setId(URI.create((String)annotation.asPrimitive()));
    } else if (annotation.getTerm().equals(Constants.JSON_ETAG.substring(1))){
      inlineEntity.setETag((String)annotation.asPrimitive());
    }
  }
}
 
Example 9
Source File: TransactionalEntityManager.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Entity copyEntity(final Entity entity) {
  final Entity newEntity = new Entity();
  newEntity.setBaseURI(entity.getBaseURI());
  newEntity.setEditLink(entity.getEditLink());
  newEntity.setETag(entity.getETag());
  newEntity.setId(entity.getId());
  newEntity.setMediaContentSource(entity.getMediaContentSource());
  newEntity.setMediaContentType(entity.getMediaContentType());
  newEntity.setSelfLink(entity.getSelfLink());
  newEntity.setMediaETag(entity.getMediaETag());
  newEntity.setType(entity.getType());
  newEntity.getProperties().addAll(entity.getProperties());
  
  return newEntity;
}
 
Example 10
Source File: TransactionalEntityManager.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Entity copyEntity(final Entity entity) {
  final Entity newEntity = new Entity();
  newEntity.setBaseURI(entity.getBaseURI());
  newEntity.setEditLink(entity.getEditLink());
  newEntity.setETag(entity.getETag());
  newEntity.setId(entity.getId());
  newEntity.setMediaContentSource(entity.getMediaContentSource());
  newEntity.setMediaContentType(entity.getMediaContentType());
  newEntity.setSelfLink(entity.getSelfLink());
  newEntity.setMediaETag(entity.getMediaETag());
  newEntity.setType(entity.getType());
  newEntity.getProperties().addAll(entity.getProperties());
  
  return newEntity;
}
 
Example 11
Source File: TransactionalEntityManager.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private Entity copyEntity(final Entity entity) {
  final Entity newEntity = new Entity();
  newEntity.setBaseURI(entity.getBaseURI());
  newEntity.setEditLink(entity.getEditLink());
  newEntity.setETag(entity.getETag());
  newEntity.setId(entity.getId());
  newEntity.setMediaContentSource(entity.getMediaContentSource());
  newEntity.setMediaContentType(entity.getMediaContentType());
  newEntity.setSelfLink(entity.getSelfLink());
  newEntity.setMediaETag(entity.getMediaETag());
  newEntity.setType(entity.getType());
  newEntity.getProperties().addAll(entity.getProperties());
  
  return newEntity;
}
 
Example 12
Source File: Services.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@GET
@Path("/Products({entityId})/Microsoft.Test.OData.Services.ODataWCFService.GetProductDetails({param:.*})")
public Response functionGetProductDetails(
    @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept,
    @PathParam("entityId") final String entityId,
    @QueryParam("$format") @DefaultValue(StringUtils.EMPTY) final String format) {

  try {
    final Accept acceptType;
    if (StringUtils.isNotBlank(format)) {
      acceptType = Accept.valueOf(format.toUpperCase());
    } else {
      acceptType = Accept.parse(accept);
    }

    final Entity entry = new Entity();
    entry.setType("Microsoft.Test.OData.Services.ODataWCFService.ProductDetail");
    final Property productId = new Property();
    productId.setName("ProductID");
    productId.setType("Edm.Int32");
    productId.setValue(ValueType.PRIMITIVE, Integer.valueOf(entityId));
    entry.getProperties().add(productId);
    final Property productDetailId = new Property();
    productDetailId.setName("ProductDetailID");
    productDetailId.setType("Edm.Int32");
    productDetailId.setValue(ValueType.PRIMITIVE, 2);
    entry.getProperties().add(productDetailId);

    final Link link = new Link();
    link.setRel("edit");
    link.setHref(URI.create(
        Constants.get(ConstantKey.DEFAULT_SERVICE_URL)
            + "ProductDetails(ProductID=6,ProductDetailID=1)").toASCIIString());
    entry.setEditLink(link);

    final EntityCollection feed = new EntityCollection();
    feed.getEntities().add(entry);

    final ResWrap<EntityCollection> container = new ResWrap<EntityCollection>(
        URI.create(Constants.get(ConstantKey.ODATA_METADATA_PREFIX) + "ProductDetail"), null,
        feed);

    return xml.createResponse(
        null,
        xml.writeEntitySet(acceptType, container),
        null,
        acceptType);
  } catch (Exception e) {
    return xml.createFaultResponse(accept, e);
  }
}
 
Example 13
Source File: DataCreator.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void setEntityType(EntityCollection entityCollection, final EdmEntityType type) {
  for (Entity entity : entityCollection.getEntities()) {
    entity.setType(type.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}
 
Example 14
Source File: ODataBinderImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public Entity getEntity(final ClientEntity odataEntity) {
  final Entity entity = new Entity();

  entity.setType(odataEntity.getTypeName() == null ? null : odataEntity.getTypeName().toString());

  // -------------------------------------------------------------
  // Add edit and self link
  // -------------------------------------------------------------
  final URI odataEditLink = odataEntity.getEditLink();
  if (odataEditLink != null) {
    final Link editLink = new Link();
    editLink.setTitle(entity.getType());
    editLink.setHref(odataEditLink.toASCIIString());
    editLink.setRel(Constants.EDIT_LINK_REL);
    entity.setEditLink(editLink);
  }

  if (odataEntity.isReadOnly()) {
    final Link selfLink = new Link();
    selfLink.setTitle(entity.getType());
    selfLink.setHref(odataEntity.getLink().toASCIIString());
    selfLink.setRel(Constants.SELF_LINK_REL);
    entity.setSelfLink(selfLink);
  }
  // -------------------------------------------------------------

  links(odataEntity, entity);

  // -------------------------------------------------------------
  // Append edit-media links
  // -------------------------------------------------------------
  for (ClientLink link : odataEntity.getMediaEditLinks()) {
    LOG.debug("Append edit-media link\n{}", link);
    entity.getMediaEditLinks().add(getLink(link));
  }
  // -------------------------------------------------------------

  if (odataEntity.isMediaEntity()) {
    entity.setMediaContentSource(odataEntity.getMediaContentSource());
    entity.setMediaContentType(odataEntity.getMediaContentType());
    entity.setMediaETag(odataEntity.getMediaETag());
  }

  for (ClientProperty property : odataEntity.getProperties()) {
    entity.getProperties().add(getProperty(property));
  }

  entity.setId(odataEntity.getId());
  annotations(odataEntity, entity);
  return entity;
}
 
Example 15
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private EntityCollection createCars() {
  EntityCollection entitySet = new EntityCollection();
  Entity el = new Entity()
      .addProperty(createPrimitive("Id", 1))
      .addProperty(createPrimitive("Model", "F1 W03"))
      .addProperty(createPrimitive("ModelYear", "2012"))
      .addProperty(createPrimitive("Price", 189189.43))
      .addProperty(createPrimitive("Currency", "EUR"));
  el.setId(createId(CarsEdmProvider.ES_CARS_NAME, 1));
  entitySet.getEntities().add(el);

  el = new Entity()
      .addProperty(createPrimitive("Id", 2))
      .addProperty(createPrimitive("Model", "F1 W04"))
      .addProperty(createPrimitive("ModelYear", "2013"))
      .addProperty(createPrimitive("Price", 199999.99))
      .addProperty(createPrimitive("Currency", "EUR"));
  el.setId(createId(CarsEdmProvider.ES_CARS_NAME, 2));
  entitySet.getEntities().add(el);

  el = new Entity()
      .addProperty(createPrimitive("Id", 3))
      .addProperty(createPrimitive("Model", "F2012"))
      .addProperty(createPrimitive("ModelYear", "2012"))
      .addProperty(createPrimitive("Price", 137285.33))
      .addProperty(createPrimitive("Currency", "EUR"));
  el.setId(createId(CarsEdmProvider.ES_CARS_NAME, 3));
  entitySet.getEntities().add(el);

  el = new Entity()
      .addProperty(createPrimitive("Id", 4))
      .addProperty(createPrimitive("Model", "F2013"))
      .addProperty(createPrimitive("ModelYear", "2013"))
      .addProperty(createPrimitive("Price", 145285.00))
      .addProperty(createPrimitive("Currency", "EUR"));
  el.setId(createId(CarsEdmProvider.ES_CARS_NAME, 4));
  entitySet.getEntities().add(el);

  el = new Entity()
      .addProperty(createPrimitive("Id", 5))
      .addProperty(createPrimitive("Model", "F1 W02"))
      .addProperty(createPrimitive("ModelYear", "2011"))
      .addProperty(createPrimitive("Price", 167189.00))
      .addProperty(createPrimitive("Currency", "EUR"));
  el.setId(createId(CarsEdmProvider.ES_CARS_NAME, 5));
  entitySet.getEntities().add(el);

  for (Entity entity:entitySet.getEntities()) {
    entity.setType(CarsEdmProvider.ET_CAR.getFullQualifiedNameAsString());
  }
  return entitySet;
}