private void postChangeIfChanged(
     GwtConversation conversation, AEntity entity, Map properties, User user, String property) {
   if (properties.containsKey(property)) {
     Object oldValue = Reflect.getProperty(entity, property);
     Object newValue = properties.get(property);
     Change change = changeDao.postChange(entity, user, property, oldValue, newValue);
     conversation.sendToClient(change);
   }
 }
Exemple #2
0
  /**
   * Autowire the object <code>bean</code> with beans provided by <code>beanProvider</code>. Use the
   * given <code>objectStringMapper</code> to convert from/to strings if required. For each bean
   * provided by <code>beanProvider</code> a setter is called on the given object <code>bean</code>.
   *
   * @param objectStringMapper optional
   * @return the given <code>bean</code>
   */
  public static <T> T autowire(
      T bean, final BeanProvider beanProvider, final ObjectStringMapper objectStringMapper) {
    // Logger.DEBUG("***** autowiring", "<" + Utl.toStringWithType(bean) + ">", "with", "<"
    // + Utl.toStringWithType(beanProvider) + ">");
    final Set<String> availableBeanNames = beanProvider.beanNames();
    Class beanClass = bean.getClass();
    BeanInfo beanInfo;
    try {
      beanInfo = Introspector.getBeanInfo(beanClass);
    } catch (IntrospectionException ex) {
      throw new RuntimeException(ex);
    }
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    if (propertyDescriptors != null) {
      for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
        if (propertyDescriptor != null) {
          String name = propertyDescriptor.getName();
          // if ("parentContext".equals(name)) continue;
          Method writeMethod = propertyDescriptor.getWriteMethod();
          if (writeMethod != null) {
            if (writeMethod.getAnnotation(AutowireHostile.class) != null) continue;
            if (availableBeanNames.contains(name)) {
              invokeSetter(bean, writeMethod, beanProvider.getBean(name), objectStringMapper);
            } else if ("beanProvider".equals(name)) {
              invokeSetter(bean, writeMethod, beanProvider, objectStringMapper);
            }
          }
        }
      }
    }

    Reflect.processAnnotations(
        bean,
        new Reflect.FieldAnnotationHandler() {

          @Override
          public void handle(Annotation annotation, Field field, Object object) {
            if (!(annotation instanceof In)) return;
            String name = field.getName();
            if (!availableBeanNames.contains(name)) return;
            field.setAccessible(true);
            Object value = beanProvider.getBean(name);
            Class paramType = field.getType();
            try {
              if (objectStringMapper != null
                  && value instanceof String
                  && objectStringMapper.isTypeSupported(paramType)) {
                value = objectStringMapper.stringToObject((String) value, paramType);
              } else {
                value = convertType(paramType, value);
              }
              field.set(object, value);
            } catch (Exception ex) {
              String valueStr =
                  value == null
                      ? "<" + value + ">"
                      : value.getClass().getSimpleName() + ": <" + value + ">";
              throw new RuntimeException(
                  "Setting field "
                      + object.getClass().getSimpleName()
                      + "."
                      + name
                      + " to "
                      + valueStr
                      + " failed.",
                  ex);
            }
          }
        });

    return bean;
  }