Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package web;
- import dtos.ClientDTO;
- import dtos.CompanyDTO;
- import dtos.ConfigurationDTO;
- import dtos.CourseDTO;
- import dtos.DocumentDTO;
- import dtos.SoftwareDTO;
- import dtos.StudentDTO;
- import dtos.TemplateDTO;
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import javax.faces.application.Application;
- import java.util.logging.Logger;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.ManagedProperty;
- import javax.faces.bean.SessionScoped;
- import javax.faces.component.UIComponent;
- import javax.faces.component.UIParameter;
- import javax.faces.context.FacesContext;
- import javax.faces.event.ActionEvent;
- import javax.ws.rs.client.Client;
- import javax.ws.rs.client.ClientBuilder;
- import javax.ws.rs.client.Entity;
- import javax.ws.rs.core.GenericType;
- import javax.ws.rs.core.MediaType;
- import javax.ws.rs.core.Response;
- import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
- @ManagedBean
- @SessionScoped
- public class AdministratorManager implements Serializable {
- private final Logger logger = Logger.getLogger("web.AdministratorManager");
- private final String baseUri = "http://localhost:8080/AcademicManagement-war/webapi";
- private Client client;
- private UIComponent component;
- private StudentDTO newStudent;
- private StudentDTO currentStudent;
- private ConfigurationDTO newConfiguration;
- private ConfigurationDTO currentConfiguration;
- private TemplateDTO newTemplate;
- private TemplateDTO currentTemplate;
- private ClientDTO newClient;
- private ClientDTO currentClient;
- private SoftwareDTO newSoftware;
- private SoftwareDTO currentSoftware;
- private CompanyDTO newCompany;
- private CompanyDTO currentCompany;
- private DocumentDTO document;
- private String filePath;
- private String newCharacteristicJSONString;
- private String currentCharacteristicJSONString;
- private String newCharacteristicKey; //IE: "Version"
- private String newCharacteristicValue; //IE: "Alpha 1.0"
- @ManagedProperty("#{userManager}")
- UserManager userManager;
- @ManagedProperty(value = "#{uploadManager}")
- private UploadManager uploadManager;
- public AdministratorManager() {
- newStudent = new StudentDTO();
- currentStudent = new StudentDTO();
- newConfiguration = new ConfigurationDTO();
- currentConfiguration = new ConfigurationDTO();
- newTemplate = new TemplateDTO();
- currentTemplate = new TemplateDTO();
- newClient = new ClientDTO();
- currentClient = new ClientDTO();
- newSoftware = new SoftwareDTO();
- currentSoftware = new SoftwareDTO();
- newCompany = new CompanyDTO();
- currentCompany = new CompanyDTO();
- //Not related to USER clients!!
- client = ClientBuilder.newClient();
- }
- private void addHeaderBASIC() {
- FacesContext context = FacesContext.getCurrentInstance();
- Application app = context.getApplication();
- UserManager userManager = app.evaluateExpressionGet(context, "#{userManager}", UserManager.class);
- String username = userManager.getUsername();
- String password = userManager.getPassword();
- HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(username, password);
- client.register(feature);
- }
- public List<StudentDTO> getAllStudents() {
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("students")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<StudentDTO>>() {});
- } catch (Exception e) {
- logger.warning("Unexpected error. Try again latter!");
- return null;
- }
- }
- private <T> Entity<T> asXML(T instance) {
- return Entity.entity(instance, MediaType.APPLICATION_XML);
- }
- public String createStudent() {
- try {
- addHeaderBASIC();
- Response response = client.target(baseUri)
- .path("students")
- .request(MediaType.APPLICATION_XML)
- .post(asXML(newStudent));
- if (response.getStatus() != 200) {
- // error
- }
- newStudent.clear();
- return "/admin/index?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- }
- return "/admin/students/create";
- }
- public String updateStudent() {
- try {
- addHeaderBASIC();
- Response response = client.target(baseUri)
- .path("students")
- .request(MediaType.APPLICATION_XML)
- .put(asXML(currentStudent));
- if (response.getStatus() != 200) {
- // error
- }
- currentStudent.clear();
- return "/admin/index?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- }
- return "/admin/students/update";
- }
- public String removeStudent(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteStudentId");
- String username = param.getValue().toString();
- client.target(baseUri).path("students/" + username).request().delete();
- return "/admin/index?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/index";
- }
- }
- public String removeClient(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteClientId");
- String username = param.getValue().toString();
- client.target(baseUri).path("clients/" + username).request().delete();
- return "/admin/clients/list?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/clients/list";
- }
- }
- public String removeConfiguration(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteConfigurationId");
- String code = param.getValue().toString();
- client.target(baseUri).path("configurations/" + code).request().delete();
- return "/admin/configurations/list?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/configurations/list";
- }
- }
- public String removeTemplate(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteTemplateId");
- String code = param.getValue().toString();
- client.target(baseUri).path("templates/" + code).request().delete();
- return "/admin/templates/list?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/templates/list";
- }
- }
- public String removeCompany(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCompanyId");
- String code = param.getValue().toString();
- client.target(baseUri).path("companys/" + code).request().delete();
- return "/admin/companys/list?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/companys/list";
- }
- }
- public String removeTemplateCharacteristic(ActionEvent event) {
- try {
- addHeaderBASIC();
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCharacteristicId");
- String key = param.getValue().toString();
- return "/admin/templates/details?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/templates/list";
- }
- }
- public String removeCourse(ActionEvent event){
- try {
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCourseId");
- Integer code = (Integer) param.getValue();
- addHeaderBASIC();
- client.target(baseUri).path("courses/" + code).request().delete();
- return "/admin/index?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/index";
- }
- }
- public String removeSoftware(ActionEvent event){
- try {
- UIParameter param = (UIParameter) event.getComponent().findComponent("deleteSoftwareId");
- Integer code = (Integer) param.getValue();
- addHeaderBASIC();
- client.target(baseUri).path("softwares/" + code).request().delete();
- return "/admin/softwares/list?faces-redirect=true";
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return "/admin/softwares/list";
- }
- }
- public List<CourseDTO> getAllCourses() {
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("courses")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<CourseDTO>>() {});
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return null;
- }
- }
- public List<SoftwareDTO> getAllSoftwares() {
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("softwares")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<SoftwareDTO>>() {});
- } catch (Exception e) {
- logger.warning(e.getMessage());
- return null;
- }
- }
- public List<CompanyDTO> getAllCompanies(){
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("companys")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<CompanyDTO>>() {});
- } catch (Exception e) {
- logger.warning("Unexpected error. Try again latter!");
- return null;
- }
- }
- public List<TemplateDTO> getAllTemplates(){
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("templates")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<TemplateDTO>>() {});
- } catch (Exception e) {
- logger.warning("Unexpected error. Try again latter!");
- return null;
- }
- }
- public List<ConfigurationDTO> getAllConfigurations(){
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("configurations")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<ConfigurationDTO>>() {});
- } catch (Exception e) {
- logger.warning("Unexpected error. Try again latter!");
- return null;
- }
- }
- public List<ClientDTO> getAllClients(){
- try {
- addHeaderBASIC();
- return client.target(baseUri)
- .path("clients")
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<List<ClientDTO>>() {});
- } catch (Exception e) {
- logger.warning("Unexpected error. Try again latter!");
- return null;
- }
- }
- public String uploadDocument() {
- try {
- document = new DocumentDTO(uploadManager.getCompletePathFile(), uploadManager.getFilename(), uploadManager.getFile().getContentType());
- client.target(baseUri)
- .path("/configurations/addDocument")
- .path(currentConfiguration.getCode().toString())
- .request(MediaType.APPLICATION_XML)
- .put(Entity.xml(document));
- } catch (Exception e) {
- FacesExceptionHandler.handleException(e, "Unexpected error! Try again latter!", logger);
- return null;
- }
- return "/admin/configurations/details?faces-redirect=true";
- }
- public Collection<DocumentDTO> getDocuments() {
- Collection<DocumentDTO> documents = null;
- try {
- documents = client.target(baseUri)
- .path("/configurations/documents")
- .path(currentConfiguration.getCode().toString())
- .request(MediaType.APPLICATION_XML)
- .get(new GenericType<Collection<DocumentDTO>>() {
- });
- } catch (Exception e) {
- FacesExceptionHandler.handleException(e, "Unexpected error! Try again latter!", logger);
- }
- return documents;
- }
- public UIComponent getComponent() {
- return component;
- }
- public void setComponent(UIComponent component) {
- this.component = component;
- }
- public StudentDTO getNewStudent() {
- return newStudent;
- }
- public void setNewStudent(StudentDTO newStudent) {
- this.newStudent = newStudent;
- }
- public StudentDTO getCurrentStudent() {
- return currentStudent;
- }
- public void setCurrentStudent(StudentDTO currentStudent) {
- this.currentStudent = currentStudent;
- }
- public Client getClient() {
- return client;
- }
- public void setClient(Client client) {
- this.client = client;
- }
- public ConfigurationDTO getNewConfiguration() {
- return newConfiguration;
- }
- public void setNewConfiguration(ConfigurationDTO newConfiguration) {
- this.newConfiguration = newConfiguration;
- }
- public ConfigurationDTO getCurrentConfiguration() {
- return currentConfiguration;
- }
- public void setCurrentConfiguration(ConfigurationDTO currentConfiguration) {
- this.currentConfiguration = currentConfiguration;
- }
- public TemplateDTO getNewTemplate() {
- return newTemplate;
- }
- public void setNewTemplate(TemplateDTO newTemplate) {
- this.newTemplate = newTemplate;
- }
- public TemplateDTO getCurrentTemplate() {
- return currentTemplate;
- }
- public void setCurrentTemplate(TemplateDTO currentTemplate) {
- this.currentTemplate = currentTemplate;
- }
- public ClientDTO getNewClient() {
- return newClient;
- }
- public void setNewClient(ClientDTO newClient) {
- this.newClient = newClient;
- }
- public ClientDTO getCurrentClient() {
- return currentClient;
- }
- public void setCurrentClient(ClientDTO currentClient) {
- this.currentClient = currentClient;
- }
- public SoftwareDTO getNewSoftware() {
- return newSoftware;
- }
- public void setNewSoftware(SoftwareDTO newSoftware) {
- this.newSoftware = newSoftware;
- }
- public SoftwareDTO getCurrentSoftware() {
- return currentSoftware;
- }
- public void setCurrentSoftware(SoftwareDTO currentSoftware) {
- this.currentSoftware = currentSoftware;
- }
- public CompanyDTO getNewCompany() {
- return newCompany;
- }
- public void setNewCompany(CompanyDTO newCompany) {
- this.newCompany = newCompany;
- }
- public CompanyDTO getCurrentCompany() {
- return currentCompany;
- }
- public void setCurrentCompany(CompanyDTO currentCompany) {
- this.currentCompany = currentCompany;
- }
- public DocumentDTO getDocument() {
- return document;
- }
- public void setDocument(DocumentDTO document) {
- this.document = document;
- }
- public String getFilePath() {
- return filePath;
- }
- public void setFilePath(String filePath) {
- this.filePath = filePath;
- }
- public UserManager getUserManager() {
- return userManager;
- }
- public void setUserManager(UserManager userManager) {
- this.userManager = userManager;
- }
- public UploadManager getUploadManager() {
- return uploadManager;
- }
- public void setUploadManager(UploadManager uploadManager) {
- this.uploadManager = uploadManager;
- }
- public String getNewCharacteristicKey() {
- return newCharacteristicKey;
- }
- public void setNewCharacteristicKey(String newCharacteristicKey) {
- this.newCharacteristicKey = newCharacteristicKey;
- }
- public String getNewCharacteristicValue() {
- return newCharacteristicValue;
- }
- public void setNewCharacteristicValue(String newCharacteristicValue) {
- this.newCharacteristicValue = newCharacteristicValue;
- }
- public String getNewCharacteristicJSONString() {
- return newCharacteristicJSONString;
- }
- public void setNewCharacteristicJSONString(String newCharacteristicJSONString) {
- this.newCharacteristicJSONString = newCharacteristicJSONString;
- }
- public String getCurrentCharacteristicJSONString() {
- return currentCharacteristicJSONString;
- }
- public void setCurrentCharacteristicJSONString(String currentCharacteristicJSONString) {
- this.currentCharacteristicJSONString = currentCharacteristicJSONString;
- }
- }
Add Comment
Please, Sign In to add comment