001package edu.pdx.cs410J.phonebillweb;
002
003import edu.pdx.cs410J.ParserException;
004
005import java.io.IOException;
006import java.io.PrintStream;
007import java.io.StringWriter;
008import java.util.Map;
009
010/**
011 * The main class that parses the command line and communicates with the
012 * Phone Bill server using REST.
013 */
014public class Project4 {
015
016    public static final String MISSING_ARGS = "Missing command line arguments";
017
018    public static void main(String... args) {
019        String hostName = null;
020        String portString = null;
021        String word = null;
022        String definition = null;
023
024        for (String arg : args) {
025            if (hostName == null) {
026                hostName = arg;
027
028            } else if ( portString == null) {
029                portString = arg;
030
031            } else if (word == null) {
032                word = arg;
033
034            } else if (definition == null) {
035                definition = arg;
036
037            } else {
038                usage("Extraneous command line argument: " + arg);
039            }
040        }
041
042        if (hostName == null) {
043            usage( MISSING_ARGS );
044
045        } else if ( portString == null) {
046            usage( "Missing port" );
047        }
048
049        int port;
050        try {
051            port = Integer.parseInt( portString );
052            
053        } catch (NumberFormatException ex) {
054            usage("Port \"" + portString + "\" must be an integer");
055            return;
056        }
057
058        PhoneBillRestClient client = new PhoneBillRestClient(hostName, port);
059
060        String message;
061        try {
062            if (word == null) {
063                // Print all word/definition pairs
064                Map<String, String> dictionary = client.getAllDictionaryEntries();
065                StringWriter sw = new StringWriter();
066                PrettyPrinter pretty = new PrettyPrinter(sw);
067                pretty.dump(dictionary);
068                message = sw.toString();
069
070            } else if (definition == null) {
071                // Print all dictionary entries
072                message = PrettyPrinter.formatDictionaryEntry(word, client.getDefinition(word));
073
074            } else {
075                // Post the word/definition pair
076                client.addDictionaryEntry(word, definition);
077                message = Messages.definedWordAs(word, definition);
078            }
079
080        } catch (IOException | ParserException ex ) {
081            error("While contacting server: " + ex.getMessage());
082            return;
083        }
084
085        System.out.println(message);
086    }
087
088    private static void error( String message )
089    {
090        PrintStream err = System.err;
091        err.println("** " + message);
092    }
093
094    /**
095     * Prints usage information for this program and exits
096     * @param message An error message to print
097     */
098    private static void usage( String message )
099    {
100        PrintStream err = System.err;
101        err.println("** " + message);
102        err.println();
103        err.println("usage: java Project4 host port [word] [definition]");
104        err.println("  host         Host of web server");
105        err.println("  port         Port of web server");
106        err.println("  word         Word in dictionary");
107        err.println("  definition   Definition of word");
108        err.println();
109        err.println("This simple program posts words and their definitions");
110        err.println("to the server.");
111        err.println("If no definition is specified, then the word's definition");
112        err.println("is printed.");
113        err.println("If no word is specified, all dictionary entries are printed");
114        err.println();
115    }
116}