Package web.operator

Source Code of web.operator.ClientController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package web.operator;

import domain.Client;
import domain.District;
import domain.EntityWithId;
import domain.HasTitle;
import domain.Lpu;
import domain.Name;
import domain.Patronymic;
import domain.Sex;
import domain.Surname;
import domain.address.Address;
import domain.address.AddressObject;
import domain.address.AddressObjectType;
import domain.editors.Editor;
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 org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import utils.AddressManager;
import utils.Converter;
import utils.DateTimeUtils;
import utils.FormatChecker;
import web.generic.CH;
import web.generic.GenericSFController;

/**
* Контроллер создания нового или редактирования существующего пациента
* Необязательные параметры:
*     client - ID пациента
* @author petr
*/
public class ClientController extends GenericSFController {

    protected static final Logger log = Logger.getLogger(ClientController.class);

    public ClientController() {
        setSessionForm(true);
        setCommandClass(ClientDTO.class);
        setFormView("operator/client");
    }



    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        ClientDTO clientDTO = new ClientDTO();
        Client client = null;

        try {
            client = CH.getOptional(getDao(), Client.class, request.getParameter("client"));
        } catch (Exception ex) {
        }
       
        boolean editbypacient = Boolean.parseBoolean(request.getParameter("editbypacient"));
        clientDTO.setClient(client);
        clientDTO.setEditbypacient(editbypacient);
        if (client != null) {
            Address address = getDao().getById(Address.class, client.getAddressID());
            AddressObject ao = address.getAddressObject();
            if (ao.getType().getLevel() == 6){
                ao = ao.getParent();
            }

            Date born = client.getBorn();
            GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
            cal.setTime(born);
            clientDTO.setBornDay(cal.get(Calendar.DAY_OF_MONTH));
            clientDTO.setBornMonth(cal.get(Calendar.MONTH));
            clientDTO.setBornYear(Integer.toString(cal.get(Calendar.YEAR)));
            clientDTO.setSurname(client.getSurname() == null ? "" : client.getSurname().getTitle());
            clientDTO.setName(client.getName() == null ? "" : client.getName().getTitle());
            clientDTO.setPatronymic(client.getPatronymic() == null ? "" : client.getPatronymic().getTitle());
            clientDTO.setPhoneNumber(client.getTelephones());
            clientDTO.setSex(client.getSex());

            clientDTO.setStreet(ao);
            ao = ao.getParent();
            if (ao.getType().getLevel() == 4){
                clientDTO.setVillage(ao);
                ao = ao.getParent();
            }
            clientDTO.setCity(ao);
            ao = ao.getParent();
            clientDTO.setArea(ao);
            ao = ao.getParent();

            if (client.getDistrict() != null) {
                clientDTO.setDistrict(client.getDistrict());
            }
            clientDTO.setBuilding(address.getBuilding());
            clientDTO.setFlat(address.getFlat());
        }
        return clientDTO;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        ClientDTO clientDTO = (ClientDTO) command;
        //checkDTO
        if (clientDTO.getSurname() == null || clientDTO.getSurname().trim().isEmpty()) {
            errors.rejectValue("surname", "register.emptysurname");
            return showForm(request, errors, getFormView());
        }
        if (clientDTO.getName() == null || clientDTO.getName().trim().isEmpty()) {
            errors.rejectValue("name", "register.emptyname");
            return showForm(request, errors, getFormView());
        }
        int year = 0;
        try {
            year = Integer.parseInt(clientDTO.getBornYear());
        } catch (NumberFormatException ex) {

        }
        if (!DateTimeUtils.dateValid(
                year,
                clientDTO.getBornMonth(),
                clientDTO.getBornDay())) {
            errors.rejectValue("bornDay", "register.datewrong");
            return showForm(request, errors, getFormView());
        }
        if (FormatChecker.checkPhones(clientDTO.getPhoneNumber()) != null) {
            errors.rejectValue("phoneNumber", "register.wrongphone");
            return showForm(request, errors, getFormView());
        }
        //end checkDTO

        Client client = clientDTO.getClient();
        if (client == null) {
            client = new Client();
            try {
                Address address = createAddress(clientDTO);
                client.setAddressID(address.getId());
                client.setAddressCashe(address.getAsStringCashe());
            } catch (Exception ex) {
                errors.rejectValue("area", "register.wrongaddress");
                return showForm(request, errors, getFormView());
            }
        }else{
            Address address = getDao().getById(Address.class, client.getAddressID());
            address.setAddressObject(clientDTO.getStreet());
            address.setBuilding(clientDTO.getBuilding());
            address.setFlat(clientDTO.getFlat());
            try {
                AddressManager.correctAddress(getDao(), address);
            } catch (Exception ex) {
                errors.rejectValue("area", "register.wrongaddress");
                return showForm(request, errors, getFormView());
            }
            getDao().save(address);
            client.setAddressCashe(address.getAsStringCashe());
        }
        client.setBorn(getBornDate(clientDTO));
        //STUB
        //client.setAddress(null);
        //client.setDistrict(null);
        //client.setOms(urdto.getOms());
        //END STUB
        client.setName(getDirectoryItem(Name.class, clientDTO.getName()));
        client.setPatronymic(getDirectoryItem(Patronymic.class, clientDTO.getPatronymic()));
        client.setTelephones(clientDTO.getPhoneNumber());
        client.setSex(clientDTO.getSex());
        client.setSurname(getDirectoryItem(Surname.class, clientDTO.getSurname()));
        getDao().save(client);
        return new ModelAndView("redirect:selectclient.htm");
    }

    private Address createAddress(ClientDTO clientDTO){
        Address address = new Address();
              if (clientDTO.getStreet() != null){
            address.setAddressObject(clientDTO.getStreet());
        }else if (clientDTO.getVillage() != null){
            address.setAddressObject(clientDTO.getVillage());
        }else if (clientDTO.getCity() != null){
            address.setAddressObject(clientDTO.getCity());
        }else if (clientDTO.getArea() != null){
            address.setAddressObject(clientDTO.getArea());
        }else{
            log.debug("Адрес клиента не указан");
        }
        address.setBuilding(clientDTO.getBuilding());
        address.setFlat(clientDTO.getFlat());
        AddressManager.correctAddress(getDao(), address);
        getDao().saveAndRefresh(address);
        return address;
    }

    @Override
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
        Map referenceData = new HashMap();
        ClientDTO clientDTO = (ClientDTO) command;
        List<Sex> sexs = getDao().getList(Sex.class);
        List<Lpu> lpus = getDao().getList(Lpu.class);
        List<District> districts = new LinkedList<District>();
        if (clientDTO.getDistrict() != null) {
            districts = getDao().getDistrictList(clientDTO.getDistrict().getLpu());
        }
       
        List<AddressObject> areas = getDao().getChildList(getDao().getById(AddressObject.class, 22527));
        List<AddressObject> cities = getDao().getChildList(clientDTO.getArea(), 3);
        List<AddressObject> villages = getDao().getChildList(clientDTO.getCity(), 4);
        List<AddressObject> streets = null;
        if (clientDTO.getVillage() != null){
            streets = getDao().getChildList(clientDTO.getVillage(), 5);
        }else{
            streets = getDao().getChildList(clientDTO.getCity(), 5);
        }

        String dateBornString = Converter.dateToString(new Date(), "MM/dd/yyyy");
        if (clientDTO.getClient() != null){
            dateBornString = Converter.dateToString(clientDTO.getClient().getBorn(), "MM/dd/yyyy");
        }
        
        referenceData.put("dateBornString", dateBornString);
        referenceData.put("sexs", sexs);
        referenceData.put("lpus", lpus);
        referenceData.put("areas", areas);
        referenceData.put("cities", cities);
        referenceData.put("villages", villages);
        referenceData.put("streets", streets);
        referenceData.put("districts", districts);
        return referenceData;
    }

    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Sex.class, new Editor(getDao(), Sex.class));
        binder.registerCustomEditor(District.class, new Editor(getDao(), District.class));
        binder.registerCustomEditor(AddressObject.class, new Editor(getDao(), AddressObject.class));
        binder.registerCustomEditor(Lpu.class, new Editor(getDao(), Lpu.class));
    }

    private Date getBornDate(ClientDTO urdto) {
        int year = Integer.parseInt(urdto.getBornYear());
        GregorianCalendar gc = new GregorianCalendar(year, urdto.getBornMonth(), urdto.getBornDay());
        return gc.getTime();
    }

    private <T extends HasTitle> T getDirectoryItem(Class<T> clazz, String title) {
        String upper = title.trim().toUpperCase();
        List<T> list = getDao().getList(clazz);
        for (T item : list) {
            if (item.getTitle().trim().toUpperCase().equals(upper)) {
                return item;
            }
        }
        try {
            T item = clazz.newInstance();
            item.setTitle(title);
            getDao().saveAndRefresh((EntityWithId)item);
            return item;
        } catch (InstantiationException ex) {
            log.debug("Не удалось создать " + clazz.getSimpleName(), ex);
        } catch (IllegalAccessException ex) {
            log.debug("Не удалось создать " + clazz.getSimpleName(), ex);
        }
        return null;
    }
}
TOP

Related Classes of web.operator.ClientController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.