private void appendProperties(final XMLStreamWriter writer, final EdmStructuredType type)
      throws XMLStreamException {
    List<String> propertyNames = new ArrayList<String>(type.getPropertyNames());
    if (type.getBaseType() != null) {
      propertyNames.removeAll(type.getBaseType().getPropertyNames());
    }
    for (String propertyName : propertyNames) {
      EdmProperty property = type.getStructuralProperty(propertyName);
      writer.writeEmptyElement(XML_PROPERTY);
      writer.writeAttribute(XML_NAME, propertyName);
      String fqnString;
      if (property.isPrimitive()) {
        fqnString = getFullQualifiedName(property.getType(), property.isCollection());
      } else {
        fqnString = getAliasedFullQualifiedName(property.getType(), property.isCollection());
      }
      writer.writeAttribute(XML_TYPE, fqnString);

      // Facets
      if (!property.isNullable()) {
        writer.writeAttribute(XML_NULLABLE, "" + property.isNullable());
      }

      if (!property.isUnicode()) {
        writer.writeAttribute(XML_UNICODE, "" + property.isUnicode());
      }

      if (property.getDefaultValue() != null) {
        writer.writeAttribute(XML_DEFAULT_VALUE, property.getDefaultValue());
      }

      if (property.getMaxLength() != null) {
        writer.writeAttribute(XML_MAX_LENGTH, "" + property.getMaxLength());
      }

      if (property.getPrecision() != null) {
        writer.writeAttribute(XML_PRECISION, "" + property.getPrecision());
      }

      if (property.getScale() != null) {
        writer.writeAttribute(XML_SCALE, "" + property.getScale());
      }
    }
  }
  private void appendNavigationProperties(
      final XMLStreamWriter writer, final EdmStructuredType type) throws XMLStreamException {
    List<String> navigationPropertyNames = new ArrayList<String>(type.getNavigationPropertyNames());
    if (type.getBaseType() != null) {
      navigationPropertyNames.removeAll(type.getBaseType().getNavigationPropertyNames());
    }
    for (String navigationPropertyName : navigationPropertyNames) {
      EdmNavigationProperty navigationProperty = type.getNavigationProperty(navigationPropertyName);

      writer.writeStartElement(XML_NAVIGATION_PROPERTY);
      writer.writeAttribute(XML_NAME, navigationPropertyName);
      writer.writeAttribute(
          XML_TYPE,
          getAliasedFullQualifiedName(
              navigationProperty.getType(), navigationProperty.isCollection()));
      if (!navigationProperty.isNullable()) {
        writer.writeAttribute(XML_NULLABLE, "" + navigationProperty.isNullable());
      }

      if (navigationProperty.getPartner() != null) {
        EdmNavigationProperty partner = navigationProperty.getPartner();
        writer.writeAttribute(XML_PARTNER, partner.getName());
      }

      if (navigationProperty.containsTarget()) {
        writer.writeAttribute(XML_CONTAINS_TARGET, "" + navigationProperty.containsTarget());
      }

      if (navigationProperty.getReferentialConstraints() != null) {
        for (EdmReferentialConstraint constraint : navigationProperty.getReferentialConstraints()) {
          writer.writeEmptyElement("ReferentialConstraint");
          writer.writeAttribute(XML_PROPERTY, constraint.getPropertyName());
          writer.writeAttribute("ReferencedProperty", constraint.getReferencedPropertyName());
        }
      }

      writer.writeEndElement();
    }
  }