Ejemplo n.º 1
0
 @Override
 public Organization delete(long id) {
   Session session = null;
   Transaction transaction = null;
   Organization organization = null;
   try {
     session = sessionFactory.openSession();
     transaction = session.beginTransaction();
     organization = (Organization) session.get(Organization.class, id);
     if (organization == null) {
       throw new HibernateException("Can't find organization record with id = " + id);
     } else if (organization.getPersons() != null && organization.getPersons().size() != 0) {
       organization = null;
       throw new HibernateException(
           "Can't delete organization record because it contains members.");
     }
     session.delete(organization);
     transaction.commit();
   } catch (HibernateException e) {
     if (transaction != null) {
       transaction.rollback();
     }
   } finally {
     if (session != null) {
       session.close();
     }
   }
   return organization;
 }
  @RequestMapping(value = "/person/{personId}", method = RequestMethod.POST)
  @ResponseBody
  public Person updatePerson(
      @PathVariable int personId,
      @RequestParam(value = "firstname", required = true) String firstname,
      @RequestParam(value = "lastname", required = true) String lastname,
      @RequestParam(value = "email", required = true) String email,
      @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,
      @RequestParam(value = "orgId", required = false) Integer orgId) {

    if (firstname == "" || lastname == "" || email == "") {
      throw new InvalidParameterException();
    }

    Person person = new Person(firstname, lastname, email);
    person.setId(personId);
    person.setDescription(description);
    Address address = new Address(street, city, state, zip);
    person.setAddress(address);
    if (orgId != null) {
      Organization org = new Organization();
      org.setId(orgId);
      person.setOrg(org);
    }

    personService.update(person);

    return person;
  }
  @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;
  }
  @RequestMapping(value = "/org/{orgId}", method = RequestMethod.GET)
  public String getOrg(@PathVariable int orgId, Model model) {

    Organization org = orgService.findByOrgId(orgId);
    if (org == null) {
      throw new ResourceNotFoundException();
    }
    model.addAttribute("id", org.getId());
    model.addAttribute("name", org.getName());
    model.addAttribute("description", org.getDescription());
    model.addAttribute("address", org.getAddress());
    return "organization";
  }
Ejemplo n.º 6
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;
  }