tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > SOJO > Set Complex Nested Property

Set Complex Nested Property 

SOJO (Simplified Old Java Objects) is a Java framework which converts object graph into a specific structure or representation. This requires the library sojo-1.0.0.jar to be in classpath. The following example shows using PathExecuter.setNestedProperty() API.

File Name  :  
com/bethecoder/tutorials/sojo/common/Student.java 
   
package com.bethecoder.tutorials.sojo.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/sojo/tests/SetComplexNestedOGN.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.sojo.tests;

import java.io.IOException;
import java.util.Arrays;

import net.sf.sojo.navigation.PathExecuter;

import com.bethecoder.tutorials.sojo.common.ComplexBean;
import com.bethecoder.tutorials.sojo.common.Student;

public class SetComplexNestedOGN {

  /**
   @param args
   */
  public static void main(String[] argsthrows IOException {
    
    Student student = new Student("Sriram"2"Chess");
    Student student2 = new Student("Sudhakar"29"Painting");
    Student student3 = new Student("Charan"18"Reading books");
    Student student4 = new Student("Anu"28"Cooking");
    
    ComplexBean complexBean = new ComplexBean();
    complexBean.getStudentList().addAll(Arrays.asList(student, student2, student3));
    complexBean.getSubjectStudentMap().put("MATHS"
        Arrays.asList(student, student2, student3));
    
    complexBean.getSubjectStudentMap().put("PHYSICS"
        Arrays.asList(student, student2));
    
    //Adds 'student4' to the student list
    PathExecuter.setNestedProperty(complexBean, "studentList", student4);
    System.out.println(complexBean.getStudentList());
    
    //Updates the first student name in the student list
    PathExecuter.setNestedProperty(complexBean, "studentList[0].name""Sriram Kasireddi");
    System.out.println(complexBean.getStudentList().get(0).getName());
    
    //Updates student list for PHYSICS in subjectStudentMap
    PathExecuter.setNestedProperty(complexBean, "subjectStudentMap(PHYSICS)", Arrays.asList(student));
    System.out.println(complexBean.getSubjectStudentMap().get("PHYSICS"));
    
    //Updates second students age in MATHS student list
    PathExecuter.setNestedProperty(complexBean, "subjectStudentMap(MATHS).[1].age"26);
    System.out.println(complexBean.getSubjectStudentMap().get("MATHS").get(1).getAge());
    
    //Updates second students name in MATHS student list
    PathExecuter.setNestedProperty(complexBean, "subjectStudentMap.MATHS[1].name""Sudhakar Kasireddi");
    System.out.println(complexBean.getSubjectStudentMap().get("MATHS").get(1).getName());

  }
}
   

It gives the following output,
[Student[name = Sriram, age = 2, hobby = Chess], 
	Student[name = Sudhakar, age = 29, hobby = Painting], 
	Student[name = Charan, age = 18, hobby = Reading books], 
	Student[name = Anu, age = 28, hobby = Cooking]]
	
Sriram Kasireddi

[Student[name = Sriram Kasireddi, age = 2, hobby = Chess]]

26

Sudhakar Kasireddi



 
  


  
bl  br