Package org.dozer.fieldmap

Examples of org.dozer.fieldmap.FieldMap


    List<FieldMap> fieldMaps = classMap.getFieldMaps();

    assertNotNull("The fieldmaps should not be null", fieldMaps);
    assertEquals("The fieldmap should have one mapping", 1, fieldMaps.size());

    FieldMap fieldMap = fieldMaps.get(0);

    assertNotNull("The fieldmap should not be null", fieldMap);
    assertEquals("The customconverterparam should be correct", "CustomConverterParamTest", fieldMap.getCustomConverterParam());
  }
View Full Code Here


    }
    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);
            }
          }
View Full Code Here

  public FieldMap getFieldMapUsingDest(String destFieldName, boolean isMap) {
    if (fieldMaps == null) {
      return null;
    }

    FieldMap result = null;

    for (FieldMap fieldMap : fieldMaps) {
      String fieldName = fieldMap.getDestFieldName();

      if (isMap && MappingUtils.isDeepMapping(fieldName)) {
View Full Code Here

  String provideAlternateName(String fieldName) {
    return fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
  }

  public FieldMap getFieldMapUsingSrc(String srcFieldName) {
    FieldMap result = null;

    if (fieldMaps != null) {
      for (FieldMap fieldMap : fieldMaps) {
        String fieldName = fieldMap.getSrcFieldName();
View Full Code Here

    if (fieldMap.getDestHintContainer() == null) {
      Class<?> genericType = fieldMap.getGenericType(destObj.getClass());
      if (genericType != null) {
        HintContainer destHintContainer = new HintContainer();
        destHintContainer.setHintName(genericType.getName());
        FieldMap cloneFieldMap = (FieldMap) fieldMap.clone();
        cloneFieldMap.setDestHintContainer(destHintContainer); // should affect only this time as fieldMap is cloned
        fieldMap = cloneFieldMap;
      }
    }

    // if it is an iterator object turn it into a List
View Full Code Here

    }
  }

  private static void addFieldMapping(ClassMap classMap, Configuration configuration,
                                      String srcName, String destName) {
    FieldMap fieldMap = new GenericFieldMap(classMap);

    DozerField sourceField = new DozerField(srcName, null);
    DozerField destField = new DozerField(destName, null);

    sourceField.setAccessible(true);
    destField.setAccessible(true);

    fieldMap.setSrcField(sourceField);
    fieldMap.setDestField(destField);

    // add CopyByReferences per defect #1728159
    MappingUtils.applyGlobalCopyByReference(configuration, fieldMap, classMap);
    classMap.addFieldMapping(fieldMap);
  }
View Full Code Here

    classMap.addFieldMapping(fieldMap);
  }

  private static void addGenericMapping(ClassMap classMap, Configuration configuration,
                                        String srcName, String destName) {
    FieldMap fieldMap = new GenericFieldMap(classMap);

    fieldMap.setSrcField(new DozerField(srcName, null));
    fieldMap.setDestField(new DozerField(destName, null));

    // add CopyByReferences per defect #1728159
    MappingUtils.applyGlobalCopyByReference(configuration, fieldMap, classMap);
    classMap.addFieldMapping(fieldMap);
  }
View Full Code Here

        // already mapped
        if (!destinationIsMap && classMap.getFieldMapUsingDest(fieldName, true) != null) {
          continue;
        }

        FieldMap fieldMap = new MapFieldMap(classMap);
        DozerField srcField = new DozerField(MappingUtils.isSupportedMap(srcClass) ? DozerConstants.SELF_KEYWORD : fieldName, null);
        srcField.setKey(fieldName);

        if (StringUtils.isNotEmpty(classMap.getSrcClassMapGetMethod()) || StringUtils.isNotEmpty(classMap.getSrcClassMapSetMethod())) {
          srcField.setMapGetMethod(classMap.getSrcClassMapGetMethod());
          srcField.setMapSetMethod(classMap.getSrcClassMapSetMethod());
          srcField.setName(DozerConstants.SELF_KEYWORD);
        }

        DozerField destField = new DozerField(MappingUtils.isSupportedMap(destClass) ? DozerConstants.SELF_KEYWORD : fieldName, null);
        srcField.setKey(fieldName);

        if (StringUtils.isNotEmpty(classMap.getDestClassMapGetMethod())
                || StringUtils.isNotEmpty(classMap.getDestClassMapSetMethod())) {
          destField.setMapGetMethod(classMap.getDestClassMapGetMethod());
          destField.setMapSetMethod(classMap.getDestClassMapSetMethod());
          destField.setName(DozerConstants.SELF_KEYWORD);
        }

        fieldMap.setSrcField(srcField);
        fieldMap.setDestField(destField);

        classMap.addFieldMapping(fieldMap);
      }
      return true;
    }
View Full Code Here

      Class<?> destClass = classMap.getDestClassToMap();
      return MappingUtils.isSupportedCollection(srcClass) && MappingUtils.isSupportedCollection(destClass);
    }

    public boolean apply(ClassMap classMap, Configuration configuration) {
      FieldMap fieldMap = new GenericFieldMap(classMap);
      DozerField selfReference = new DozerField(DozerConstants.SELF_KEYWORD, null);
      fieldMap.setSrcField(selfReference);
      fieldMap.setDestField(selfReference);
      classMap.addFieldMapping(fieldMap);
      return true;
    }
View Full Code Here

      this.customConverterParam = attribute;
    }

    public void build() {
      // TODO Check Map to Map mapping
      FieldMap result;
      if (srcField.isMapTypeCustomGetterSetterField() || destField.isMapTypeCustomGetterSetterField()
          || classMap.isSrcClassMapTypeCustomGetterSetter() || classMap.isDestClassMapTypeCustomGetterSetter()) {
        result = new MapFieldMap(classMap);
      } else if (srcField.isCustomGetterSetterField() || destField.isCustomGetterSetterField()) {
        result = new CustomGetSetMethodFieldMap(classMap);
      } else {
        result = new GenericFieldMap(classMap);
      }

      result.setSrcField(srcField);
      result.setDestField(destField);
      result.setType(type);
      result.setRelationshipType(relationshipType);
      result.setRemoveOrphans(removeOrphans);

      result.setSrcHintContainer(srcHintContainer);
      result.setDestHintContainer(destHintContainer);
      result.setSrcDeepIndexHintContainer(srcDeepIndexHintContainer);
      result.setDestDeepIndexHintContainer(destDeepIndexHintContainer);

      if (copyByReferenceSet) {
        result.setCopyByReference(copyByReference);
      }
      result.setMapId(mapId);

      result.setCustomConverter(customConverter);
      result.setCustomConverterId(customConverterId);
      result.setCustomConverterParam(customConverterParam);

      classMap.addFieldMapping(result);
    }
View Full Code Here

TOP

Related Classes of org.dozer.fieldmap.FieldMap

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.