Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Condition to Pupil/Teacher entities

Irshad
2012-12-04
2013-01-11
  • Irshad
    Irshad
    2012-12-04

    Dear Forum,

    I am a newbie and trying out OX.
    I have modified the Pupil/Teacher class as follows:

    • Teacher has a branch code which is not part of the primary key
    • Pupil also has a branch code not part of the primary key

    When a new Pupil is captured, I will capture the Id, Name and Branch.
    Then when the Teacher is specified, I want the Branch specified on the Pupil to be used as the default in the Teacher. The reason is that I only want to select Teacher belonging to the same branch as the Pupil.

    Teacher.java

    package org.openxava.management.model;
    import javax.persistence.;
    import org.openxava.annotations.
    ;
    import java.util.*;

    @Entity
    public class Teacher {

    @Id @Column(length=5) @Required   
    private String id;
    
    @Column(length=40) @Required
    private String name;
    
    @Column(length=5) @Required
    private String branch;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getBranch() {
        return branch;
    }
    
    public void setBranch(String branch) {
        this.branch = branch;
    }
    

    }

    Pupil.java

    package org.openxava.management.model;
    import javax.persistence.;
    import org.openxava.annotations.
    ;
    import java.util.*;

    @Entity
    public class Teacher {

    @Id @Column(length=5) @Required   
    private String id;
    
    @Column(length=40) @Required
    private String name;
    
    @Column(length=5) @Required
    private String branch;
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getBranch() {
        return branch;
    }
    
    public void setBranch(String branch) {
        this.branch = branch;
    }
    

    }

    Thanks and regards,
    Irshad

     
  • Javier Paniza
    Javier Paniza
    2012-12-07

    Hi Irshad,

    use @SearhAction in teacher reference of Pupil:

    @ManyToOne
    @SeachAction("Pupil.searchTeacher")
    private Teache teacher;
    

    Also, it's also interesting to use @OnChangeSearch to prevent obtaining teacher of other brands typing the code.

    You have a complete example of @SearchAction and @OnChangeSearch in the section 12.1 of the book.


    Help others in this forum as I help you.
    Before posting a question search this forum for the answer,
    and read the OpenXava documentation in openxava.org.

     
  • Irshad
    Irshad
    2013-01-11

    Thank you Javier it worked,

    This is what I have done

    ==================================
    @Entity
    public class Pupil {
    @Id @Column(length=2) @Required
    private int number;

    @Column(length=40) @Required
    private String name;
    
    @Column(length=5) @Required
    @ManyToOne
    @SearchAction("Pupil.searchTeacher")
    private Teacher teacher;
    
    public int getNumber() {
       return number;
    }
    

    ...

    package org.openxava.management.actions;
    import org.openxava.actions.*;

    public class SearchTeacherAction extends ReferenceSearchAction {

    public void execute() throws Exception {
    super.execute();

      String branchcode;
      branchcode = (String)getPreviousView().getValue("branch");
    
      if (! branchcode.trim().isEmpty() ) {
         getTab().setBaseCondition("${branch} = '" + branchcode.trim() + "'");
      }
    

    }
    }