dan-s1 commented on code in PR #7544:
URL: https://github.com/apache/nifi/pull/7544#discussion_r1476145113


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/ColumnNameNormalizerUtility.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.standard.db;
+
+
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+/**
+ * ColumnNameNormalizerUtility is a utility class that helps to normalize 
column names. It provides various strategies to
+ * modify column names based on the TranslationStrategy enum. Column names can 
be normalized by removing underscores,
+ * spaces, all special characters, or by using a custom regular expression 
defined by the user.
+ */
+public class ColumnNameNormalizerUtility {
+    // Regular expression to remove all special characters from a string.
+    private static final Pattern REMOVE_ALL_SPECIAL_CHAR_REGEX = 
Pattern.compile("[^a-zA-Z0-9]");
+
+    /**
+     * Normalizes the given column name based on the specified strategy.
+     *
+     * @param colName The column name to be normalized.
+     * @param isTranslationEnabled Boolean value to denote normalization is 
enabled
+     * @param strategy The TranslationStrategy for normalizing column name
+     * @param translationRegex Regex For translation
+     * @return The normalized column name as a String.
+     */
+    public static String getNormalizedName(final String colName, boolean 
isTranslationEnabled, TranslationStrategy strategy, Pattern translationRegex) {
+        // If the column name is null or translation is not enabled, return 
the original column name.
+        if (colName == null || !isTranslationEnabled) {
+            return colName;
+        }
+
+        return switch (Objects.requireNonNull(strategy)) {
+            case REMOVE_UNDERSCORE -> colName.toUpperCase().replace("_", "");
+            case REMOVE_SPACE -> colName.toUpperCase().replace(" ", "");
+            case REMOVE_ALL_SPECIAL_CHAR -> REMOVE_ALL_SPECIAL_CHAR_REGEX 
.matcher(colName.toUpperCase()).replaceAll("");

Review Comment:
   There is a space between `REMOVE_ALL_SPECIAL_CHAR_REGEX` and `.matcher`
   ```suggestion
               case REMOVE_ALL_SPECIAL_CHAR -> 
REMOVE_ALL_SPECIAL_CHAR_REGEX.matcher(colName.toUpperCase()).replaceAll("");
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/TranslationStrategy.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.standard.db;
+
+import org.apache.nifi.components.DescribedValue;
+
+/**
+ * Enumeration of supported Database column name Translation Strategy

Review Comment:
   ```suggestion
    * Enumeration of supported Database column name Translation Strategies
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/PutDatabaseRecord.java:
##########
@@ -1577,6 +1608,9 @@ static class DMLSettings {
 
         DMLSettings(ProcessContext context) {
             translateFieldNames = 
context.getProperty(TRANSLATE_FIELD_NAMES).asBoolean();
+            translationStrategy = 
TranslationStrategy.valueOf(context.getProperty(TRANSLATION_STRATEGY).getValue());
+            final String translationRegex = 
context.getProperty(TRANSLATION_PATTERN).getValue();
+            translationPattern = 
translationRegex==null?null:Pattern.compile(translationRegex);

Review Comment:
   ```suggestion
               translationPattern = translationRegex == null ? null : 
Pattern.compile(translationRegex);
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/TranslationStrategy.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.standard.db;
+
+import org.apache.nifi.components.DescribedValue;
+
+/**
+ * Enumeration of supported Database column name Translation Strategy
+ */
+public enum TranslationStrategy implements DescribedValue {
+    REMOVE_UNDERSCORE {
+        @Override
+        public String getDisplayName() {
+            return "Remove Underscore";
+        }
+
+        @Override
+        public String getDescription() {
+            return "Underscore(_) will be removed from column name with empty 
string Ex. Pics_1_11 become PICS111";
+        }
+    },
+    REMOVE_SPACE {
+        @Override
+        public String getDisplayName() {
+            return "Remove Space";
+        }
+
+        @Override
+        public String getDescription() {
+            return "Spaces will be removed from column name with empty string 
Ex. 'User Name' become 'USERNAME'";
+        }
+    },
+    REMOVE_ALL_SPECIAL_CHAR {
+        @Override
+        public String getDisplayName() {
+            return "Remove All Special Character";
+        }
+
+        @Override
+        public String getDescription() {
+            return "Remove All Special Character";
+        }
+    },
+    PATTERN{
+    @Override
+    public String getDisplayName() {
+        return "Regular Expression";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Remove character matched Regular Expression from column name";
+    }
+};
+
+
+    @Override
+    public String getValue() {
+        return name();
+    }
+
+    }

Review Comment:
   Although your implementation of this enum which extends `DescribedValue` 
works, if you browse the Apache NIFI code base you will notice that this not 
the standard way  of implementing it. I have included what the standard way is.
   ```suggestion
   public enum TranslationStrategy implements DescribedValue {
      REMOVE_UNDERSCORE("Remove Underscore",
               "Underscore(_) will be removed from column name with empty 
string Ex. Pics_1_11 become PICS111"),
       REMOVE_SPACE("Remove Space", "Spaces will be removed from column name 
with empty string Ex. 'User Name' become 'USERNAME'"),
       REMOVE_ALL_SPECIAL_CHAR("Remove All Special Character", "Remove All 
Special Character"),
       PATTERN("Regular Expression", "Remove character matched Regular 
Expression from column name");
       
       private final String displayName;
       private final String description;
   
       TranslationStrategy(String displayName, String description) {
           this.displayName = displayName;
           this.description = description;
       }
   
       @Override
       public String getValue() {
           return name();
       }
   
       @Override
       public String getDisplayName() {
           return displayName;
       }
   
       @Override
       public String getDescription() {
           return description;
       }
   }
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/ColumnNameNormalizerUtilityTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.standard.db;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class ColumnNameNormalizerUtilityTest {
+
+
+
+    @Test
+    void testNormalizingColumnName_RemoveUnderscore() {
+        String inputColumnName = "example_column_name";
+        String expectedNormalized = "EXAMPLECOLUMNNAME";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_UNDERSCORE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveSpace() {
+        String inputColumnName = "Column Name With Spaces";
+        String expectedNormalized = "COLUMNNAMEWITHSPACES";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_SPACE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveAllSpecialCharacters() {
+        String inputColumnName = "Special!Characters@Here$";
+        String expectedNormalized = "SPECIALCHARACTERSHERE";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_ALL_SPECIAL_CHAR, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_Regex() {
+        String inputColumnName = "Your @Input -String Here";
+        Pattern translationPattern = Pattern.compile("[@-]");
+        String expectedNormalized = "YOUR INPUT STRING HERE";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.PATTERN, translationPattern);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+
+    @Test
+    void testNormalizingColumnName_NotEnabled() {
+        String inputColumnName = "example_column_name";
+
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,false, 
TranslationStrategy.REMOVE_UNDERSCORE, null);

Review Comment:
   ```suggestion
           String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName, false, 
TranslationStrategy.REMOVE_UNDERSCORE, null);
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/PutDatabaseRecordTest.java:
##########
@@ -273,7 +273,7 @@ void testGeneratePreparedStatements() throws SQLException, 
MalformedRecordExcept
                         new ColumnDescription("name", 12, true, 255, true),
                         new ColumnDescription("code", 4, true, 10, true)
                 ),
-                false,
+                false,null,null,

Review Comment:
   ```suggestion
                   false, null, null,
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/ColumnNameNormalizerUtilityTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.standard.db;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class ColumnNameNormalizerUtilityTest {
+
+
+
+    @Test
+    void testNormalizingColumnName_RemoveUnderscore() {
+        String inputColumnName = "example_column_name";
+        String expectedNormalized = "EXAMPLECOLUMNNAME";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_UNDERSCORE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveSpace() {
+        String inputColumnName = "Column Name With Spaces";
+        String expectedNormalized = "COLUMNNAMEWITHSPACES";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_SPACE, null);

Review Comment:
   ```suggestion
           String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName, true, 
TranslationStrategy.REMOVE_SPACE, null);
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/PutDatabaseRecordTest.java:
##########
@@ -311,7 +311,7 @@ void testGeneratePreparedStatementsFailUnmatchedField() {
                         new ColumnDescription("name", 12, true, 255, true),
                         new ColumnDescription("code", 4, true, 10, true)
                 ),
-                false,
+                false,null,null,

Review Comment:
   ```suggestion
                   false, null, null,
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/ColumnNameNormalizerUtilityTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.standard.db;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class ColumnNameNormalizerUtilityTest {
+
+
+
+    @Test
+    void testNormalizingColumnName_RemoveUnderscore() {
+        String inputColumnName = "example_column_name";
+        String expectedNormalized = "EXAMPLECOLUMNNAME";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_UNDERSCORE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveSpace() {
+        String inputColumnName = "Column Name With Spaces";
+        String expectedNormalized = "COLUMNNAMEWITHSPACES";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_SPACE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveAllSpecialCharacters() {
+        String inputColumnName = "Special!Characters@Here$";
+        String expectedNormalized = "SPECIALCHARACTERSHERE";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_ALL_SPECIAL_CHAR, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_Regex() {
+        String inputColumnName = "Your @Input -String Here";
+        Pattern translationPattern = Pattern.compile("[@-]");
+        String expectedNormalized = "YOUR INPUT STRING HERE";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.PATTERN, translationPattern);

Review Comment:
   ```suggestion
           String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName, true, 
TranslationStrategy.PATTERN, translationPattern);
   ```



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/db/ColumnNameNormalizerUtilityTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.standard.db;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+
+public class ColumnNameNormalizerUtilityTest {
+
+
+
+    @Test
+    void testNormalizingColumnName_RemoveUnderscore() {
+        String inputColumnName = "example_column_name";
+        String expectedNormalized = "EXAMPLECOLUMNNAME";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_UNDERSCORE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveSpace() {
+        String inputColumnName = "Column Name With Spaces";
+        String expectedNormalized = "COLUMNNAMEWITHSPACES";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_SPACE, null);
+
+        assertEquals(expectedNormalized, normalized);
+    }
+
+    @Test
+    void testNormalizingColumnName_RemoveAllSpecialCharacters() {
+        String inputColumnName = "Special!Characters@Here$";
+        String expectedNormalized = "SPECIALCHARACTERSHERE";
+        String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName,true, 
TranslationStrategy.REMOVE_ALL_SPECIAL_CHAR, null);

Review Comment:
   ```suggestion
           String normalized = 
ColumnNameNormalizerUtility.getNormalizedName(inputColumnName, true, 
TranslationStrategy.REMOVE_ALL_SPECIAL_CHAR, null);
   ```



-- 
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

Reply via email to