com.pontorural.pedidovenda.converter.CicloConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.pontorural.pedidovenda.converter.CicloConverter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pontorural.pedidovenda.converter;

import com.pontorural.pedidovenda.model.Ciclo;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author rafael.lima
 */
@FacesConverter(forClass = Ciclo.class)
public class CicloConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value != null) {
            return this.getAttributesFrom(component).get(value);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (StringUtils.isNotEmpty(value.toString())) {
            Ciclo classe = (Ciclo) value;
            this.addAttribute(component, classe);
            Integer codigo = classe.getCodigo();
            if (codigo != null) {
                return String.valueOf(codigo);
            }
        }
        return (String) value;
    }

    private void addAttribute(UIComponent component, Ciclo classe) {
        this.getAttributesFrom(component).put(classe.getCodigo().toString(), classe);
    }

    private Map<String, Object> getAttributesFrom(UIComponent component) {
        return component.getAttributes();
    }
}