Lior Vernia has uploaded a new change for review.

Change subject: webadmin: KeyValueModel to reuse CustomPropertiesUtils code
......................................................................

webadmin: KeyValueModel to reuse CustomPropertiesUtils code

Previously it duplicated the code, but now that the Utils class is
visible to the frontend this isn't necessary. Also took the chance to
refactor related code with the same class.

Change-Id: I5294f8d14149bff14f26a68d13b522a4cc4e97f2
Signed-off-by: Lior Vernia <[email protected]>
---
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/customprop/CustomPropertiesUtils.java
M 
frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
M 
frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/key_value/KeyValueModel.java
3 files changed, 39 insertions(+), 109 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/87/27387/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/customprop/CustomPropertiesUtils.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/customprop/CustomPropertiesUtils.java
index 2c277bd..664842a 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/customprop/CustomPropertiesUtils.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/customprop/CustomPropertiesUtils.java
@@ -281,7 +281,19 @@
         return sb.toString();
     }
 
-    protected Map<String, String> convertProperties(String properties, 
Map<String, String> regExMap) {
+    /**
+     * Converts custom properties from string to map.
+     *
+     * @param properties
+     *            specified device properties
+     * @param regExMap
+     *            a map containing valid keys and regular expressions that 
describe the valid form of their values
+     * @exception IllegalArgumentException
+     *                if specified properties has syntax errors
+     * @return map containing all valid custom properties ({@code 
LinkedHashMap} is used to ensure properties order is
+     *         constant)
+     */
+    public Map<String, String> convertProperties(String properties, 
Map<String, String> regExMap) {
         if (syntaxErrorInProperties(properties)) {
             throw new IllegalArgumentException("Invalid properties syntax!");
         }
diff --git 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
index 4664c5e..7f32802 100644
--- 
a/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
+++ 
b/frontend/webadmin/modules/gwt-common/src/main/resources/org/ovirt/engine/core/Common.gwt.xml
@@ -198,6 +198,7 @@
                <include name="common/AuditLogTimeInterval.java" />
                <include name="common/AuditLogSeverity.java" />
                <include name="common/utils/EnumUtils.java" />
+               <include name="common/utils/CustomPropertiesUtils.java" />
                <include name="common/utils/RpmVersionUtils.java" />
                <include name="common/utils/ListUtils.java" />
                <include name="common/utils/ObjectUtils.java" />
diff --git 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/key_value/KeyValueModel.java
 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/key_value/KeyValueModel.java
index 481531e..2bd245e 100644
--- 
a/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/key_value/KeyValueModel.java
+++ 
b/frontend/webadmin/modules/uicommonweb/src/main/java/org/ovirt/engine/ui/uicommonweb/models/vms/key_value/KeyValueModel.java
@@ -1,28 +1,25 @@
 package org.ovirt.engine.ui.uicommonweb.models.vms.key_value;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.ovirt.engine.core.compat.StringHelper;
+import org.ovirt.engine.core.common.utils.customprop.CustomPropertiesUtils;
+import 
org.ovirt.engine.core.common.utils.customprop.SimpleCustomPropertiesUtil;
 import org.ovirt.engine.ui.uicommonweb.validation.IValidation;
 import org.ovirt.engine.ui.uicommonweb.validation.RegexValidation;
-import org.ovirt.engine.ui.uicommonweb.validation.ValidationResult;
 import org.ovirt.engine.ui.uicompat.ConstantsManager;
 
 public class KeyValueModel extends BaseKeyModel {
 
-    public final static String PROPERTIES_DELIMETER = ";"; //$NON-NLS-1$
-    public final static String KEY_VALUE_DELIMETER = "="; //$NON-NLS-1$
+    private static CustomPropertiesUtils customPropertiesUtils = 
SimpleCustomPropertiesUtil.getInstance();
     private String saveEntity;
 
     public KeyValueModel() {
         super(ConstantsManager.getInstance().getConstants().pleaseSelectKey(), 
ConstantsManager.getInstance().getConstants().noKeyAvailable());
     }
-    Map<String, String> allKeyValueMap;
+    Map<String, String> allKeyValueMap = new HashMap<String, String>();
     Map<String, List<String>> allRegExKeys;
     private Map<String, String> keyValueMap_used = new HashMap<String, 
String>();
 
@@ -60,52 +57,8 @@
         }
 
         //always reset the list of items when the item changes
-        keyValueMap_used = new HashMap<String, String>();
-        if (value != null && !value.isEmpty()) {
-            String[] lines = value.split(PROPERTIES_DELIMETER);
-            String[] splitLine;
-            for (String line : lines) {
-                if (line.isEmpty()) {
-                    continue;
-                }
-
-                splitLine = line.split(KEY_VALUE_DELIMETER, 2);
-                String key = splitLine[0];
-                if (allKeyValueMap.containsKey(key)) {
-                    keyValueMap_used.put(key, splitLine[1]);
-                }
-
-            }
-        }
+        keyValueMap_used = customPropertiesUtils.convertProperties(value, 
allKeyValueMap);
         init(allKeyValueMap.keySet(), keyValueMap_used.keySet());
-    }
-
-    public void setKeyValueString(List<String> lines) {
-        if (lines == null) {
-            return;
-        }
-        allKeyValueMap = new HashMap<String, String>();
-        allRegExKeys = new HashMap<String, List<String>>();
-        RegexValidation regexValidation = new RegexValidation();
-        
regexValidation.setExpression("\\^\\((([a-zA-Z0-9_]+[|]+)*)[a-zA-Z0-9_]+\\)\\$");
 //$NON-NLS-1$
-        String[] splitLine;
-        for (String line : lines) {
-            if (line.isEmpty()) {
-                continue;
-            }
-            splitLine = line.split(KEY_VALUE_DELIMETER, 2);
-            String key = splitLine[0];
-            allKeyValueMap.put(key, splitLine[1]);
-            ValidationResult valid = 
regexValidation.validate(allKeyValueMap.get(key));
-            if (valid.getSuccess()) {
-                String[] values = allKeyValueMap.get(key)
-                        .substring(2, allKeyValueMap.get(key).length() - 2)
-                        .split("\\|"); //$NON-NLS-1$
-                allRegExKeys.put(key, Arrays.asList(values));
-            }
-        }
-
-        deserialize(saveEntity);
     }
 
     public void setKeyValueMap(Map<String, String> keyValueMap) {
@@ -113,33 +66,34 @@
             return;
         }
 
-        List<String> lines = new ArrayList<String>();
+        allKeyValueMap.clear();
+        allKeyValueMap.putAll(keyValueMap);
+
+        allRegExKeys = new HashMap<String, List<String>>();
+        RegexValidation regexValidation = new RegexValidation();
+        
regexValidation.setExpression("\\^\\((([a-zA-Z0-9_]+[|]+)*)[a-zA-Z0-9_]+\\)\\$");
 //$NON-NLS-1$
         for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
-            lines.add(entry.getKey() + '=' + entry.getValue());
+            if (regexValidation.validate(entry.getValue()).getSuccess()) {
+                String[] values = entry.getValue().substring(2, 
entry.getValue().length() - 2).split("\\|"); //$NON-NLS-1$
+                allRegExKeys.put(entry.getKey(), Arrays.asList(values));
+            }
         }
-        setKeyValueString(lines);
+
+        deserialize(saveEntity);
     }
 
     public String serialize() {
-        StringBuilder builder = new StringBuilder();
         if (getItems() == null) {
             return "";
         }
+
+        Map<String, String> actualKeyValues = new HashMap<String, String>();
         for (KeyValueLineModel keyValueLineModel : (List<KeyValueLineModel>) 
getItems()) {
-            String key = keyValueLineModel.getKeys().getSelectedItem();
-            if (!isKeyValid(key)) {
-                continue;
-            }
-            builder.append(key);
-            builder.append(KEY_VALUE_DELIMETER);
-            if (keyValueLineModel.getValue().getIsAvailable()) {
-                builder.append(keyValueLineModel.getValue().getEntity());
-            } else if (keyValueLineModel.getValues().getIsAvailable()) {
-                
builder.append(keyValueLineModel.getValues().getSelectedItem());
-            }
-            builder.append(PROPERTIES_DELIMETER);
+            actualKeyValues.put(keyValueLineModel.getKeys().getSelectedItem(), 
keyValueLineModel.getValue()
+                    .getIsAvailable() ? 
keyValueLineModel.getValue().getEntity() : keyValueLineModel.getValues()
+                    .getSelectedItem());
         }
-        return builder.toString();
+        return customPropertiesUtils.convertProperties(actualKeyValues);
     }
 
     public boolean validate() {
@@ -168,49 +122,12 @@
         return isValid;
     }
 
-    /**
-     * Converts properties from string to map. Method assumes, that properties 
are syntactically valid
-     *
-     * @param properties
-     *            specified properties
-     * @return map containing all properties ({@code LinkedHashMap} is used to 
ensure properties order is
-     *         constant)
-     */
     public static Map<String, String> convertProperties(String properties) {
-        Map<String, String> map = new LinkedHashMap<String, String>();
-        if (!StringHelper.isNullOrEmpty(properties)) {
-            String keyValuePairs[] = properties.split(PROPERTIES_DELIMETER);
-            for (String keyValuePairStr : keyValuePairs) {
-                String[] pairParts = 
keyValuePairStr.split(KEY_VALUE_DELIMETER, 2);
-                String key = pairParts[0];
-                // property value may be null
-                String value = pairParts[1];
-                map.put(key, value);
-            }
-        }
-        return map;
+        return customPropertiesUtils.convertProperties(properties);
     }
 
-    /**
-     * Converts properties from map to string.
-     *
-     * @param properties
-     *            specified properties
-     * @return string containing all properties in map
-     */
     public static String convertProperties(Map<String, String> map) {
-        StringBuilder sb = new StringBuilder();
-        if (map != null && !map.isEmpty()) {
-            for (Map.Entry<String, String> e : map.entrySet()) {
-                sb.append(e.getKey());
-                sb.append(KEY_VALUE_DELIMETER);
-                sb.append(e.getValue());
-                sb.append(PROPERTIES_DELIMETER);
-            }
-            // remove last PROPERTIES_DELIMETER
-            sb.deleteCharAt(sb.length() - 1);
-        }
-        return sb.toString();
+        return customPropertiesUtils.convertProperties(map);
     }
 
 }


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

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

Reply via email to