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 UpdateMarriage {
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 UpdateMarriage familyName host port " +
022                "husbandId wifeId");
023    err.println("  -date Date");
024    err.println("  -location Location");
025    err.println("");
026    err.println("This program updates the information about a marriage "
027                + "between two people in a remote family tree.");
028    err.println("If the marriage does not already exist, a "
029                + "new person will be created.");
030    err.println("");
031    err.println("Dates should be in the form MM/DD/YYYY");
032    err.println("");
033    System.exit(1);
034  }
035
036  public static void main(String[] args) {
037    String familyName = null;
038    String host = null;
039    int port = -1;
040    int husbandId = -1;
041    int wifeId = -1;
042    Date date = null;
043    String location = null;
044
045    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
046
047    for (int i = 0; i < args.length; i++) {
048      if (args[i].equals("-date")) {
049        if (++i >= args.length) {
050          usage("Missing date");
051        }
052
053        try {
054          date = df.parse(args[i]);
055          
056        } catch (ParseException ex) {
057          usage("Malformed date: " + args[i]);
058        }
059
060      } else if (args[i].equals("-location")) {
061        if (++i >= args.length) {
062          usage("Missing location");
063        }
064
065        location = args[i];
066
067      } else if (familyName == null) {
068        familyName = args[i];
069
070      } else if (host == null) {
071        host = args[i];
072
073      } else if (port == -1) {
074        try {
075          port = Integer.parseInt(args[i]);
076
077        } catch (NumberFormatException ex) {
078          usage("Invalid port: " + args[i]);
079        }
080
081      } else if (husbandId == -1) {
082        try {
083          husbandId = Integer.parseInt(args[i]);
084
085        } catch (NumberFormatException ex) {
086          usage("Invalid husband id: " + args[i]);
087        }
088
089      } else if (wifeId == -1) {
090        try {
091          wifeId = Integer.parseInt(args[i]);
092
093        } catch (NumberFormatException ex) {
094          usage("Invalid wife id: " + args[i]);
095        }
096
097      } else {
098        usage("Spurious command line: " + args[i]);
099      }
100    }
101
102    if (familyName == null) {
103      usage("Missing family name");
104
105    } else if (host == null) {
106      usage("Missing host");
107
108    } else if (port == -1) {
109      usage("Missing port");
110
111    } else if (husbandId == -1) {
112      usage("Missing husband id");
113
114    } else if (wifeId == -1) {
115      usage("Missing wife id");
116    }
117
118    // Install an RMISecurityManager, if there is not a
119    // SecurityManager already installed
120    if (System.getSecurityManager() == null) {
121      System.setSecurityManager(new RMISecurityManager());
122    }
123
124    // Look up the remote family tree object
125    String name = "rmi://" + host + ":" + port + "/" + familyName;
126
127    try {
128      RemoteFamilyTree tree = 
129        (RemoteFamilyTree) Naming.lookup(name);
130
131      RemoteMarriage marriage = tree.getMarriage(husbandId, wifeId);
132      if (marriage == null) {
133        marriage = tree.createMarriage(husbandId, wifeId);
134      }
135
136      if (date != null) {
137        marriage.setDate(date);
138      }
139
140      if (location != null) {
141        marriage.setLocation(location);
142      }
143
144      out.println("Updated: " + marriage.getDescription());
145
146    } catch (Exception ex) {
147      ex.printStackTrace(err);
148    }
149
150  }
151
152}