1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public Map<String, Object> detecterModification(Object current, Object orig, List<String> exclusionsList) {
// Validation des objets input
if (current == null) {
throw new ExceptionTechnique("Objet current null");
}
if (orig == null) {
throw new ExceptionTechnique("Objet origine null");
}
if (!current.getClass().equals(orig.getClass())) {
throw new ExceptionTechnique("Les objets ne sont pas pas de la même instance");
}
// Instanciation de bean
final PropertyUtilsBean pub = BeanUtilsBean.getInstance().getPropertyUtils();
// récupération des proprietes
List<PropertyDescriptor> origDescriptors = new ArrayList<PropertyDescriptor>(Arrays.asList(pub.getPropertyDescriptors(orig)));
// Copie des propriétés. Les propriété exclues seront ignoré
Stream<PropertyDescriptor> streamPropertyDescriptor;
if (CollectionUtils.isEmpty(exclusionsList)) {
streamPropertyDescriptor = origDescriptors.stream();
} else {
// Execlure les attributs de la liste des exclusion
streamPropertyDescriptor = origDescriptors.stream().filter(x -> !(exclusionsList.contains(x.getName())));
}
// comparaison des propriétés
Map<String, Object> mapDifference = new HashMap<String, Object>();
streamPropertyDescriptor.forEach(origDescriptor -> {
String name = origDescriptor.getName();
if (pub.isReadable(orig, name) && pub.isReadable(current, name)) {
try {
Object valueOrig;
valueOrig = pub.getSimpleProperty(orig, name);
Object valueCurrent;
valueCurrent = pub.getSimpleProperty(current, name);
if (origDescriptor.getPropertyType().isComplexType()) { // comment détecter un bean complexe?
mapDifference.putAll(this.detecterModification(valueCurrent, valueOrig, null));
} else {
if(valueCurrent == null && valueOrig == null){
//nothing
}else if(valueCurrent != null && valueOrig == null){
mapDifference.put(name, valueCurrent);
}else if(valueCurrent == null && valueOrig != null){
mapDifference.put(name, valueCurrent);
}else if(!valueOrig.equals(valueCurrent)){
mapDifference.put(name, valueCurrent);
}
}
} catch (Exception e) {
logger.severe(() -> MessageFormat.format("Erreur de détection des modifications de {0} de la classe {1} : {2}", name, current.getClass(), e.getMessage()));
throw new ExceptionTechnique(e);
}
}
});
return mapDifference;
} |
Partager