unflattenedMap = cotConverter.unflattenRField(observerDocumentMap);
+
+ // Determine document type based on 'w' field
+ String docType = getDocumentTypeFromMap(unflattenedMap);
+ if (docType == null) {
+ // Try to convert as GenericDocument if no type is found
+ return convertMapToDocument(unflattenedMap, GenericDocument.class);
+ }
+
+ // Convert to appropriate schema class based on type
+ if (isApiDocumentType(docType)) {
+ return convertMapToDocument(unflattenedMap, ApiDocument.class);
+ } else if (isChatDocumentType(docType)) {
+ return convertMapToDocument(unflattenedMap, ChatDocument.class);
+ } else if (isFileDocumentType(docType)) {
+ return convertMapToDocument(unflattenedMap, FileDocument.class);
+ } else if (isMapItemType(docType)) {
+ return convertMapToDocument(unflattenedMap, MapItemDocument.class);
+ } else {
+ return convertMapToDocument(unflattenedMap, GenericDocument.class);
+ }
+
+ } catch (Exception e) {
+ // Log the error for debugging
+ System.err.println("Error converting observer document: " + e.getMessage());
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ /**
+ * Convert observer document Map to JSON with reconstructed r-fields
+ *
+ * This function takes the Map<String, Object> from an observer document and reconstructs
+ * the hierarchical r-field structure from flattened r_* fields. This gives you
+ * the full document structure as it would appear in the original CoT event.
+ *
+ * @param observerDocumentMap Map<String, Object> from item.getValue() in observer callback
+ * @return JSON string with r-field reconstruction or null if conversion fails
+ *
+ * Example usage:
+ *
+ * // Example with flattened r_* fields
+ * Map<String, Object> docMap = item.getValue(); // Contains r_contact_callsign, etc.
+ * String jsonStr = converter.observerMapToJsonWithRFields(docMap);
+ *
+ * // Result JSON will have nested structure:
+ * // {
+ * // "_id": "test",
+ * // "w": "a-u-r-loc-g",
+ * // "r": {
+ * // "contact": {
+ * // "callsign": "TestUnit"
+ * // }
+ * // }
+ * // }
+ *
+ */
+ public String observerMapToJsonWithRFields(Map observerDocumentMap) {
+ if (observerDocumentMap == null) {
+ return null;
+ }
+
+ try {
+ // Unflatten r_* fields back to nested r field
+ Map unflattenedMap = cotConverter.unflattenRField(observerDocumentMap);
+
+ // Convert to JSON string
+ return objectMapper.writeValueAsString(unflattenedMap);
+ } catch (JsonProcessingException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Convert observer document JSON string to typed schema object
+ *
+ * This function takes a JSON string representation of an observer document
+ * and converts it to the appropriate schema class. Useful when you have
+ * JSON from other sources or need to work with JSON representations.
+ *
+ * @param observerDocumentJson JSON string representation of the document
+ * @return The converted schema object or null if conversion fails
+ *
+ * Example usage:
+ *