com.evasion.common.LocationConverter Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.common;
import com.evasion.entity.geolocation.Location;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author sglon
*/
@FacesConverter(value = "locationConverter")
public class LocationConverter implements Converter, Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(LocationConverter.class);
private static final long serialVersionUID = -1L;
private List locs = null;
public LocationConverter(List locs) {
super();
LOGGER.debug("init converter.");
this.locs = locs;
}
public LocationConverter() {
super();
}
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
if (submittedValue.trim().equals("")) {
return null;
}else if (locs==null || locs.isEmpty()) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
} else {
try {
int number = Integer.parseInt(submittedValue);
for (Location location : locs) {
if (location.getId() == number) {
return location;
}
}
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid player"));
}
return null;
}
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
if (value == null) {
return "";
}
Long id = ((Location) value).getId();
if (id != null) {
return Long.toString(id);
} else {
return "";
}
}
}