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.codex.validator; import com.seajas.search.codex.model.command.IdentityCommand; import org.apache.commons.lang.StringUtils; import org.apache.commons.validator.EmailValidator; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.Validator; /** * Profile validator. * * @author Jasper van Veghel <jasper@seajas.com> */ @Component("identityValidator") public class IdentityValidator implements Validator { /** * Determine whether this validator supports the command object. * * @param klass * @return boolean */ @Override @SuppressWarnings("rawtypes") public boolean supports(final Class klass) { return klass.equals(IdentityCommand.class); } /** * Validate the given command object. * * @param command * @param errors */ @Override public void validate(final Object command, final Errors errors) { IdentityCommand identity = (IdentityCommand) command; if ("add".equals(identity.getAction()) || "edit".equals(identity.getAction())) { if (StringUtils.isEmpty(identity.getName())) errors.rejectValue("name", "identities.error.name.empty"); if (StringUtils.isEmpty(identity.getNotifierEmail())) errors.rejectValue("notifierEmail", "identities.error.notifier.email.empty"); else if (!EmailValidator.getInstance().isValid(identity.getNotifierEmail())) errors.rejectValue("notifierEmail", "identities.error.notifier.email.invalid"); // Everything else may be either empty, or null } if ("add-account".equals(identity.getAccountAction()) || "edit-account".equals(identity.getAccountAction())) { if (StringUtils.isEmpty(identity.getUsername())) errors.rejectValue("username", "identities.account.error.username.empty"); if (StringUtils.isEmpty(identity.getPassword())) errors.rejectValue("password", "identities.account.error.password.empty"); if (StringUtils.isEmpty(identity.getType())) errors.rejectValue("type", "identities.account.error.type.empty"); } } }