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

br.com.jarch.faces.converter.BaseIdentityJsfConverter Maven / Gradle / Ivy

package br.com.jarch.faces.converter;

import br.com.jarch.core.model.IIdentity;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import java.util.Map;

public abstract class BaseIdentityJsfConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        if (value == null) {
            return null;
        }

        return getAttributesFrom(component).get(value);
    }

    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        if (value != null && !"".equals(value)) {
            IIdentity identity = (IIdentity) value;
            addAttribute(component, identity);
            return getKey(identity);
        }

        return (String) value;
    }

    private static void addAttribute(UIComponent component, IIdentity identity) {
        String key = getKey(identity);
        getAttributesFrom(component).put(key, identity);
    }

    private static String getKey(IIdentity identity) {
        return identity.getId() == null ? identity.toString() : Long.toString(identity.getId());
    }

    private static Map getAttributesFrom(UIComponent component) {
        return component.getAttributes();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy