dan-s1 commented on code in PR #8610: URL: https://github.com/apache/nifi/pull/8610#discussion_r1572407325
########## nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongoRecord.java: ########## @@ -141,8 +135,8 @@ public class PutMongoRecord extends AbstractMongoProcessor { .displayName("Update Mode") .dependsOn(UPDATE_KEY_FIELDS) .description("Choose between updating a single document or multiple documents per incoming record.") - .allowableValues(UPDATE_ONE, UPDATE_MANY, UPDATE_FF_ATTRIBUTE) - .defaultValue(UPDATE_ONE.getValue()) + .allowableValues(MongoUpdateOption.allowedValues()) Review Comment: ```suggestion .allowableValues(MongoUpdateOption.class) ``` ########## nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongoHelper.java: ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.processors.mongodb; + +import java.util.EnumSet; +import org.apache.nifi.components.DescribedValue; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.flowfile.FlowFile; + +public class PutMongoHelper { + + public static final String ATTRIBUTE_MONGODB_UPDATE_MODE = "mongodb.update.mode"; + + /** + * Checks if given update mode option matches for the incoming flow file + * @param updateModeToMatch the value against which processor's mode is compared + * @param processorMode the value coming from running processor + * @param flowFile incoming flow file to extract processor mode + * @return true if the incoming files update mode matches with updateModeToMatch + */ + public static boolean updateModeMatches(MongoUpdateOption updateModeToMatch, PropertyValue processorMode, FlowFile flowFile) { + String updateMode = processorMode.getValue(); + + return updateModeToMatch.matches(updateMode) || (MongoUpdateOption.UPDATE_FF_ATTRIBUTE.matches(updateMode) && updateModeToMatch.getValue() + .equalsIgnoreCase(flowFile.getAttribute(PutMongoHelper.ATTRIBUTE_MONGODB_UPDATE_MODE))); + } + + public enum MongoUpdateOption implements DescribedValue { + UPDATE_ONE("one", "Update One", "Updates only the first document that matches the query."), + UPDATE_MANY("many", "Update Many", "Updates every document that matches the query."), + UPDATE_FF_ATTRIBUTE("flowfile-attribute", "Use '" + ATTRIBUTE_MONGODB_UPDATE_MODE + "' flowfile attribute.", + "Use the value of the '" + ATTRIBUTE_MONGODB_UPDATE_MODE + "' attribute of the incoming flowfile. Acceptable values are 'one' and 'many'."); + private final String value; + private final String displayName; + private final String description; + + MongoUpdateOption(final String value, final String displayName, final String description) { + this.value = value; + this.displayName = displayName; + this.description = description; + } + + public static EnumSet<MongoUpdateOption> allowedValues() { + return EnumSet.of( + UPDATE_ONE, + UPDATE_MANY, + UPDATE_FF_ATTRIBUTE + ); + } + Review Comment: ```suggestion ``` This method is not necessary as `.allowableValues` in `PropertyDescriptor.Builder` takes a class e.g. `.allowableValues(MongoUpdateOption.class)` ########## nifi-nar-bundles/nifi-mongodb-bundle/nifi-mongodb-processors/src/main/java/org/apache/nifi/processors/mongodb/PutMongo.java: ########## @@ -82,39 +96,51 @@ public class PutMongo extends AbstractMongoProcessor { .description("When true, inserts a document if no document matches the update query criteria; this property is valid only when using update mode, " + "otherwise it is ignored") .required(true) + .dependsOn(MODE,MODE_UPDATE) .allowableValues("true", "false") .addValidator(StandardValidators.BOOLEAN_VALIDATOR) .defaultValue("false") .build(); static final PropertyDescriptor UPDATE_QUERY_KEY = new PropertyDescriptor.Builder() .name("Update Query Key") - .description("Key name used to build the update query criteria; this property is valid only when using update mode, " - + "otherwise it is ignored. Example: _id") + .description("Comma separated key names used to build the update query criteria. Their values are taken from incoming flowfile. Example: _id") .required(false) + .dependsOn(MODE,MODE_UPDATE) .addValidator(StandardValidators.NON_BLANK_VALIDATOR) .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .build(); static final PropertyDescriptor UPDATE_QUERY = new PropertyDescriptor.Builder() .name("putmongo-update-query") .displayName("Update Query") - .description("Specify a full MongoDB query to be used for the lookup query to do an update/upsert.") + .description("Specify a full MongoDB query to be used for the lookup query to do an update/upsert. NOTE: this field is ignored if the '%s' value is not empty." + .formatted(UPDATE_QUERY_KEY.getDisplayName())) .required(false) + .dependsOn(MODE,MODE_UPDATE) .addValidator(JsonValidator.INSTANCE) .expressionLanguageSupported(ExpressionLanguageScope.FLOWFILE_ATTRIBUTES) .build(); - static final PropertyDescriptor UPDATE_MODE = new PropertyDescriptor.Builder() + static final PropertyDescriptor UPDATE_OPERATION_MODE = new PropertyDescriptor.Builder() .displayName("Update Mode") .name("put-mongo-update-mode") .required(true) + .dependsOn(MODE,MODE_UPDATE) .allowableValues(UPDATE_WITH_DOC, UPDATE_WITH_OPERATORS) - .defaultValue(UPDATE_WITH_DOC.getValue()) + .defaultValue(UPDATE_WITH_DOC) .description("Choose an update mode. You can either supply a JSON document to use as a direct replacement " + "or specify a document that contains update operators like $set, $unset, and $inc. " + "When Operators mode is enabled, the flowfile content is expected to be the operator part " + "for example: {$set:{\"key\": \"value\"},$inc:{\"count\":1234}} and the update query will come " + "from the configured Update Query property.") .build(); + static final PropertyDescriptor MONGO_UPDATE_MODE = new PropertyDescriptor.Builder() + .name("Mongo Update Query Mode") + .displayName("Mongo Update Query Mode") + .dependsOn(UPDATE_OPERATION_MODE,UPDATE_WITH_OPERATORS) + .description("Choose between 'updateOne' or 'updateMany' Mongo documents per incoming flow file.") + .allowableValues(MongoUpdateOption.allowedValues()) Review Comment: ```suggestion .allowableValues(MongoUpdateOption.class) ``` -- 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: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org