private void buildRole()

in odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/model/JPAEdmReferentialConstraintRole.java [142:208]


    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;
            }
          }
        }

      }
    }