/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.operator;
import services.ReceptionUpdater;
import web.rechelper.ReceptionDTO;
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.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Required;
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.Converter;
import utils.DateTimeUtils;
import utils.Day;
import web.generic.CH;
import web.generic.GenericSFController;
import web.rechelper.IncomingParamsValidator;
import web.rechelper.ReceptionHelper;
import web.rechelper.Ticket;
import web.rechelper.WeekDay;
/**
* Контроллер записи на прием оператором
* Обязательные параметры:
* lpu - ID ЛПУ
* collaborator - ID врача к которому идет запись
* type - ID типа приема записи
* Необязательные параметры:
* client - ID пациента, записываемого на прием. если отсутствует, пациент выбирается дальше
* @author axe
*/
public class ReceptionController extends GenericSFController {
private ReceptionUpdater updater;
public ReceptionController() {
setCommandClass(ReceptionDTO.class);
setFormView("operator/reception");
setBindOnNewForm(true);
}
@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
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
Client client = CH.getOptional(getDao(), Client.class, request.getParameter("client"));
Collaborator collab = CH.getOptional(getDao(), Collaborator.class, request.getParameter("collaborator"));
WorkType type = CH.getOptional(getDao(), WorkType.class, request.getParameter("type"));
Lpu lpu = CH.getOptional(getDao(), Lpu.class, request.getParameter("lpu"));
if (collab == null) {
return IncomingParamsValidator.redirectTo("/operator/collaborator.htm", client, type, lpu, collab);
}
if ((lpu == null) && (type == null)) {
return IncomingParamsValidator.redirectTo("/operator/index.htm", client, type, lpu, collab);
}
else if (lpu == null) {
return IncomingParamsValidator.redirectTo("/operator/index.htm", client, type, lpu, collab);
}
else if (type == null) {
return IncomingParamsValidator.redirectTo("/operator/collaborator.htm", client, type, lpu, collab);
}
return super.handleRequest(request, response);
}
@Override
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
ReceptionDTO dto = (ReceptionDTO) command;
int uid = updater.registerPage(dto.getCollaborator(), dto.getType());
Day beginDay = new Day();
Calendar calEnd = beginDay.getCalendar();
calEnd.add(Calendar.WEEK_OF_YEAR, 2);
calEnd.add(Calendar.MILLISECOND, -1);
ReceptionHelper rh = new ReceptionHelper(getDao(), dto, beginDay.getDate(), calEnd.getTime());
List<WeekDay> mm = rh.getModel();
int kk = mm.size();
HashMap model = new HashMap();
model.put("uid", uid);
model.put("days", rh.getModel());
model.put("pageBreadcrumb",
initBreadCrumb(dto.getClient(), dto.getType(), dto.getLpu(), dto.getCollaborator(), 0));
return model;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
ReceptionDTO dto = (ReceptionDTO) 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);
Calendar dayEnd = day.getCalendar();
dayEnd.add(Calendar.DAY_OF_MONTH, 1);
dayEnd.add(Calendar.MILLISECOND, -1);
ReceptionHelper rh = new ReceptionHelper(getDao(), dto, day.getDate(), dayEnd.getTime());
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()) {
long ticketTime = t.getTime();
if(ticketTime == 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());
}
if(dto.getClient() == null) {
return new ModelAndView("redirect:/operator/chooseclient.htm"
+ "?type=" + dto.getType().getId()
+ "&lpu=" + dto.getLpu().getId()
+ "&collaborator=" + dto.getCollaborator().getId()
+ "&time=" + dto.getTime()
);
}
//проверка для автоматического подтверждения
//В один день нельзя записываться к сотрудникам с одинаковыми типами работ 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();
reception.setAuthor(getDao().getById(Collaborator.class, getUserInfo().getCurrentCollaboratorId()));
reception.setCollaborator(dto.getCollaborator());
reception.setBegin(cal.getTime());
reception.setClient(dto.getClient());
reception.setDuration(ticket.getDuration());
reception.setDescription(dto.getType().getDescription());
reception.setConfirmed(autoconfirm);
reception.setRegister(new Date());
reception.setWorkType(dto.getType());
getDao().save(reception);
ticket.setBuzy(true);
updater.addTicketP(dto.getCollaborator(), dto.getType(), ticket);
String date = Converter.dateToString(cal.getTime());
String time = Converter.dateToString(cal.getTime(), "HH:mm");
return new ModelAndView("redirect:/operator/complete.htm"
+ "?client=" + dto.getClient().getId()
+ "&lpu=" + dto.getLpu().getId()
+ "&collaborator=" + dto.getCollaborator().getId()
+ "&date=" + date
+ "&time=" + time
+ "&type=" + dto.getType().getId());
}
/**
*
* @param client NOT NULL
* @param work
* @param lpu
* @param coll
* @return
*/
public static List<BreadCrumb> initBreadCrumb(Client client, WorkType type,
Lpu lpu, Collaborator coll, long time) {
List<BreadCrumbParam> params = new LinkedList<BreadCrumbParam>();
if (lpu != null) {
params.add(new BreadCrumbParam("lpu", lpu.getId()));
}
if (coll != null) {
params.add(new BreadCrumbParam("collaborator", coll.getId()));
}
if(time != 0) {
params.add(new BreadCrumbParam("time", new Long(time).toString()));
}
if(type != null) {
params.add(new BreadCrumbParam("type", type.getId()));
}
if(client != null) {
params.add(new BreadCrumbParam("client", client.getId()));
}
List<BreadCrumb> pageBreadcrumb = new LinkedList<BreadCrumb>();
if (lpu != null) {
pageBreadcrumb.add(new BreadCrumb(">", lpu.toString(),
"/operator/index.htm", params));
}
if (coll != null) {
pageBreadcrumb.add(new BreadCrumb(">", coll.getClient().toString(),
"/operator/collaborator.htm", params));
}
if(time != 0) {
Day day = new Day(time);
pageBreadcrumb.add(new BreadCrumb(">", day.toString(),
"/operator/reception.htm", params));
}
if(client != null) {
pageBreadcrumb.add(new BreadCrumb(">", client.toString(),
"/operator/chooseclient.htm", params));
}
return pageBreadcrumb;
}
public ReceptionUpdater getUpdater() {
return updater;
}
@Required
public void setUpdater(ReceptionUpdater updater) {
this.updater = updater;
}
}