Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.zalando.typemapper.core.fieldMapper;
  
  
  import java.util.List;
  
  import org.slf4j.Logger;
  
 
 public class ObjectFieldMapper {
 
     private static final Logger LOG = LoggerFactory.getLogger(ObjectFieldMapper.class);
 
     private static boolean isRowWithAllFieldsNull(final ObjectResultNode node) {
         if (node != null && node.getChildren() != null) {
             for (DbResultNode dbResultNode : node.getChildren()) {
                 if (dbResultNode != null && dbResultNode.getValue() != null) {
                     return false;
                 }
             }
         }
 
         return true;
     }
 
     public static final Object mapFromDbObjectNode(final Class classzfinal ObjectResultNode node,
             final Mapping mappingthrows InstantiationExceptionIllegalAccessExceptionIllegalArgumentException,
         DatabaseField databaseField = mapping.getField().getAnnotation(DatabaseField.class);
         final Object value;
 
         if (mapping.isOptionalField() && isRowWithAllFieldsNull(node)) {
             value = null;
         } else if (databaseField.mapper() != DefaultObjectMapper.class) {
             ObjectMapper mapper = databaseField.mapper().newInstance();
             value = mapper.unmarshalFromDbNode(node);
         } else {
             value = mapField(mapping.getFieldClass(), node);
         }
 
         return value;
     }
 
     @SuppressWarnings("unchecked")
     public static final Object mapField(@SuppressWarnings("rawtypes"final Class clazzfinal ObjectResultNode node)
             NotsupportedTypeException {
         if (node.getChildren() == null) {
             return null;
         }
 
         Object result = null;
 
         // instantiate enums by string value
         if (clazz.isEnum()) {
             final DbResultNode currentNode = node.getChildByName(node.getType());
             result = Enum.valueOf(clazzcurrentNode.getValue());
         } else {
             result = clazz.newInstance();
 
             final List<Mappingmappings = Mapping.getMappingsForClass(clazz);
 
             for (final Mapping mapping : mappings) {
                 final DbResultNode currentNode = node.getChildByName(mapping.getName());
                 if (currentNode == null) {
                     if (mapping.isOptionalField()) {
                         mapping.map(resultnull);
                     } else {
                         .warn("Could not find value of mapping: {}"mapping.getName());
                     }
 
                     continue;
                 }
 
                 // TODO pribeiro. We should use polymorphism instead. Build like a chain.
                 try {
                     if (..equals(currentNode.getNodeType())) {
                         mapping.map(result,
                             mapping.getFieldMapper().mapField(currentNode.getValue(), mapping.getFieldClass()));
                     } else if (..equals(currentNode.getNodeType())) {
                         mapping.map(resultmapFromDbObjectNode(clazz, (ObjectResultNodecurrentNodemapping));
                     } else if (..equals(currentNode.getNodeType())) {
                         mapping.map(result,
                             ArrayFieldMapper.mapField(mapping.getField(), (ArrayResultNodecurrentNode));
                     }
                 } catch (final Exception e) {
                     .error("Failed to map property {} of class {}",
                         new Object[] {mapping.getName(), clazz.getSimpleName(), e});
                 }
             }
         }
        return result;
    }
New to GrepCode? Check out our FAQ X