@RequestMapping(value = "/org/{orgId}", method = RequestMethod.POST)
  @ResponseBody
  public Organization updateOrg(
      @PathVariable int orgId,
      @RequestParam(value = "name", required = true) String name,
      @RequestParam(value = "description", required = false) String description,
      @RequestParam(value = "street", required = false) String street,
      @RequestParam(value = "city", required = false) String city,
      @RequestParam(value = "state", required = false) String state,
      @RequestParam(value = "zip", required = false) String zip) {

    if (name == "") {
      throw new InvalidParameterException();
    }

    Organization org = new Organization(name);
    org.setId(orgId);
    org.setDescription(description);
    Address address = new Address(street, city, state, zip);
    org.setAddress(address);

    int error = orgService.update(org);
    if (error == -1) {
      throw new ResourceNotFoundException();
    }

    return org;
  }
  @RequestMapping(value = "/org", method = RequestMethod.POST)
  @ResponseBody
  public Organization createOrg(
      @RequestParam(value = "name", required = true) String name,
      @RequestParam(value = "description", required = false) String description,
      @RequestParam(value = "street", required = false) String street,
      @RequestParam(value = "city", required = false) String city,
      @RequestParam(value = "state", required = false) String state,
      @RequestParam(value = "zip", required = false) String zip) {

    if (name == "") {
      throw new InvalidParameterException();
    }

    Organization org = new Organization(name);
    if (description != null) {
      org.setDescription(description);
    }
    Address address = new Address(street, city, state, zip);
    org.setAddress(address);

    orgService.save(org);

    return org;
  }
예제 #3
0
  @Override
  public Organization update(Organization organization) {
    Session session = null;
    Transaction transaction = null;
    Organization organizationOld = null;
    try {
      session = sessionFactory.openSession();
      transaction = session.beginTransaction();
      organizationOld = (Organization) session.get(Organization.class, organization.getId());
      if (organizationOld == null) {
        throw new HibernateException("can't find organization with id = " + organization.getId());
      }
      if (organization.getDescription() != null) {
        organizationOld.setDescription(organization.getDescription());
      }
      if (organization.getName() != null) {
        organizationOld.setName(organization.getName());
      }
      if (organization.getAddress() != null) {
        Address address =
            new Address(
                organization.getAddress().getStreet(),
                organization.getAddress().getCity(),
                organization.getAddress().getState(),
                organization.getAddress().getZip());
        organizationOld.setAddress(address);
      }
      session.update(organizationOld);
      transaction.commit();
    } catch (HibernateException e) {
      if (transaction != null) {
        transaction.rollback();
      }
    } finally {
      if (session != null) {
        session.close();
      }
    }

    return organizationOld;
  }