Exemplo n.º 1
0
 @Override
 public void validateChangedObject(FacesContext context, UIComponent component, Object arg2)
     throws ValidatorException {
   String value = (String) arg2;
   if (StringUtils.isEmpty(value)) {
     throw new ValidatorException(
         MessageFactory.getMessage(
             context,
             "javax.faces.component.UIInput.REQUIRED",
             MessageFactory.getLabel(context, component)));
   }
   try {
     if (!vdtService.equalsPersistenceValue(entityClass, fieldName, id, value)) {
       if (StringUtils.isNotEmpty(message)) {
         throw new ValidatorException(Messages.createError(message));
       } else {
         throw new ValidatorException(
             MessageFactory.getMessage(
                 context,
                 "com.esoft.core.validator.AlreadyExistValidator.PERSISTENCE_VALUE",
                 MessageFactory.getLabel(context, component)));
       }
     }
   } catch (SecurityException e) {
     e.printStackTrace();
     throw new ValidatorException(
         MessageFactory.getMessage(
             context,
             "javax.faces.converter.NO_SUCH_FIELD",
             MessageFactory.getLabel(context, component),
             "get" + StringUtils.capitalize(fieldName)));
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
     throw new ValidatorException(
         MessageFactory.getMessage(
             context,
             "javax.faces.converter.CLASS_NOT_FOUND",
             MessageFactory.getLabel(context, component),
             entityClass));
   } catch (NoSuchMethodException e) {
     e.printStackTrace();
     throw new ValidatorException(
         MessageFactory.getMessage(
             context,
             "javax.faces.converter.NO_SUCH_FIELD",
             MessageFactory.getLabel(context, component),
             "get" + StringUtils.capitalize(fieldName)));
   }
 }
Exemplo n.º 2
0
 @Override
 public void validate(FacesContext context, UIComponent component, Object value)
     throws ValidatorException {
   if (value != null && extensions != null && extensions.length > 0) {
     if (value instanceof InputFileInfo) {
       InputFileInfo info = (InputFileInfo) value;
       String choice = info.getConvertedChoice();
       if (!InputFileChoice.isUploadOrKeepTemp(choice)) {
         return;
       }
       String filename = info.getConvertedFilename();
       if (filename != null) {
         String lowerCaseFilename = filename.toLowerCase();
         boolean error = authorized;
         for (String extension : extensions) {
           String lowerCaseExtension = extension.trim().toLowerCase();
           if (lowerCaseFilename.endsWith(lowerCaseExtension)) {
             error = !authorized;
             break;
           }
         }
         // TODO: handle content types
         if (error) {
           String messageId =
               authorized
                   ? MIMETYPE_AUTHORIZED_EXTENSIONS_MESSAGE_ID
                   : MIMETYPE_UNAUTHORIZED_EXTENSIONS_MESSAGE_ID;
           throw new ValidatorException(
               MessageFactory.getMessage(context, messageId, StringUtils.join(extensions, ", ")));
         }
       }
     }
   }
 }
 /**
  * Add keyed error/message.
  *
  * @param level
  * @param key message key
  */
 private void addError(FacesContext context, String key) {
   FacesMessage fMessage = MessageFactory.getMessage(key);
   if (fMessage != null) {
     FacesContext facesContext = FacesContext.getCurrentInstance();
     fMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
     facesContext.addMessage(null, fMessage);
   }
 }