
package de.ivu.elect.jee.faces;

import java.util.HashMap;
import java.util.Map;

import javax.faces.component.UIComponent;

import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;

public final class FacesComponentUtil {

  private FacesComponentUtil() {
    super();
  }

  public static Object getLocalizedLabelOrId(UIComponent component) {
    return getAttribute(component, "label");
  }

  public static Object getAttributeFromDynamicFormControlOrNormal(UIComponent component, String attributeName) {
    Object result = null;

    Map< ? , ? > attributes = getAttributesFromDynamicFormControl(component);

    if (!attributes.isEmpty()) {
      result = attributes.get(attributeName);
    }

    if (result == null) {
      result = getAttribute(component, attributeName);
    }

    return result;
  }

  public static Map< ? , ? > getAttributesFromDynamicFormControl(UIComponent component) {
    Object attributes = getAttribute(component, FormBuilder.DYNAMIC_ATTRIBUTES_KEY);

    if (attributes instanceof Map< ? , ? >) {
      return (Map< ? , ? >) attributes;
    }

    return new HashMap<>();
  }

  public static Object getAttribute(UIComponent component, String attributeName) {
    return component.getAttributes().get(attributeName);
  }
}
