public void writeInternal(final Object entity, final Document targetDocument, final DocumentDbPersistentEntity<?> entityInformation) { if (entity == null) { return; } if (entityInformation == null) { throw new MappingException("no mapping metadata for entity type: " + entity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(entity); final DocumentDbPersistentProperty idProperty = entityInformation.getIdProperty(); if (idProperty != null) { targetDocument.setId((String) accessor.getProperty(idProperty)); } for (final Field field : entity.getClass().getDeclaredFields()) { if (null != idProperty && field.getName().equals(idProperty.getName())) { continue; } targetDocument.set(field.getName(), accessor.getProperty(entityInformation.getPersistentProperty(field.getName()))); } }
public void writeInternal(final Object entity, final Document targetDocument, final DocumentDbPersistentEntity<?> entityInformation) { if (entity == null) { return; } if (entityInformation == null) { throw new MappingException("no mapping metadata for entity type: " + entity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(entity); final DocumentDbPersistentProperty idProperty = entityInformation.getIdProperty(); if (idProperty != null) { targetDocument.setId((String) accessor.getProperty(idProperty)); } for (final Field field : entity.getClass().getDeclaredFields()) { if (null != idProperty && field.getName().equals(idProperty.getName())) { continue; } final PersistentProperty property = entityInformation.getPersistentProperty(field.getName()); Assert.notNull(property, "Property is null."); targetDocument.set(field.getName(), mapToDocumentDBValue(accessor.getProperty(property))); } }
public Document writeDoc(Object sourceEntity) { if (sourceEntity == null) { return null; } final DocumentDbPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(sourceEntity.getClass()); if (persistentEntity == null) { throw new MappingException("no mapping metadata for entity type: " + sourceEntity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(sourceEntity); final DocumentDbPersistentProperty idProperty = persistentEntity.getIdProperty(); final Document document; try { document = new Document(objectMapper.writeValueAsString(sourceEntity)); } catch (JsonProcessingException e) { throw new DocumentDBAccessException("Failed to map document value.", e); } if (idProperty != null) { final Object value = accessor.getProperty(idProperty); final String id = value == null ? null : value.toString(); document.setId(id); } return document; }
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; }