001package edu.pdx.cs410J.family;
002
003import java.rmi.*;
004import java.util.*;
005
006/**
007 * <P>This is a concrete class that implements the
008 * <code>RemotePerson</code> interface.  It delegates most of its
009 * functionality to an underlying {@link Person} object.</P>
010 *
011 * <P>Note that because this class is only executed on the
012 * server-side, it does not have to be public.  However, the fact that
013 * it is a <code>UnicastRemoteObject</code> allows the client to
014 * execute its methods remotely.</P>
015 */
016@SuppressWarnings("serial")
017class RemotePersonImpl extends java.rmi.server.UnicastRemoteObject
018  implements RemotePerson {
019
020  /** The underlying Person that is being modeled */
021  private transient Person person;
022
023  /** The remote family tree that created this remote person.  Note
024   * this field is transient and is therefore not serialized. */
025  private transient RemoteFamilyTree tree;
026
027  ////////////////////  Constructors  ////////////////////////
028
029  /**
030   * Creates a new <code>RemotePersonImpl</code> that delegates most
031   * of its behavior to a given <code>Person</code>.
032   */
033  RemotePersonImpl(RemoteFamilyTree tree, Person person) 
034    throws RemoteException {
035
036    this.tree = tree;
037    this.person = person;
038  }
039
040  ////////////////////  Instance Methods  ////////////////////
041
042  public int getId() throws RemoteException {
043    return this.person.getId();
044  }
045
046  public Person.Gender getGender() throws RemoteException {
047    return this.person.getGender();
048  }
049
050  public String getFirstName() throws RemoteException {
051    return this.person.getFirstName();
052  }
053
054  public void setFirstName(String firstName) throws RemoteException {
055    this.person.setFirstName(firstName);
056  }
057
058  public String getMiddleName() throws RemoteException {
059    return this.person.getMiddleName();
060  }
061
062  public void setMiddleName(String middleName) 
063    throws RemoteException {
064    this.person.setMiddleName(middleName);
065  }
066
067  public String getLastName() throws RemoteException {
068    return this.person.getLastName();
069  }
070
071  public void setLastName(String lastName) throws RemoteException {
072    this.person.setLastName(lastName);
073  }
074
075  public int getFatherId() throws RemoteException {
076    return this.person.getFatherId();
077  }
078
079  public void setFatherId(int fatherId) throws RemoteException {
080    RemotePersonImpl rPerson = 
081      (RemotePersonImpl) this.tree.getPerson(fatherId);
082    if (rPerson == null) {
083      String s = "Could not find person with id " + fatherId;
084      throw new FamilyTreeException(s);
085    }
086    this.person.setFather(rPerson.person);
087  }
088
089  public int getMotherId() throws RemoteException {
090    return this.person.getMotherId();
091  }
092
093  public void setMotherId(int motherId) throws RemoteException {
094    RemotePersonImpl rPerson = 
095      (RemotePersonImpl) this.tree.getPerson(motherId);
096    if (rPerson == null) {
097      String s = "Could not find person with id " + motherId;
098      throw new FamilyTreeException(s);
099    }
100    this.person.setMother(rPerson.person);
101  }
102
103  public Date getDateOfBirth() throws RemoteException {
104    return this.person.getDateOfBirth();
105  }
106
107  public void setDateOfBirth(Date dob) throws RemoteException {
108    this.person.setDateOfBirth(dob);
109  }
110
111  public Date getDateOfDeath() throws RemoteException {
112    return this.person.getDateOfDeath();
113  }
114
115  public void setDateOfDeath(Date dod) throws RemoteException {
116    this.person.setDateOfDeath(dod);
117  }
118
119  public String getDescription() throws RemoteException {
120    return this.person.toString();
121  }
122
123  ////////////////////  Utility Methods  ////////////////////
124
125  /**
126   * Two <code>RemotePersonImpl</code>s are considered equal if their
127   * underlying persons have the same id.
128   */
129  public boolean equals(Object o) {
130    if (o instanceof RemotePerson) {
131      RemotePerson other = (RemotePerson) o;
132      try {
133        return this.getId() == other.getId();
134
135      } catch (RemoteException ex) {
136        return false;
137      }
138 
139    } else {
140      return false;
141    }
142  }
143
144}