/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package web;
import dao.LDAPSupport;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.ldaptemplate.LdapTemplate;
import net.sf.ldaptemplate.support.DistinguishedName;
import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import utils.DateTimeUtils;
import utils.FormatChecker;
import web.generic.GenericSFController;
/**
*
* @author axe
*/
public class RegisterController extends GenericSFController implements LDAPSupport {
protected static final Logger log = Logger.getLogger(IndexController.class);
private LdapTemplate ldapTemplate;
public RegisterController() {
setSessionForm(true);
setCommandClass(RegisterDTO.class);
setFormView("register");
}
@Override
public LdapTemplate getLdapTemplate() {
return ldapTemplate;
}
@Override
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
RegisterDTO dto = new RegisterDTO();
/*dto.setBirthDay(7);
dto.setBirthMonth(4);
dto.setBirthYear(1978);
dto.setLogin("medved");
dto.setMail("mail@mail.ru");
dto.setPhone("02");
dto.setName("Димко");
dto.setSurname("Медведев");
dto.setPatronymic("Хренович");
dto.setPasswd("1");*/
return dto;
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors) throws Exception {
RegisterDTO dto = (RegisterDTO) command;
if (dto.getSurname() == null || dto.getSurname().trim().isEmpty()) {
errors.rejectValue("surname", "register.emptysurname");
return showForm(request, errors, getFormView());
}
if (dto.getName() == null || dto.getName().trim().isEmpty()) {
errors.rejectValue("name", "register.emptyname");
return showForm(request, errors, getFormView());
}
if (!DateTimeUtils.dateValid(
dto.getBirthYear(),
dto.getBirthMonth(),
dto.getBirthDay())) {
errors.rejectValue("birthDay", "register.datewrong");
return showForm(request, errors, getFormView());
}
if (FormatChecker.checkPhones(dto.getPhone()) != null) {
errors.rejectValue("phone", "register.wrongphone");
return showForm(request, errors, getFormView());
}
if (FormatChecker.checkEmail(dto.getMail()) != null) {
errors.rejectValue("mail", "register.wrongemail");
return showForm(request, errors, getFormView());
}
txAction(dto);
return new ModelAndView("redirect:/login.htm?error=3");
}
private void txAction(RegisterDTO dto) throws Exception {
BasicAttributes attributes = new BasicAttributes();
BasicAttribute ocattr = new BasicAttribute("objectclass");
ocattr.add("inetOrgPerson");
attributes.put(ocattr);
String fio = dto.getSurname() + " " + dto.getName() +" " + dto.getPatronymic();
attributes.put("givenName", fio);
attributes.put("sn", dto.getLogin());
attributes.put("mail", dto.getMail());
attributes.put("telephoneNumber", dto.getPhone());
attributes.put("userPassword", dto.getPasswd());
log.info("FIO:" + fio);
DistinguishedName webaccounts = new DistinguishedName();
webaccounts.add("ou","webaccounts");
webaccounts.add("cn", dto.getLogin());
getLdapTemplate().bind(webaccounts, null, attributes);
}
}