/** * 自定义属性映射 * * @param sourceField 源对象属性名称 * @param targetField 目标对象属性名称 * @param converterId 自定义转换规则 * @return this */ public OrikaCopier.Builder<F, T> field(String sourceField, String targetField, String converterId) { builder.fieldMap(sourceField, targetField) .converter(converterId) .add(); return this; }
/** * 自定义属性映射 * * @param sourceField 源对象属性名称 * @param targetField 目标对象属性名称 * @param sourceType 源对象属性类型 * @param targetType 目标对象属性类型 * @param converterId 自定义转换规则 * @return this */ public OrikaCopier.Builder<F, T> field(String sourceField, String targetField, Class<?> sourceType, Class<?> targetType, String converterId) { builder.fieldMap(sourceField, targetField) .aElementType(sourceType) .bElementType(targetType) .converter(converterId) .add(); return this; }
/** * * * @param fieldA * @param fieldB * @param byDefault * @return */ public FieldMapBuilder<A, B> fieldMap(Property fieldA, Property fieldB, boolean byDefault) { return new FieldMapBuilder<A, B>(this, fieldA, fieldB, byDefault, sourcesMappedOnNull, destinationsMappedOnNull); }
/** * Map a field in one direction only (from fieldNameB to fieldNameA) * * @param fieldNameB the (source) fieldName from type B * @param fieldNameA the (destination) fieldName from type A * @return */ public ClassMapBuilder<A, B> fieldBToA(String fieldNameB, String fieldNameA) { return fieldMap(fieldNameA, fieldNameB).bToA().add(); }
/** * Map a field in one direction only (from fieldNameA to fieldNameB) * * @param fieldNameA * the (source) fieldName from type A * @param fieldNameB * the (destination) fieldName from type B * @return */ public ClassMapBuilder<A, B> fieldAToB(String fieldNameA, String fieldNameB) { return fieldMap(fieldNameA, fieldNameB).aToB().add(); }
/** * * * @param fieldA * @param fieldB * @return */ public ClassMapBuilder<A, B> field(Property fieldA, Property fieldB) { return fieldMap(fieldA, fieldB, false).add(); }
@Test public void testInverseOneToManyMapping() { MapperFactory factory = MappingUtil.getMapperFactory(); ClassMapBuilder<PublisherDTO, Publisher> classMapBuilder = factory.classMap(PublisherDTO.class, Publisher.class); classMapBuilder.fieldMap("books").bInverse("publisher").add(); factory.registerClassMap(classMapBuilder.byDefault().toClassMap()); BoundMapperFacade<Publisher,PublisherDTO> mapper = factory.getMapperFacade(Publisher.class, PublisherDTO.class); BookDTO parisNoirDTO = new BookDTO(); parisNoirDTO.setTitle("Paris Noir"); BookDTO chiensFousDTO = new BookDTO(); chiensFousDTO.setTitle("Chiens Fous"); PublisherDTO publisherDTO = new PublisherDTO(); publisherDTO.setName("Asphalte Editions"); publisherDTO.getBooks().add(parisNoirDTO); publisherDTO.getBooks().add(chiensFousDTO); Publisher publisher = mapper.mapReverse(publisherDTO); Assert.assertTrue(publisher == publisher.getBooks().iterator().next().getPublisher()); }
/** * 自定义属性映射 * * @param sourceField 源对象属性名称 * @param targetField 目标对象属性名称 * @param sourceType 源对象属性类型 * @param targetType 目标对象属性类型 * @return this */ public OrikaCopier.Builder<F, T> field(String sourceField, String targetField, Class<?> sourceType, Class<?> targetType) { builder.fieldMap(sourceField, targetField) .aElementType(sourceType) .bElementType(targetType) .add(); return this; }
@Override public ClassMapBuilder<A, B> byDefault(MappingDirection direction, DefaultFieldMapper... withDefaults) { super.byDefault(direction, withDefaults); for (final String propertyNameA : getPropertiesForTypeA()) { if (!getMappedPropertiesForTypeA().contains(propertyNameA)) { String lowercaseName = propertyNameA.toLowerCase(); if (lowercasePropertiesForB.containsKey(lowercaseName)) { String propertyNameB = lowercasePropertiesForB.get(lowercaseName); if (!getMappedPropertiesForTypeB().contains(propertyNameB)) { /* * Don't include the default mapping of Class to Class; this * property is resolved for all types, but can't be mapped * in either direction. */ if (!propertyNameA.equals("class")) { fieldMap(propertyNameA, propertyNameB, true).direction(direction).add(); } } } } } return this; }
/** * Specify element type for A side property<br> * <i>When dealing with legacy code, prior to Java 1.5 you can add element * type of collections, this is not required when using generics</i> * * @param rawType the elementType of of the 'B' field * @return a reference to this FieldMapBuilder */ public FieldMapBuilder<A, B> aElementType(final Class<?> rawType) { return aElementType(TypeFactory.valueOf(rawType)); }
/** * Specify element type for B side property<br> * <i>When dealing with legacy code, prior to Java 1.5 you can add element * type of collections, this is not required when using generics</i> * * @param rawType the elementType of of the 'B' field * @return a reference to this FieldMapBuilder */ public FieldMapBuilder<A, B> bElementType(final Class<?> rawType) { return bElementType(TypeFactory.valueOf(rawType)); }
/** * * * @param fieldNameA * @param fieldB * @return */ public ClassMapBuilder<A, B> field(String fieldNameA, Property fieldB) { return fieldMap(fieldNameA, fieldB, false).add(); }
@Test public void testInverseOneToManyMapping() { MapperFactory mapperFactory = MappingUtil.getMapperFactory(); ClassMapBuilder<PublisherDTO, Publisher> classMapBuilder = ClassMapBuilder.map(PublisherDTO.class, Publisher.class); classMapBuilder.fieldMap("books").bInverse("publisher").add(); mapperFactory.registerClassMap(classMapBuilder.byDefault().toClassMap()); mapperFactory.build(); MapperFacade mapper = mapperFactory.getMapperFacade(); BookDTO parisNoirDTO = new BookDTO(); parisNoirDTO.setTitle("Paris Noir"); BookDTO chiensFousDTO = new BookDTO(); chiensFousDTO.setTitle("Chiens Fous"); PublisherDTO publisherDTO = new PublisherDTO(); publisherDTO.setName("Asphalte Editions"); publisherDTO.getBooks().add(parisNoirDTO); publisherDTO.getBooks().add(chiensFousDTO); Publisher publisher = mapper.map(publisherDTO, Publisher.class); Assert.assertTrue(publisher == publisher.getBooks().iterator().next().getPublisher()); }
/** * Map a field in one direction only (from fieldNameB to fieldNameA) * * @param fieldNameB * the (source) fieldName from type B * @param fieldNameA * the (destination) fieldName from type A * @return */ public ClassMapBuilder<A, B> fieldBToA(String fieldNameB, String fieldNameA) { return fieldMap(fieldNameA, fieldNameB).bToA().add(); }
/** * Map a field in one direction only (from fieldNameA to fieldNameB) * * @param fieldNameA the (source) fieldName from type A * @param fieldNameB the (destination) fieldName from type B * @return */ public ClassMapBuilder<A, B> fieldAToB(String fieldNameA, String fieldNameB) { return fieldMap(fieldNameA, fieldNameB).aToB().add(); }
/** * Exclude the specified field from mapping * * @param fieldName * the name of the field/property to exclude * @return this ClassMapBuilder */ public ClassMapBuilder<A, B> exclude(String fieldName) { if (propertyResolver.existsProperty(getAType(), fieldName) && propertyResolver.existsProperty(getBType(), fieldName)) { return fieldMap(fieldName).exclude().add(); } else { return this; } }
converterFactory.registerConverter("dateConverter2", new DateToStringConverter("dd-MM-yyyy")); factory.registerClassMap(ClassMapBuilder.map(A.class, B.class).fieldMap("date").converter("dateConverter1").add().toClassMap()); factory.registerClassMap(ClassMapBuilder.map(A.class, C.class).fieldMap("date").converter("dateConverter2").add().toClassMap());