	public String onSubmit(HttpServletRequest request, AppointmentBlock appointmentBlock, BindingResult result,
	        @RequestParam(value = "timeSlotLength", required = false) String timeSlotLength,
	        @RequestParam(value = "emptyTypes", required = false) String emptyTypes) throws Exception {
		HttpSession httpSession = request.getSession();
		if (Context.isAuthenticated()) {
			if (emptyTypes.equals("yes")) {
				appointmentBlock.setTypes(null);
			}
			AppointmentService appointmentService = Context.getService(AppointmentService.class);
			if (request.getParameter("save") != null) {
				new AppointmentBlockValidator().validate(appointmentBlock, result);
				if (result.hasErrors()) {
					return null;
				} else {
					if (appointmentBlock.getStartDate().before(Calendar.getInstance().getTime())) {
						result.rejectValue("startDate", "appointment.AppointmentBlock.error.dateCannotBeInThePast");
						return null;
					}
					if (!appointmentBlock.getStartDate().before(appointmentBlock.getEndDate())) {
						result.rejectValue("endDate", "appointment.AppointmentBlock.error.InvalidDateInterval");
						return null;
					}
					if (timeSlotLength.isEmpty() || Integer.parseInt(timeSlotLength) <= 0) {
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.selectTimeSlot");
						return null;
					}
					long appointmentBlocklengthInMinutes = (appointmentBlock.getEndDate().getTime() - appointmentBlock
					        .getStartDate().getTime()) / 60000;
					if (!timeSlotLength.isEmpty() && (Integer.parseInt(timeSlotLength) > appointmentBlocklengthInMinutes)) {
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.maximalTimeSlot");
						return null;
					}
					List<TimeSlot> currentTimeSlots = null;
					if (appointmentBlock.getCreator() != null) {
						boolean canBeUpdated = true;
						currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
						for (TimeSlot timeSlot : currentTimeSlots) {
							if (appointmentService.getAppointmentsInTimeSlot(timeSlot).size() > 0) {
								canBeUpdated = false;
								break;
							}
						}
						if (!canBeUpdated) {
							httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
							    "appointment.AppointmentBlock.error.appointmentsExist");
							return null;
						}
					}
					if (appointmentService.getOverlappingAppointmentBlocks(appointmentBlock).size() > 0) { 
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.appointmentBlockOverlap");
						return null;
					}
					appointmentService.saveAppointmentBlock(appointmentBlock);
					Integer slotLength = Integer.parseInt(timeSlotLength);
					int howManyTimeSlotsToCreate = (int) (appointmentBlocklengthInMinutes / slotLength);
					if (currentTimeSlots == null) {
						currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
					}
					if (currentTimeSlots.size() != howManyTimeSlotsToCreate) { 
						for (TimeSlot timeSlot : currentTimeSlots) {
							appointmentService.purgeTimeSlot(timeSlot);
						}
						Date startDate = appointmentBlock.getStartDate();
						Date endDate = null;
						Calendar cal;
						for (int i = 0; i < howManyTimeSlotsToCreate; i++) {
							cal = Context.getDateTimeFormat().getCalendar();
							cal.setTime(startDate);
							cal.add(Calendar.MINUTE, slotLength); 
							endDate = cal.getTime();
							TimeSlot timeSlot = new TimeSlot(appointmentBlock, startDate, endDate);
							startDate = endDate;
							appointmentService.saveTimeSlot(timeSlot);
						}
						if (startDate.before(appointmentBlock.getEndDate())) {
							TimeSlot timeSlot = new TimeSlot(appointmentBlock, startDate, appointmentBlock.getEndDate());
							appointmentService.saveTimeSlot(timeSlot);
						}
					}
					httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.saved");
				}
			}
			else if (request.getParameter("unvoid") != null) {
				appointmentService.unvoidAppointmentBlock(appointmentBlock);
				httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.unvoidedSuccessfully");
			}
		}
		return "redirect:appointmentBlockList.list";
	}

	public String onSubmit(HttpServletRequest request, AppointmentBlock appointmentBlock, BindingResult result,
	        @RequestParam(value = "timeSlotLength", required = false) String timeSlotLength,
	        @RequestParam(value = "emptyTypes", required = false) String emptyTypes) throws Exception {
		HttpSession httpSession = request.getSession();
		if (Context.isAuthenticated()) {
			if (emptyTypes.equals("yes")) {
				appointmentBlock.setTypes(null);
			}
			AppointmentService appointmentService = Context.getService(AppointmentService.class);
			if (request.getParameter("save") != null) {
				new AppointmentBlockValidator().validate(appointmentBlock, result);
				if (result.hasErrors()) {
					return null;
				} else {
					if (appointmentBlock.getStartDate().before(Calendar.getInstance().getTime())) {
						result.rejectValue("startDate", "appointment.AppointmentBlock.error.dateCannotBeInThePast");
						return null;
					}
					if (!appointmentBlock.getStartDate().before(appointmentBlock.getEndDate())) {
						result.rejectValue("endDate", "appointment.AppointmentBlock.error.InvalidDateInterval");
						return null;
					}
					if (timeSlotLength.isEmpty() || Integer.parseInt(timeSlotLength) <= 0) {
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.selectTimeSlot");
						return null;
					}
					long appointmentBlocklengthInMinutes = (appointmentBlock.getEndDate().getTime() - appointmentBlock
					        .getStartDate().getTime()) / 60000;
					if (!timeSlotLength.isEmpty() && (Integer.parseInt(timeSlotLength) > appointmentBlocklengthInMinutes)) {
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.maximalTimeSlot");
						return null;
					}
					List<TimeSlot> currentTimeSlots = null;
					if (appointmentBlock.getCreator() != null) {
						boolean canBeUpdated = true;
						currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
						for (TimeSlot timeSlot : currentTimeSlots) {
							if (appointmentService.getAppointmentsInTimeSlot(timeSlot).size() > 0) {
								canBeUpdated = false;
								break;
							}
						}
						if (!canBeUpdated) {
							httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
							    "appointment.AppointmentBlock.error.appointmentsExist");
							return null;
						}
					}
					if (appointmentService.getOverlappingAppointmentBlocks(appointmentBlock).size() > 0) { 
						httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
						    "appointment.AppointmentBlock.error.appointmentBlockOverlap");
						return null;
					}
					appointmentService.saveAppointmentBlock(appointmentBlock);
					Integer slotLength = Integer.parseInt(timeSlotLength);
					int howManyTimeSlotsToCreate = (int) (appointmentBlocklengthInMinutes / slotLength);
					if (currentTimeSlots == null) {
						currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
					}
					if (currentTimeSlots.size() != howManyTimeSlotsToCreate) { 
						for (TimeSlot timeSlot : currentTimeSlots) {
							appointmentService.purgeTimeSlot(timeSlot);
						}
						Date startDate = appointmentBlock.getStartDate();
						Date endDate = null;
						Calendar cal;
						for (int i = 0; i < howManyTimeSlotsToCreate - 1; i++) {
							cal = Context.getDateTimeFormat().getCalendar();
							cal.setTime(startDate);
							cal.add(Calendar.MINUTE, slotLength); 
							endDate = cal.getTime();
							TimeSlot timeSlot = new TimeSlot(appointmentBlock, startDate, endDate);
							startDate = endDate;
							appointmentService.saveTimeSlot(timeSlot);
						}
						TimeSlot timeSlot = new TimeSlot(appointmentBlock, startDate, appointmentBlock.getEndDate());
						appointmentService.saveTimeSlot(timeSlot);
					}
					httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.saved");
				}
			}
			else if (request.getParameter("unvoid") != null) {
				appointmentService.unvoidAppointmentBlock(appointmentBlock);
				httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.unvoidedSuccessfully");
			}
		}
		return "redirect:appointmentBlockList.list";
	}
