Skip to content

Commit

Permalink
AM-17 bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamLauz committed Jan 14, 2013
1 parent 5b51861 commit 9a0103a
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 111 deletions.
2 changes: 2 additions & 0 deletions api/src/main/resources/messages.properties
Expand Up @@ -70,6 +70,8 @@ ${project.parent.artifactId}.AppointmentBlock.endDate=End Date
${project.parent.artifactId}.AppointmentBlock.addType=Add type
${project.parent.artifactId}.AppointmentBlock.apply=Apply
${project.parent.artifactId}.AppointmentBlock.minutes=Minutes
${project.parent.artifactId}.AppointmentBlock.deleteDialog.title=Warning


${project.parent.artifactId}.Appointment.emptyTimeSlot=Empty appointment time slot
${project.parent.artifactId}.Appointment.emptyPatient=Empty appointment patient
Expand Down
Expand Up @@ -5,26 +5,15 @@
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.PersonAttribute;
import org.openmrs.Provider;
import org.openmrs.api.APIException;
import org.openmrs.api.PatientService;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointment.Appointment;
import org.openmrs.module.appointment.AppointmentBlock;
import org.openmrs.module.appointment.TimeSlot;
import org.openmrs.module.appointment.api.AppointmentService;
import org.openmrs.web.WebConstants;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.RequestParam;

/**
* DWR patient methods. The methods in here are used in the webapp to get data from the database via
Expand Down Expand Up @@ -100,6 +89,28 @@ public List<AppointmentBlockData> getAppointmentBlocks(String fromDate, String t
return appointmentBlockDatalist;
}

public Integer getNumberOfAppointmentsInAppointmentBlock(Integer appointmentBlockId) {
Integer numberOfAppointments = null;
if (Context.isAuthenticated()) {
AppointmentService as = Context.getService(AppointmentService.class);
if (appointmentBlockId != null) {
//Assumption - Exists such an appointment block in the data base with the given Id
AppointmentBlock appointmentBlock = as.getAppointmentBlock(appointmentBlockId);
List<Appointment> appointments = new ArrayList<Appointment>();
//Getting the timeslots of the given appointment block
List<TimeSlot> timeSlots = as.getTimeSlotsInAppointmentBlock(appointmentBlock);
for (TimeSlot timeSlot : timeSlots) {
List<Appointment> appointmentsInTimeSlot = as.getAppointmentsInTimeSlot(timeSlot);
for (Appointment appointment : appointmentsInTimeSlot) {
appointments.add(appointment);
}
}
numberOfAppointments = new Integer(appointments.size());
}
}
return numberOfAppointments;
}

private String buildLocationList(Location location) {
String ans = "";
if (location != null) {
Expand Down Expand Up @@ -129,4 +140,5 @@ private String getTimeSlotLength(Integer appointmentBlockId) {
}
return "";
}

}
Expand Up @@ -52,7 +52,7 @@ public class AppointmentBlockListController {

@RequestMapping(value = "/module/appointment/appointmentBlockList", method = RequestMethod.GET)
public void showForm(HttpServletRequest request, ModelMap model) throws ParseException {
model.addAttribute("appointmentsCount", "");
//Initializing the default properties of the appointment block list page.
if (Context.isAuthenticated()) {
if (request.getSession().getAttribute("chosenLocation") != null) {
Location location = (Location) request.getSession().getAttribute("chosenLocation");
Expand Down Expand Up @@ -89,7 +89,7 @@ public String onSubmit(HttpServletRequest request, ModelMap model,
@RequestParam(value = "toDate", required = false) Date toDate,
@RequestParam(value = "locationId", required = false) Location location,
@RequestParam(value = "appointmentBlockId", required = false) Integer appointmentBlockId,
@RequestParam(value = "toVoid", required = false) String toVoid) throws Exception {
@RequestParam(value = "action", required = false) String action) throws Exception {
//save details from the appointment block list page using http session
HttpSession httpSession = request.getSession();
httpSession.setAttribute("chosenLocation", location);
Expand All @@ -99,89 +99,90 @@ public String onSubmit(HttpServletRequest request, ModelMap model,
AppointmentBlock appointmentBlock = null;
if (Context.isAuthenticated()) {
AppointmentService appointmentService = Context.getService(AppointmentService.class);
if (appointmentBlockId != null) {
// if the user is adding a new AppointmentBlock
if (request.getParameter("add") != null) {
return "redirect:appointmentBlockForm.form";
} else if (appointmentBlockId != null) {
appointmentBlock = appointmentService.getAppointmentBlock(appointmentBlockId);
}
if (toVoid != null && toVoid.equals("true")) {
if (appointmentBlock != null) {
String voidReason = "Some Reason";//request.getParameter("voidReason");
if (!(StringUtils.hasText(voidReason))) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
"appointment.AppointmentBlock.error.voidReasonEmpty");
return null;
}
List<TimeSlot> currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
List<Appointment> appointments = new ArrayList<Appointment>();
for (TimeSlot timeSlot : currentTimeSlots) {
List<Appointment> appointmentsInSlot = appointmentService.getAppointmentsInTimeSlot(timeSlot);
for (Appointment appointment : appointmentsInSlot) {
appointments.add(appointment);
}
}
//set appointments statuses to "Cancelled"
for (Appointment appointment : appointments) {

appointmentService.changeAppointmentStatus(appointment, "Cancelled");
//TODO check with Tobin if I should delete this line
//appointmentService.voidAppointment(appointment, voidReason);
}
//voiding appointment block
appointmentService.voidAppointmentBlock(appointmentBlock, voidReason);
//voiding time slots
for (TimeSlot timeSlot : currentTimeSlots) {
appointmentService.voidTimeSlot(timeSlot, voidReason);

}
//if the user is voiding the selected appointmnet block
if (action != null && action.equals("void")) {
if (appointmentBlockId == null) {
//In case appointment block was not selected
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.voidedSuccessfully");
model.addAttribute("toVoid", "false");
"appointment.AppointmentBlock.error.selectAppointmentBlock");
return null;
}
String voidReason = "Some Reason";//request.getParameter("voidReason");

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Jan 21, 2013

Member

When do we plan to remove the hardcoded void reason?

This comment has been minimized.

Copy link
@AdamLauz

AdamLauz Jan 21, 2013

Author Member

The specefication doesn't say to ask the user for void reason.
I will ask Tobin if it will be ok to ask the user for such an input.

if (!(StringUtils.hasText(voidReason))) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,
"appointment.AppointmentBlock.error.voidReasonEmpty");
return null;
}
List<TimeSlot> currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
List<Appointment> appointments = new ArrayList<Appointment>();
for (TimeSlot timeSlot : currentTimeSlots) {
List<Appointment> appointmentsInSlot = appointmentService.getAppointmentsInTimeSlot(timeSlot);
for (Appointment appointment : appointmentsInSlot) {
appointments.add(appointment);
}
}
//set appointments statuses to "Cancelled"
for (Appointment appointment : appointments) {

appointmentService.changeAppointmentStatus(appointment, "Cancelled");

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Jan 21, 2013

Member

How about using AppointmentStatus.CANCELLED instead of "Cancelled"?

This comment has been minimized.

Copy link
@AdamLauz

AdamLauz Jan 21, 2013

Author Member

I updated this 👍

//TODO check with Tobin if I should delete this line
//appointmentService.voidAppointment(appointment, voidReason);
}
//voiding appointment block
appointmentService.voidAppointmentBlock(appointmentBlock, voidReason);
//voiding time slots
for (TimeSlot timeSlot : currentTimeSlots) {
appointmentService.voidTimeSlot(timeSlot, voidReason);

}
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.voidedSuccessfully");

return "redirect:appointmentBlockList.list";
}
//If the user is deleting the AppointmentBlock
else if (request.getParameter("delete") != null) {
if (appointmentBlock != null) {
//appointment block can't pureged if an appointment is scheduled in it.
List<TimeSlot> currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
List<Appointment> appointments = new ArrayList<Appointment>();
//If the user is purging the AppointmentBlock
else if (action != null && action.equals("purge")) {
if (appointmentBlockId == null) {
//In case appointment block was not selected
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
return null;
}
List<TimeSlot> currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
//In case there are appointments within the appointment block we don't mind to purge it
//purging the appointment block
try {
//purging the time slots
for (TimeSlot timeSlot : currentTimeSlots) {
List<Appointment> appointmentsInSlot = appointmentService.getAppointmentsInTimeSlot(timeSlot);
for (Appointment appointment : appointmentsInSlot) {
appointments.add(appointment);
}
appointmentService.purgeTimeSlot(timeSlot);
}
if (appointments.size() > 0) {
model.addAttribute("appointmentsCount", appointments.size());
model.addAttribute("appointmentBlockId", appointmentBlockId);
model.addAttribute("toVoid", "true");
return null;
} else {
//In case there are appointments within the appointment block we don't mind to purge it
//purging the appointment block
try {
//purging the time slots
for (TimeSlot timeSlot : currentTimeSlots) {
appointmentService.purgeTimeSlot(timeSlot);
}
appointmentService.purgeAppointmentBlock(appointmentBlock);
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.purgedSuccessfully");
}
catch (DataIntegrityViolationException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
}
catch (APIException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: "
+ e.getLocalizedMessage());
}
}
return null;
} else
appointmentService.purgeAppointmentBlock(appointmentBlock);
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
"appointment.AppointmentBlock.purgedSuccessfully");
}
catch (DataIntegrityViolationException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.object.inuse.cannot.purge");
}
catch (APIException e) {
httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.general: " + e.getLocalizedMessage());
}
return "redirect:appointmentBlockList.list";

}

// if the user is unvoiding the AppointmentBlock
else if (request.getParameter("unvoid") != null) {
if (appointmentBlockId == null) {
//In case appointment block was not selected
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
return null;
}
List<TimeSlot> currentTimeSlots = appointmentService.getTimeSlotsInAppointmentBlock(appointmentBlock);
List<Appointment> appointmentsThatShouldBeUnvoided = new ArrayList<Appointment>();
for (TimeSlot timeSlot : currentTimeSlots) {
Expand All @@ -203,24 +204,34 @@ else if (request.getParameter("unvoid") != null) {
}

httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "appointment.AppointmentBlock.unvoidedSuccessfully");
}

// if the user is adding a new AppointmentBlock
else if (request.getParameter("add") != null) {
return "redirect:appointmentBlockForm.form";

return "redirect:appointmentBlockList.list";
}

// if the user is editing an existing AppointmentBlock
else if (request.getParameter("edit") != null) {
if (appointmentBlockId == null) {
//In case appointment block was not selected
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
return null;
}
if (appointmentBlockId != null) {
return "redirect:appointmentBlockForm.form?appointmentBlockId=" + appointmentBlockId;
} else {
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
}
} else if (action != null && action.equals("notifyToSelectAppointmentBlock")) {
if (appointmentBlockId == null) {
//In case appointment block was not selected
httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,
"appointment.AppointmentBlock.error.selectAppointmentBlock");
return null;
}
}

}
} // Context authentication.
return null;
}
}
2 changes: 2 additions & 0 deletions omod/src/main/resources/config.xml
Expand Up @@ -52,6 +52,7 @@
<include method="getPatientDescription" />
<include method="getAvailableTimeSlots" />
<include method="getAppointmentBlocks"/>
<include method="getNumberOfAppointmentsInAppointmentBlock"/>

</create>
</allow>
Expand All @@ -61,6 +62,7 @@
import org.openmrs.module.appointment.web.DWRAppointmentService;
DWRAppointmentService.getPatientDescription(Integer patientId);
DWRAppointmentService.getAppointmentBlocks(String fromDate,String toDate,Integer locationId);
DWRAppointmentService.getNumberOfAppointmentsInAppointmentBlock(Integer appointmentBlockId);
]]>
<![CDATA[
import org.openmrs.module.appointment.web.DWRAppointmentService;
Expand Down

0 comments on commit 9a0103a

Please sign in to comment.