Galera,
o que vocês acham:
public abstract class AbastractController<T, PK extends Serializable> {
private final JSFMessageUtil messageUtil = new JSFMessageUtil();
private final BaseService<T, PK> baseService;
private T entity;
public AbastractController(BaseService<T, PK> baseService, Class<T> clazz) throws Exception {
super();
this.entity = clazz.newInstance();
this.baseService = baseService;
reset();
}
public abstract String actionEdit();
public abstract String actionSave();
public abstract String actionSaveAndContinue();
@SuppressWarnings("unchecked")
public void onRowSelect(SelectEvent event) {
entity = (T) event.getObject();
}
public void saveOrUpdate(ActionEvent event) {
try {
getBaseService().save(entity);
displayInfoMessageToUser("Saved With Sucess");
reset();
} catch (Exception e) {
displayErrorMessageToUser("Ops, we could not create. Try again later");
e.printStackTrace();
}
}
public void delete(ActionEvent event) {
try {
getBaseService().delete(entity);
displayInfoMessageToUser("Deleted With Sucess");
reset();
} catch (Exception e) {
displayErrorMessageToUser("Ops, we could not delete. Try again later");
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void reset() throws Exception {
entity = (T) entity.getClass().newInstance();
}
protected abstract LazyDataModel<T> getLazyModel();
protected void displayErrorMessageToUser(String message) {
this.getMessageUtil().sendErrorMessageToUser(message);
}
protected void displayInfoMessageToUser(String message) {
this.getMessageUtil().sendInfoMessageToUser(message);
}
protected RequestContext getRequestContext() {
return RequestContext.getCurrentInstance();
}
public BaseService<T, PK> getBaseService() {
return baseService;
}
public JSFMessageUtil getMessageUtil() {
return messageUtil;
}
public T getEntity() {
return entity;
}
public void setEntity(T entity) {
this.entity = entity;
}
}
Uma implementação seria:
@Component
@ViewScoped
public class GrupoController extends AbastractController<Grupo, Integer> {
private LazyDataModel<Grupo> lazyModel;
@Autowired
public GrupoController(GrupoService grupoService) throws Exception {
super(grupoService, Grupo.class);
lazyModel = new LazyDataModel<Grupo>() {//ommited};
}
@Override
public LazyDataModel<Grupo> getLazyModel() {
return lazyModel;
}
@Override
public String actionEdit() {
return "/view/grupo/form";
}
@Override
public String actionSave() {
return "/view/grupo/index";
}
@Override
public String actionSaveAndContinue() {
return "/view/grupo/form";
}
}
A minha view no p:datatable
eu tenho:
<p:column>
<p:commandButton value="#{msg['button.edit']}" action="#{grupoController.actionEdit}" ajax="false">
<f:setPropertyActionListener value="#{grupo}" target="#{grupoController.entity}" />
</p:commandButton>
<p:commandButton value="#{msg['button.delete']}" onclick="confirmation.show()" >
<f:setPropertyActionListener value="#{grupo}" target="#{grupoController.entity}" />
</p:commandButton>
</p:column>
No meu form eu tenho os botões:
<p:commandButton value="#{msg['button.save']}" ajax="false"
actionListener="#{grupoController.saveOrUpdate}"
action="#{grupoController.actionSave}" />
<p:commandButton value="#{msg['button.saveAndContinue']}" ajax="false"
actionListener="#{grupoController.saveOrUpdate}"
action="#{grupoController.actionSaveAndContinue}" />
Bom, estou com alguns erros na navegação.
É pq eu não quero nada no faces.config
(navigation-rule)… Eu li em algum lugar que retornando o path da view, o proprio framework
se encarrega de procurar o arquivo… Mas não está funcionando… Quando eu clico em Navegar
recebo a exception:
java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at br.com.noronha.control.GrupoController$1.getRowData(GrupoController.java:85)
at br.com.noronha.control.GrupoController$1.getRowData(GrupoController.java:1)
Pq o primefaces ta me retornando null… a linha é a seguinte que fica na classe do LazyDataModel:
@Override
public Grupo getRowData(String key) {
return getBaseService().findOne(Integer.parseInt(key));
}
Sugestões??? 