String button;
button = request.getParameter("button");
if(button != null){
if(button.equals("Add side effect")){
try{
AddSideForm form = formBeanFactory.create(request);
request.setAttribute("addsideform", 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 "logSide.jsp";
}
int allNum = sideDAO.size();
if(allNum != 0){
delListside = (List<String>) session.getAttribute("deletelistside");
if(delListside != null){
if(!delListside.isEmpty()){
delSide = delListside.get(delListside.size()-1);
delListside.remove(delListside.size()-1);
synchronized(session){session.setAttribute("deletelistside", delListside);}
}else{
System.out.println("The dellist is zero");
delSide = null;
synchronized(session){session.setAttribute("deletelistside", null);}
}
}else{
delSide = null;
synchronized(session){session.setAttribute("deletelistside", null);}
}
}
String newSide;
//if user want some side effects be deleted.
if(delSide != null){
newSide = delSide;
addSide = new SideEffect(Integer.parseInt(newSide));
addSide.setName(form.getName());
addSide.setOwner(user.getEmailAddress());
//addSide.setAllNum(allNum + 1);
//create a new user.
sideDAO.create(addSide);
synchronized(session){session.setAttribute("deletelistside",delListside);}
//if no scheduled medication be deleted.
}else{
int allSize = sideDAO.size();
//initialization situation.
if(allSize == 0){
newSide = Integer.toString(allSize);
addSide = new SideEffect(Integer.parseInt(newSide));
addSide.setName(form.getName());
addSide.setOwner(user.getEmailAddress());
sideDAO.create(addSide);
synchronized(session){session.setAttribute("deletelistside",delListside);}
}else{
allSize = sideDAO.getLastId();
newSide = Integer.toString(allSize);
addSide = new SideEffect(Integer.parseInt(newSide) + 1);
addSide.setName(form.getName());
addSide.setOwner(user.getEmailAddress());
sideDAO.create(addSide);
synchronized(session){session.setAttribute("deletelistside",delListside);}
}
}