001package edu.pdx.cs410J.family;
002
003import java.io.*;
004import java.rmi.*;
005import java.text.*;
006import java.util.*;
007
008/**
009 * This program is an RMI client that adds a person to a remote family
010 * tree
011 */
012public class UpdatePerson {
013  private static PrintStream err = System.err;
014  private static PrintStream out = System.out;
015
016  /**
017   * Prints usage information about this program
018   */
019  private static void usage(String s) {
020    err.println("\n** " + s + "\n");
021    err.println("usage: java UpdatePerson familyName host port " +
022                "gender firstName lastName");
023    err.println("  -middleName middleName");
024    err.println("  -mother motherId");
025    err.println("  -father fatherId");
026    err.println("  -dob Date      The peron's date of birth");
027    err.println("  -dod Date      The peron's date of death");
028    err.println("");
029    err.println("This program updates the information about a person "
030                + "in a remote family tree.");
031    err.println("If the person does not already exist in the tree, a "
032                + "new person will be created.");
033    err.println("");
034    err.println("Dates should be in the form MM/DD/YYYY");
035    err.println("");
036    System.exit(1);
037  }
038
039  public static void main(String[] args) {
040    String familyName = null;
041    String host = null;
042    int port = -1;
043    Person.Gender gender = null;
044    String firstName = null;
045    String lastName = null;
046    String middleName = null;
047    int motherId = -1;
048    int fatherId = -1;
049    Date dob = null;
050    Date dod = null;
051
052    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
053
054    for (int i = 0; i < args.length; i++) {
055      if (args[i].equals("-middleName")) {
056        if (++i >= args.length) {
057          usage("Missing middle name");
058        }
059
060        middleName = args[i];
061
062      } else if (args[i].equals("-mother")) {
063        if (++i >= args.length) {
064          usage("Missing mother id");
065        }
066
067        try {
068          motherId = Integer.parseInt(args[i]);
069
070        } catch (NumberFormatException ex) {
071          usage("Invalid mother id: " + args[i]);
072        }
073
074      } else if (args[i].equals("-father")) {
075        if (++i >= args.length) {
076          usage("Missing father id");
077        }
078
079        try {
080          fatherId = Integer.parseInt(args[i]);
081
082        } catch (NumberFormatException ex) {
083          usage("Invalid father id: " + args[i]);
084        }
085
086      } else if (args[i].equals("-dob")) {
087        if (++i >= args.length) {
088          usage("Missing date of birth");
089        }
090
091        try {
092          dob = df.parse(args[i]);
093          
094        } catch (ParseException ex) {
095          usage("Malformed date: " + args[i]);
096        }
097
098      } else if (args[i].equals("-dod")) {
099        if (++i >= args.length) {
100          usage("Missing date of death");
101        }
102
103        try {
104          dod = df.parse(args[i]);
105          
106        } catch (ParseException ex) {
107          usage("Malformed date: " + args[i]);
108        }
109
110      } else if (familyName == null) {
111        familyName = args[i];
112
113      } else if (host == null) {
114        host = args[i];
115
116      } else if (port == -1) {
117        try {
118          port = Integer.parseInt(args[i]);
119
120        } catch (NumberFormatException ex) {
121          usage("Invalid port: " + args[i]);
122        }
123
124      } else if (gender == null) {
125        if (args[i].equalsIgnoreCase("male")) {
126          gender = Person.MALE;
127
128        } else if (args[i].equalsIgnoreCase("female")) {
129          gender = Person.FEMALE;
130
131        } else {
132          usage("Invalid gender: " + args[i]);
133        }
134
135      } else if (firstName == null) {
136        firstName = args[i];
137
138      } else if (lastName == null) {
139        lastName = args[i];
140
141      } else {
142        usage("Spurious command line: " + args[i]);
143      }
144    }
145
146    if (familyName == null) {
147      usage("Missing family name");
148
149    } else if (host == null) {
150      usage("Missing host");
151
152    } else if (port == -1) {
153      usage("Missing port");
154
155    } else if (gender == null) {
156      usage("Missing gender");
157
158    } else if (firstName == null) {
159      usage("Missing first name");
160
161    } else if (lastName == null) {
162      usage("Missing last name");
163    }
164
165    // Install an RMISecurityManager, if there is not a
166    // SecurityManager already installed
167    if (System.getSecurityManager() == null) {
168      System.setSecurityManager(new RMISecurityManager());
169    }
170
171    // Look up the remote family tree object
172    String name = "rmi://" + host + ":" + port + "/" + familyName;
173
174    try {
175      RemoteFamilyTree tree = 
176        (RemoteFamilyTree) Naming.lookup(name);
177
178      RemotePerson person = tree.getPerson(firstName, lastName);
179      if (person == null) {
180        person = tree.createPerson(gender);
181        person.setFirstName(firstName);
182        person.setLastName(lastName);
183      }
184
185      if (middleName != null) {
186        person.setMiddleName(middleName);
187      }
188
189      if (motherId != -1) {
190        person.setMotherId(motherId);
191      }
192
193      if (fatherId != -1) {
194        person.setFatherId(fatherId);
195      }
196
197      if (dob != null) {
198        person.setDateOfBirth(dob);
199      }
200
201      if (dod != null) {
202        person.setDateOfDeath(dod);
203      }
204
205      out.println("Updated: " + person.getDescription());
206
207    } catch (Exception ex) {
208      ex.printStackTrace(err);
209    }
210
211  }
212
213}