0

Hi I have implemented java code for finding the difference of 2 java objects using the comparator interface with the help of few folks here. Now i want to implement the code which has to log(Audit Trial) all the differences between 2 objects. My code is here,

public class A implements Comparator<A> {
    private int id1, id2; 
    /* setters and getters for id1 and id2 */                 

    public int compare(A o1, A o2) {
        if (o1 == o2) {
            return 0;
        } else if (o1 == null) {
            return -1;
        } else if (o2 == null) {
            return 1;
        }
        if (o1.getId1() != o2.getId1()) {
            return o1.getId1() - o2.getId1();
        } else {
            return o1.getId2() - o2.getId2();
        }
    }   
    public static void main(String args[]) {

        A obj1 = new A();
        obj1.id1 = 10;
        obj1.id2 = 20;

        A obj2 = new A();
        obj2.id1 = 10;
        obj2.id2 = 30;

        if (obj1.compare(obj1, obj2) == 0) {
            System.out.println("EQUALS");
        } else {
            System.out.println("NOT EQUALS");
        }
       }

 Please advise me how i can implement the code for AuditTrial here. Thanks.
4

1 回答 1

0

Use java Reflection API to achieve this.I have done this earlier where for Audit Trail we can not use tigers.

Pass the array of properties where you want to compare it takes the two beans of same type.

public static List<String[]> compareBeans(Object bean1, Object bean2,
        String... propertyNames) throws IntrospectionException,
        IllegalAccessException, InvocationTargetException {
    List<String[]> lList=new ArrayList<String[]>();
    Set<String> names = new HashSet<String>(Arrays.asList(propertyNames));
    BeanInfo beanInfo = Introspector.getBeanInfo(bean1.getClass());

    for (PropertyDescriptor prop : beanInfo.getPropertyDescriptors()) {
        if (names.remove(prop.getName())) {
            Method getter = prop.getReadMethod();
            Object value1 = getter.invoke(bean1);
            Object value2 = getter.invoke(bean2);
            if (value1 == value2
                    || (value1 != null && value1.equals(value2))) {
                continue;
   // if values are equal then no need to log it in database.
            } else {

                String[] changeArray=new String[4];
                changeArray[0]=getTableName(bean1);
                changeArray[1]=prop.getName();
                changeArray[2]=value1.toString();
                changeArray[3]=value2.toString();
                lList.add(changeArray);

            }
        }
    }

    for(String[] s:lList)
        System.out.println(s[0]+" , "+ s[1]+" , "+s[2]+" , "+s[3] );
    return lList;
}
   // Above code will compare two beans and give you the difference .

   // below method get the class name and then get the table name.
private static String getTableName(Object obj){

    String qualiFiedName=obj.getClass().getName();
    String name=qualiFiedName.substring(qualiFiedName.lastIndexOf(".")+1);

    // Your logic to get table name mapped with corresponding object

    return name;

}

Once you got all the details where in which column , table the changes are easily your AuditDao can log these changes in database.

Hopefully i have answered your question.

于 2012-07-06T10:48:21.700 回答