private void appendAtomMandatoryParts(
      final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data)
      throws EntityProviderException {
    try {
      writer.writeStartElement(FormatXml.ATOM_ID);
      location = properties.getServiceRoot().toASCIIString() + createSelfLink(eia, data, null);
      writer.writeCharacters(location);
      writer.writeEndElement();

      writer.writeStartElement(FormatXml.ATOM_TITLE);
      writer.writeAttribute(FormatXml.ATOM_TYPE, FormatXml.ATOM_TEXT);
      EntityPropertyInfo titleInfo = eia.getTargetPathInfo(EdmTargetPath.SYNDICATION_TITLE);
      if (titleInfo != null) {
        EdmSimpleType st = (EdmSimpleType) titleInfo.getType();
        Object object = data.get(titleInfo.getName());
        String title = st.valueToString(object, EdmLiteralKind.DEFAULT, titleInfo.getFacets());
        if (title != null) {
          writer.writeCharacters(title);
        }
      } else {
        writer.writeCharacters(eia.getEntitySetName());
      }
      writer.writeEndElement();

      writer.writeStartElement(FormatXml.ATOM_UPDATED);

      writer.writeCharacters(getUpdatedString(eia, data));

      writer.writeEndElement();
    } catch (XMLStreamException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    }
  }
  private static String createEntryKey(
      final EntityInfoAggregator entityInfo, final Map<String, Object> data)
      throws EntityProviderException {
    final List<EntityPropertyInfo> keyPropertyInfos = entityInfo.getKeyPropertyInfos();

    StringBuilder keys = new StringBuilder();
    for (final EntityPropertyInfo keyPropertyInfo : keyPropertyInfos) {
      if (keys.length() > 0) {
        keys.append(',');
      }

      final String name = keyPropertyInfo.getName();
      if (keyPropertyInfos.size() > 1) {
        keys.append(Encoder.encode(name)).append('=');
      }

      final EdmSimpleType type = (EdmSimpleType) keyPropertyInfo.getType();
      try {
        keys.append(
            Encoder.encode(
                type.valueToString(
                    data.get(name), EdmLiteralKind.URI, keyPropertyInfo.getFacets())));
      } catch (final EdmSimpleTypeException e) {
        throw new EntityProviderException(EntityProviderException.COMMON, e);
      }
    }

    return keys.toString();
  }
 private void appendCustomProperties(
     final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data)
     throws EntityProviderException {
   List<String> noneSyndicationTargetPaths = eia.getNoneSyndicationTargetPathNames();
   for (String tpName : noneSyndicationTargetPaths) {
     EntityPropertyInfo info = eia.getTargetPathInfo(tpName);
     final String name = info.getName();
     XmlPropertyEntityProducer aps = new XmlPropertyEntityProducer();
     aps.appendCustomProperty(writer, name, info, data.get(name));
   }
 }
 private String getTargetPathValue(
     final EntityInfoAggregator eia, final String targetPath, final Map<String, Object> data)
     throws EntityProviderException {
   try {
     EntityPropertyInfo info = eia.getTargetPathInfo(targetPath);
     if (info != null) {
       EdmSimpleType type = (EdmSimpleType) info.getType();
       Object value = data.get(info.getName());
       return type.valueToString(value, EdmLiteralKind.DEFAULT, info.getFacets());
     }
     return null;
   } catch (EdmSimpleTypeException e) {
     throw new EntityProviderException(EntityProviderException.COMMON, e);
   }
 }
 private boolean isNotMappedViaCustomMapping(final EntityPropertyInfo propertyInfo) {
   EdmCustomizableFeedMappings customMapping = propertyInfo.getCustomMapping();
   if (customMapping != null && customMapping.isFcKeepInContent() != null) {
     return customMapping.isFcKeepInContent().booleanValue();
   }
   return true;
 }
 String getUpdatedString(final EntityInfoAggregator eia, final Map<String, Object> data)
     throws EdmSimpleTypeException {
   Object updateDate = null;
   EdmFacets updateFacets = null;
   EntityPropertyInfo updatedInfo = eia.getTargetPathInfo(EdmTargetPath.SYNDICATION_UPDATED);
   if (updatedInfo != null) {
     updateDate = data.get(updatedInfo.getName());
     if (updateDate != null) {
       updateFacets = updatedInfo.getFacets();
     }
   }
   if (updateDate == null) {
     updateDate = new Date();
   }
   String valueToString =
       EdmDateTimeOffset.getInstance()
           .valueToString(updateDate, EdmLiteralKind.DEFAULT, updateFacets);
   return valueToString;
 }
  public void append(final Writer writer, final EntityPropertyInfo propertyInfo, final List<?> data)
      throws EntityProviderException {
    JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);

    try {
      jsonStreamWriter.beginObject();
      jsonStreamWriter.name(FormatJson.D);
      jsonStreamWriter.beginObject();

      jsonStreamWriter.name(FormatJson.METADATA);
      jsonStreamWriter.beginObject();
      jsonStreamWriter.namedStringValueRaw(
          FormatJson.TYPE,
          "Collection("
              + propertyInfo.getType().getNamespace()
              + Edm.DELIMITER
              + propertyInfo.getType().getName()
              + ")");
      jsonStreamWriter.endObject();
      jsonStreamWriter.separator();

      jsonStreamWriter.name(FormatJson.RESULTS);
      jsonStreamWriter.beginArray();
      boolean first = true;
      for (final Object item : data) {
        if (first) {
          first = false;
        } else {
          jsonStreamWriter.separator();
        }
        JsonPropertyEntityProducer.appendPropertyValue(jsonStreamWriter, propertyInfo, item);
      }
      jsonStreamWriter.endArray();

      jsonStreamWriter.endObject();
      jsonStreamWriter.endObject();
    } catch (final IOException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    } catch (final EdmException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    }
  }
 protected static void appendPropertyValue(
     final JsonStreamWriter jsonStreamWriter,
     final EntityPropertyInfo propertyInfo,
     final Object value)
     throws IOException, EdmException, EntityProviderException {
   if (propertyInfo.isComplex()) {
     if (value == null || value instanceof Map<?, ?>) {
       jsonStreamWriter.beginObject();
       appendPropertyMetadata(jsonStreamWriter, propertyInfo.getType());
       for (final EntityPropertyInfo childPropertyInfo :
           ((EntityComplexPropertyInfo) propertyInfo).getPropertyInfos()) {
         jsonStreamWriter.separator();
         final String name = childPropertyInfo.getName();
         jsonStreamWriter.name(name);
         appendPropertyValue(
             jsonStreamWriter,
             childPropertyInfo,
             value == null ? null : ((Map<?, ?>) value).get(name));
       }
       jsonStreamWriter.endObject();
     } else {
       throw new EntityProviderException(
           EntityProviderException.ILLEGAL_ARGUMENT.addContent(
               "A complex property must have a Map as data"));
     }
   } else {
     final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType();
     final Object contentValue =
         value instanceof Map ? ((Map<?, ?>) value).get(propertyInfo.getName()) : value;
     final String valueAsString =
         type.valueToString(contentValue, EdmLiteralKind.JSON, propertyInfo.getFacets());
     switch (EdmSimpleTypeKind.valueOf(type.getName())) {
       case String:
         jsonStreamWriter.stringValue(valueAsString);
         break;
       case Boolean:
       case Byte:
       case SByte:
       case Int16:
       case Int32:
         jsonStreamWriter.unquotedValue(valueAsString);
         break;
       case DateTime:
       case DateTimeOffset:
         // Although JSON escaping is (and should be) done in the JSON
         // serializer, we backslash-escape the forward slash here explicitly
         // because it is not required to escape it in JSON but in OData.
         jsonStreamWriter.stringValueRaw(
             valueAsString == null ? null : valueAsString.replace("/", "\\/"));
         break;
       default:
         jsonStreamWriter.stringValueRaw(valueAsString);
         break;
     }
   }
 }
  public void append(final Writer writer, final EntityPropertyInfo propertyInfo, final Object value)
      throws EntityProviderException {
    JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);

    try {
      jsonStreamWriter.beginObject().name(FormatJson.D).beginObject();

      jsonStreamWriter.name(propertyInfo.getName());
      appendPropertyValue(
          jsonStreamWriter,
          propertyInfo.isComplex() ? (EntityComplexPropertyInfo) propertyInfo : propertyInfo,
          value);

      jsonStreamWriter.endObject().endObject();
    } catch (final IOException e) {
      throw new EntityProviderException(
          EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
    } catch (final EdmException e) {
      throw new EntityProviderException(
          EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
    }
  }
  private void appendProperties(
      final XMLStreamWriter writer, final EntityInfoAggregator eia, final Map<String, Object> data)
      throws EntityProviderException {
    try {
      List<String> propertyNames = eia.getSelectedPropertyNames();
      if (!propertyNames.isEmpty()) {
        writer.writeStartElement(Edm.NAMESPACE_M_2007_08, FormatXml.M_PROPERTIES);

        for (String propertyName : propertyNames) {
          EntityPropertyInfo propertyInfo = eia.getPropertyInfo(propertyName);

          if (isNotMappedViaCustomMapping(propertyInfo)) {
            Object value = data.get(propertyName);
            XmlPropertyEntityProducer aps = new XmlPropertyEntityProducer();
            aps.append(writer, propertyInfo.getName(), propertyInfo, value);
          }
        }

        writer.writeEndElement();
      }
    } catch (XMLStreamException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    }
  }
  protected static String createETag(final EntityInfoAggregator eia, final Map<String, Object> data)
      throws EntityProviderException {
    try {
      String etag = null;

      Collection<EntityPropertyInfo> propertyInfos = eia.getETagPropertyInfos();
      for (EntityPropertyInfo propertyInfo : propertyInfos) {
        EdmType edmType = propertyInfo.getType();
        if (edmType instanceof EdmSimpleType) {
          EdmSimpleType edmSimpleType = (EdmSimpleType) edmType;
          if (etag == null) {
            etag =
                edmSimpleType.valueToString(
                    data.get(propertyInfo.getName()),
                    EdmLiteralKind.DEFAULT,
                    propertyInfo.getFacets());
          } else {
            etag =
                etag
                    + Edm.DELIMITER
                    + edmSimpleType.valueToString(
                        data.get(propertyInfo.getName()),
                        EdmLiteralKind.DEFAULT,
                        propertyInfo.getFacets());
          }
        }
      }

      if (etag != null) {
        etag = "W/\"" + etag + "\"";
      }

      return etag;
    } catch (EdmSimpleTypeException e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    }
  }
 private boolean isKeepInContent(final EntityPropertyInfo info) {
   EdmCustomizableFeedMappings customMapping = info.getCustomMapping();
   return Boolean.TRUE.equals(customMapping.isFcKeepInContent());
 }