//****************************************************************************************
// File:          Simpson.java
// Description:   Demo of attribute access with Java Access Provider
//                Describes how to retrieve the value stored in a RecordAttribute
// Created by:    Kapila
//****************************************************************************************

import ifs.fnd.ap.Record;
import ifs.fnd.ap.DataType;
import ifs.fnd.ap.RecordAttribute;
import ifs.fnd.ap.RecordAttributeCollection;
import ifs.fnd.ap.RecordCollection;
import ifs.fnd.ap.Server;
import ifs.fnd.ap.APException;

public abstract class Simpson {
   
   public static void main(String [] pars) {
      
      // First create the data structure
      Record person = new Record("PERSON");
      person.add("FIRST_NAME", "", DataType.TEXT);
      person.add("LAST_NAME", "", DataType.TEXT);
      person.add("AGE", 0, DataType.INTEGER);
      
      Record spouse = new Record("SPOUSE");
      spouse.setAttributes((RecordAttributeCollection)person.getAttributes().clone());
      
      Record child = new Record("CHILDREN");
      child.setAttributes((RecordAttributeCollection)person.getAttributes().clone());
      
      person.addAggregate("SPOUSE", spouse);
      RecordCollection children = person.addArray("CHILDREN");
      
      
      // Then create the example data
      Record personRecord = (Record)person.clone();
      personRecord.find("FIRST_NAME").setValue("Homer");
      personRecord.find("LAST_NAME").setValue("Simpson");
      personRecord.find("AGE").setValue(38);
      
      Record spouseRecord = personRecord.find("SPOUSE").getAggregate();
      spouseRecord.find("FIRST_NAME").setValue("Marge");
      
      RecordCollection childrenArray = personRecord.find("CHILDREN").getArray();
      
      Record childRecord = (Record)child.clone();
      childRecord.find("FIRST_NAME").setValue("Bart");
      childrenArray.add(childRecord);
      
      childRecord = (Record)child.clone();
      childRecord.find("FIRST_NAME").setValue("Lisa");
      childrenArray.add(childRecord);
      
      childRecord = (Record)child.clone();
      childRecord.find("FIRST_NAME").setValue("Maggie");
      childrenArray.add(childRecord);
      
      // Trace data
      System.out.println();
      System.out.println("************** TRACE *********************");
      System.out.println(personRecord.toString());
      System.out.println("******************************************");
      System.out.println();
      
      // Then read the data
      // 1. Get Homer
      String name = (String)personRecord.findValue("FIRST_NAME");
      System.out.println("Name is " + name);
      
      // 2. Get Marge
      spouseRecord = personRecord.find("SPOUSE").getAggregate();
      name = (String)spouseRecord.findValue("FIRST_NAME");
      System.out.println("Spouse is " + name);
      
      // 3. Get Children
      name = "";
      childrenArray = personRecord.find("CHILDREN").getArray();
      for(int i = 0; i < childrenArray.size(); i++) {
         if(i > 0)
            name += ", ";
         name += (String)childrenArray.get(i).findValue("FIRST_NAME");
      }
      System.out.println("Children are " + name);
      System.out.println();
   }
}