Java Code Examples for org.apache.olingo.odata2.api.edm.provider.EntityType#getProperties()

The following examples show how to use org.apache.olingo.odata2.api.edm.provider.EntityType#getProperties() . 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: JPAEdmExtension.java    From odata-boilerplate with MIT License 6 votes vote down vote up
@Override
public void extendJPAEdmSchema(JPAEdmSchemaView view) {
	ResourceBundle i18n = ODataContextUtil.getResourceBundle("i18n");
	final Schema edmSchema = view.getEdmSchema();
	
	for (EntityType entityType : edmSchema.getEntityTypes()) {
		for (Property property : entityType.getProperties()) {
			String label = null;
			if (i18n != null) { try { label = i18n.getString(entityType.getName() + "." + property.getName()); } catch (Exception e) {} }
			List<AnnotationAttribute> annotationAttributeList = new ArrayList<AnnotationAttribute>();
			if (label != null) {
				annotationAttributeList.add(new AnnotationAttribute()
						.setNamespace(SAP_NAMESPACE)
						.setPrefix(SAP_PREFIX)
						.setName(LABEL).setText(label));
			}
			annotationAttributeList.addAll(getSapPropertyAnnotations(entityType, property));
			property.setAnnotationAttributes(annotationAttributeList); 
		}
	}
	
	addSmartAnnotations(edmSchema);
}
 
Example 2
Source File: AnnotationEdmProviderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexTypeLocation() throws Exception {
  // validate employee
  EntityType employee = aep.getEntityType(new FullQualifiedName(ModelSharedConstants.NAMESPACE_1, "Employee"));
  final List<Property> properties = employee.getProperties();
  Property location = null;
  for (Property property : properties) {
    if (property.getName().equals("Location")) {
      location = property;
    }
  }
  assertNotNull(location);
  assertEquals("Location", location.getName());

  // validate location complex type
  ComplexType locationType = aep.getComplexType(
      new FullQualifiedName(ModelSharedConstants.NAMESPACE_1, "c_Location"));
  assertEquals("c_Location", locationType.getName());
  assertEquals(2, locationType.getProperties().size());
  assertEquals(false, location.getFacets().isNullable());
}
 
Example 3
Source File: AttributeEntitySet.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public EntityType getEntityType ()
{
   EntityType res = Model.ITEM.getEntityType();

   List<Property> properties = res.getProperties ();
   properties.add ((Property) new SimpleProperty ().setName (VALUE)
         .setType (EdmSimpleTypeKind.String));
   properties.add((Property) new SimpleProperty().setName(CATEGORY)
         .setType(EdmSimpleTypeKind.String)
         .setFacets(new Facets().setDefaultValue(null)));

   return res.setName (ENTITY_NAME).setProperties (properties);
}
 
Example 4
Source File: AnnotationEdmProviderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultNameAndNamespaceGeneration() throws ODataException {
  Collection<Class<?>> localAnnotatedClasses = new ArrayList<Class<?>>();
  localAnnotatedClasses.add(GeneratedNamesTestClass.class);
  localAnnotatedClasses.add(GeneratedNamesComplexTestClass.class);
  AnnotationEdmProvider localAep = new AnnotationEdmProvider(localAnnotatedClasses);
  // validate
  EntityType testType = localAep.getEntityType(new FullQualifiedName(
      GeneratedNamesTestClass.class.getPackage().getName(),
      GeneratedNamesTestClass.class.getSimpleName()));
  assertNotNull("Requested entity not found.", testType);
  assertEquals("GeneratedNamesTestClass", testType.getName());
  assertNull("This should not have a base type", testType.getBaseType());
  List<Property> properties = testType.getProperties();
  assertEquals(1, properties.size());
  ComplexProperty propComplex = (ComplexProperty) properties.get(0);
  assertEquals("MyComplexProperty", propComplex.getName());
  assertEquals(GeneratedNamesComplexTestClass.class.getPackage().getName(), propComplex.getType().getNamespace());
  assertEquals(GeneratedNamesComplexTestClass.class.getSimpleName(), propComplex.getType().getName());


  ComplexType testComplexType = localAep.getComplexType(
      new FullQualifiedName(GeneratedNamesComplexTestClass.class.getPackage().getName(),
          GeneratedNamesComplexTestClass.class.getSimpleName()));
  assertNotNull("Requested entity not found.", testComplexType);
  assertEquals("GeneratedNamesComplexTestClass", testComplexType.getName());
  assertNull("This should not have a base type", testComplexType.getBaseType());
}
 
Example 5
Source File: AnnotationEdmProviderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void entityTypePhotoWithTwoKeyProperties() throws Exception {
  // validate team
  EntityType photo = aep.getEntityType(new FullQualifiedName(ModelSharedConstants.NAMESPACE_1, "Photo"));
  assertEquals("Photo", photo.getName());
  final List<Property> properties = photo.getProperties();
  assertEquals(5, properties.size());
  assertTrue(containsProperty(properties, "Name"));
  assertTrue(containsProperty(properties, "ImageFormat"));
  assertTrue(containsProperty(properties, "MimeType"));
  assertTrue(containsProperty(properties, "ImageUrl"));
  assertTrue(containsProperty(properties, "Image"));
  assertFalse(photo.isAbstract());
  assertTrue(photo.isHasStream());
  assertEquals("MimeType",photo.getMapping().getMediaResourceMimeTypeKey());
  assertEquals("ImageUrl",photo.getMapping().getMediaResourceSourceKey());

  Key photoKey = photo.getKey();
  List<PropertyRef> keyReferences = photoKey.getKeys();
  assertEquals(2, keyReferences.size());
  PropertyRef name = getPropertyRef(keyReferences, "Name");
  assertEquals("Name", name.getName());
  PropertyRef imageFormat = getPropertyRef(keyReferences, "ImageFormat");
  assertEquals("ImageFormat", imageFormat.getName());

  // assertEquals(0, photo.getNavigationProperties().size());
  assertNull(photo.getNavigationProperties());
}
 
Example 6
Source File: BasicEntityProvider.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the necessary data service version for the metadata serialization
 * @param schemas
 * @return DataServiceversion as String
 */
private String calculateDataServiceVersion(final List<Schema> schemas) {

  String dataServiceVersion = ODataServiceVersion.V10;

  if (schemas != null) {
    for (Schema schema : schemas) {
      List<EntityType> entityTypes = schema.getEntityTypes();
      if (entityTypes != null) {
        for (EntityType entityType : entityTypes) {
          List<Property> properties = entityType.getProperties();
          if (properties != null) {
            for (Property property : properties) {
              if (property.getCustomizableFeedMappings() != null) {
                if (property.getCustomizableFeedMappings().getFcKeepInContent() != null) {
                  if (!property.getCustomizableFeedMappings().getFcKeepInContent()) {
                    dataServiceVersion = ODataServiceVersion.V20;
                    return dataServiceVersion;
                  }
                }
              }
            }
            if (entityType.getCustomizableFeedMappings() != null) {
              if (entityType.getCustomizableFeedMappings().getFcKeepInContent() != null) {
                if (entityType.getCustomizableFeedMappings().getFcKeepInContent()) {
                  dataServiceVersion = ODataServiceVersion.V20;
                  return dataServiceVersion;
                }
              }
            }
          }
        }
      }
    }
  }

  return dataServiceVersion;
}
 
Example 7
Source File: ProductEntitySet.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public EntityType getEntityType ()
{
   EntityType res = Model.NODE.getEntityType();

   // Properties
   List<Property> properties = res.getProperties ();

   properties.add (new SimpleProperty ().setName (CREATION_DATE)
      .setType (EdmSimpleTypeKind.DateTime)
      .setFacets (new Facets ().setNullable (false)));
   properties.add (new SimpleProperty ()
      .setName (INGESTION_DATE)
      .setType (EdmSimpleTypeKind.DateTime)
      .setCustomizableFeedMappings (
         new CustomizableFeedMappings ()
            .setFcTargetPath (EdmTargetPath.SYNDICATION_UPDATED)));
   properties.add (new SimpleProperty ().setName (EVICTION_DATE).setType (
      EdmSimpleTypeKind.DateTime));
   properties.add(new ComplexProperty().setName(CONTENT_DATE).setType(Model.TIME_RANGE));
   properties.add(new ComplexProperty().setName(CHECKSUM).setType(Model.CHECKSUM));
   properties.add (new SimpleProperty ().setName (CONTENT_GEOMETRY).setType (
      EdmSimpleTypeKind.String));
   properties.add (new SimpleProperty ().setName (METALINK).setType (
      EdmSimpleTypeKind.String));

   if (Security.currentUserHasRole(Role.ARCHIVE_MANAGER))
   {
      properties.add (new SimpleProperty ().setName (LOCAL_PATH).setType (
         EdmSimpleTypeKind.String));
   }

   // Navigation Properties
   List<NavigationProperty> navigationProperties =
      new ArrayList<NavigationProperty> ();

   navigationProperties.add (new NavigationProperty ().setName (getName ())
      .setRelationship (ASSO_PRODUCT_PRODUCT)
      .setFromRole (ROLE_PRODUCT_PRODUCT).setToRole (ROLE_PRODUCT_PRODUCTS));
   navigationProperties.add (new NavigationProperty ()
      .setName(Model.NODE.getName()).setRelationship(ASSO_PRODUCT_NODE)
      .setFromRole (ROLE_NODE_PRODUCT).setToRole (ROLE_PRODUCT_NODES));
   navigationProperties.add (new NavigationProperty ()
      .setName(Model.ATTRIBUTE.getName())
      .setRelationship (ASSO_PRODUCT_ATTRIBUTE)
      .setFromRole (ROLE_ATTRIBUTE_PRODUCT)
      .setToRole (ROLE_PRODUCT_ATTRIBUTES));
   navigationProperties.add (new NavigationProperty ().setName ("Class")
      .setRelationship (ASSO_PRODUCT_CLASS)
      .setFromRole (ROLE_CLASS_PRODUCTS).setToRole (ROLE_PRODUCT_CLASS));

   // TODO (OData v3) setContainsTarget(true) setBaseType(ENTITY_ITEM)
   return res.setName (ENTITY_NAME).setProperties (properties)
      .setHasStream (true).setNavigationProperties (navigationProperties);
}
 
Example 8
Source File: JPAEdmReferentialConstraintRole.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private void buildRole() throws SecurityException, NoSuchFieldException {

      int index = 0;
      if (currentRole == null) {
        currentRole = new ReferentialConstraintRole();
        String jpaAttributeType = null;
        String jpaColumnName = null;
        EntityType edmEntityType = null;

        if (roleType == RoleType.PRINCIPAL) {
          jpaAttributeType = jpaAttribute.getJavaType().getSimpleName();
          if (jpaAttribute.isCollection()) {
            Type type =
                ((ParameterizedType) jpaAttribute.getJavaMember().getDeclaringClass().getDeclaredField(
                    jpaAttribute.getName()).getGenericType()).getActualTypeArguments()[0];
            int lastIndexOfDot = type.toString().lastIndexOf(".");
            jpaAttributeType = type.toString().substring(lastIndexOfDot + 1);
          }
          edmEntityType = entityTypeView.searchEdmEntityType(jpaAttributeType);
          index = 1;
        } else if (roleType == RoleType.DEPENDENT) {
          edmEntityType =
              entityTypeView.searchEdmEntityType(jpaAttribute.getDeclaringType().getJavaType().getSimpleName());
        }

        List<PropertyRef> propertyRefs = new ArrayList<PropertyRef>();
        if (edmEntityType != null) {
          for (String[] columnName : jpaColumnNames) {
            for (Property property : edmEntityType.getProperties()) {
              jpaColumnName = ((JPAEdmMapping) property.getMapping()).getJPAColumnName();
              if (columnName[index].equals(jpaColumnName) ||
                  columnName[index].equals(property.getName())) {
                PropertyRef propertyRef = new PropertyRef();
                propertyRef.setName(property.getName());
                propertyRefs.add(propertyRef);
                break;
              }
            }
          }
          currentRole.setPropertyRefs(propertyRefs);
          if (propertyRefs.isEmpty()) {
            isConsistent = false;
            return;
          }
          // First condition is required for Self Joins where the entity type on both ends are same
          AssociationEnd end1 = association.getEnd1();
          AssociationEnd end2 = association.getEnd2();
          if (end1.getType().getName().equals(end2.getType().getName())) {
            if (roleType == RoleType.PRINCIPAL) {
              currentRole.setRole(end1.getRole());
            } else {
              currentRole.setRole(end2.getRole());
            }
            isConsistent = true;
          } else {
            if (end1.getType().getName().equals(edmEntityType.getName())) {
              currentRole.setRole(end1.getRole());
              isConsistent = true;
            } else if (end2.getType().getName().equals(edmEntityType.getName())) {
              currentRole.setRole(end2.getRole());
              isConsistent = true;
            }
          }
        }

      }
    }
 
Example 9
Source File: XmlMetadataConsumerTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotations() throws XMLStreamException, EntityProviderException {
  final String xmlWithAnnotations =
      "<edmx:Edmx Version=\"1.0\" xmlns:edmx=\""
          + Edm.NAMESPACE_EDMX_2007_06
          + "\" xmlns:annoPrefix=\"http://annoNamespace\">"
          + "<edmx:DataServices m:DataServiceVersion=\"2.0\" xmlns:m=\""
          + Edm.NAMESPACE_M_2007_08
          + "\">"
          + "<Schema Namespace=\""
          + NAMESPACE
          + "\" xmlns=\""
          + Edm.NAMESPACE_EDM_2008_09
          + "\">"
          + "<EntityType Name= \"Employee\" prefix1:href=\"http://foo\" xmlns:prefix1=\"namespaceForAnno\">"
          + "<Key><PropertyRef Name=\"EmployeeId\"/></Key>"
          + "<Property Name=\"EmployeeId\" Type=\"Edm.String\" Nullable=\"false\"/>"
          + "<Property Name=\"EmployeeName\" Type=\"Edm.String\" m:FC_TargetPath=\"SyndicationTitle\" " +
          "annoPrefix:annoName=\"annoText\">"
          + "<annoPrefix:propertyAnnoElement>text</annoPrefix:propertyAnnoElement>"
          + "<annoPrefix:propertyAnnoElement2 />"
          + "</Property>"
          + "</EntityType>"
          + "<annoPrefix:schemaElementTest1>"
          + "<prefix:schemaElementTest2 xmlns:prefix=\"namespace\">text3"
          + "</prefix:schemaElementTest2>"
          + "<annoPrefix:schemaElementTest3 rel=\"self\" pre:href=\"http://foo\" " +
          "xmlns:pre=\"namespaceForAnno\">text4</annoPrefix:schemaElementTest3>"
          + " </annoPrefix:schemaElementTest1>" + "</Schema>"
          + "</edmx:DataServices>" + "</edmx:Edmx>";
  XmlMetadataConsumer parser = new XmlMetadataConsumer();
  XMLStreamReader reader = createStreamReader(xmlWithAnnotations);
  DataServices result = parser.readMetadata(reader, false);
  for (Schema schema : result.getSchemas()) {
    assertEquals(1, schema.getAnnotationElements().size());
    for (AnnotationElement annoElement : schema.getAnnotationElements()) {
      for (AnnotationElement childAnnoElement : annoElement.getChildElements()) {
        if ("schemaElementTest2".equals(childAnnoElement.getName())) {
          assertEquals("prefix", childAnnoElement.getPrefix());
          assertEquals("namespace", childAnnoElement.getNamespace());
          assertEquals("text3", childAnnoElement.getText());
        } else if ("schemaElementTest3".equals(childAnnoElement.getName())) {
          assertEquals("text4", childAnnoElement.getText());
          assertEquals("rel", childAnnoElement.getAttributes().get(0).getName());
          assertEquals("self", childAnnoElement.getAttributes().get(0).getText());
          assertEquals("", childAnnoElement.getAttributes().get(0).getPrefix());
          assertNull(childAnnoElement.getAttributes().get(0).getNamespace());
          assertEquals("href", childAnnoElement.getAttributes().get(1).getName());
          assertEquals("pre", childAnnoElement.getAttributes().get(1).getPrefix());
          assertEquals("namespaceForAnno", childAnnoElement.getAttributes().get(1).getNamespace());
          assertEquals("http://foo", childAnnoElement.getAttributes().get(1).getText());
        } else {
          throw new EntityProviderException(null, "xmlWithAnnotations");
        }
      }
    }
    for (EntityType entityType : schema.getEntityTypes()) {
      assertEquals(1, entityType.getAnnotationAttributes().size());
      AnnotationAttribute attr = entityType.getAnnotationAttributes().get(0);
      assertEquals("href", attr.getName());
      assertEquals("prefix1", attr.getPrefix());
      assertEquals("namespaceForAnno", attr.getNamespace());
      assertEquals("http://foo", attr.getText());
      for (Property property : entityType.getProperties()) {
        if ("EmployeeName".equals(property.getName())) {
          assertEquals(2, property.getAnnotationElements().size());
          for (AnnotationElement anElement : property.getAnnotationElements()) {
            if ("propertyAnnoElement".equals(anElement.getName())) {
              assertEquals("text", anElement.getText());
            }
          }
          for (AnnotationAttribute anAttribute : property.getAnnotationAttributes()) {
            assertEquals("annoName", anAttribute.getName());
            assertEquals("annoPrefix", anAttribute.getPrefix());
            assertEquals("annoText", anAttribute.getText());
            assertEquals("http://annoNamespace", anAttribute.getNamespace());
          }
        }
      }
    }
  }
}