/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web.operator.reception;
import domain.Client;
import domain.Collaborator;
import domain.Lpu;
import domain.address.Address;
import domain.editors.Editor;
import domain.shedule.SheduleReception;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.GenericCommandController;
import web.operator.ReceptionController;
/**
*Контроллер поиска записей на прием оператором
* @author lacoste
*/
public class ReceptionSearchController extends GenericCommandController {
public ReceptionSearchController() {
setCommandClass(ReceptionSearchDTO.class);
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Lpu.class, new Editor(getDao(), Lpu.class));
binder.registerCustomEditor(SheduleReception.class, new Editor(getDao(), SheduleReception.class));
binder.registerCustomEditor(Client.class, new Editor(getDao(), Client.class));
}
@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object object, BindException exception) throws Exception {
ReceptionSearchDTO dto = (ReceptionSearchDTO) object;
HashMap model = new HashMap();
model.put("command", dto);
boolean canWork = false;
DetachedCriteria queryCriteria = DetachedCriteria.forClass(SheduleReception.class);
DetachedCriteria clientCriteria = queryCriteria.createCriteria("client");
if (dto.getConfirmed() != 0) {
boolean confirmed = false;
if (dto.getConfirmed() == 1) {
confirmed = false;
}
else if (dto.getConfirmed() == 2) {
confirmed = true;
}
queryCriteria.add(Property.forName("confirmed").eq(confirmed));
}
//ФИО
if (dto.getSurname() != null && !dto.getSurname().isEmpty()
|| dto.getName() != null && !dto.getName().isEmpty()
|| dto.getPatron() != null && !dto.getPatron().isEmpty()) {
canWork = true;
if(dto.getSurname() != null && !dto.getSurname().isEmpty()) {
clientCriteria.createCriteria("surname").add(
Property.forName("title").like(dto.getSurname() + "%").ignoreCase());
}
if(dto.getName() != null && !dto.getName().isEmpty()) {
clientCriteria.createCriteria("name").add(
Property.forName("title").like(dto.getName() + "%").ignoreCase());
}
if(dto.getPatron() != null && !dto.getPatron().isEmpty()) {
clientCriteria.createCriteria("patronymic").add(
Property.forName("title").like(dto.getPatron() + "%").ignoreCase());
}
}
//Телефон
if(dto.getTelephone() != null && !dto.getTelephone().isEmpty()) {
canWork = true;
clientCriteria.add(Property.forName("telephones").like("%" + dto.getTelephone() + "%"));
}
//Год рождения
if (dto.getBirthYear() != null && !dto.getBirthYear().isEmpty()) {
try {
canWork = true;
int year = Integer.parseInt(dto.getBirthYear());
GregorianCalendar gc = new GregorianCalendar(year, 0, 1);
Date thisYear = gc.getTime();
gc.add(GregorianCalendar.YEAR, 1);
Date nextYear = gc.getTime();
clientCriteria.add(Property.forName("born").ge(thisYear));
clientCriteria.add(Property.forName("born").lt(nextYear));
} catch (NumberFormatException ex) {
//DO NOTHING
}
}
//Год рождения
if ((dto.getDay() != 0) && (dto.getMonth() != 0) && (dto.getYear() != 0)) {
try {
canWork = true;
Calendar gc = Calendar.getInstance();
gc.set(Calendar.YEAR, dto.getYear());
gc.set(Calendar.MONTH, dto.getMonth());
gc.set(Calendar.DAY_OF_MONTH, dto.getDay());
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
Date begin = gc.getTime();
gc.add(Calendar.DAY_OF_YEAR, 1);
Date end = gc.getTime();
queryCriteria.add(Property.forName("begin").ge(begin));
queryCriteria.add(Property.forName("begin").lt(end));
} catch (NumberFormatException ex) {
//DO NOTHING
}
}
if (dto.getLpu() != null) {
canWork = true;
queryCriteria.createCriteria("collaborator").add(Property.forName("lpu").eq(dto.getLpu()));
}
queryCriteria.addOrder( Property.forName("begin").desc());
List<ReceptionGroup> lines = new ArrayList<ReceptionGroup>();
List<SheduleReception> receptions = getDao().getList(queryCriteria, null, 51);
model.put("count", receptions.size() > 50);
HashMap<Integer, ReceptionGroup> map = new HashMap<Integer, ReceptionGroup>();
for (SheduleReception reception : receptions) {
Client client = reception.getClient();
Address address = null;
if (client.getAddressID() != null) {
address = getDao().getById(Address.class, client.getAddressID());
}
ReceptionGroup group = map.get(client.getId());
if (group == null) {
group = new ReceptionGroup();
lines.add(group);
map.put(client.getId(), group);
group.client = client;
group.addressClient = address != null ? address.getAsStringShort() : "";
group.receptionLines = new ArrayList<ReceptionLine>();
}
ReceptionLine receptLine = new ReceptionLine(reception);
group.receptionLines.add(receptLine);
Collections.sort(group.receptionLines);
}
Collections.sort(lines);
model.put("lines", lines);
List<Lpu> lpus = getDao().getList(Lpu.class);
model.put("lpus", lpus);
return new ModelAndView("operator/reception/receptionsearch", model);
}
}