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

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

package br.com.jarch.faces.converter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

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

@FacesConverter("br.com.jarch.faces.converter.pickListMapConverter")
public class PickListMapJsfConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
        Object ret = null;
        if (arg1 instanceof PickList) {
            Object dualList = ((PickList) arg1).getValue();

            DualListModel dl = (DualListModel) dualList;
            for (Iterator iterator = dl.getSource().iterator(); iterator.hasNext(); ) {
                Object o = iterator.next();
                String id = (new StringBuilder()).append(o).toString();

                if (isEqual(arg2, id)) {
                    ret = o;
                    break;
                }
            }
            if (ret == null) {
                for (Iterator iterator1 = dl.getTarget().iterator(); iterator1.hasNext(); ) {
                    Object o = iterator1.next();
                    String id = (new StringBuilder()).append(o).toString();
                    if (isEqual(arg2, id)) {
                        ret = o;
                        break;
                    }
                }
            }
        }
        return ret;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
        if (arg2 instanceof Map
                && arg2 != null) {
            return arg2.toString();
        }

        return "";
    }

    private boolean isEqual(String id1, String id2){
        if (id1.indexOf("attribute") > 0) {
            if (id1
                    .substring(id1.indexOf("attribute") + 9)
                    .contains(id2.substring(id2.lastIndexOf("=") + 1))){
                return true;
            }
        } else {
            if (id1
                    .replaceAll("=", "=")
                    .replaceAll("/", "=")
                    .equals(id2)) {
                return true;
            }
        }

        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy