0

I've a website which build using struts framework.

I want to keep track if there any changes made to value that previously save in database. So I want to compare value between actionForm and entities class.

The idea is something like this,

public class CompareValue extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        MyActionForm actionFrom = (MyActionForm) form;//form/view which contains new value that are going to be save to the database

        EntitiesJpaController entitesController = new EntitiesJpaController();
        EntitiesClass entities = entitesController.findEntitiesById(actionFrom.getId());//get data previously save to database

        if(entities!=null){
            if(!entities.getName().equals(actionForm.getName())){
                System.out.println("Update applicant name");
            }

            if(!entities.getAddress().equals(actionForm.getAddress()){
                System.out.println("Update applicant address");
            }

            if(!entities.getNrIcNo().equals(actionForm.getNrIcNo()){
                System.out.println("Update applicant social security no");
            }

            /*log the changes somewhere*/

            EntitiesClass entitiesWithNewValue = new EntitiesClass();
            BeanUtil utilBean = new BeanUtil();
            utilBean.copyProperties(entitiesWithNewValue, actionForm);//copy new value from actionForm to entitiesClasee
            entitesController.edit(entitiesWithNewValue);//save new value to database

        }

        return mapping.findForward(SUCCESS);
    }
}

but if my entities class only contains 3 fields then is not a problem to me. So I want solution where I can iterate to every fields in entities class and compare the value with new value from actionform.

Zahary
  • 329
  • 8
  • 23
  • IF the fieldnames are completely the same on both classes, you could use java reflection to iterate over the methods, and copy the get and set values. – Robin Jonsson Jan 26 '14 at 13:29
  • @RobinJonsson reflection does not maintain order of method from Main class so it shall cause problem even when both class have same method – java seeker Jan 26 '14 at 13:50
  • the entities are simple pojos, why not the entities to implement comparable. – Roman C Jan 26 '14 at 14:57
  • @RomanC can you provide more clear example, is it something like this retrieved previous data Entities oldEntities = entitiesJpaController.findEntityById(id);, Entities newentities = new Entities();, copy new value from actionForm to newentities then compare oldEntities with newentities? – Zahary Jan 27 '14 at 03:45
  • @javaseeker: The order of methods is irrelevant. – Aleksandr M Jan 27 '14 at 10:14

0 Answers0