예제 #1
0
  // TODO change to use Contact.create() method
  @Security.Authenticated(Secured.class)
  public static Result newContact() {

    Form<Contact> filledForm = contactForm.bindFromRequest();

    String name = filledForm.data().get("name");
    String firstName = filledForm.data().get("firstName");
    String title = filledForm.data().get("title");
    String email = filledForm.data().get("email");
    String street = filledForm.data().get("street");
    String appendix1 = filledForm.data().get("appendix1");
    String appendix2 = filledForm.data().get("appendix2");
    String zipcode = filledForm.data().get("zipcode");
    String country = filledForm.data().get("country");
    String city = filledForm.data().get("city");
    String phone = filledForm.data().get("phone");
    String yearbook = filledForm.data().get("yearbookSubscription");
    String memberCategory = filledForm.data().get("memberCategory");
    String membershipSince = filledForm.data().get("membershipSince");

    Contact newContact = new Contact();
    newContact.name = name;
    newContact.firstName = firstName;
    newContact.title = title;
    newContact.email = email;
    newContact.street = street;
    newContact.appendix1 = appendix1;
    newContact.appendix2 = appendix2;
    newContact.zipcode = zipcode;
    newContact.city = city;
    newContact.country = country;
    newContact.phone = phone;

    if (yearbook.equals("true")) newContact.yearbookSubscription = true;
    newContact.memberCategory = memberCategory;

    for (int j = 0; j < ContactGroup.options().size(); j++) {
      String item = "belongsTo[" + j + "]";
      if (filledForm.data().get(item) != null) {
        ContactGroup cg =
            ContactGroup.find.byId((long) Integer.parseInt(filledForm.data().get(item)));
        newContact.belongsTo.add(cg);
      }
    }

    if (newContact.belongsTo.isEmpty())
      filledForm.reject("belongsTo[]", "Keine Sektion ausgewählt");

    // TODO Check fields for errors

    if (filledForm.hasErrors()) System.out.println(filledForm.errors().toString());

    newContact.membershipSince = membershipSince;
    newContact.createdAt = new Timestamp(new Date().getTime());
    newContact.lastEditedAt = newContact.createdAt;
    newContact.save();
    flash("success", "Kontakt " + newContact + " erstellt und gespeichert.");
    return redirect(routes.Application.contacts());
  }