GenericIntegerEntityConverter.java
1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}
}