String button;
button = request.getParameter("button");
if(button != null){
if(button.equals("Add medication")){
try{
AddMedForm form = formBeanFactory.create(request);
request.setAttribute("addmedform", form);
if(!form.isPresent()){
return "homepage.jsp";
}
HttpSession session = request.getSession(false);
//check the errors.
errors.addAll(form.getValidationErrors());
if (errors.size()!= 0) {
request.setAttribute("errors",errors);
return "addMed.jsp";
}
int allNum = medDAO.size();
if(allNum != 0){
delList = (List<String>) session.getAttribute("deletelist");
if(delList != null){
if(!delList.isEmpty()){
delMed = delList.get(delList.size()-1);
System.out.println("the demed is " + delMed);
delList.remove(delList.size()-1);
synchronized(session) {session.setAttribute("deletelist", delList);}
}else{
System.out.println("The dellist is zero");
delMed = null;
synchronized(session){session.setAttribute("deletelist", null);}
}
}else{
delMed = null;
synchronized(session){session.setAttribute("deletelist", null);}
}
}
String newMed;
/*
* For Multiple Selection options.
* */
String[] dayCheckList = request.getParameterValues("dayChecks");
String dayCheckDL = null;
for(String daychecks : dayCheckList){
dayCheckDL = dayCheckDL + daychecks;
}
dayCheckDL = dayCheckDL.substring(4,dayCheckDL.length());
//if user want some medication schedule be deleted.
if(delMed != null){
newMed = delMed;
addMed = new Medication(Integer.parseInt(newMed));
createMed(addMed,form);
addMed.setUsername(user.getEmailAddress());
addMed.setDayChecks(dayCheckDL);
//addMed.setAllNum(allNum + 1);
//create a new user.
medDAO.create(addMed);
synchronized(session){
session.setAttribute("deletelist",delList);
request.setAttribute("addmedform", null);
request.setAttribute("message","Successfully added " + form.getName()+ ". ");
}
//if no scheduled medication be deleted.
}else{
int allSize = medDAO.size();
//System.out.println("all size is " + Integer.toString(allSize));
//initialization situation.
if(allSize == 0){
newMed = Integer.toString(allSize);
System.out.println("all size is " + newMed);
addMed = new Medication(Integer.parseInt(newMed));
createMed(addMed,form);
addMed.setUsername(user.getEmailAddress());
addMed.setDayChecks(dayCheckDL);
medDAO.create(addMed);
synchronized(session){
session.setAttribute("deletelist", null);
request.setAttribute("addmedform", null);
request.setAttribute("message","Successfully added " + form.getName() + ".");
}
}else{
allSize = medDAO.getLastId();
newMed = Integer.toString(allSize);
addMed = new Medication(Integer.parseInt(newMed) + 1);
createMed(addMed,form);
addMed.setUsername(user.getEmailAddress());
addMed.setDayChecks(dayCheckDL);
medDAO.create(addMed);
synchronized(session){
session.setAttribute("deletelist", null);
request.setAttribute("addmedform", null);
request.setAttribute("message","Successfully added " + form.getName()+ ".");
}
}
}
synchronized(session){
session.setAttribute("redirectto", null);