Java tutorial
/* * Copyright 2012 AMG.lab, a Bull Group Company * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.xlcloud.console.virtualClusterDefinitions.validators; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.xlcloud.console.controllers.i18n.MessageController; import org.xlcloud.console.scope.ViewScoped; import org.xlcloud.console.virtualClusterDefinitions.utils.ParameterByNamePredicate; import org.xlcloud.service.heat.template.Parameter; /** * Validates template parameter name uniqueness. * @author Konrad Krl, AMG.net */ @Named @ViewScoped public class TemplateParameterNameValidator implements Validator, Serializable { private static final long serialVersionUID = 2989806136593889737L; @Inject private MessageController messages; private List<Parameter> existingElements = new ArrayList<Parameter>(); /** {@inheritDoc} */ @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { String validatedName = (String) value; Predicate predicate = new ParameterByNamePredicate(validatedName); if (CollectionUtils.exists(existingElements, predicate)) { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, messages.getValue("error"), messages.getValue("vcDefinitions.editor.parameters.nameUnique"))); } } /** * Gets the value of {@link #existingElements}. * @return value of {@link #existingElements} */ public List<Parameter> getExistingElements() { return existingElements; } /** * Sets the value of {@link #existingElements}. * @param existingElements - value */ public void setExistingElements(List<Parameter> existingElements) { this.existingElements = existingElements; } }