Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.profiler.validator; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.Validator; import com.seajas.search.profiler.model.command.UserCommand; import com.seajas.search.profiler.service.remoting.RemotingService; /** * User validator. * * @author Jasper van Veghel <jasper@seajas.com> */ @Component("userValidator") public class UserValidator implements Validator { /** * Constants. */ private static final Integer PASSWORD_LENGTH_MINIMUM = 6; /** * Remoting service. */ @Autowired private RemotingService remotingService; /** * Determine whether this validator supports the command object. * * @param klass * @return boolean */ @Override public boolean supports(final Class<?> klass) { return klass.equals(UserCommand.class); } /** * Validate the given command object. * * @param command * @param errors */ @Override public void validate(final Object command, final Errors errors) { UserCommand user = (UserCommand) command; if (user.getAction().equals("add")) { if (StringUtils.isEmpty(user.getFullname())) errors.rejectValue("fullname", "users.error.fullname.empty"); if (StringUtils.isEmpty(user.getUsername())) errors.rejectValue("username", "users.error.username.empty"); else if (remotingService.existsUsername(user.getUsername())) errors.rejectValue("username", "users.error.username.invalid"); if (StringUtils.isEmpty(user.getPassword())) errors.rejectValue("password", "users.error.password.empty"); else if (!user.getPassword().equals(user.getPasswordRepeat())) errors.rejectValue("password", "users.error.password.repeat"); else if (user.getPassword().length() < PASSWORD_LENGTH_MINIMUM) errors.rejectValue("password", "users.error.password.length"); } else if (user.getAction().equals("edit")) { if (StringUtils.isEmpty(user.getFullname())) errors.rejectValue("fullname", "users.error.fullname.empty"); if (StringUtils.isEmpty(user.getUsername())) errors.rejectValue("username", "users.error.username.empty"); else if (!user.getUsername().equals(user.getOriginalUsername()) && remotingService.existsUsername(user.getUsername())) errors.rejectValue("username", "users.error.username.invalid"); if (!StringUtils.isEmpty(user.getPassword()) && !user.getPassword().equals(user.getPasswordRepeat())) errors.rejectValue("password", "users.error.password.repeat"); else if (!StringUtils.isEmpty(user.getPassword()) && user.getPassword().length() < PASSWORD_LENGTH_MINIMUM) errors.rejectValue("password", "users.error.password.length"); } } }