protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); final JSONObject jsonObject = new JSONObject(sourceDocument.toJson()); if (idProperty != null) { // Replace the key id to the actual id field name in domain jsonObject.remove(Constants.ID_PROPERTY_NAME); jsonObject.put(idProperty.getName(), idValue); } return objectMapper.readValue(jsonObject.toString(), type); } catch (IOException e) { throw new IllegalStateException("Failed to read the source document " + sourceDocument.toJson() + " to target type " + type, e); } }
protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.registerModule(provideAdvancedSerializersModule()); try { final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); final JSONObject jsonObject = new JSONObject(sourceDocument.toJson()); if (idProperty != null) { // Replace the key id to the actual id field name in domain jsonObject.remove(Constants.ID_PROPERTY_NAME); jsonObject.put(idProperty.getName(), idValue); } return objectMapper.readValue(jsonObject.toString(), type); } catch (IOException e) { throw new IllegalStateException("Failed to read the source document " + sourceDocument.toJson() + " to target type " + type, e); } }
private RxDocumentServiceRequest getCreateDocumentRequest(String documentCollectionLink, Object document, RequestOptions options, boolean disableAutomaticIdGeneration, OperationType operationType) { if (StringUtils.isEmpty(documentCollectionLink)) { throw new IllegalArgumentException("documentCollectionLink"); } if (document == null) { throw new IllegalArgumentException("document"); } Document typedDocument = documentFromObject(document); RxDocumentClientImpl.validateResource(typedDocument); if (typedDocument.getId() == null && !disableAutomaticIdGeneration) { // We are supposed to use GUID. Basically UUID is the same as GUID // when represented as a string. typedDocument.setId(UUID.randomUUID().toString()); } String path = Utils.joinPath(documentCollectionLink, Paths.DOCUMENTS_PATH_SEGMENT); Map<String, String> requestHeaders = this.getRequestHeaders(options); RxDocumentServiceRequest request = RxDocumentServiceRequest.create(operationType, ResourceType.Document, path, typedDocument, requestHeaders); // NOTE: if the collection is not currently cached this will be a // blocking call DocumentCollection collection = this.collectionCache.resolveCollection(request); this.addPartitionKeyInformation(request, typedDocument, options, collection); return request; }
private DocumentServiceRequest getCreateDocumentRequest(String documentCollectionLink, Object document, RequestOptions options, boolean disableAutomaticIdGeneration, OperationType operationType) { if (StringUtils.isEmpty(documentCollectionLink)) { throw new IllegalArgumentException("documentCollectionLink"); } if (document == null) { throw new IllegalArgumentException("document"); } Document typedDocument = Document.FromObject(document, objectMapper); DocumentClient.validateResource(typedDocument); if (typedDocument.getId() == null && !disableAutomaticIdGeneration) { // We are supposed to use GUID. Basically UUID is the same as GUID // when represented as a string. typedDocument.setId(Utils.getSecureRandomUUID().toString()); } String path = Utils.joinPath(documentCollectionLink, Paths.DOCUMENTS_PATH_SEGMENT); Map<String, String> requestHeaders = getRequestHeaders(options); DocumentServiceRequest request = DocumentServiceRequest.create(operationType, ResourceType.Document, path, typedDocument, requestHeaders); this.addPartitionKeyInformation(request, typedDocument, options); return request; }
protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { final R result = instantiate(type); final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(result); final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); if (idProperty != null) { accessor.setProperty(idProperty, idValue); } entity.doWithProperties((PropertyHandler<DocumentDbPersistentProperty>) prop -> { if (idProperty != null && idProperty.equals(prop)) { return; } accessor.setProperty(prop, sourceDocument.get(prop.getName())); } ); return result; }