|
27 | 27 |
|
28 | 28 | import java.time.Duration; |
29 | 29 | import java.util.Map; |
| 30 | +import java.util.Optional; |
30 | 31 | import java.util.logging.Level; |
31 | 32 | import java.util.logging.Logger; |
32 | 33 | import java.util.stream.Collectors; |
@@ -76,12 +77,23 @@ public CommunicationEntity update(CommunicationEntity entity) { |
76 | 77 | requireNonNull(entity, "entity is required"); |
77 | 78 | String collectionName = entity.name(); |
78 | 79 | checkCollection(collectionName); |
79 | | - entity.find(KEY, String.class) |
80 | | - .orElseThrow(() -> new IllegalArgumentException("The document does not provide" + |
81 | | - " the _key column")); |
| 80 | + Optional<String> keyElement = entity.find(KEY, String.class); |
| 81 | + Optional<String> idElement = entity.find(ID, String.class); |
| 82 | + if (keyElement.isEmpty() && idElement.isEmpty()) { |
| 83 | + throw new IllegalArgumentException("The entity requires either key or id"); |
| 84 | + } |
| 85 | + var key = keyElement.orElseGet(() -> { |
| 86 | + String id = idElement.orElseThrow(); |
| 87 | + var elements = id.split("/"); |
| 88 | + if (elements.length == 2) { |
| 89 | + return elements[1]; |
| 90 | + } else { |
| 91 | + return elements[0]; |
| 92 | + } |
| 93 | + }); |
82 | 94 | JsonObject jsonObject = ArangoDBUtil.toJsonObject(entity); |
83 | 95 | DocumentUpdateEntity<Void> arangoDocument = arangoDB.db(database) |
84 | | - .collection(collectionName).updateDocument(jsonObject.getString(KEY), jsonObject); |
| 96 | + .collection(collectionName).updateDocument(key, jsonObject); |
85 | 97 | updateEntity(entity, arangoDocument.getKey(), arangoDocument.getId(), arangoDocument.getRev()); |
86 | 98 | return entity; |
87 | 99 | } |
|
0 commit comments