Gilad Chaplik has uploaded a new change for review.

Change subject: engine: simple custom properties util
......................................................................

engine: simple custom properties util

Allows to validate custom properties, against
a provided regex map.
Using this class a dynamic custom properties validation can be done.

Signed-off-by: Gilad Chaplik <[email protected]>
Change-Id: I4116f92333f6224aac7071b5ef0aa057c75d7fd7
---
A 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java
1 file changed, 65 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/04/16604/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java
new file mode 100644
index 0000000..c59d5b7
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/customprop/SimpleCustomPropertiesUtil.java
@@ -0,0 +1,65 @@
+package org.ovirt.engine.core.utils.customprop;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+public class SimpleCustomPropertiesUtil extends CustomPropertiesUtils {
+
+    private static final SimpleCustomPropertiesUtil instance = new 
SimpleCustomPropertiesUtil();
+
+    public static SimpleCustomPropertiesUtil getInstance() {
+        return instance;
+    }
+
+    /**
+     * validate a map of specific custom properties against provided regex map
+     * @param regExMap
+     *      <key, regex> map
+     * @param properties
+     *      <key, value> map, custom properties to validate
+     * @return
+     */
+    public List<ValidationError> validateProperties(Map<String, String> 
regExMap,
+            Map<String, String> properties) {
+
+        if (properties == null || properties.isEmpty()) {
+            // No errors in case of empty value
+            return Collections.emptyList();
+        }
+
+        if (syntaxErrorInProperties(properties)) {
+            return invalidSyntaxValidationError;
+        }
+
+        Set<ValidationError> errorsSet = new HashSet<ValidationError>();
+        Map<String, Pattern> keysToRegex = new HashMap<String, Pattern>();
+        for (Entry<String, String> property : regExMap.entrySet()) {
+            Pattern pattern = Pattern.compile(property.getValue());
+            keysToRegex.put(property.getKey(), pattern);
+        }
+
+        for (Entry<String, String> e : properties.entrySet()) {
+            String key = e.getKey();
+            String value = e.getValue();
+            if (key == null || !regExMap.containsKey(key)) {
+                errorsSet.add(new 
ValidationError(ValidationFailureReason.KEY_DOES_NOT_EXIST, key));
+                continue;
+            }
+
+            if (value == null || 
!keysToRegex.get(key).matcher(value).matches()) {
+                errorsSet.add(new 
ValidationError(ValidationFailureReason.INCORRECT_VALUE, key));
+                continue;
+            }
+        }
+        List<ValidationError> results = new ArrayList<ValidationError>();
+        results.addAll(errorsSet);
+        return results;
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/16604
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4116f92333f6224aac7071b5ef0aa057c75d7fd7
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Gilad Chaplik <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to