Search in sources :

Example 81 with FacesMessage

use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.

the class UpdatePersonAction method validateConfirmPassword.

public void validateConfirmPassword(FacesContext context, UIComponent comp, Object value) {
    Pattern pattern = null;
    String attributeValue = (String) value;
    if (StringHelper.isEmpty(attributeValue)) {
        FacesMessage message = new FacesMessage("Value is required");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
    AttributeValidation validation = attributeService.getAttributeByName("userPassword").getAttributeValidation();
    boolean canValidate = validation != null && validation.getRegexp() != null && !validation.getRegexp().isEmpty();
    if (comp.getClientId().endsWith("custpasswordId")) {
        this.password = (String) value;
    } else if (comp.getClientId().endsWith("custconfirmpasswordId")) {
        this.confirmPassword = (String) value;
    }
    if (canValidate) {
        pattern = Pattern.compile(validation.getRegexp());
    }
    if (!StringHelper.equalsIgnoreCase(password, confirmPassword) && this.confirmPassword != null) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage("Both passwords should be the same!");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
    if (canValidate && (!pattern.matcher(this.password).matches() || !pattern.matcher(this.confirmPassword).matches())) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"), facesMessages.evalResourceAsString("#{msgs['password.validation.invalid']}"));
        context.addMessage(comp.getClientId(context), message);
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) Pattern(java.util.regex.Pattern) ValidatorException(javax.faces.validator.ValidatorException) FacesMessage(javax.faces.application.FacesMessage) UIInput(javax.faces.component.UIInput)

Example 82 with FacesMessage

use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.

the class OxTrustAttributeValidator method validate.

@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
    GluuAttribute attribute = (GluuAttribute) comp.getAttributes().get("attribute");
    if (attribute == null) {
        ((UIInput) comp).setValid(true);
        return;
    }
    AttributeValidation attributeValidation = attribute.getAttributeValidation();
    Integer minvalue = attributeValidation != null ? attributeValidation.getMinLength() : null;
    Integer maxValue = attributeValidation != null ? attributeValidation.getMaxLength() : null;
    String regexpValue = attributeValidation != null ? attributeValidation.getRegexp() : null;
    String attributeValue = (String) value;
    // Minimum length validation
    if (minvalue != null) {
        int min = attributeValidation.getMinLength();
        if ((attributeValue != null) && (attributeValue.length() < min)) {
            ((UIInput) comp).setValid(false);
            FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should be at least " + min + " symbols. ");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(comp.getClientId(context), message);
        }
    }
    // default maxlength
    int max = 400;
    if (maxValue != null) {
        max = attributeValidation.getMaxLength();
    }
    // Maximum Length validation
    if ((attributeValue != null) && (attributeValue.length() > max)) {
        ((UIInput) comp).setValid(false);
        FacesMessage message = new FacesMessage(attribute.getDisplayName() + " should not exceed " + max + " symbols. ");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(comp.getClientId(context), message);
    }
    if ((attribute.getName().equalsIgnoreCase("mail") && ((regexpValue == null) || (StringHelper.isEmpty(regexpValue))))) {
        regexpValue = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" + "[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
    }
    if ((regexpValue != null) && StringHelper.isNotEmpty(regexpValue)) {
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regexpValue);
        if ((attributeValue != null) && !(attributeValue.trim().equals(""))) {
            java.util.regex.Matcher matcher = pattern.matcher(attributeValue);
            boolean flag = matcher.matches();
            if (!flag) {
                ((UIInput) comp).setValid(false);
                FacesMessage message = new FacesMessage(attribute.getDisplayName() + " Format is invalid. ");
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                context.addMessage(comp.getClientId(context), message);
            }
        }
    }
}
Also used : AttributeValidation(org.gluu.model.attribute.AttributeValidation) UIInput(javax.faces.component.UIInput) FacesMessage(javax.faces.application.FacesMessage) GluuAttribute(org.gluu.model.GluuAttribute)

Example 83 with FacesMessage

use of javax.faces.application.FacesMessage in project oxTrust by GluuFederation.

the class GluuAttributeValidator method validate.

@Override
public void validate(FacesContext context, UIComponent comp, Object value) {
    String attributeValue;
    if (value instanceof AttributeDataType) {
        attributeValue = ((AttributeDataType) value).getValue();
    } else {
        attributeValue = (String) value;
    }
    if (StringHelper.isEmpty(attributeValue)) {
        FacesMessage message = new FacesMessage("Value is required");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
}
Also used : AttributeDataType(org.gluu.model.attribute.AttributeDataType) ValidatorException(javax.faces.validator.ValidatorException) FacesMessage(javax.faces.application.FacesMessage)

Example 84 with FacesMessage

use of javax.faces.application.FacesMessage in project quickstart by wildfly.

the class BatchController method generate.

public void generate() throws IOException {
    File tempFile = new File(System.getProperty("java.io.tmpdir"), fileName);
    try (BufferedWriter bos = new BufferedWriter(new FileWriter(tempFile, false))) {
        log.info("Starting to generate " + numRecords + " records in file " + tempFile);
        String previousName = null;
        final Random random = new Random();
        for (int x = 0; x < numRecords; x++) {
            // generate random name
            String name = random.ints('a', 'z' + 1).limit(10).collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString();
            // generate random phone number
            String phone = random.ints('0', '9' + 1).limit(9).collect(StringBuilder::new, (sb, i) -> sb.append((char) i), StringBuilder::append).toString();
            // Generate a duplicate name;
            if (generateWithError && x == (numRecords / 2)) {
                name = previousName;
            }
            String record = (x + 1) + "|" + name + "|" + phone;
            bos.write(record + "\n");
            previousName = name;
        }
        log.info("File generated at " + tempFile);
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File generated with " + numRecords + " records to be imported. File name: " + getFileName(), null));
        if (generateWithError) {
            facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Attention: This file contains duplicate records for test purpose.", null));
        }
    }
}
Also used : Properties(java.util.Properties) BatchRuntime(javax.batch.runtime.BatchRuntime) JobExecution(javax.batch.runtime.JobExecution) Model(javax.enterprise.inject.Model) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Random(java.util.Random) Min(javax.validation.constraints.Min) Logger(java.util.logging.Logger) JobOperator(javax.batch.operations.JobOperator) File(java.io.File) TreeSet(java.util.TreeSet) NoSuchJobException(javax.batch.operations.NoSuchJobException) Inject(javax.inject.Inject) List(java.util.List) JobInstance(javax.batch.runtime.JobInstance) FacesContext(javax.faces.context.FacesContext) Map(java.util.Map) FacesMessage(javax.faces.application.FacesMessage) Max(javax.validation.constraints.Max) Comparator(java.util.Comparator) Random(java.util.Random) FileWriter(java.io.FileWriter) File(java.io.File) FacesMessage(javax.faces.application.FacesMessage) BufferedWriter(java.io.BufferedWriter)

Example 85 with FacesMessage

use of javax.faces.application.FacesMessage in project quickstart by wildfly.

the class AuthController method createUser.

private User createUser(String username) {
    try {
        User user = new User(username);
        userDao.createUser(user);
        facesContext.addMessage(null, new FacesMessage("User successfully created"));
        return user;
    } catch (Exception e) {
        facesContext.addMessage(null, new FacesMessage("Failed to create user '" + username + "'", e.getMessage()));
        return null;
    }
}
Also used : FacesMessage(javax.faces.application.FacesMessage)

Aggregations

FacesMessage (javax.faces.application.FacesMessage)370 ConceptHelper (mom.trd.opentheso.bdd.helper.ConceptHelper)43 SQLException (java.sql.SQLException)40 Connection (java.sql.Connection)34 FacesContext (javax.faces.context.FacesContext)25 ArrayList (java.util.ArrayList)24 UIInput (javax.faces.component.UIInput)24 ValidatorException (javax.faces.validator.ValidatorException)24 GroupHelper (mom.trd.opentheso.bdd.helper.GroupHelper)22 NodeAutoCompletion (mom.trd.opentheso.bdd.helper.nodes.NodeAutoCompletion)22 CandidateHelper (mom.trd.opentheso.bdd.helper.CandidateHelper)19 UserHelper2 (mom.trd.opentheso.bdd.helper.UserHelper2)19 IOException (java.io.IOException)16 NoteHelper (mom.trd.opentheso.bdd.helper.NoteHelper)15 Test (org.junit.Test)13 TermHelper (mom.trd.opentheso.bdd.helper.TermHelper)12 Term (mom.trd.opentheso.bdd.datas.Term)11 UploadedFile (org.primefaces.model.UploadedFile)11 HikariDataSource (com.zaxxer.hikari.HikariDataSource)10 Date (java.util.Date)10