/*
   * ************************************************************************
   * EDM EntitySet Name - RULES
   * ************************************************************************
   * Entity Set Name = JPA Entity Type Name + Literal "s"
   * ************************************************************************
   * EDM EntitySet Name - RULES
   * ************************************************************************
   */
  public static void build(
      final JPAEdmEntitySetView view, final JPAEdmEntityTypeView entityTypeView) {
    FullQualifiedName fQname = view.getEdmEntitySet().getEntityType();
    JPAEdmMappingModelAccess mappingModelAccess = view.getJPAEdmMappingModelAccess();
    String entitySetName = null;
    if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
      Mapping mapping = entityTypeView.getEdmEntityType().getMapping();
      if (mapping != null) {
        entitySetName = mappingModelAccess.mapJPAEntitySet(mapping.getInternalName());
      }
    }

    if (entitySetName == null) {
      entitySetName = fQname.getName() + ENTITY_SET_SUFFIX;
    }

    view.getEdmEntitySet().setName(entitySetName);
  }
  public static void build(
      final JPAEdmAssociationView associationView,
      final JPAEdmPropertyView propertyView,
      final JPAEdmNavigationPropertyView navPropertyView,
      final int count) {

    String toName = null;
    String fromName = null;
    String navPropName = null;
    NavigationProperty navProp = navPropertyView.getEdmNavigationProperty();
    String namespace = buildNamespace(associationView);

    Association association = associationView.getEdmAssociation();
    navProp.setRelationship(new FullQualifiedName(namespace, association.getName()));

    FullQualifiedName associationEndTypeOne = association.getEnd1().getType();
    FullQualifiedName associationEndTypeTwo = association.getEnd2().getType();

    Attribute<?, ?> jpaAttribute = propertyView.getJPAAttribute();
    navProp.setMapping(new Mapping().setInternalName(jpaAttribute.getName()));

    String jpaEntityTypeName = propertyView.getJPAEdmEntityTypeView().getJPAEntityType().getName();
    JPAEdmMappingModelAccess mappingModelAccess = navPropertyView.getJPAEdmMappingModelAccess();

    try {
      PluralAttribute<?, ?, ?> jpattr = (PluralAttribute<?, ?, ?>) propertyView.getJPAAttribute();

      if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
        toName =
            mappingModelAccess.mapJPAEntityType(
                jpattr.getElementType().getJavaType().getSimpleName());
        fromName = mappingModelAccess.mapJPAEntityType(jpaEntityTypeName);
        navPropName = mappingModelAccess.mapJPARelationship(jpaEntityTypeName, jpattr.getName());
      }
      if (toName == null) {
        toName = jpattr.getElementType().getJavaType().getSimpleName();
      }
      if (fromName == null) {
        fromName = jpaEntityTypeName;
      }

      if (navPropName == null) {
        navPropName = toName.concat(NAVIGATION_NAME);
      }
      if (count > 1) {
        navPropName = navPropName + Integer.toString(count - 1);
      }
      navProp.setName(navPropName);

      if (toName.equals(associationEndTypeOne.getName())) {
        navProp.setFromRole(association.getEnd2().getRole());
        navProp.setToRole(association.getEnd1().getRole());
      } else if (toName.equals(associationEndTypeTwo.getName())) {
        navProp.setToRole(association.getEnd2().getRole());
        navProp.setFromRole(association.getEnd1().getRole());
      }

    } catch (Exception e) {
      if (mappingModelAccess != null && mappingModelAccess.isMappingModelExists()) {
        navPropName =
            mappingModelAccess.mapJPARelationship(jpaEntityTypeName, jpaAttribute.getName());
        toName = mappingModelAccess.mapJPAEntityType(jpaAttribute.getJavaType().getSimpleName());
        fromName = mappingModelAccess.mapJPAEntityType(jpaEntityTypeName);
      }
      if (toName == null) {
        toName = jpaAttribute.getJavaType().getSimpleName();
      }
      if (fromName == null) {
        fromName = jpaEntityTypeName;
      }

      if (navPropName == null) {
        navPropName = toName.concat(NAVIGATION_NAME);
      }
      if (count > 1) {
        navPropName = navPropName + Integer.toString(count - 1);
      }
      navProp.setName(navPropName);

      if (toName.equals(associationEndTypeOne.getName())) {
        navProp.setFromRole(association.getEnd2().getRole());
        navProp.setToRole(association.getEnd1().getRole());
      } else if (toName.equals(associationEndTypeTwo.getName())) {

        navProp.setToRole(association.getEnd2().getRole());
        navProp.setFromRole(association.getEnd1().getRole());
      }
    }
  }