Package web.operator.request

Source Code of web.operator.request.WriteController

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

package web.operator.request;

import domain.Client;
import domain.EntityWithId;
import domain.HasTitle;
import domain.Name;
import domain.Patronymic;
import domain.Surname;
import domain.UserClient;
import domain.UserClientPK;
import domain.UserRequest;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import web.generic.CH;
import web.generic.GenericAbstractController;

/**
*
* @author lacoste
*/
public class WriteController extends GenericAbstractController {

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

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String requestedStr = request.getParameter("clientrequested");
        if (requestedStr == null || requestedStr.isEmpty()) {
            return new ModelAndView("redirect:/operator/request/index.htm");
        }
        UserRequest requested = null;
        try {
            requested = CH.get(getDao(), UserRequest.class, requestedStr);
        } catch (Exception ex) {
        }
        if (requested == null) {
            return new ModelAndView("redirect:/operator/request/index.htm");
        }

        Client client = new Client();
        client.setSurname(getDirectoryItem(Surname.class, requested.getSurname()));
        client.setName(getDirectoryItem(Name.class, requested.getName()));
        client.setPatronymic(getDirectoryItem(Patronymic.class, requested.getPatronymic()));
        client.setBorn(requested.getBornDate());
        client.setSex(requested.getSex());
        client.setTelephones(requested.getPhoneNumber());
        client.setAddressID(requested.getAddress().getId());
        client.setAddressCashe(requested.getAddress().getAsStringCashe());
        client.setDistrict(requested.getDistrict());
        getDao().save((EntityWithId)client);

        UserClient userClient = new UserClient();
        userClient.setUserClientPK(new UserClientPK(getUserInfo().getLogin(), client.getId()));
        userClient.setClient(client);
        userClient.setUserLogin(requested.getRequesterLogin());
        getDao().save(userClient);

        getDao().delete(requested);

        return new ModelAndView("redirect:/operator/client.htm?client=" + client.getId());
    }

    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.request.WriteController

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.