protected void setProperty()

in odata2-jpa-processor/jpa-core/src/main/java/org/apache/olingo/odata2/jpa/processor/core/access/data/JPAEntity.java [480:564]


  protected void setProperty(final Method method, final Object entity, final Object entityPropertyValue,
      final EdmSimpleType type, String propertyName, boolean isNullable) throws
      IllegalAccessException, IllegalArgumentException, InvocationTargetException, ODataJPARuntimeException, 
      EdmException {
    if (entityPropertyValue != null || isNullable) {
      if (propertyName != null) {
        method.invoke(entity, propertyName, entityPropertyValue);
        return;
      }
      Class<?> parameterType = method.getParameterTypes()[0];
      if (type != null && type.getDefaultType().equals(String.class)) {
        if (parameterType.equals(String.class)) {
          method.invoke(entity, entityPropertyValue);
        } else if (parameterType.equals(char[].class)) {
          char[] characters = entityPropertyValue != null ? ((String) entityPropertyValue).toCharArray() : null;
          method.invoke(entity, characters);
        } else if (parameterType.equals(char.class)) {
          char c = entityPropertyValue != null ? ((String) entityPropertyValue).charAt(0) : '\u0000';
          method.invoke(entity, c);
        } else if (parameterType.equals(Character[].class)) {
          Character[] characters = entityPropertyValue != null ? 
              JPAEntityParser.toCharacterArray((String) entityPropertyValue) : null;
          method.invoke(entity, (Object) characters);
        } else if (parameterType.equals(Character.class)) {
          Character c = entityPropertyValue != null ? 
              Character.valueOf(((String) entityPropertyValue).charAt(0)) : null;
          method.invoke(entity, c);
        } else if (parameterType.isEnum()) {
          Enum e = entityPropertyValue != null ?
              Enum.valueOf((Class<Enum>) parameterType, (String) entityPropertyValue) : null;
          method.invoke(entity, e);
        } else {
          String setterName = method.getName();
      	  String getterName = setterName.replace("set", "get");
      	  try {
            Method getMethod = entity.getClass().getDeclaredMethod(getterName);
            if(getMethod.isAnnotationPresent(XmlJavaTypeAdapter.class)) {
              XmlAdapter xmlAdapter = getMethod.getAnnotation(XmlJavaTypeAdapter.class)
                  .value().newInstance();
              method.invoke(entity, xmlAdapter.unmarshal(entityPropertyValue));
            }
          } catch (Exception e) {
            throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL, e);
      	  }
        }
      } else if (parameterType.equals(Blob.class)) {
        if (onJPAWriteContent == null) {
          throw ODataJPARuntimeException
              .throwException(ODataJPARuntimeException.ERROR_JPA_BLOB_NULL, null);
        } else {
          method.invoke(entity, entityPropertyValue != null ? 
              onJPAWriteContent.getJPABlob((byte[]) entityPropertyValue) : null);
        }
      } else if (parameterType.equals(Clob.class)) {
        if (onJPAWriteContent == null) {
          throw ODataJPARuntimeException
              .throwException(ODataJPARuntimeException.ERROR_JPA_CLOB_NULL, null);
        } else {
          method.invoke(entity, entityPropertyValue != null ? 
              onJPAWriteContent.getJPAClob(((String) entityPropertyValue).toCharArray()) : null);
        }
      } else if (parameterType.equals(Timestamp.class)) {
        Timestamp ts = entityPropertyValue != null ? 
            new Timestamp(((Calendar) entityPropertyValue).getTimeInMillis()) : null;
        method.invoke(entity, ts);
      } else if (parameterType.equals(java.util.Date.class)) {
        Date d = entityPropertyValue != null ? ((Calendar) entityPropertyValue).getTime(): null;
        method.invoke(entity, d);
      } else if (parameterType.equals(java.sql.Date.class)) {
        java.sql.Date d = entityPropertyValue != null ? 
            new java.sql.Date(((Calendar) entityPropertyValue).getTimeInMillis()) : null;
        method.invoke(entity, d);
      } else if (parameterType.equals(java.sql.Time.class)) {
        java.sql.Time t = entityPropertyValue != null ? 
            new java.sql.Time(((Calendar) entityPropertyValue).getTimeInMillis()) : null;
        method.invoke(entity, t);
      } else if (parameterType.equals(byte.class)) {
        byte b = entityPropertyValue != null ? 
             Byte.parseByte(entityPropertyValue.toString()) : 0;
        method.invoke(entity, b);
      } else {
        method.invoke(entity, entityPropertyValue);
      }
    }
  }