package rhp.aof4oop.apps;

 

/**

 * This demo shows several type of operations: creation, updating and deleting objects

 */

import rhp.aof4oop.dataobjects.Address;

import rhp.aof4oop.dataobjects.Family;

import rhp.aof4oop.dataobjects.Person;

import rhp.aof4oop.framework.core.CPersistentRoot;

 

public class Demo01 {

 

      /**

       * @param args

       * @throws Exception

       */

      public static void main(String[] args) throws Exception

      {

            // run sequentially program01 to program08, one at a time

            // programShowDataBase() shows the database content

            program01();

      }

 

      /**

       * Create root object

       */

      public static void program01()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Create root object");

            Family family=new Family();

            psRoot.setRootObject(family);

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * Add the father and the mother

       */

      public static void program02()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Add the father and the mother");

            Family family;

            family=psRoot.getRootObject();

           

            Person father=new Person();

            father.setName("Rui");

            family.setFather(father);

           

            Person mother=new Person();

            mother.setName("Isabel");

            family.setMother(mother);

           

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * Set the father age

       */

      public static void program03()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Set the father age");

            Family family;

            family=psRoot.getRootObject();

           

            family.getFather().setAge(39);

           

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * Set the mother address

       */

      public static void program04()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Set the mother address ");

            Family family;

            family=psRoot.getRootObject();

           

            family.getMother().setAddress(new Address("Rua xpto",526));

           

            showFamily(family);

            psRoot.dumpCache();          

      }

      /**

       * Add two childs

       */

      public static void program05()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("add two childs");

            Family family;

            family=psRoot.getRootObject();

           

            family.setChilds(new Person[]{new Person("Ana",null,null,9),new Person("Inês",null,null,1)});

           

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * Set the same address to all family

       */

      public static void program06()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Set the same address to all family");

            Family family;

            family=psRoot.getRootObject();

           

            family.getFather().setAddress(family.getMother().getAddress());

            for(Person p:family.getChilds())

            {

                  p.setAddress(family.getFather().getAddress());

            }

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * Delete the father

       */

      public static void program07()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Delete the father");

            Family family;

            family=psRoot.getRootObject();

           

            family.setFather(null);

           

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * add a father in two steps

       */

      public static void program08()

      {

            CPersistentRoot psRoot=new CPersistentRoot();

           

            System.out.println("Add a new father with no name and, following that, set the correct name");

            Family family;

            family=psRoot.getRootObject();

           

            family.setFather(new Person("no name",null,family.getMother().getAddress(),39));// add a father without name

            family.getFather().setName("Rui"); // sets the correct name

           

            showFamily(family);

            psRoot.dumpCache();

      }

      /**

       * This phase only shows the family object content

       */

      public static void programShowDataBase()

      {

            CPersistentRoot psRoot;

            psRoot=new CPersistentRoot();

            showFamily(psRoot.getRootObject());

            psRoot.dumpCache();

      }

      private static void showFamily(Family family)

      {

            int n=0;

            if(family==null)

            {

                  System.out.println("no family");

                  return;

            }

            System.out.println(family);

            showPerson("Father",family.getFather());

            showPerson("Mother",family.getMother());

            if(family.getChilds()!=null)

            {

                  for(Person p:family.getChilds())

                  {

                        showPerson("Child "+(++n),p);

                  }

            }

      }

      private static void showPerson(String label,Person person)

      {

            System.out.println(label+" Name:"+(person!=null?person.getName():"null"));

            System.out.println(label+" Age:"+(person!=null?person.getAge():"null"));

            System.out.println(label+" Address:"+(person!=null?person.getAddress():"null"));     

      }

}