private OEntity createEmployeeEntity() {
   EdmEntitySet entitySet = this.getMetadata().findEdmEntitySet("Employees");
   OEntityKey entityKey =
       OEntityKey.parse("EmployeeId='" + ServiceOperationsProducerMock.EMPLOYEE_ID + "'");
   ArrayList<OProperty<?>> properties = new ArrayList<OProperty<?>>();
   properties.add(OProperties.string("EmployeeName", ServiceOperationsProducerMock.EMPLOYEE_NAME));
   properties.add(OProperties.string("EmployeeId", ServiceOperationsProducerMock.EMPLOYEE_ID));
   OEntity entity = OEntities.create(entitySet, entityKey, properties, null);
   return entity;
 }
 /**
  * Transforms each object's field into OProperty and object into OEntity. Used by OData4J
  * framework to serialize output.
  *
  * @param namespace Namespace where this entity will be placed
  * @return This object encoded as OEntity
  */
 public OEntity asEntity(String namespace) {
   final List<OProperty<?>> properties = new ArrayList<OProperty<?>>();
   properties.add(OProperties.string("language", getLanguage()));
   properties.add(OProperties.string("value", getValue()));
   return OEntities.create(
       getEntitySet(namespace),
       OEntityKey.create(getLanguage()),
       properties,
       new ArrayList<OLink>(),
       getValue(),
       null);
 }
  public static Iterable<OProperty<?>> parseProperties(
      XMLEventReader2 reader, StartElement2 propertiesElement) {
    List<OProperty<?>> rt = new ArrayList<OProperty<?>>();

    while (reader.hasNext()) {
      XMLEvent2 event = reader.nextEvent();

      if (event.isEndElement()
          && event.asEndElement().getName().equals(propertiesElement.getName())) {
        return rt;
      }

      if (event.isStartElement()
          && event.asStartElement().getName().getNamespaceURI().equals(NS_DATASERVICES)) {

        String name = event.asStartElement().getName().getLocalPart();
        Attribute2 typeAttribute = event.asStartElement().getAttributeByName(M_TYPE);
        Attribute2 nullAttribute = event.asStartElement().getAttributeByName(M_NULL);
        boolean isNull = nullAttribute != null && "true".equals(nullAttribute.getValue());

        OProperty<?> op = null;

        String type = null;
        boolean isComplexType = false;
        if (typeAttribute != null) {
          type = typeAttribute.getValue();
          EdmType et = EdmType.get(type);
          isComplexType = !et.isSimple();
        }

        if (isComplexType) {
          op =
              OProperties.complex(
                  name,
                  type,
                  isNull
                      ? null
                      : Enumerable.create(parseProperties(reader, event.asStartElement()))
                          .toList());
        } else {
          op = OProperties.parse(name, type, isNull ? null : reader.getElementText());
        }
        rt.add(op);
      }
    }

    throw new RuntimeException();
  }
 private OEntity createCustomersEntity(EdmDataServices metadata) {
   EdmEntitySet entitySet = metadata.findEdmEntitySet("Customers");
   OEntityKey entityKey = OEntityKey.parse("CustomerID='12'");
   ArrayList<OProperty<?>> properties = new ArrayList<OProperty<?>>();
   properties.add(OProperties.string("CompanyName", "teiid"));
   properties.add(OProperties.string("ContactName", "contact-name"));
   properties.add(OProperties.string("ContactTitle", "contact-title"));
   properties.add(OProperties.string("Address", "address"));
   properties.add(OProperties.string("City", "city"));
   properties.add(OProperties.string("Region", "region"));
   properties.add(OProperties.string("PostalCode", "postal-code"));
   properties.add(OProperties.string("Country", "country"));
   properties.add(OProperties.string("Phone", "555-1212"));
   properties.add(OProperties.string("Fax", "555-1212"));
   OEntity entity = OEntities.create(entitySet, entityKey, properties, null);
   return entity;
 }
  private OEntity entityFromAtomEntry(
      EdmDataServices metadata,
      EdmEntitySet entitySet,
      DataServicesAtomEntry dsae,
      FeedCustomizationMapping mapping) {

    List<OProperty<?>> props = dsae.properties;
    if (mapping != null) {
      Enumerable<OProperty<?>> properties = Enumerable.create(dsae.properties);
      if (mapping.titlePropName != null)
        properties = properties.concat(OProperties.string(mapping.titlePropName, dsae.title));
      if (mapping.summaryPropName != null)
        properties = properties.concat(OProperties.string(mapping.summaryPropName, dsae.summary));

      props = properties.toList();
    }

    OEntityKey key =
        entityKey != null
            ? entityKey
            : dsae.id != null
                ? dsae.id.endsWith(")")
                    ? parseEntityKey(dsae.id)
                    : OEntityKey.infer(entitySet, props)
                : null;

    if (key == null)
      return OEntities.createRequest(
          entitySet,
          props,
          toOLinks(metadata, entitySet, dsae.atomLinks, mapping),
          dsae.title,
          dsae.categoryTerm);

    return OEntities.create(
        entitySet,
        key,
        props,
        toOLinks(metadata, entitySet, dsae.atomLinks, mapping),
        dsae.title,
        dsae.categoryTerm);
  }
  private OComplexObject createComplexTypeLocation() {
    ArrayList<OProperty<?>> propertiesCity = new ArrayList<OProperty<?>>();
    propertiesCity.add(OProperties.string("PostalCode", ServiceOperationsProducerMock.POSTAL_CODE));
    propertiesCity.add(OProperties.string("CityName", ServiceOperationsProducerMock.CITY));

    ArrayList<OProperty<?>> propertiesLocation = new ArrayList<OProperty<?>>();
    propertiesLocation.add(
        OProperties.complex(
            "City",
            this.getMetadata()
                .findEdmComplexType(ServiceOperationsProducerMock.COMPLEY_TYPE_NAME_CITY),
            propertiesCity));
    propertiesLocation.add(OProperties.string("Country", ServiceOperationsProducerMock.COUNTRY));

    OComplexObject locationType =
        OComplexObjects.create(
            this.getMetadata()
                .findEdmComplexType(ServiceOperationsProducerMock.COMPLEY_TYPE_NAME_LOCATION),
            propertiesLocation);

    return locationType;
  }
Example #7
0
 static OProperty<?> buildPropery(
     String propName, EdmType type, Object value, String invalidCharacterReplacement)
     throws TransformationException, SQLException, IOException {
   if (!(type instanceof EdmSimpleType)) {
     if (type instanceof EdmCollectionType) {
       EdmCollectionType collectionType = (EdmCollectionType) type;
       EdmType componentType = collectionType.getItemType();
       Builder<OObject> b = OCollections.newBuilder(componentType);
       if (value instanceof Array) {
         value = ((Array) value).getArray();
       }
       int length = java.lang.reflect.Array.getLength(value);
       for (int i = 0; i < length; i++) {
         Object o = java.lang.reflect.Array.get(value, i);
         OProperty p = buildPropery("x", componentType, o, invalidCharacterReplacement);
         if (componentType instanceof EdmSimpleType) {
           b.add(OSimpleObjects.create((EdmSimpleType) componentType, p.getValue()));
         } else {
           throw new AssertionError("Multi-dimensional arrays are not yet supported.");
           // b.add((OCollection)p.getValue());
         }
       }
       return OProperties.collection(propName, collectionType, b.build());
     }
     throw new AssertionError("non-simple types are not yet supported");
   }
   EdmSimpleType expectedType = (EdmSimpleType) type;
   if (value == null) {
     return OProperties.null_(propName, expectedType);
   }
   Class<?> sourceType = DataTypeManager.getRuntimeType(value.getClass());
   Class<?> targetType =
       DataTypeManager.getDataTypeClass(
           ODataTypeManager.teiidType(expectedType.getFullyQualifiedTypeName()));
   if (sourceType != targetType) {
     Transform t = DataTypeManager.getTransform(sourceType, targetType);
     if (t == null && BlobType.class == targetType) {
       if (sourceType == ClobType.class) {
         return OProperties.binary(propName, ClobType.getString((Clob) value).getBytes());
       }
       if (sourceType == SQLXML.class) {
         return OProperties.binary(propName, ((SQLXML) value).getString().getBytes());
       }
     }
     value = DataTypeManager.convertToRuntimeType(value, true);
     value = t != null ? t.transform(value, targetType) : value;
     value = replaceInvalidCharacters(expectedType, value, invalidCharacterReplacement);
     if (value instanceof BinaryType) {
       value = ((BinaryType) value).getBytesDirect();
     }
     return OProperties.simple(propName, expectedType, value);
   }
   value = replaceInvalidCharacters(expectedType, value, invalidCharacterReplacement);
   return OProperties.simple(propName, expectedType, value);
 }
 /**
  * Transforms each object's field into OProperty and object into OEntity. Used by OData4J
  * framework to serialize output.
  *
  * @param ees Entity set that describes this object. This was defined in metadata for this object,
  *     see {@link org.informea.odata.pojo.AbstractContact#getSchema(String)}.
  * @return This object encoded as OEntity
  */
 @Override
 public OEntity asEntity(EdmEntitySet ees) {
   final List<OProperty<?>> properties = new ArrayList<OProperty<?>>();
   properties.add(OProperties.int16("protocolVersion", getProtocolVersion()));
   properties.add(OProperties.string("id", getId()));
   properties.add(OProperties.string("country", getCountry()));
   properties.add(OProperties.string("prefix", getPrefix()));
   properties.add(OProperties.string("firstName", getFirstName()));
   properties.add(OProperties.string("lastName", getLastName()));
   properties.add(OProperties.string("position", getPosition()));
   properties.add(OProperties.string("institution", getInstitution()));
   properties.add(OProperties.string("department", getDepartment()));
   properties.add(OProperties.string("address", getAddress()));
   properties.add(OProperties.string("email", getEmail()));
   properties.add(OProperties.string("phone", getPhoneNumber()));
   properties.add(OProperties.string("fax", getFax()));
   properties.add(OProperties.datetime("updated", getUpdated()));
   return OEntities.create(
       ees, OEntityKey.create(getId()), properties, new ArrayList<OLink>(), null, null);
 }
Example #9
0
  /**
   * adds the property. This property can be a navigation property too. In this case a link will be
   * added. If it's the meta data the information will be added to the entry too.
   *
   * @param entry JsonEntry
   * @param ees EdmEntitySet
   * @param name PropertyName
   * @param jsr JsonStreamReader
   * @return EdmEntitySet
   */
  protected EdmEntitySet addProperty(
      JsonEntry entry, EdmEntitySet ees, String name, JsonStreamReader jsr) {

    JsonEvent event = jsr.nextEvent();

    if (event.isEndProperty()) {
      // scalar property
      EdmProperty ep = entry.getEntityType().findProperty(name);
      if (ep == null) {
        // OpenEntityTypeの場合は、プロパティを追加する
        NamespacedAnnotation<?> openType =
            findAnnotation(ees.getType(), null, Edm.EntityType.OpenType);
        if (openType != null && openType.getValue() == "true") {
          Object propValue = null;
          try {
            propValue = event.asEndProperty().getObject();
          } catch (NumberFormatException e) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name).reason(e);
          }

          // 型によって登録するEntityPropertyを変更する
          if (propValue instanceof Boolean) {
            entry.properties.add(
                JsonTypeConverter.parse(
                    name, (EdmSimpleType<?>) EdmSimpleType.BOOLEAN, propValue.toString()));
          } else if (propValue instanceof Double) {
            entry.properties.add(
                JsonTypeConverter.parse(
                    name, (EdmSimpleType<?>) EdmSimpleType.DOUBLE, propValue.toString()));
          } else {
            if (propValue == null) {
              entry.properties.add(
                  JsonTypeConverter.parse(name, (EdmSimpleType<?>) EdmSimpleType.STRING, null));
            } else {
              entry.properties.add(
                  JsonTypeConverter.parse(
                      name, (EdmSimpleType<?>) EdmSimpleType.STRING, propValue.toString()));
            }
          }
        } else {
          throw DcCoreException.OData.FIELED_INVALID_ERROR.params(
              "unknown property " + name + " for " + entry.getEntityType().getName());
        }
      } else {
        // StaticPropertyの値チェック
        String propValue = event.asEndProperty().getValue();
        if (propValue != null) {
          EdmType type = ep.getType();
          if (type.equals(EdmSimpleType.BOOLEAN) && !ODataUtils.validateBoolean(propValue)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          } else if (type.equals(EdmSimpleType.STRING) && !ODataUtils.validateString(propValue)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          } else if (type.equals(EdmSimpleType.DATETIME)) {
            if (!ODataUtils.validateDateTime(propValue)) {
              throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
            }
            if (Common.SYSUTCDATETIME.equals(propValue)) {
              String crrTime = String.valueOf(getCurrentTimeMillis());
              propValue = String.format("/Date(%s)/", crrTime);
            }
          } else if (type.equals(EdmSimpleType.SINGLE) && !ODataUtils.validateSingle(propValue)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          } else if (type.equals(EdmSimpleType.INT32) && !ODataUtils.validateInt32(propValue)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          } else if (type.equals(EdmSimpleType.DOUBLE) && !ODataUtils.validateDouble(propValue)) {
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          }
        }
        if (ep.getType().isSimple()) {
          // シンプル型(文字列や数値など)であればプロパティに追加する
          entry.properties.add(
              JsonTypeConverter.parse(name, (EdmSimpleType<?>) ep.getType(), propValue));
        } else {
          if (propValue == null) {
            // ComplexType型で、値がnullの場合はエラーにしない
            entry.properties.add(
                JsonTypeConverter.parse(name, (EdmSimpleType<?>) EdmSimpleType.STRING, null));
          } else {
            // ComplexType型で、ComplexType型以外の値が指定された場合("aaa")はエラーとする
            throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
          }
        }
      }
    } else if (event.isStartObject()) {
      // JSONオブジェクトの場合は値を取得する
      JsonObjectPropertyValue val = getValue(event, ees, name, jsr, entry);

      if (val.complexObject != null) {
        // ComplexTypeデータであればプロパティに追加する
        entry.properties.add(
            OProperties.complex(
                name,
                (EdmComplexType) val.complexObject.getType(),
                val.complexObject.getProperties()));
      } else {
        // ComplexTypeデータ以外はエラーとする
        throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
      }
    } else if (event.isStartArray()) {
      // 配列オブジェクトの場合
      JsonObjectPropertyValue val = new JsonObjectPropertyValue();

      // スキーマ定義が存在してCollectionKindがNoneでなければ、配列としてパースする
      EdmProperty eprop = entry.getEntityType().findProperty(name);
      if (null != eprop && eprop.getCollectionKind() != CollectionKind.NONE) {
        val.collectionType = new EdmCollectionType(eprop.getCollectionKind(), eprop.getType());
        DcJsonCollectionFormatParser cfp =
            new DcJsonCollectionFormatParser(val.collectionType, this.metadata, name);
        val.collection = cfp.parseCollection(jsr);
      }

      // パースに成功した場合は、プロパティに追加する
      if (val.collectionType != null && val.collection != null) {
        entry.properties.add(OProperties.collection(name, val.collectionType, val.collection));
      } else {
        throw DcCoreException.OData.REQUEST_FIELD_FORMAT_ERROR.params(name);
      }
    } else {
      throw DcCoreException.OData.INVALID_TYPE_ERROR.params(name);
    }
    return ees;
  }