/** * Get list of properties that differs. * * @param oldBean * @param newBean * @return */ public static List<String> getBeanDiffProperties(Object oldBean, Object newBean) { List<String> result = new ArrayList<String>(); Map<String, String> oldMap = getBeanAsMap(oldBean); Map<String, String> newMap = getBeanAsMap(newBean); return getDiffProperties(oldMap, newMap); }
/** * Diff maps. * * @param mapOld * @param mapNew * @return list of different properties */ public static List<String> getDiffProperties(Map<String, String> mapOld, Map<String, String> mapNew) { List<String> result = new ArrayList<String>(); if (mapOld == null) { mapOld = new HashMap<String, String>(); } if (mapNew == null) { mapNew = new HashMap<String, String>(); } // Create common list of properties Set<String> properties = new HashSet<String>(); properties.addAll(mapOld.keySet()); properties.addAll(mapNew.keySet()); // Compare for (String beanProperty : properties) { String oldValue = mapOld.get(beanProperty); String newValue = mapNew.get(beanProperty); if (differs(oldValue, newValue)) { result.add(beanProperty); } } return result; }
/** * Create update entry from maps. * * @param system * @param user * @param targetType * @param target * @param oldMap * @param newMap * @return */ public static Tapahtuma createUPDATEMaps(String system, String user, String targetType, String target, Map<String, String> oldMap, Map<String, String> newMap) { Tapahtuma t = createUPDATE(system, user, targetType, target); try { Collection<String> changedProperties = SimpleBeanSerializer.getDiffProperties(oldMap, newMap); for (String propertyName : changedProperties) { t.addValueChange(propertyName, oldMap != null ? oldMap.get(propertyName) : null, newMap != null ? newMap.get(propertyName) : null); } } catch (Throwable ex) { t.addValue("DIFF FAILED", ex.toString()); ex.printStackTrace(); } return t; }
/** * Create UPDATE message, tries to generate field change information - best approximaption. * * @param system * @param user * @param targetType * @param target * @param oldObject * @param newObject * @return created event */ public static Tapahtuma createUPDATEBeans(String system, String user, String targetType, String target, Object oldObject, Object newObject) { Tapahtuma t = createUPDATE(system, user, targetType, target); try { Map<String, String> oldMap = SimpleBeanSerializer.getBeanAsMap(oldObject); Map<String, String> newMap = SimpleBeanSerializer.getBeanAsMap(newObject); Collection<String> changedProperties = SimpleBeanSerializer.getDiffProperties(oldMap, newMap); for (String propertyName : changedProperties) { t.addValueChange(propertyName, oldMap.get(propertyName), newMap.get(propertyName)); } } catch (Throwable ex) { t.addValue("SERIALIZATION/DIFF FAILED", ex.toString()); } return t; }