package com.gtp.controller;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import com.gtp.domain.Authority;
import com.gtp.service.AuthoryService;
import commons.ExistedException;
import commons.NotFoundException;
public class AuthorityController {
private String name, url;
private List<Authority> authories;
private Authority authority = null;
AuthoryService authorityService;
public String addAuthority() {
authority = new Authority();
authority.setName(name);
authority.setUrl(url);
try {
authorityService.addAuthory(authority);
} catch (ExistedException e) {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Authority already exists",
"Please choose different one"));
e.printStackTrace();
setName("");
setUrl("");
return null;
}
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Successful"));
setName("");
setUrl("");
return null;
}
public String update() {
try {
authorityService.updateAuthory(authority);
} catch (ExistedException e) {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Authority already exists",
"Please choose different one"));
e.printStackTrace();
setName("");
setUrl("");
return null;
} catch (NotFoundException e) {
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Authority not found",
"Please choose different one"));
e.printStackTrace();
setName("");
setUrl("");
return null;
}
setName("");
setUrl("");
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Successful"));
return null;
}
public String remove() {
try {
authorityService.removeAuthory(authority);
} catch (NotFoundException e) {
e.printStackTrace();
}
authority = null;
return null;
}
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
public Object[] getAuthories() {
authories = authorityService.findAllAuthories();
return authories.toArray();
}
public void setAuthories(List<Authority> authories) {
this.authories = authories;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAuthorityService(AuthoryService authorityService) {
this.authorityService = authorityService;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}