package org.ovirt.engine.core.compat.backendcompat;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

// This will wrap java beans introspection
public class TypeCompat {

    private static final String CLASS = "class";
    private static final Log log = LogFactory.getLog(TypeCompat.class);

    public static List<PropertyInfo> GetProperties(Class<?> type) {
        List<PropertyInfo> returnValue = new ArrayList<PropertyInfo>();
        try {
            PropertyDescriptor[] pds = Introspector.getBeanInfo(type).getPropertyDescriptors();
            for (PropertyDescriptor pd : pds) {
                // Class is a bogus property, remove it
                if (!CLASS.equals(pd.getName())) {
                    returnValue.add(new PropertyInfo(pd));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return returnValue;
    }

    /**
     * The following method will get values for properties of provided object and will keep them in map
     * @param obj - this is an object which values will be taken
     * @param properties - a set of properties names
     * @param values - a map which will contains all values of properties
     */
    public static void getPropertyValues(Object obj, Set<String> properties, Map<String, String> values) {
        try {
            PropertyDescriptor[] pds = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors();
            int hitCount = 0;
            for (PropertyDescriptor pd : pds) {
                String propertyName = pd.getName().toLowerCase();
                if (properties.contains(propertyName)) {
                    Object value = null;
                    hitCount++;
                    try {
                        value = pd.getReadMethod().invoke(obj);
                        String stringValue = value != null ? value.toString() : null;
                        values.put(propertyName, stringValue);
                    } catch (Exception e) {
                        log.warn("Unable to get value of property: " + pd.getDisplayName() + " for class "
                                + obj.getClass().getName());
                    }
                    if (hitCount == properties.size()) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

@@ -1931,38 +1931,107 @@
                 
-try %7B%0A
+if(!values.containsKey(propertyName))  %7B%0A                        try %7B%0A    
                 
@@ -2078,16 +2078,20 @@
 e(obj);%0A
+    
         
@@ -2188,24 +2188,28 @@
             
+    
 values.put(p
@@ -2247,32 +2247,36 @@
                 
+    
 %7D catch (Excepti
@@ -2275,32 +2275,36 @@
 (Exception e) %7B%0A
+    
                 
@@ -2426,16 +2426,20 @@
         
+  
+  
 + obj.ge
@@ -2455,24 +2455,50 @@
 getName());%0A
+                        %7D%0A
             

fix overriding placeholder val in audit log