Package org.dozer.classmap

Examples of org.dozer.classmap.ClassMappings


    List<MappingFileData> mappingFileDataList = loadFromFiles(mappingFiles);
    mappingFileDataList.addAll(builderMappings);

    Configuration globalConfiguration = findConfiguration(mappingFileDataList);

    ClassMappings customMappings = new ClassMappings();
    // Decorate the raw ClassMap objects and create ClassMap "prime" instances
    for (MappingFileData mappingFileData : mappingFileDataList) {
      List<ClassMap> classMaps = mappingFileData.getClassMaps();
      ClassMappings customMappingsPrime = mappingsParser.processMappings(classMaps, globalConfiguration);
      customMappings.addAll(customMappingsPrime);
    }

    // Add default mappings using matching property names if wildcard policy
    // is true. The addDefaultFieldMappings will check the wildcard policy of each classmap
View Full Code Here


  public ClassMappings processMappings(List<ClassMap> classMaps, Configuration globalConfiguration) {
    if (globalConfiguration == null) {
      throw new IllegalArgumentException("Global configuration parameter cannot be null");
    }
    ClassMappings result = new ClassMappings();
    if (classMaps == null || classMaps.size() == 0) {
      return result;
    }
    FieldMap fieldMapPrime;
    // need to create bi-directional mappings now.
    ClassMap classMapPrime;
    Set<String> mapIds = new HashSet<String>();
    for (ClassMap classMap : classMaps) {
      classMap.setGlobalConfiguration(globalConfiguration);

      // add our first class map to the result map and initialize PropertyDescriptor Cache
      ReflectionUtils.findPropertyDescriptor(classMap.getSrcClassToMap(), "", null);
      ReflectionUtils.findPropertyDescriptor(classMap.getDestClassToMap(), "", null);

      // Check to see if this is a duplicate map id, irregardless of src and dest class names.
      // Duplicate map-ids are not allowed
      if (!MappingUtils.isBlankOrNull(classMap.getMapId())) {
        if (mapIds.contains(classMap.getMapId())) {
          throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId());
        }
        mapIds.add(classMap.getMapId());
      }

      result.add(classMap.getSrcClassToMap(), classMap.getDestClassToMap(), classMap.getMapId(), classMap);
      // now create class map prime
      classMapPrime = new ClassMap(globalConfiguration);
      MappingUtils.reverseFields(classMap, classMapPrime);

      if (classMap.getFieldMaps() != null) {
        Object[] fms = classMap.getFieldMaps().toArray();
        // iterate through the fields and see wether or not they should be mapped
        // one way class mappings we do not need to add any fields
        if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
          for (Object fm1 : fms) {
            FieldMap fieldMap = (FieldMap) fm1;
            fieldMap.validate();

            // If we are dealing with a Map data type, transform the field map into a MapFieldMap type
            // only apply transformation if it is map to non-map mapping.
            if (!(fieldMap instanceof ExcludeFieldMap)) {
              if ((isSupportedMap(classMap.getDestClassToMap())
                      && !isSupportedMap(classMap.getSrcClassToMap()))
                  || (isSupportedMap(classMap.getSrcClassToMap())
                      && !isSupportedMap(classMap.getDestClassToMap()))
                  || (isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))
                      && !isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
                  || (isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())))
                      && !isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))) {
                FieldMap fm = new MapFieldMap(fieldMap);
                classMap.removeFieldMapping(fieldMap);
                classMap.addFieldMapping(fm);
                fieldMap = fm;
              }
            }

            if (!(MappingDirection.ONE_WAY.equals(fieldMap.getType()) && !(fieldMap instanceof ExcludeFieldMap))) {
              // make a prime field map
              fieldMapPrime = (FieldMap) fieldMap.clone();
              fieldMapPrime.setClassMap(classMapPrime);
              // check to see if it is only an exclude one way
              if (fieldMapPrime instanceof ExcludeFieldMap && MappingDirection.ONE_WAY.equals(fieldMap.getType())) {
                // need to make a generic field map for the other direction
                fieldMapPrime = new GenericFieldMap(classMapPrime);
              }
              // reverse the fields
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);

              // iterate through copyByReferences and set accordingly
              if (!(fieldMap instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap);
              }
              if (!(fieldMapPrime instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime);
              }
            } else { // if it is a one-way field map make the other field map excluded
              // make a prime field map
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);
            }
            classMapPrime.addFieldMapping(fieldMapPrime);
          }
        } else {
          // since it is one-way...we still need to validate if it has some type of method mapping and validate the
          // field maps
          for (Object fm : fms) {
            FieldMap oneWayFieldMap = (FieldMap) fm;
            oneWayFieldMap.validate();

            MappingUtils.applyGlobalCopyByReference(globalConfiguration, oneWayFieldMap, classMap);
            // check to see if we need to exclude the map
            if (MappingDirection.ONE_WAY.equals(oneWayFieldMap.getType())) {
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime);
              classMapPrime.addFieldMapping(fieldMapPrime);
            }
          }
        }
      }
      // if it is a one way mapping or a method/iterate method mapping we can not bi-directionally map
      // Map Prime could actually be empty
      if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
        result.add(classMap.getDestClassToMap(), classMap.getSrcClassToMap(), classMap.getMapId(), classMapPrime);
      }
    }
    return result;
  }
View Full Code Here

  }

  @Test
  public void testEmptyMappings() throws Exception {
    MappingFileData mappingFileData = new MappingFileData();
    ClassMappings result = parser.processMappings(mappingFileData.getClassMaps(), new Configuration());
    assertNotNull("result should not be null", result);
    assertEquals("result should be empty", 0, result.size());
  }
View Full Code Here

   
    List<MappingFileData> mappingFileDataList = loadFromFiles(mappingFiles);

    Configuration globalConfiguration = findConfiguration(mappingFileDataList);

    ClassMappings customMappings = new ClassMappings();
    // Decorate the raw ClassMap objects and create ClassMap "prime" instances
    for (MappingFileData mappingFileData : mappingFileDataList) {
      List<ClassMap> classMaps = mappingFileData.getClassMaps();
      ClassMappings customMappingsPrime = mappingsParser.processMappings(classMaps, globalConfiguration);
      customMappings.addAll(customMappingsPrime);
    }

    // Add default mappings using matching property names if wildcard policy
    // is true. The addDefaultFieldMappings will check the wildcard policy of each classmap
View Full Code Here

  public ClassMappings processMappings(List<ClassMap> classMaps, Configuration globalConfiguration) {
    if (globalConfiguration == null) {
      throw new IllegalArgumentException("Global configuration parameter cannot be null");
    }
    ClassMappings result = new ClassMappings();
    if (classMaps == null || classMaps.size() == 0) {
      return result;
    }
    FieldMap fieldMapPrime;
    // need to create bi-directional mappings now.
    ClassMap classMapPrime;
    Set<String> mapIds = new HashSet<String>();
    for (ClassMap classMap : classMaps) {
      classMap.setGlobalConfiguration(globalConfiguration);

      // add our first class map to the result map and initialize PropertyDescriptor Cache
      ReflectionUtils.findPropertyDescriptor(classMap.getSrcClassToMap(), "", null);
      ReflectionUtils.findPropertyDescriptor(classMap.getDestClassToMap(), "", null);

      // Check to see if this is a duplicate mapping. If so, throw an Exception
      if (result.contains(classMap.getSrcClassToMap(), classMap.getDestClassToMap(), classMap.getMapId())) {
        throw new IllegalArgumentException("Duplicate Class Mapping Found. Source: " + classMap.getSrcClassToMap().getName()
            + " Destination: " + classMap.getDestClassToMap().getName());
      }

      // Check to see if this is a duplicate map id, irregardless of src and dest class names.
      // Duplicate map-ids are not allowed
      if (!MappingUtils.isBlankOrNull(classMap.getMapId())) {
        if (mapIds.contains(classMap.getMapId())) {
          throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId());
        }
        mapIds.add(classMap.getMapId());
      }

      result.add(classMap.getSrcClassToMap(), classMap.getDestClassToMap(), classMap.getMapId(), classMap);
      // now create class map prime
      classMapPrime = new ClassMap(globalConfiguration);
      MappingUtils.reverseFields(classMap, classMapPrime);

      if (classMap.getFieldMaps() != null) {
        Object[] fms = classMap.getFieldMaps().toArray();
        // iterate through the fields and see wether or not they should be mapped
        // one way class mappings we do not need to add any fields
        if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
          for (Object fm1 : fms) {
            FieldMap fieldMap = (FieldMap) fm1;
            fieldMap.validate();

            // If we are dealing with a Map data type, transform the field map into a MapFieldMap type
            if (!(fieldMap instanceof ExcludeFieldMap)) {
              if (MappingUtils.isSupportedMap(classMap.getDestClassToMap())
                  || MappingUtils.isSupportedMap(classMap.getSrcClassToMap())
                  || MappingUtils.isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))
                  || MappingUtils.isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap()))) {
                FieldMap fm = new MapFieldMap(fieldMap);
                classMap.removeFieldMapping(fieldMap);
                classMap.addFieldMapping(fm);
                fieldMap = fm;
              }
            }

            if (!(MappingDirection.ONE_WAY.equals(fieldMap.getType()) && !(fieldMap instanceof ExcludeFieldMap))) {
              // make a prime field map
              fieldMapPrime = (FieldMap) fieldMap.clone();
              fieldMapPrime.setClassMap(classMapPrime);
              // check to see if it is only an exclude one way
              if (fieldMapPrime instanceof ExcludeFieldMap && MappingDirection.ONE_WAY.equals(fieldMap.getType())) {
                // need to make a generic field map for the other direction
                fieldMapPrime = new GenericFieldMap(classMapPrime);
              }
              // reverse the fields
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);

              // iterate through copyByReferences and set accordingly
              if (!(fieldMap instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap);
              }
              if (!(fieldMapPrime instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime);
              }
            } else { // if it is a one-way field map make the other field map excluded
              // make a prime field map
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);
            }
            classMapPrime.addFieldMapping(fieldMapPrime);
          }
        } else {
          // since it is one-way...we still need to validate if it has some type of method mapping and validate the
          // field maps
          for (Object fm : fms) {
            FieldMap oneWayFieldMap = (FieldMap) fm;
            oneWayFieldMap.validate();

            MappingUtils.applyGlobalCopyByReference(globalConfiguration, oneWayFieldMap, classMap);
            // check to see if we need to exclude the map
            if (MappingDirection.ONE_WAY.equals(oneWayFieldMap.getType())) {
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime);
              classMapPrime.addFieldMapping(fieldMapPrime);
            }
          }
        }
      }
      // if it is a one way mapping or a method/iterate method mapping we can not bi-directionally map
      // Map Prime could actually be empty
      if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
        result.add(classMap.getDestClassToMap(), classMap.getSrcClassToMap(), classMap.getMapId(), classMapPrime);
      }
    }
    return result;
  }
View Full Code Here

  }

  @Test
  public void testEmptyMappings() throws Exception {
    MappingFileData mappingFileData = new MappingFileData();
    ClassMappings result = parser.processMappings(mappingFileData.getClassMaps(), new Configuration());
    assertNotNull("result should not be null", result);
    assertEquals("result should be empty", 0, result.size());
  }
View Full Code Here

   */
  public ClassMappings processMappings(List<ClassMap> classMaps, Configuration globalConfiguration) {
    if (globalConfiguration == null) {
      throw new IllegalArgumentException("Global configuration parameter cannot be null");
    }
    ClassMappings result = new ClassMappings();
    if (classMaps == null || classMaps.size() == 0) {
      return result;
    }
    FieldMap fieldMapPrime;
    // need to create bi-directional mappings now.
    ClassMap classMapPrime;
    Set<String> mapIds = new HashSet<String>();
    for (ClassMap classMap : classMaps) {
      classMap.setGlobalConfiguration(globalConfiguration);

      // add our first class map to the result map and initialize PropertyDescriptor Cache
      ReflectionUtils.findPropertyDescriptor(classMap.getSrcClassToMap(), "", null);
      ReflectionUtils.findPropertyDescriptor(classMap.getDestClassToMap(), "", null);

      // Check to see if this is a duplicate map id, irregardless of src and dest class names.
      // Duplicate map-ids are not allowed
      if (!MappingUtils.isBlankOrNull(classMap.getMapId())) {
        if (mapIds.contains(classMap.getMapId())) {
          throw new IllegalArgumentException("Duplicate Map Id's Found. Map Id: " + classMap.getMapId());
        }
        mapIds.add(classMap.getMapId());
      }

      result.add(classMap.getSrcClassToMap(), classMap.getDestClassToMap(), classMap.getMapId(), classMap);
      // now create class map prime
      classMapPrime = new ClassMap(globalConfiguration);
      MappingUtils.reverseFields(classMap, classMapPrime);

      if (classMap.getFieldMaps() != null) {
        List<FieldMap> fms = classMap.getFieldMaps();
        // iterate through the fields and see wether or not they should be mapped
        // one way class mappings we do not need to add any fields
        if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
          for (FieldMap fieldMap : fms.toArray(new FieldMap[]{})) {
            fieldMap.validate();

            // If we are dealing with a Map data type, transform the field map into a MapFieldMap type
            // only apply transformation if it is map to non-map mapping.
            if (!(fieldMap instanceof ExcludeFieldMap)) {
              if ( ( isSupportedMap(classMap.getDestClassToMap()) ^ isSupportedMap(classMap.getSrcClassToMap()) )
               || ( isSupportedMap(fieldMap.getDestFieldType(classMap.getDestClassToMap()))
                    ^ isSupportedMap(fieldMap.getSrcFieldType(classMap.getSrcClassToMap())) ) ) {
                FieldMap fm = new MapFieldMap(fieldMap);
                classMap.removeFieldMapping(fieldMap);
                classMap.addFieldMapping(fm);
                fieldMap = fm;
              }
            }
           
            // if the source is a java.util.Map, and not already mapped as key=>value,
            // map the field as key=>value, not as bean property
            if (isSupportedMap(classMap.getSrcClassToMap()) && fieldMap.getSrcFieldKey() == null) {
              DozerField newSrcField = fieldMap.getSrcFieldCopy();
              newSrcField.setName(DozerConstants.SELF_KEYWORD);
              newSrcField.setKey(fieldMap.getSrcFieldName());
              fieldMap.setSrcField(newSrcField);
            }
            // like above but the reverse:
            // if the destination is a java.util.Map, and not already mapped as key=>value,
            // map the field as key=>value, not as bean property
            if (isSupportedMap(classMap.getDestClassToMap()) && fieldMap.getDestFieldKey() == null) {
              DozerField newDestField = fieldMap.getDestFieldCopy();
              newDestField.setName(DozerConstants.SELF_KEYWORD);
              newDestField.setKey(fieldMap.getDestFieldName());
              fieldMap.setDestField(newDestField);
            }

            if (!(MappingDirection.ONE_WAY.equals(fieldMap.getType()) && !(fieldMap instanceof ExcludeFieldMap))) {
              // make a prime field map
              fieldMapPrime = (FieldMap) fieldMap.clone();
              fieldMapPrime.setClassMap(classMapPrime);
              // check to see if it is only an exclude one way
              if (fieldMapPrime instanceof ExcludeFieldMap && MappingDirection.ONE_WAY.equals(fieldMap.getType())) {
                // need to make a generic field map for the other direction
                fieldMapPrime = new GenericFieldMap(classMapPrime);
              }
              // reverse the fields
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);

              // iterate through copyByReferences and set accordingly
              if (!(fieldMap instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMap, classMap);
              }
              if (!(fieldMapPrime instanceof ExcludeFieldMap)) {
                MappingUtils.applyGlobalCopyByReference(globalConfiguration, fieldMapPrime, classMapPrime);
              }
            } else { // if it is a one-way field map make the other field map excluded
              // make a prime field map
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(fieldMap, fieldMapPrime);
            }
            classMapPrime.addFieldMapping(fieldMapPrime);
          }
        } else {
          // since it is one-way...we still need to validate if it has some type of method mapping and validate the
          // field maps
          for (FieldMap oneWayFieldMap : fms.toArray(new FieldMap[]{})) {
            oneWayFieldMap.validate();

            MappingUtils.applyGlobalCopyByReference(globalConfiguration, oneWayFieldMap, classMap);
            // check to see if we need to exclude the map
            if (MappingDirection.ONE_WAY.equals(oneWayFieldMap.getType())) {
              fieldMapPrime = new ExcludeFieldMap(classMapPrime);
              MappingUtils.reverseFields(oneWayFieldMap, fieldMapPrime);
              classMapPrime.addFieldMapping(fieldMapPrime);
            }
          }
        }
      }
      // if it is a one way mapping or a method/iterate method mapping we can not bi-directionally map
      // Map Prime could actually be empty
      if (!MappingDirection.ONE_WAY.equals(classMap.getType())) {
        result.add(classMap.getDestClassToMap(), classMap.getSrcClassToMap(), classMap.getMapId(), classMapPrime);
      }
    }
    return result;
  }
View Full Code Here

TOP

Related Classes of org.dozer.classmap.ClassMappings

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.