Пример #1
0
  public void saveToEdit(Gym gym) {
    Assert.notNull(gym);
    Assert.isTrue(actorService.checkAuthority("ADMIN"), "Only an admin can save gyms");

    Collection<ServiceEntity> services;
    Collection<ServiceEntity> servicesPreSave;
    ServiceEntity fitness;

    servicesPreSave = new ArrayList<ServiceEntity>();

    fitness = serviceService.findOneByName("Fitness");
    if (gym.getServices() == null) {
      gym.setServices(new ArrayList<ServiceEntity>());
    }
    gym.addService(fitness);

    if (gym.getId() != 0) {
      Gym gymPreSave;
      Collection<Booking> bookings;
      Collection<FeePayment> feePayments;
      Collection<Comment> comments;

      gymPreSave = gymRepository.findOne(gym.getId());

      bookings = gymPreSave.getBookings();
      feePayments = gymPreSave.getFeePayments();
      comments = gymPreSave.getComments();
      servicesPreSave = new ArrayList<ServiceEntity>(gymPreSave.getServices());

      Assert.isTrue(
          gym.getBookings().containsAll(bookings) && gym.getBookings().size() == bookings.size());
      Assert.isTrue(
          gym.getFeePayments().containsAll(feePayments)
              && gym.getFeePayments().size() == feePayments.size());
      Assert.isTrue(
          gym.getComments().containsAll(comments) && gym.getComments().size() == comments.size());
    }
    services = gym.getServices();

    gym = this.save(gym);

    for (ServiceEntity service : services) {
      if (!servicesPreSave.contains(service)) {
        service.addGym(gym);
        serviceService.save(service);
      }
    }

    for (ServiceEntity service : servicesPreSave) {
      if (!services.contains(service)) {
        service.removeGym(gym);
        serviceService.save(service);
      }
    }
  }
Пример #2
0
  public void delete(Gym gym) {
    Assert.notNull(gym);
    Assert.isTrue(gym.getId() != 0);
    Assert.isTrue(actorService.checkAuthority("ADMIN"), "Only an admin can delete gyms");
    Assert.isTrue(gym.getFeePayments().isEmpty());
    Assert.isTrue(gym.getBookings().isEmpty());
    Assert.isTrue(gym.getComments().isEmpty());

    Collection<ServiceEntity> services;

    services = serviceService.findAll();

    for (ServiceEntity service : services) {
      service.removeGym(gym);
      serviceService.save(service);
    }

    gymRepository.delete(gym);
  }