/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.reception;
import web.rechelper.Ticket;
import web.rechelper.WeekDay;
import web.rechelper.ReceptionHelper;
import domain.Client;
import domain.Collaborator;
import domain.Lpu;
import domain.editors.Editor;
import domain.shedule.SheduleReception;
import domain.shedule.WorkType;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import utils.BreadCrumb;
import utils.BreadCrumbParam;
import utils.DateTimeUtils;
import utils.Day;
import utils.WeekIndex;
import utils.acegi.UserInfo;
import web.generic.CH;
import web.generic.GenericSFController;
import web.rechelper.IncomingParamsValidator;
/**
* Запись на прием
* Обязательные параметры:
* client - ID пациента на запись
* lpu - ID ЛПУ
* type - ID типа приема
* collaborator - ID врача
* @author axe
*/
public class ReceptionController extends GenericSFController {
protected static final Logger log = Logger.getLogger(ReceptionController.class);
public ReceptionController() {
setCommandClass(ReceptionWeekDTO.class);
setFormView("reception/reception");
setBindOnNewForm(true);
setValidateOnBinding(true);
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
Client client = CH.getOptional(getDao(), Client.class, request.getParameter("client"));
WorkType type = CH.getOptional(getDao(), WorkType.class, request.getParameter("type"));
Lpu lpu = CH.getOptional(getDao(), Lpu.class, request.getParameter("lpu"));
Collaborator collab = CH.getOptional(getDao(), Collaborator.class, request.getParameter("collaborator"));
if (client == null) {
return IncomingParamsValidator.redirectTo("/reception/index.htm", client, type, lpu, collab);
} else if (collab == null) {
return IncomingParamsValidator.redirectTo("/reception/collaborator.htm", client, type, lpu, collab);
} else if (lpu == null && type == null) {
return IncomingParamsValidator.redirectTo("/reception/work.htm", client, type, lpu, collab);
} else if (lpu == null) {
return IncomingParamsValidator.redirectTo("/reception/lpu.htm", client, type, lpu, collab);
} else if (type == null) {
return IncomingParamsValidator.redirectTo("/reception/work.htm", client, type, lpu, collab);
}
return super.handleRequest(request, response);
}
/**
*
* @param request
* @param binder
* @throws Exception
*/
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Client.class, new Editor(getDao(), Client.class));
binder.registerCustomEditor(WorkType.class, new Editor(getDao(), WorkType.class));
binder.registerCustomEditor(Lpu.class, new Editor(getDao(), Lpu.class));
binder.registerCustomEditor(Collaborator.class, new Editor(getDao(), Collaborator.class));
}
@Override
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
ReceptionWeekDTO dto = (ReceptionWeekDTO) command;
HashMap model = new HashMap();
WeekIndex weekIndex = new WeekIndex();
weekIndex = new WeekIndex(weekIndex.getIndex() + dto.getWeek());
ReceptionHelper rh = new ReceptionHelper(getDao(), dto,
weekIndex.getBegin().getTime(), weekIndex.getEnd().getTime());
model.put("weekdays", rh.getModel());
model.put("pageBreadcrumb", initBreadCrumb(dto.getClient(), dto.getType(), dto.getLpu(), dto.getCollaborator()));
return model;
}
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {
try{
return super.showForm(request, response, errors, controlModel);
}catch(Exception e){
log.error("Error", e);
return new ModelAndView("redirect:index.htm");
}
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
ReceptionWeekDTO dto = (ReceptionWeekDTO) command;
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(dto.getTime());
if(dto.getTime() == 0) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
Day day = new Day(cal);
ReceptionHelper rh = new ReceptionHelper(getDao(), dto, day.getDate(), day.getEndDate());
List<WeekDay> model = rh.getModel();
if(model.size() != 1) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
WeekDay weekDay = model.get(0);
Ticket ticket = null;
if(weekDay.getTickets() != null) {
for(Ticket t: weekDay.getTickets()) {
if(t.getTime() == dto.getTime()) {
ticket = t;
break;
}
}
}
if(ticket == null) {
errors.reject("reception.incorrect.time");
return showForm(request, errors, getFormView());
}
if(ticket.isBuzy()) {
errors.reject("reception.buzy");
return showForm(request, errors, getFormView());
}
//проверка для автоматического подтверждения
//В один день нельзя записываться к сотрудникам с одинаковыми типами работ 2 раза
Date thisDate = DateTimeUtils.getDateOnly(cal).getTime();
Date nextDate = DateTimeUtils.getDateOnly(cal).getTime();
boolean autoconfirm = true;
DetachedCriteria queryCriteria = DetachedCriteria.forClass(SheduleReception.class)
.add(Property.forName("register").ge(thisDate))
.add(Property.forName("register").lt(nextDate))
.add(Property.forName("client").eq(dto.getClient()))
.add(Property.forName("workType").eq(dto.getType()));
List<SheduleReception> conflictList = getDao().getList(queryCriteria, 0, 0);
if (conflictList.size() > 0) {
autoconfirm = false;
//т.к. это рабочее место оператора, то посылать сообщение пользователю ненадо
}
SheduleReception reception = new SheduleReception();
//STUB
reception.setAuthor(getDao().getById(Collaborator.class, UserInfo.CURRENT_COLLABORATOR_ID));
//reception.setAuthor(dto.getCollaborator());
//ENDSTUB
reception.setCollaborator(dto.getCollaborator());
reception.setBegin(cal.getTime());
reception.setClient(dto.getClient());
reception.setDuration(ticket.getDuration());
reception.setDescription(dto.getType().getDescription()== null ? "":dto.getType().getDescription());
reception.setConfirmed(autoconfirm);
reception.setRegister(new Date());
reception.setWorkType(dto.getType());
getDao().save(reception);
return new ModelAndView("redirect:/reception/list.htm");
}
/**
*
* @param client NOT NULL
* @param work
* @param lpu
* @param coll
* @return
*/
public static List<BreadCrumb> initBreadCrumb(Client client, WorkType type, Lpu lpu, Collaborator coll) {
List<BreadCrumb> pageBreadcrumb = new LinkedList<BreadCrumb>();
List<BreadCrumbParam> params = new LinkedList<BreadCrumbParam>();
if(client != null) {
pageBreadcrumb.add(new BreadCrumb("", client.toString(), "/reception/index.htm"));
params.add(new BreadCrumbParam("client", client.getId()));
}
if(type != null) {
pageBreadcrumb.add(new BreadCrumb("записывается на прием к специалисту", type.toString().toLowerCase(),
"/reception/work.htm", new LinkedList<BreadCrumbParam>(params)));
params.add(new BreadCrumbParam("type", type.getId()));
}
if (lpu != null) {
pageBreadcrumb.add(new BreadCrumb("в поликлинику", lpu.toString(),
"/reception/lpu.htm", new LinkedList<BreadCrumbParam>(params)));
params.add(new BreadCrumbParam("lpu", lpu.getId()));
}
if (coll != null) {
pageBreadcrumb.add(new BreadCrumb("к врачу", coll.getClient().toString(),
"/reception/collaborator.htm", new LinkedList<BreadCrumbParam>(params)));
}
return pageBreadcrumb;
}
}