Search code examples
javaconvertersobject-object-mappingmodelmapper

Java Modelmapper: Converter doesnt get used


im working on a little java-program that synchronizes active directory users with users in my db. therefor, i am using modelmapper. seems to be working fine and is also pretty fast.

anyway i added a converter to my mapping configuration. shows no markers, and i checked the syntax, so it should be fine. but when i fire the syncer to see if hes mapping everything correctly, nothing happens. i mean, the objects get mapped correctly but not the property i set the converter for.

i already went into debug-mode, the convert method is not even entered, not once

so heres my modelmapper-propertymapconfiguration

private PropertyMap<ActiveDirectoryUser, User> createUserMap = new PropertyMap<ActiveDirectoryUser, User>() {
    protected void configure() {
        map(source.getCn(), destination.getFullName());
        map(source.getsAMAccountName(), destination.getLoginName());
        map(source.getMail(), destination.getEmail());
        map(source.isEnabled(), destination.isActive());
        using(new ModelmapperMemberOfToIsAdminConverter(Arrays.asList(ConfigApp.get(ConfigKeys.AD_DISTINGUISHEDNAME_ADMINS).split(";")))
                ).map(source.getGroupMembership(), destination.isAdmin());
    };
};

and theres my converter:

package ch.itp.absencemanagersync.synchronize;

import java.util.ArrayList;
import java.util.List;

import org.modelmapper.AbstractConverter;

public class ModelmapperMemberOfToIsAdminConverter extends AbstractConverter<ArrayList<String>, Boolean>{

    private List<String> comparisonList;

    protected ModelmapperMemberOfToIsAdminConverter(List<String> blablablist){
        comparisonList = blablablist;
    }

    @Override
    protected Boolean convert(ArrayList<String> source) {
        //empty for now, will do some logic here later
        //for testing, always return true
        return true;
    }

}

so if i run the syncer, in theory, every user in my db should turn admin, but that doesnt happen i dont know what im doing wrong here, any help is appreciated^^

ps: dont worry about the Arrays.asList shit in the config, thats working just fine

greetings,

mike


Solution

  • working code:

    mapping config:

    private PropertyMap<ActiveDirectoryUser, User> createUserMap = new PropertyMap<ActiveDirectoryUser, User>() {
        protected void configure() {
            using(myConverter).map(source.getGroupMembership()).setAdmin(false);
            map(source.getCn(), destination.getFullName());
            map(source.getsAMAccountName(), destination.getLoginName());
            map(source.getMail(), destination.getEmail());
            map(source.isEnabled(), destination.isActive());
        };
    };
    

    and converter:

    package ch.itp.absencemanagersync.synchronize;
    
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    import org.modelmapper.AbstractConverter;
    
    import ch.itp.absencemanagersync.util.ConfigApp;
    import ch.itp.absencemanagersync.util.ConfigKeys;
    
    public class ModelmapperMemberOfToIsAdminConverter extends AbstractConverter<List<String>, Boolean>{
    
        private List<String> comparisonList = Arrays.asList(ConfigApp.get(ConfigKeys.AD_DISTINGUISHEDNAME_ADMINS).split(";"));
    
        protected ModelmapperMemberOfToIsAdminConverter(){  
        }
        @Override
        protected Boolean convert(List<String> source) {
            if (!Collections.disjoint(source, comparisonList)){
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    }
    

    is now mapping the desired users to be admins, working correctly