2012-07-06 40 views
0

您好我已經實現了java代碼,用於在這裏少數人的幫助下使用比較器接口查找2個java對象的差異。現在我想實現必須記錄(審計試驗)兩個對象之間所有差異的代碼。我的代碼是在這裏,使用審計試驗顯示2個Java對象的差異

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. 

回答

0

使用java Reflection API來實現this.I已經這樣做了早些時候對地方審計跟蹤,我們不能使用tigers

傳遞想要比較的屬性數組,它需要兩個相同類型的bean。

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; 

} 

一旦你得到了所有的細節,哪些列,表中的變化很容易,你的AuditDao可以在數據庫中記錄這些變化。

希望我已經回答了你的問題。

+0

嗨阿美,我怎麼能實現上述邏輯(反射API)在我的上述代碼。請告知這一點。謝謝。 – Dhruthi 2012-07-06 10:52:47

+0

@Dhruthi一旦你傳遞了兩個相同類型的對象,但在上面的狀態不同的類將打印兩個對象之間的差異,那麼你可以登錄在表中。 – amicngh 2012-07-06 10:56:16

+0

@Dhruthi:我會更新我的答案。 – amicngh 2012-07-06 11:00:59