sfc-gh-pvillard commented on code in PR #10716:
URL: https://github.com/apache/nifi/pull/10716#discussion_r2676850880
##########
nifi-extension-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/UpdateBoxFileMetadataInstance.java:
##########
@@ -236,106 +242,116 @@ private Map<String, Object> readDesiredState(final
ProcessSession session,
return desiredState;
}
- private void updateMetadata(final Metadata metadata,
- final Map<String, Object> desiredState) {
- final List<String> currentKeys = metadata.getPropertyPaths();
+ private List<UpdateFileMetadataByIdRequestBody>
buildUpdateOperations(final MetadataFull currentMetadata,
+
final Map<String, Object> desiredState) {
+ final List<UpdateFileMetadataByIdRequestBody> operations = new
ArrayList<>();
- // Remove fields not in desired state
- for (final String propertyPath : currentKeys) {
- final String fieldName = propertyPath.substring(1); // Remove
leading '/'
+ // Get current field names from extra data
+ final Set<String> currentKeys = new HashSet<>();
+ final Map<String, Object> extraData = currentMetadata.getExtraData();
+ if (extraData != null) {
+ currentKeys.addAll(extraData.keySet());
+ }
+ // Remove fields not in desired state
+ for (final String fieldName : currentKeys) {
if (!desiredState.containsKey(fieldName)) {
- metadata.remove(propertyPath);
+ final String path = "/" + fieldName;
getLogger().debug("Removing metadata field: {}", fieldName);
+ operations.add(new UpdateFileMetadataByIdRequestBody.Builder()
+ .op(UpdateFileMetadataByIdRequestBodyOpField.REMOVE)
+ .path(path)
+ .build());
}
}
// Add or update fields
for (final Map.Entry<String, Object> entry : desiredState.entrySet()) {
final String fieldName = entry.getKey();
final Object value = entry.getValue();
- final String propertyPath = "/" + fieldName;
+ final String path = "/" + fieldName;
+ final boolean exists = currentKeys.contains(fieldName);
- updateField(metadata, propertyPath, value,
currentKeys.contains(propertyPath));
+ final UpdateFileMetadataByIdRequestBody operation =
buildFieldOperation(path, value, exists, extraData);
+ if (operation != null) {
+ operations.add(operation);
+ }
}
+
+ return operations;
}
- private void updateField(final Metadata metadata,
- final String propertyPath,
- final Object value,
- final boolean exists) {
+ private UpdateFileMetadataByIdRequestBody buildFieldOperation(final String
path,
+ final Object
value,
+ final
boolean exists,
+ final
Map<String, Object> extraData) {
if (value == null) {
- throw new IllegalArgumentException("Null value found for property
path: " + propertyPath);
+ throw new IllegalArgumentException("Null value found for property
path: " + path);
}
- if (exists) {
- final Object currentValue = metadata.getValue(propertyPath);
-
- // Only update if values are different
+ // If exists, check if values are different
+ if (exists && extraData != null) {
+ final String fieldName = path.substring(1);
+ final Object currentValue = extraData.get(fieldName);
if (Objects.equals(currentValue, value)) {
- return;
+ return null; // No change needed
}
+ }
+
+ final MetadataInstanceValue metadataValue =
convertToMetadataInstanceValue(value, path);
+
+ // Box API uses replace for both adding new fields and updating
existing fields
+ return new UpdateFileMetadataByIdRequestBody.Builder()
+ .op(UpdateFileMetadataByIdRequestBodyOpField.REPLACE)
+ .path(path)
+ .value(metadataValue)
+ .build();
+ }
- // Update
- switch (value) {
- case Number n -> metadata.replace(propertyPath,
n.doubleValue());
- case List<?> l -> metadata.replace(propertyPath,
convertListToStringList(l, propertyPath));
- case LocalDate d -> metadata.replace(propertyPath,
BoxDate.of(d).format());
- default -> metadata.replace(propertyPath, value.toString());
+ private MetadataInstanceValue convertToMetadataInstanceValue(final Object
value, final String path) {
+ if (value instanceof Number n) {
+ if (value instanceof Double || value instanceof Float) {
+ return new MetadataInstanceValue(n.doubleValue());
+ } else {
+ return new MetadataInstanceValue(n.longValue());
}
+ } else if (value instanceof List<?> l) {
+ final List<String> stringList = l.stream()
+ .map(obj -> {
+ if (obj == null) {
+ throw new IllegalArgumentException("Null value
found in list for field: " + path);
+ }
+ return obj.toString();
+ })
+ .collect(Collectors.toList());
+ return new MetadataInstanceValue(stringList);
+ } else if (value instanceof LocalDate d) {
+ return new MetadataInstanceValue(BoxDate.of(d).format());
} else {
- // Add new field
- switch (value) {
- case Number n -> metadata.add(propertyPath, n.doubleValue());
- case List<?> l -> metadata.add(propertyPath,
convertListToStringList(l, propertyPath));
- case LocalDate d -> metadata.add(propertyPath,
BoxDate.of(d).format());
- default -> metadata.add(propertyPath, value.toString());
- }
+ return new MetadataInstanceValue(value.toString());
}
}
- private List<String> convertListToStringList(final List<?> list,
- final String fieldName) {
- return list.stream()
- .map(obj -> {
- if (obj == null) {
- throw new IllegalArgumentException("Null value found
in list for field: " + fieldName);
- }
- return obj.toString();
- })
- .collect(Collectors.toList());
- }
-
/**
* Retrieves the metadata for a Box file.
* Visible for testing purposes.
*
- * @param boxFile The Box file to retrieve metadata from.
+ * @param fileId The ID of the file.
* @param templateKey The key of the metadata template.
* @return The metadata for the Box file.
*/
- Metadata getMetadata(final BoxFile boxFile,
- final String templateKey) {
- return boxFile.getMetadata(templateKey);
- }
-
- /**
- * Returns a BoxFile object for the given file ID.
- *
- * @param fileId The ID of the file.
- * @return A BoxFile object for the given file ID.
- */
- BoxFile getBoxFile(final String fileId) {
- return new BoxFile(boxAPIConnection, fileId);
+ MetadataFull getMetadata(final String fileId, final String templateKey) {
+ return boxClient.getFileMetadata().getFileMetadataById(fileId,
GetFileMetadataByIdScope.ENTERPRISE, templateKey);
}
/**
* Updates the metadata for a Box file.
*
- * @param boxFile The Box file to update.
- * @param metadata The metadata to update.
+ * @param fileId The ID of the file.
+ * @param templateKey The key of the metadata template.
+ * @param operations The list of update operations.
*/
- void updateBoxFileMetadata(final BoxFile boxFile, final Metadata metadata)
{
- boxFile.updateMetadata(metadata);
+ void updateBoxFileMetadata(final String fileId, final String templateKey,
final List<UpdateFileMetadataByIdRequestBody> operations) {
+ boxClient.getFileMetadata().updateFileMetadataById(fileId,
UpdateFileMetadataByIdScope.ENTERPRISE, templateKey, operations);
Review Comment:
done
##########
nifi-extension-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/UpdateBoxFileMetadataInstance.java:
##########
@@ -236,106 +242,116 @@ private Map<String, Object> readDesiredState(final
ProcessSession session,
return desiredState;
}
- private void updateMetadata(final Metadata metadata,
- final Map<String, Object> desiredState) {
- final List<String> currentKeys = metadata.getPropertyPaths();
+ private List<UpdateFileMetadataByIdRequestBody>
buildUpdateOperations(final MetadataFull currentMetadata,
+
final Map<String, Object> desiredState) {
+ final List<UpdateFileMetadataByIdRequestBody> operations = new
ArrayList<>();
- // Remove fields not in desired state
- for (final String propertyPath : currentKeys) {
- final String fieldName = propertyPath.substring(1); // Remove
leading '/'
+ // Get current field names from extra data
+ final Set<String> currentKeys = new HashSet<>();
+ final Map<String, Object> extraData = currentMetadata.getExtraData();
+ if (extraData != null) {
+ currentKeys.addAll(extraData.keySet());
+ }
+ // Remove fields not in desired state
+ for (final String fieldName : currentKeys) {
if (!desiredState.containsKey(fieldName)) {
- metadata.remove(propertyPath);
+ final String path = "/" + fieldName;
getLogger().debug("Removing metadata field: {}", fieldName);
+ operations.add(new UpdateFileMetadataByIdRequestBody.Builder()
+ .op(UpdateFileMetadataByIdRequestBodyOpField.REMOVE)
+ .path(path)
+ .build());
}
}
// Add or update fields
for (final Map.Entry<String, Object> entry : desiredState.entrySet()) {
final String fieldName = entry.getKey();
final Object value = entry.getValue();
- final String propertyPath = "/" + fieldName;
+ final String path = "/" + fieldName;
+ final boolean exists = currentKeys.contains(fieldName);
- updateField(metadata, propertyPath, value,
currentKeys.contains(propertyPath));
+ final UpdateFileMetadataByIdRequestBody operation =
buildFieldOperation(path, value, exists, extraData);
+ if (operation != null) {
+ operations.add(operation);
+ }
}
+
+ return operations;
}
- private void updateField(final Metadata metadata,
- final String propertyPath,
- final Object value,
- final boolean exists) {
+ private UpdateFileMetadataByIdRequestBody buildFieldOperation(final String
path,
+ final Object
value,
+ final
boolean exists,
+ final
Map<String, Object> extraData) {
if (value == null) {
- throw new IllegalArgumentException("Null value found for property
path: " + propertyPath);
+ throw new IllegalArgumentException("Null value found for property
path: " + path);
}
- if (exists) {
- final Object currentValue = metadata.getValue(propertyPath);
-
- // Only update if values are different
+ // If exists, check if values are different
+ if (exists && extraData != null) {
+ final String fieldName = path.substring(1);
+ final Object currentValue = extraData.get(fieldName);
if (Objects.equals(currentValue, value)) {
- return;
+ return null; // No change needed
Review Comment:
done
##########
nifi-extension-bundles/nifi-box-bundle/nifi-box-processors/src/main/java/org/apache/nifi/processors/box/UpdateBoxFileMetadataInstance.java:
##########
@@ -236,106 +242,116 @@ private Map<String, Object> readDesiredState(final
ProcessSession session,
return desiredState;
}
- private void updateMetadata(final Metadata metadata,
- final Map<String, Object> desiredState) {
- final List<String> currentKeys = metadata.getPropertyPaths();
+ private List<UpdateFileMetadataByIdRequestBody>
buildUpdateOperations(final MetadataFull currentMetadata,
+
final Map<String, Object> desiredState) {
+ final List<UpdateFileMetadataByIdRequestBody> operations = new
ArrayList<>();
- // Remove fields not in desired state
- for (final String propertyPath : currentKeys) {
- final String fieldName = propertyPath.substring(1); // Remove
leading '/'
+ // Get current field names from extra data
+ final Set<String> currentKeys = new HashSet<>();
+ final Map<String, Object> extraData = currentMetadata.getExtraData();
+ if (extraData != null) {
+ currentKeys.addAll(extraData.keySet());
+ }
+ // Remove fields not in desired state
+ for (final String fieldName : currentKeys) {
if (!desiredState.containsKey(fieldName)) {
- metadata.remove(propertyPath);
+ final String path = "/" + fieldName;
getLogger().debug("Removing metadata field: {}", fieldName);
+ operations.add(new UpdateFileMetadataByIdRequestBody.Builder()
+ .op(UpdateFileMetadataByIdRequestBodyOpField.REMOVE)
+ .path(path)
+ .build());
}
}
// Add or update fields
for (final Map.Entry<String, Object> entry : desiredState.entrySet()) {
final String fieldName = entry.getKey();
final Object value = entry.getValue();
- final String propertyPath = "/" + fieldName;
+ final String path = "/" + fieldName;
+ final boolean exists = currentKeys.contains(fieldName);
- updateField(metadata, propertyPath, value,
currentKeys.contains(propertyPath));
+ final UpdateFileMetadataByIdRequestBody operation =
buildFieldOperation(path, value, exists, extraData);
+ if (operation != null) {
+ operations.add(operation);
+ }
}
+
+ return operations;
}
- private void updateField(final Metadata metadata,
- final String propertyPath,
- final Object value,
- final boolean exists) {
+ private UpdateFileMetadataByIdRequestBody buildFieldOperation(final String
path,
+ final Object
value,
+ final
boolean exists,
+ final
Map<String, Object> extraData) {
if (value == null) {
- throw new IllegalArgumentException("Null value found for property
path: " + propertyPath);
+ throw new IllegalArgumentException("Null value found for property
path: " + path);
}
- if (exists) {
- final Object currentValue = metadata.getValue(propertyPath);
-
- // Only update if values are different
+ // If exists, check if values are different
+ if (exists && extraData != null) {
+ final String fieldName = path.substring(1);
+ final Object currentValue = extraData.get(fieldName);
if (Objects.equals(currentValue, value)) {
- return;
+ return null; // No change needed
}
+ }
+
+ final MetadataInstanceValue metadataValue =
convertToMetadataInstanceValue(value, path);
+
+ // Box API uses replace for both adding new fields and updating
existing fields
+ return new UpdateFileMetadataByIdRequestBody.Builder()
+ .op(UpdateFileMetadataByIdRequestBodyOpField.REPLACE)
+ .path(path)
+ .value(metadataValue)
+ .build();
+ }
- // Update
- switch (value) {
- case Number n -> metadata.replace(propertyPath,
n.doubleValue());
- case List<?> l -> metadata.replace(propertyPath,
convertListToStringList(l, propertyPath));
- case LocalDate d -> metadata.replace(propertyPath,
BoxDate.of(d).format());
- default -> metadata.replace(propertyPath, value.toString());
+ private MetadataInstanceValue convertToMetadataInstanceValue(final Object
value, final String path) {
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]