/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.operator;
import web.reception.LpuRenderDTO;
import domain.Client;
import domain.ClinicType;
import domain.Collaborator;
import domain.Lpu;
import domain.address.AddressObject;
import domain.editors.Editor;
import domain.shedule.WorkType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import web.generic.CH;
import web.generic.GenericCommandController;
/**
* Контроллер выбора ЛПУ при записи на прием оператором
* Необязательные параметры:
* lpu - ID ЛПУ
* @author vip
*/
public class IndexController extends GenericCommandController {
protected static final Logger log = Logger.getLogger(IndexController.class);
public IndexController() {
setCommandClass(IndexDTO.class);
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(ClinicType.class, new Editor(getDao(), ClinicType.class));
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 void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception {
IndexDTO dto = (IndexDTO) command;
if(dto.getLpu() != null) {
Lpu lpu = dto.getLpu();
dto.setLpuStr(lpu.getTitle().trim());
}
}
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) throws Exception {
IndexDTO dto = (IndexDTO) command;
HashMap model = new HashMap();
model.put("command", dto);
List<ClinicType> clinicTypes = getDao().getList(ClinicType.class);
model.put("clinicTypes", clinicTypes);
log.debug("indexDto.getSurname() = " + dto.getSurname());
DetachedCriteria queryCriteria = DetachedCriteria.forClass(Lpu.class).setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
if(dto.getLpuStr() != null && !dto.getLpuStr().isEmpty()) {
queryCriteria.add(Property.forName("title").like("%" + dto.getLpuStr() + "%").ignoreCase());
}
if (
dto.getSurname() != null && !dto.getSurname().isEmpty()
|| dto.getName() != null && !dto.getName().isEmpty()
|| dto.getPatron() != null && !dto.getPatron().isEmpty()) {
DetachedCriteria colls = queryCriteria
.createCriteria("collaboratorCollection")
.createCriteria("client");
if(dto.getSurname() != null && !dto.getSurname().isEmpty()) {
colls.createCriteria("surname")
.add(Property.forName("title").like(dto.getSurname() + "%").ignoreCase());
}
if(dto.getName() != null && !dto.getName().isEmpty()) {
colls.createCriteria("name")
.add(Property.forName("title").like(dto.getName()+ "%").ignoreCase());
}
if(dto.getPatron() != null && !dto.getPatron().isEmpty()) {
colls.createCriteria("patronymic")
.add(Property.forName("title").like(dto.getPatron()+ "%").ignoreCase());
}
}
if (dto.getClinicType() != null) {
queryCriteria.add(Property.forName("clinicType").eq(dto.getClinicType()));
}
List<Lpu> lpus = getDao().getList(queryCriteria, null, null);
List<LpuRenderDTO> lines = new ArrayList<LpuRenderDTO>();
for (Lpu lpu : lpus) {
if (dto.getCity() != null && !dto.getCity().isEmpty()) {
//Фильтруем по району/нас пункту
AddressObject item = lpu.getAddress().getAddressObject();
boolean founded = false;
while (item != null) {
if ((item.getType().getLevel() == 3 || item.getType().getLevel() == 4)
&& item.getTitle().toUpperCase().startsWith(dto.getCity().toUpperCase())) {
founded = true;
}
item = item.getParent();
}
if (!founded) {
continue;
}
}
if (dto.getStreet() != null && !dto.getStreet().isEmpty()) {
//Фильтруем по улице
AddressObject item = lpu.getAddress().getAddressObject();
boolean founded = false;
while (item != null) {
if (item.getType().getLevel() == 5
&& item.getTitle().toUpperCase().startsWith(dto.getStreet().toUpperCase())) {
founded = true;
}
item = item.getParent();
}
if (!founded) {
continue;
}
}
lines.add( new LpuRenderDTO(lpu, lpu.getAddress().getAsStringShort()));
}
model.put("lines", lines);
model.put("pageBreadcrumb", ReceptionController.initBreadCrumb(dto.getClient(),
dto.getType(),
dto.getLpu(),
dto.getCollaborator(),
dto.getTime()));
return new ModelAndView("operator/index", model);
}
}