All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.vgregion.ldapservice.search.BeanAttributesMapper Maven / Gradle / Ivy

The newest version!
package se.vgregion.ldapservice.search;

import se.vgregion.ldapservice.search.beanutil.BeanMap;

import javax.naming.NamingException;
import java.util.Map;
import java.util.regex.Pattern;

public class BeanAttributesMapper {

    private final Class type;

    public BeanAttributesMapper(Class type) {
        super();
        this.type = type;
    }

    public T mapFromAttributes(Map attributes) {
        try {
            return mapFromAttributesImpl(attributes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public T mapFromAttributesImpl(Map attributes) throws NamingException,
            IllegalAccessException, InstantiationException {
        T result = type.newInstance();
        BeanMap bm = new BeanMap(result);

        for (Map.Entry entry : attributes.entrySet()) {
            String name = toBeanPropertyName(entry.getKey());
            if (bm.containsKey(name) && bm.isWritable(name)) {
                bm.put(name, entry.getValue());
            }
        }

        return result;
    }

    static String toBeanPropertyName(String name) {
        name = removeSignFrom(name, ";");
        name = removeSignFrom(name, "-");
        return name;
    }

    static String removeSignFrom(String beanPropertyName, String sign) {
        if (beanPropertyName.contains(sign)) {
            String[] parts = beanPropertyName.split(Pattern.quote(sign));
            StringBuilder sb = new StringBuilder(parts[0]);
            for (int i = 1; i < parts.length; i++) {
                char head = Character.toUpperCase(parts[i].charAt(0));
                String tail = parts[i].substring(1);
                sb.append(head);
                sb.append(tail);
            }
            return sb.toString();
        }
        return beanPropertyName;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy