Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.fieldgroup;
  5. import java.lang.reflect.Method;
  6. import com.vaadin.data.Item;
  7. import com.vaadin.data.util.BeanItem;
  8. import com.vaadin.data.validator.BeanValidator;
  9. import com.vaadin.ui.Field;
  10. public class BeanFieldGroup<T> extends FieldGroup {
  11. private Class<T> beanType;
  12. private static Boolean beanValidationImplementationAvailable = null;
  13. public BeanFieldGroup(Class<T> beanType) {
  14. this.beanType = beanType;
  15. }
  16. @Override
  17. protected Class<?> getPropertyType(Object propertyId) {
  18. if (getItemDataSource() != null) {
  19. return super.getPropertyType(propertyId);
  20. } else {
  21. // Data source not set so we need to figure out the type manually
  22. /*
  23. * toString should never really be needed as propertyId should be of
  24. * form "fieldName" or "fieldName.subField[.subField2]" but the
  25. * method declaration comes from parent.
  26. */
  27. java.lang.reflect.Field f;
  28. try {
  29. f = getField(beanType, propertyId.toString());
  30. return f.getType();
  31. } catch (SecurityException e) {
  32. throw new BindException("Cannot determine type of propertyId '"
  33. + propertyId + "'.", e);
  34. } catch (NoSuchFieldException e) {
  35. throw new BindException("Cannot determine type of propertyId '"
  36. + propertyId + "'. The propertyId was not found in "
  37. + beanType.getName(), e);
  38. }
  39. }
  40. }
  41. private static java.lang.reflect.Field getField(Class<?> cls,
  42. String propertyId) throws SecurityException, NoSuchFieldException {
  43. if (propertyId.contains(".")) {
  44. String[] parts = propertyId.split("\\.", 2);
  45. // Get the type of the field in the "cls" class
  46. java.lang.reflect.Field field1 = getField(cls, parts[0]);
  47. // Find the rest from the sub type
  48. return getField(field1.getType(), parts[1]);
  49. } else {
  50. try {
  51. // Try to find the field directly in the given class
  52. java.lang.reflect.Field field1 = cls
  53. .getDeclaredField(propertyId);
  54. return field1;
  55. } catch (NoSuchFieldError e) {
  56. // Try super classes until we reach Object
  57. Class<?> superClass = cls.getSuperclass();
  58. if (superClass != Object.class) {
  59. return getField(superClass, propertyId);
  60. } else {
  61. throw e;
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Helper method for setting the data source directly using a bean. This
  68. * method wraps the bean in a {@link BeanItem} and calls
  69. * {@link #setItemDataSource(Item)}.
  70. *
  71. * @param bean
  72. * The bean to use as data source.
  73. */
  74. public void setItemDataSource(T bean) {
  75. setItemDataSource(new BeanItem(bean));
  76. }
  77. @Override
  78. public void setItemDataSource(Item item) {
  79. if (!(item instanceof BeanItem)) {
  80. throw new RuntimeException(getClass().getSimpleName()
  81. + " only supports BeanItems as item data source");
  82. }
  83. super.setItemDataSource(item);
  84. }
  85. @Override
  86. public BeanItem<T> getItemDataSource() {
  87. return (BeanItem<T>) super.getItemDataSource();
  88. }
  89. @Override
  90. public void bind(Field field, Object propertyId) {
  91. if (getItemDataSource() != null) {
  92. // The data source is set so the property must be found in the item.
  93. // If it is not we try to add it.
  94. try {
  95. getItemProperty(propertyId);
  96. } catch (BindException e) {
  97. // Not found, try to add a nested property;
  98. // BeanItem property ids are always strings so this is safe
  99. getItemDataSource().addNestedProperty((String) propertyId);
  100. }
  101. }
  102. super.bind(field, propertyId);
  103. }
  104. @Override
  105. protected void configureField(Field<?> field) {
  106. super.configureField(field);
  107. // Add Bean validators if there are annotations
  108. if (isBeanValidationImplementationAvailable()) {
  109. BeanValidator validator = new BeanValidator(beanType,
  110. getPropertyId(field).toString());
  111. field.addValidator(validator);
  112. if (field.getLocale() != null) {
  113. validator.setLocale(field.getLocale());
  114. }
  115. }
  116. }
  117. /**
  118. * Checks whether a bean validation implementation (e.g. Hibernate Validator
  119. * or Apache Bean Validation) is available.
  120. *
  121. * TODO move this method to some more generic location
  122. *
  123. * @return true if a JSR-303 bean validation implementation is available
  124. */
  125. protected static boolean isBeanValidationImplementationAvailable() {
  126. if (beanValidationImplementationAvailable != null) {
  127. return beanValidationImplementationAvailable;
  128. }
  129. try {
  130. Class<?> validationClass = Class
  131. .forName("javax.validation.Validation");
  132. Method buildFactoryMethod = validationClass
  133. .getMethod("buildDefaultValidatorFactory");
  134. Object factory = buildFactoryMethod.invoke(null);
  135. beanValidationImplementationAvailable = (factory != null);
  136. } catch (Exception e) {
  137. // no bean validation implementation available
  138. beanValidationImplementationAvailable = false;
  139. }
  140. return beanValidationImplementationAvailable;
  141. }
  142. }