001package edu.pdx.cs410J.servlets;
002
003import edu.pdx.cs410J.family.FamilyTree;
004import edu.pdx.cs410J.family.Person;
005import edu.pdx.cs410J.family.XmlDumper;
006
007import javax.servlet.http.HttpServlet;
008import javax.servlet.http.HttpServletRequest;
009import javax.servlet.http.HttpServletResponse;
010import javax.servlet.ServletConfig;
011import javax.servlet.ServletException;
012import java.io.IOException;
013import java.io.PrintWriter;
014import java.util.Date;
015import java.util.Collection;
016
017/**
018 * A servlet that provides access to a {@link edu.pdx.cs410J.family.FamilyTree} via REST
019 *
020 * @since Summer 2008
021 */
022public class FamilyTreeServlet extends HttpServlet {
023  private FamilyTree tree;
024
025  private int nextPersonId = 1;
026
027  public void init(ServletConfig servletConfig) throws ServletException {
028    tree = new FamilyTree();
029  }
030
031  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
032    createPerson(request, response);
033  }
034
035  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
036    String uri = request.getRequestURI();
037    String lastPart = uri.substring(uri.lastIndexOf('/') + 1, uri.length());
038
039    if (lastPart.equals("person")) {
040      Collection<Person> all = this.tree.getPeople();
041      writePeople(response, all.toArray(new Person[all.size()]));
042
043    } else {
044      try {
045        int id = Integer.parseInt(lastPart);
046        Person person = this.tree.getPerson(id);
047        if (person == null) {
048          response.sendError(HttpServletResponse.SC_NOT_FOUND);
049          
050        } else {
051         writePeople(response, person);
052        }
053
054      } catch (NumberFormatException ex) {
055        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Malformed person id: " + lastPart);
056        return;
057      }
058    }
059  }
060
061  private void createPerson(HttpServletRequest request, HttpServletResponse response) throws IOException {
062    String gender = getParameter("Gender", request);
063    if (gender == null) {
064      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing Gender");
065      return;
066    }
067
068    Person person;
069    try {
070      person = new Person(nextPersonId++, Person.Gender.valueOf(gender.toUpperCase()));
071
072    } catch (IllegalArgumentException ex) {
073      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad Gender: " + gender);
074      return;
075    }
076
077    person.setFirstName(getParameter("FirstName", request));
078    person.setMiddleName(getParameter("MiddleName", request));
079    person.setLastName(getParameter("LastName", request));
080
081    String motherId = getParameter("MotherId", request);
082    if (motherId != null) {
083      try {
084        Person mother = tree.getPerson(Integer.parseInt(motherId));
085        if (mother == null) {
086          response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown mother: " + motherId);
087          return;
088
089        } else {
090          person.setMother(mother);
091        }
092
093      } catch (NumberFormatException ex) {
094        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad mother id: " + motherId);
095        return;
096      }
097    }
098
099    String fatherId = getParameter("FatherId", request);
100    if (fatherId != null) {
101      try {
102        Person father = tree.getPerson(Integer.parseInt(fatherId));
103        if (father == null) {
104          response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown father: " + fatherId);
105          return;
106
107        } else {
108          person.setFather(father);
109        }
110
111      } catch (NumberFormatException ex) {
112        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad father id: " + fatherId);
113        return;
114      }
115    }
116
117    String dob = getParameter("DateOfBirth", request);
118    if (dob != null) {
119      try {
120        person.setDateOfBirth(new Date(Long.parseLong(dob)));
121
122      } catch (NumberFormatException ex) {
123        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad date of birth: " + dob);
124        return;
125      }
126    }
127
128    String dod = getParameter("DateOfDeath", request);
129    if (dod != null) {
130      try {
131        person.setDateOfDeath(new Date(Long.parseLong(dod)));
132
133      } catch (NumberFormatException ex) {
134        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad date of death: " + dod);
135        return;
136      }
137    }
138
139    tree.addPerson(person);
140
141    writePeople(response, person);
142  }
143
144  /**
145   * Writes people to the response
146   */
147  private void writePeople(HttpServletResponse response, Person... people) throws IOException {
148    FamilyTree tree = new FamilyTree();
149    for (Person person : people) {
150      tree.addPerson(person);
151    }
152
153    new XmlDumper(new PrintWriter(response.getWriter(), true)).dump(tree);
154  }
155
156  private String getParameter(String name, HttpServletRequest request) {
157    String value = request.getParameter(name);
158    if (value == null || "".equals(value)) {
159      return null;
160
161    } else {
162      return value;
163    }
164  }
165}