private void registerOrgIdMap(MapperFactory mapperFactory) { ClassMapBuilder<OrgId, JSONObject> classMap = mapperFactory.classMap(OrgId.class, JSONObject.class).mapNulls(false).mapNullsInReverse(false); classMap.field("id", "Id"); classMap.field("orgIdValue", "Name"); classMap.field("orgIdType", "Identifier_Type__c"); classMap.field("inactive", "Inactive__c"); classMap.field("primaryIdForType", "Primary_ID_for_type__c"); classMap.field("accountId", "Organization__c"); classMap.field("notes", "Notes__c"); classMap.register(); }
/** * 自定义映射规则 * * @param customizedMapper 自定义映射规则 * @return this */ public Builder<F, T> customize(Mapper<F, T> customizedMapper) { builder.customize(customizedMapper); return this; }
/** * Create a fieldMap for the particular field (same property name used in * both types) * * @param a * @return */ public FieldMapBuilder<A, B> fieldMap(String a) { return fieldMap(a, a); }
public static void main(String[] args) { // init mapper MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); mapperFactory.classMap(Source.class, Destination.class). field("message", "text").register(); MapperFacade mapper = mapperFactory.getMapperFacade(); // convert Source source = new Source("Hello World!"); Destination destObject = mapper.map(source, Destination.class); destObject.print(); // print Hello World! } }
/** * 排除属性 * * @param fields 要排除的属性名称 * @return this */ public OrikaCopier.Builder<F, T> skip(String... fields) { if (fields != null && fields.length != 0) { for (String field : fields) { builder.exclude(field); } // Orika 默认使用全参构造,这时 skip() 不生效,需要使用不包含 skip 属性的构造方法, // 所以 Copiers 将默认值改为了无参构造,用户也可以在调用 skip() 后使用 constructor() 方法自己指定 // https://github.com/orika-mapper/orika/issues/135 builder.constructorB(); } return this; }
mapperFactory.classMap(GroupIdRecords.class, org.orcid.jaxb.model.v3.rc2.groupid.GroupIdRecords.class).byDefault().register(); mapperFactory.classMap(GroupIdRecord.class, org.orcid.jaxb.model.v3.rc2.groupid.GroupIdRecord.class).byDefault().register(); mapperFactory.classMap(ExternalIDs.class, org.orcid.jaxb.model.v3.rc2.record.ExternalIDs.class).byDefault().register(); mapperFactory.classMap(ExternalID.class, org.orcid.jaxb.model.v3.rc2.record.ExternalID.class).byDefault().register(); mapperFactory.classMap(ContributorOrcid.class, org.orcid.jaxb.model.v3.rc2.common.ContributorOrcid.class).customize(orcidIdBaseMapper).register(); mapperFactory.classMap(SourceOrcid.class, org.orcid.jaxb.model.v3.rc2.common.SourceOrcid.class).customize(orcidIdBaseMapper).register(); mapperFactory.classMap(SourceClientId.class, org.orcid.jaxb.model.v3.rc2.common.SourceClientId.class).customize(orcidIdBaseMapper).register(); mapperFactory.classMap(OrcidIdentifier.class, org.orcid.jaxb.model.v3.rc2.common.OrcidIdentifier.class).customize(orcidIdBaseMapper).register(); mapperFactory.classMap(OtherNames.class, org.orcid.jaxb.model.v3.rc2.record.OtherNames.class).byDefault().register(); mapperFactory.classMap(OtherName.class, org.orcid.jaxb.model.v3.rc2.record.OtherName.class).byDefault().register(); mapperFactory.classMap(Keywords.class, org.orcid.jaxb.model.v3.rc2.record.Keywords.class).byDefault().register(); mapperFactory.classMap(Keyword.class, org.orcid.jaxb.model.v3.rc2.record.Keyword.class).byDefault().register(); mapperFactory.classMap(Addresses.class, org.orcid.jaxb.model.v3.rc2.record.Addresses.class).byDefault().register(); mapperFactory.classMap(Address.class, org.orcid.jaxb.model.v3.rc2.record.Address.class).byDefault().register(); mapperFactory.classMap(ResearcherUrls.class, org.orcid.jaxb.model.v3.rc2.record.ResearcherUrls.class).byDefault().register(); mapperFactory.classMap(ResearcherUrl.class, org.orcid.jaxb.model.v3.rc2.record.ResearcherUrl.class).byDefault().register(); mapperFactory.classMap(PersonExternalIdentifiers.class, org.orcid.jaxb.model.v3.rc2.record.PersonExternalIdentifiers.class).byDefault().register(); mapperFactory.classMap(PersonExternalIdentifier.class, org.orcid.jaxb.model.v3.rc2.record.PersonExternalIdentifier.class).byDefault().register();
@Test public void testMapping() { MapperFactory mapperFactory = MappingUtil.getMapperFactory(); mapperFactory.getConverterFactory().registerConverter(new OrderIdConverter()); ClassMapBuilder<Order, OrderData> builder = ClassMapBuilder.map(Order.class, OrderData.class); ClassMap<Order, OrderData> classMap = builder.field("id", "number").byDefault().toClassMap(); mapperFactory.registerClassMap(classMap); MapperFacade facade = mapperFactory.getMapperFacade(); OrderData data = new OrderData(1234l); Order order = facade.map(data, Order.class); Assert.assertEquals(Long.valueOf(1234l), order.getId()); }
/** * Constructs and registers a {@link ClassMapBuilder} into the {@link MapperFactory} using a {@link Mapper}. * * @param mapper */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void addMapper(final Mapper<?, ?> mapper) { factory.classMap(mapper.getAType(), mapper.getBType()) .byDefault() .customize((Mapper) mapper) .mapNulls(false) .mapNullsInReverse(false) .register(); }
public MapperFacade getGroupIdRecordMapperFacade() { MapperFactory mapperFactory = getNewMapperFactory(); ClassMapBuilder<GroupIdRecord, GroupIdRecordEntity> classMap = mapperFactory.classMap(GroupIdRecord.class, GroupIdRecordEntity.class); addV2CommonFields(classMap); registerSourceConverters(mapperFactory, classMap); classMap.field("name", "groupName"); classMap.field("groupId", "groupId"); classMap.field("description", "groupDescription"); classMap.field("type", "groupType"); classMap.byDefault(); classMap.register(); return mapperFactory.getMapperFacade(); }
public MapperFacade getClientMapperFacade() { MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); ClassMapBuilder<ClientSummary, ClientDetailsEntity> clientSummaryClassMap = mapperFactory.classMap(ClientSummary.class, ClientDetailsEntity.class); clientSummaryClassMap.field("name", "clientName"); clientSummaryClassMap.field("description", "clientDescription"); clientSummaryClassMap.byDefault(); clientSummaryClassMap.register(); clientClassMap.field("name", "clientName"); clientClassMap.field("description", "clientDescription"); clientClassMap.field("website", "clientWebsite"); clientClassMap.field("allowAutoDeprecate", "allowAutoDeprecate"); clientClassMap.fieldBToA("clientId", "id"); clientClassMap.fieldBToA("clientType", "clientType"); clientClassMap.fieldBToA("groupProfileId", "groupProfileId"); clientClassMap.fieldBToA("authenticationProviderId", "authenticationProviderId"); clientClassMap.fieldBToA("persistentTokensEnabled", "persistentTokensEnabled"); clientClassMap.customize(new CustomMapper<Client, ClientDetailsEntity>() { clientClassMap.register(); return mapperFactory.getMapperFacade();
@SuppressWarnings("unchecked") public void registerSourceConverters(MapperFactory mapperFactory, ClassMapBuilder<? extends SourceAware, ? extends SourceAwareEntity<?>> classMapBuilder) { @SuppressWarnings("rawtypes") SourceMapper sourceMapper = new SourceMapper(); mapperFactory.classMap(SourceAware.class, SourceAwareEntity.class).customize(sourceMapper).register(); }
/** * Constructs and registers a {@link ClassMapBuilder} into the {@link MapperFactory} using a {@link Mapper}. * * @param mapper */ @SuppressWarnings("rawtypes") public void addMapper(Mapper<?, ?> mapper) { factory.classMap(mapper.getAType(), mapper.getBType()) .byDefault() .customize((Mapper) mapper) .register(); }
public MapperFacade getKeywordMapperFacade() { MapperFactory mapperFactory = getNewMapperFactory(); mapperFactory.getConverterFactory().registerConverter("visibilityConverter", new VisibilityConverter()); ClassMapBuilder<Keyword, ProfileKeywordEntity> keywordClassMap = mapperFactory.classMap(Keyword.class, ProfileKeywordEntity.class); addV2DateFields(keywordClassMap); registerSourceConverters(mapperFactory, keywordClassMap); keywordClassMap.field("putCode", "id"); keywordClassMap.field("content", "keywordName"); keywordClassMap.fieldBToA("displayIndex", "displayIndex"); keywordClassMap.fieldMap("visibility", "visibility").converter("visibilityConverter").add(); keywordClassMap.byDefault(); keywordClassMap.register(); return mapperFactory.getMapperFacade(); }
@Test public void mapNulls_True_ClassLevel() { MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build(); mapperFactory.classMap(Container.class, Container2.class) .mapNulls(true).byDefault().register(); Container a = new Container(); Container2 b = new Container2(); b.longValue = 1L; b.stringValue = "TEST A"; b.arrayOfString = new String[]{"a", "b", "c"}; b.arrayOfInt = new int[] {1,2,3}; b.listOfString = Arrays.asList("l1","l2"); b.map = Collections.singletonMap("key", (Object)"value"); b.enumValue = Position.FIRST; mapperFactory.getMapperFacade().map(a, b); Assert.assertNull( b.stringValue ); Assert.assertNull( b.arrayOfString ); Assert.assertNull( b.arrayOfInt ); Assert.assertNull( b.listOfString ); Assert.assertNull( b.map ); Assert.assertNull( b.enumValue ); }
private void configureMapperFactory(MapperFactory factory) { factory.registerClassMap(ClassMapBuilder.map(Policy.class, PolicyDTO.class).byDefault().toClassMap()); factory.registerClassMap(ClassMapBuilder.map(CustomerElement.class, CustomerElementDTO.class).byDefault().toClassMap()); factory.registerClassMap(ClassMapBuilder.map(ProductElement.class, ProductElementDTO.class).byDefault().toClassMap()); factory.registerClassMap(ClassMapBuilder.map(OtherElement.class, OtherElementDTO.class).byDefault().toClassMap()); factory.registerClassMap(ClassMapBuilder.map(OneOtherElement.class, OneOtherElementDTO.class).byDefault().toClassMap()); }
@Before public void setUp() { mapperFactory = MappingUtil.getMapperFactory(); ClassMap<LineDTO, Line> classMap = ClassMapBuilder.map(LineDTO.class, Line.class).field("x0", "start.x").field("y0", "start.y") .field("x1", "end.x").field("y1", "end.y").toClassMap(); mapperFactory.registerClassMap(classMap); mapperFactory.build(); }
public MapperFacade getInvalidRecordDataChangeMapperFacade() { MapperFactory mapperFactory = getNewMapperFactory(); ClassMapBuilder<RecordCorrection, InvalidRecordDataChangeEntity> classMap = mapperFactory.classMap(RecordCorrection.class, InvalidRecordDataChangeEntity.class); classMap.fieldBToA("id", "sequence"); classMap.fieldBToA("sqlUsedToUpdate", "sqlUsedToUpdate"); classMap.fieldBToA("description", "description"); classMap.fieldBToA("numChanged", "numChanged"); classMap.fieldBToA("type", "type"); classMap.byDefault(); classMap.register(); return mapperFactory.getMapperFacade(); }