All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.odata4j.cxf.consumer.CxfConsumerCreateEntityRequest Maven / Gradle / Ivy

package org.odata4j.cxf.consumer;

import java.util.Arrays;

import org.apache.http.HttpResponse;
import org.odata4j.consumer.AbstractConsumerEntityPayloadRequest;
import org.odata4j.consumer.ODataClientRequest;
import org.odata4j.core.OCreateRequest;
import org.odata4j.core.ODataConstants;
import org.odata4j.core.ODataVersion;
import org.odata4j.core.OEntities;
import org.odata4j.core.OEntity;
import org.odata4j.core.OEntityKey;
import org.odata4j.core.OLinks;
import org.odata4j.core.OProperty;
import org.odata4j.core.Throwables;
import org.odata4j.edm.EdmDataServices;
import org.odata4j.edm.EdmEntitySet;
import org.odata4j.edm.EdmMultiplicity;
import org.odata4j.edm.EdmNavigationProperty;
import org.odata4j.format.Entry;
import org.odata4j.format.FormatParser;
import org.odata4j.format.FormatParserFactory;
import org.odata4j.format.FormatType;
import org.odata4j.format.Settings;
import org.odata4j.format.xml.XmlFormatWriter;
import org.odata4j.internal.FeedCustomizationMapping;
import org.odata4j.internal.InternalUtil;

class CxfConsumerCreateEntityRequest extends AbstractConsumerEntityPayloadRequest implements OCreateRequest {

  private final FormatType formatType;
  private OEntity parent;
  private String navProperty;

  private final FeedCustomizationMapping fcMapping;

  CxfConsumerCreateEntityRequest(FormatType formatType, String serviceRootUri, EdmDataServices metadata, String entitySetName, FeedCustomizationMapping fcMapping) {
    super(entitySetName, serviceRootUri, metadata);
    this.formatType = formatType;
    this.fcMapping = fcMapping;
  }

  @SuppressWarnings("unchecked")
  @Override
  public T execute() {
    ODataCxfClient client = new ODataCxfClient(this.formatType);
    try {
      EdmEntitySet ees = metadata.getEdmEntitySet(entitySetName);
      Entry entry = client.createRequestEntry(ees, null, props, links);

      StringBuilder url = new StringBuilder(serviceRootUri);
      if (parent != null) {
        url.append(InternalUtil.getEntityRelId(parent))
            .append("/")
            .append(navProperty);
      } else {
        url.append(entitySetName);
      }

      ODataClientRequest request = ODataClientRequest.post(url.toString(), entry);
      HttpResponse response = client.createEntity(request);

      ODataVersion version = InternalUtil.getDataServiceVersion(response.getFirstHeader(ODataConstants.Headers.DATA_SERVICE_VERSION).getValue());

      FormatParser parser = FormatParserFactory.getParser(Entry.class,
          client.getFormatType(), new Settings(version, metadata, entitySetName, null, fcMapping));
      entry = parser.parse(client.getFeedReader(response));

      return (T) entry.getEntity();
    } catch (Exception e) {
      throw Throwables.propagate(e);
    } finally {
      client.shuttdown();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public T get() {
    EdmEntitySet entitySet = metadata.getEdmEntitySet(entitySetName);
    return (T) OEntities.createRequest(entitySet, props, links);
  }

  @Override
  public OCreateRequest properties(OProperty... props) {
    return super.properties(this, props);
  }

  @Override
  public OCreateRequest properties(Iterable> props) {
    return super.properties(this, props);
  }

  @Override
  public OCreateRequest link(String navProperty, OEntity target) {
    return super.link(this, navProperty, target);
  }

  @Override
  public OCreateRequest link(String navProperty, OEntityKey targetKey) {
    return super.link(this, navProperty, targetKey);
  }

  @Override
  public OCreateRequest addToRelation(OEntity parent, String navProperty) {
    if (parent == null || navProperty == null) {
      throw new IllegalArgumentException("please provide the parent and the navProperty");
    }

    this.parent = parent;
    this.navProperty = navProperty;
    return this;
  }

  @Override
  public OCreateRequest inline(String navProperty, OEntity... entities) {
    EdmEntitySet entitySet = metadata.getEdmEntitySet(entitySetName);
    EdmNavigationProperty navProp = entitySet.getType().findNavigationProperty(navProperty);
    if (navProp == null)
      throw new IllegalArgumentException("unknown navigation property " + navProperty);

    // TODO get rid of XmlFormatWriter
    String rel = XmlFormatWriter.related + navProperty;
    String href = entitySetName + "/" + navProperty;
    if (navProp.getToRole().getMultiplicity() == EdmMultiplicity.MANY) {
      links.add(OLinks.relatedEntitiesInline(rel, navProperty, href, Arrays.asList(entities)));
    } else {
      if (entities.length > 1)
        throw new IllegalArgumentException("only one entity is allowed for this navigation property " + navProperty);

      links.add(OLinks.relatedEntityInline(rel, navProperty, href,
          entities.length > 0 ? entities[0] : null));
    }

    return this;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy