Package web.users

Source Code of web.users.AddController

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

package web.users;

import domain.Client;
import domain.District;
import domain.Lpu;
import domain.Message;
import domain.Sex;
import domain.UserRequest;
import domain.address.Address;
import domain.address.AddressObject;
import domain.editors.Editor;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
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.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import utils.AddressManager;
import utils.DateTimeUtils;
import utils.FormatChecker;
import web.generic.GenericSFController;

/**
* Контроллер, позволяющий добавить пациента на подтверждение
* @author petr
*/
public class AddController extends GenericSFController {

    public AddController() {
        setSessionForm(true);
        setCommandClass(UserRequestDTO.class);
        setFormView("users/add");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        UserRequestDTO userRequestDTO = new UserRequestDTO();
        userRequestDTO.setRequesterLogin(getUserInfo().getLogin());
        return userRequestDTO;
    }

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
        UserRequestDTO urdto = (UserRequestDTO)command;

        //check DTO
        int year = 0;
        try {
            year = Integer.parseInt(urdto.getBornYear());
        } catch (NumberFormatException ex) {

        }
        if (!DateTimeUtils.dateValid(
                year,
                urdto.getBornMonth(),
                urdto.getBornDay())) {
            errors.rejectValue("bornDay", "register.datewrong");
            return showForm(request, errors, getFormView());
        }
        if (urdto.getSurname() == null || urdto.getSurname().trim().isEmpty()) {
            errors.rejectValue("surname", "register.emptysurname");
            return showForm(request, errors, getFormView());
        }
        if (urdto.getName() == null || urdto.getName().trim().isEmpty()) {
            errors.rejectValue("name", "register.emptyname");
            return showForm(request, errors, getFormView());
        }
        if (urdto.getOms() == null || urdto.getOms().trim().isEmpty()) {
            errors.rejectValue("oms", "register.emptyoms");
            return showForm(request, errors, getFormView());
        }
        if (FormatChecker.checkPhones(urdto.getPhoneNumber()) != null) {
            errors.rejectValue("phoneNumber", "register.wrongphone");
            return showForm(request, errors, getFormView());
        }

        //end check DTO

        UserRequest ur = new UserRequest();
        ur.setBornDate(getBornDate(urdto));

        try {
            Integer address = createAddress(urdto);
            ur.setAddress(getDao().getById(Address.class, address));
        } catch (Exception ex) {
            errors.rejectValue("area", "register.wrongaddress");
            return showForm(request, errors, getFormView());
        }
        District defaultDistrict = AddressManager.getDefaultDistrict(getDao(), urdto.getStreet(), urdto.getBuilding());
        ur.setDistrict(defaultDistrict);
        ur.setSurname(urdto.getSurname());
        ur.setName(urdto.getName());
        ur.setPatronymic(urdto.getPatronymic());
        ur.setOms(urdto.getOms());
        ur.setPhoneNumber(urdto.getPhoneNumber());
        ur.setRequesterLogin(getUserInfo().getLogin());
        ur.setSex(urdto.getSex());
        getDao().save(ur);
        return new ModelAndView("redirect:/users/list.htm");
    }

    @Override
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
        Map referenceData = new HashMap();
        List<Lpu> lpus = getDao().getList(Lpu.class);
        List<Sex> sexs = getDao().getList(Sex.class);
        List<AddressObject> areas = getDao().getChildList(getDao().getById(AddressObject.class, 22527));
        referenceData.put("lpus", lpus);
        referenceData.put("sexs", sexs);
        referenceData.put("areas", areas);
        boolean haveRequested = getDao().getUserRequests(getUserInfo().getLogin()).size() > 0;//TODO optimize
        referenceData.put("haveRequested", haveRequested);

        List<Client> clients = getDao().getConfirmClients(getUserInfo().getLogin());
        ArrayList<Message> messages = new ArrayList<Message>();
        for (Client client : clients) {
            DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
            criteria.add(Property.forName("client").eq(client));
            List<Message> messageList = getDao().getList(criteria, 0, 0);
            messages.addAll(messageList);
        }
        int countMessages = messages.size();
        int notDeliveredMessages = 0;
        for (Message message : messages) {
            if (!message.isDelivered()) {
                notDeliveredMessages ++;
            }
        }
        referenceData.put("countMessages", countMessages);
        referenceData.put("notDeliveredMessages", notDeliveredMessages);
        return referenceData;
    }

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

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

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

   
    private AddressObject getAddressObject(int id){
        return getDao().getById(AddressObject.class, id);
    }

}
TOP

Related Classes of web.users.AddController

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.