GenericIntegerEntityConverter.java 1020 Bytes
package fi.insomnia.bortal.utilities.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import fi.insomnia.bortal.utilities.jpa.ModelInterface;

public abstract class GenericIntegerEntityConverter<T extends ModelInterface> implements Converter {

	protected abstract T find(Integer id);

	public GenericIntegerEntityConverter() {
		super();
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		T ret = null;
		Integer id = null;
		if (value != null) {
			id = Integer.parseInt(value);
			if (id != null && id > 0) {
				ret = find(id);
			}
		}
		return ret;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {

		String ret = "0";
		if (value != null && value instanceof ModelInterface) {
			ModelInterface entity = (ModelInterface) value;
			if (entity.getId() != null) {
				ret = entity.getId().toString();
			}
		}
		return ret;
	}

}