Example #1
0
  /**
   * Whether this element is required.
   *
   * @return Whether this element is required.
   */
  public boolean isRequired() {
    boolean required = BeanValidationUtils.isNotNull(this);

    if (xmlElement != null && !required) {
      required = xmlElement.required();
    }

    return required;
  }
Example #2
0
  protected boolean isMandatory(Object bean, String propertyName) {
    // lets look at the setter method and see if its got a @Required
    // annotation
    if (bean instanceof AbstractNode) {
      AbstractNode node = (AbstractNode) bean;
      Class<?> camelClass = node.getCamelDefinitionClass();
      if (camelClass != null) {
        XmlAccessorType accessorType = camelClass.getAnnotation(XmlAccessorType.class);
        boolean useMethods = true;
        if (accessorType != null) {
          if (accessorType.value().equals(XmlAccessType.FIELD)) {
            useMethods = false;
          }
        }
        try {
          BeanInfo beanInfo = Introspector.getBeanInfo(camelClass);
          PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
          if (propertyDescriptors != null) {
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
              if (propertyName.equals(propertyDescriptor.getName())) {
                Method writeMethod = propertyDescriptor.getWriteMethod();
                if (writeMethod != null) {
                  Required annotation = writeMethod.getAnnotation(Required.class);
                  if (annotation != null) {
                    return true;
                  }
                  if (useMethods) {
                    XmlElement element = writeMethod.getAnnotation(XmlElement.class);
                    if (element != null && element.required()) {
                      return true;
                    }
                    XmlAttribute attribute = writeMethod.getAnnotation(XmlAttribute.class);
                    if (attribute != null && attribute.required()) {
                      return true;
                    }
                  }
                }
                break;
              }
            }
          }
          if (!useMethods) {
            Field[] fields = camelClass.getDeclaredFields();
            for (Field field : fields) {
              if (propertyName.equals(field.getName())) {
                Required annotation = field.getAnnotation(Required.class);
                if (annotation != null) {
                  return true;
                }
                XmlElement element = field.getAnnotation(XmlElement.class);
                if (element != null && element.required()) {
                  return true;
                }
                XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);
                if (attribute != null && attribute.required()) {
                  return true;
                }
              }
            }
          }
        } catch (IntrospectionException e) {
          // ignore
        }
      }
    }

    // expression is mandatory on resequence
    if (node instanceof Resequence && "expression".equals(propertyName)) {
      return true;
    }

    // lets make all URI properties mandatory by default to avoid complex
    // validation with ref v uri
    boolean answer =
        ("uri".equals(propertyName) || propertyName.endsWith("Uri"))
            || (bean instanceof Aggregate && "strategyRef".equals(propertyName))
            || (bean instanceof ConvertBody && "type".equals(propertyName))
            || (bean instanceof ExpressionDefinition && isMandatoryExpression())
            || (bean instanceof Log && "message".equals(propertyName))
            || (bean instanceof Process && "ref".equals(propertyName))
            || (bean instanceof RemoveHeader && "headerName".equals(propertyName))
            || (bean instanceof RemoveProperty && "propertyName".equals(propertyName))
            || (bean instanceof SetHeader && "headerName".equals(propertyName))
            || (bean instanceof SetOutHeader && "headerName".equals(propertyName))
            || (bean instanceof SetProperty && "propertyName".equals(propertyName));
    return answer;
  }