/**
  * Method to prepare Complex type representation.
  *
  * @param writer
  * @param entityMetadata
  * @param propertyName
  * @param propertiesList
  * @param modelName
  */
 private void writePropertyComplexList(
     StreamWriter writer,
     EntityMetadata entityMetadata,
     EntityProperty property,
     String modelName) {
   @SuppressWarnings("unchecked")
   List<EntityProperties> propertiesList = (List<EntityProperties>) property.getValue();
   String name = entityMetadata.getEntityName() + "_" + property.getName();
   int parseCount = 0;
   for (EntityProperties properties : propertiesList) {
     String fqTypeName = modelName + Metadata.MODEL_SUFFIX + "." + name;
     // We should be able to differentiate List<ComplexType> with regular ComplexType
     if (entityMetadata.isPropertyList(property.getFullyQualifiedName())) {
       if (parseCount == 0) {
         writer.startElement(new QName(d, name, "d"));
         writer.writeAttribute(new QName(m, "type", "m"), "Bag(" + fqTypeName + ")");
         writer.startElement(new QName(d, "element", "d"));
         parseCount++;
       } else {
         writer.startElement(new QName(d, "element", "d"));
       }
     } else {
       writer.startElement(new QName(d, name, "d"));
       writer.writeAttribute(new QName(m, "type", "m"), fqTypeName);
     }
     writeProperties(writer, entityMetadata, properties, modelName);
     writer.endElement();
   }
   // For List<ComplexTypes> we should end the complex node here
   if (!propertiesList.isEmpty()
       && entityMetadata.isPropertyList(property.getFullyQualifiedName())) {
     writer.endElement();
   }
 }
 private void writeProperty(
     StreamWriter writer, EntityMetadata entityMetadata, EntityProperty property) {
   String elementText = entityMetadata.getPropertyValueAsString(property);
   writer.startElement(new QName(d, property.getName(), "d"));
   EdmType type =
       MetadataOData4j.termValueToEdmType(
           entityMetadata.getTermValue(property.getFullyQualifiedName(), TermValueType.TERM_NAME));
   boolean isNullable = entityMetadata.isPropertyNullable(property.getFullyQualifiedName());
   // Append Type Attribute
   if (!type.equals(EdmSimpleType.STRING)) {
     writer.writeAttribute(new QName(m, "type", "m"), type.getFullyQualifiedTypeName());
   }
   // Append Null attribute
   if (isNullable && (elementText.isEmpty()) && !type.equals(EdmSimpleType.STRING)) {
     writer.writeAttribute(new QName(m, "null", "m"), "true");
   }
   // Write the property text
   if (type.equals(EdmSimpleType.DATETIME) && !elementText.isEmpty()) {
     // Write dates in UTC format
     SimpleDateFormat formatUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
     writer.writeElementText(formatUTC.format((Date) property.getValue()));
   } else if (elementText != null) {
     writer.writeElementText(elementText);
   }
   writer.endElement();
 }
 private String getAbsoluteId(
     String baseUri, String entitySetName, Entity entity, EntityMetadata entityMetadata) {
   String absId = "";
   for (String key : entityMetadata.getIdFields()) {
     EntityProperty prop =
         entity.getProperties().getProperty(entityMetadata.getSimplePropertyName(key));
     if (prop != null) {
       absId +=
           absId.isEmpty()
               ? (!baseUri.endsWith("/") ? baseUri + "/" : baseUri) + entitySetName
               : ",";
       if (entityMetadata.isPropertyNumber(prop.getFullyQualifiedName())) {
         absId += "(" + entityMetadata.getPropertyValueAsString(prop) + ")";
       } else {
         absId += "('" + entityMetadata.getPropertyValueAsString(prop) + "')";
       }
     }
   }
   return absId;
 }
  private void writeProperties(
      StreamWriter writer,
      EntityMetadata entityMetadata,
      EntityProperties entityProperties,
      String modelName) {
    assert (entityMetadata != null);
    // Loop round all properties writing out fields and MV and SV sets
    Map<String, EntityProperty> properties = entityProperties.getProperties();

    for (String propertyName : properties.keySet()) {
      // Work out what the property looks like by looking at the metadata
      EntityProperty property = (EntityProperty) properties.get(propertyName);
      boolean isComplex = entityMetadata.isPropertyComplex(property.getFullyQualifiedName());
      if (!isComplex) {
        // Simple field
        writeProperty(writer, entityMetadata, property);
      } else {
        // Complex List
        writePropertyComplexList(writer, entityMetadata, property, modelName);
      }
    }
  }