Guest User

Untitled

a guest
Dec 30th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.32 KB | None | 0 0
  1. package web;
  2.  
  3. import dtos.ClientDTO;
  4. import dtos.CompanyDTO;
  5. import dtos.ConfigurationDTO;
  6. import dtos.CourseDTO;
  7. import dtos.DocumentDTO;
  8. import dtos.SoftwareDTO;
  9. import dtos.StudentDTO;
  10. import dtos.TemplateDTO;
  11. import java.io.Serializable;
  12. import java.util.Collection;
  13. import java.util.List;
  14. import javax.faces.application.Application;
  15. import java.util.logging.Logger;
  16. import javax.faces.bean.ManagedBean;
  17. import javax.faces.bean.ManagedProperty;
  18. import javax.faces.bean.SessionScoped;
  19. import javax.faces.component.UIComponent;
  20. import javax.faces.component.UIParameter;
  21. import javax.faces.context.FacesContext;
  22. import javax.faces.event.ActionEvent;
  23. import javax.ws.rs.client.Client;
  24. import javax.ws.rs.client.ClientBuilder;
  25. import javax.ws.rs.client.Entity;
  26. import javax.ws.rs.core.GenericType;
  27. import javax.ws.rs.core.MediaType;
  28. import javax.ws.rs.core.Response;
  29. import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
  30.  
  31. @ManagedBean
  32. @SessionScoped
  33. public class AdministratorManager implements Serializable {
  34.     private final Logger logger = Logger.getLogger("web.AdministratorManager");
  35.     private final String baseUri = "http://localhost:8080/AcademicManagement-war/webapi";
  36.        
  37.     private Client client;
  38.            
  39.     private UIComponent component;
  40.    
  41.     private StudentDTO newStudent;
  42.     private StudentDTO currentStudent;
  43.    
  44.     private ConfigurationDTO newConfiguration;
  45.     private ConfigurationDTO currentConfiguration;
  46.    
  47.     private TemplateDTO newTemplate;
  48.     private TemplateDTO currentTemplate;
  49.    
  50.     private ClientDTO newClient;
  51.     private ClientDTO currentClient;
  52.    
  53.     private SoftwareDTO newSoftware;
  54.     private SoftwareDTO currentSoftware;
  55.    
  56.     private CompanyDTO newCompany;
  57.     private CompanyDTO currentCompany;
  58.    
  59.     private DocumentDTO document;
  60.     private String filePath;
  61.    
  62.     private String newCharacteristicJSONString;
  63.     private String currentCharacteristicJSONString;
  64.    
  65.     private String newCharacteristicKey; //IE: "Version"
  66.     private String newCharacteristicValue; //IE: "Alpha 1.0"
  67.    
  68.     @ManagedProperty("#{userManager}")
  69.     UserManager userManager;
  70.  
  71.     @ManagedProperty(value = "#{uploadManager}")
  72.     private UploadManager uploadManager;
  73.  
  74.     public AdministratorManager() {
  75.         newStudent = new StudentDTO();
  76.         currentStudent = new StudentDTO();
  77.        
  78.         newConfiguration = new ConfigurationDTO();
  79.         currentConfiguration = new ConfigurationDTO();
  80.        
  81.         newTemplate = new TemplateDTO();
  82.         currentTemplate = new TemplateDTO();
  83.        
  84.         newClient = new ClientDTO();
  85.         currentClient = new ClientDTO();
  86.        
  87.         newSoftware = new SoftwareDTO();
  88.         currentSoftware = new SoftwareDTO();
  89.  
  90.         newCompany = new CompanyDTO();
  91.         currentCompany = new CompanyDTO();
  92.        
  93.         //Not related to USER clients!!
  94.         client = ClientBuilder.newClient();
  95.     }
  96.  
  97.     private void addHeaderBASIC() {
  98.         FacesContext context = FacesContext.getCurrentInstance();
  99.         Application app = context.getApplication();
  100.         UserManager userManager = app.evaluateExpressionGet(context, "#{userManager}", UserManager.class);
  101.        
  102.         String username = userManager.getUsername();
  103.         String password = userManager.getPassword();
  104.        
  105.         HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(username, password);
  106.         client.register(feature);
  107.     }
  108.    
  109.     public List<StudentDTO> getAllStudents() {
  110.         try {
  111.             addHeaderBASIC();
  112.             return client.target(baseUri)
  113.                         .path("students")
  114.                         .request(MediaType.APPLICATION_XML)
  115.                         .get(new GenericType<List<StudentDTO>>() {});
  116.         } catch (Exception e) {
  117.             logger.warning("Unexpected error. Try again latter!");
  118.             return null;
  119.         }
  120.     }
  121.    
  122.     private <T> Entity<T> asXML(T instance) {
  123.         return Entity.entity(instance, MediaType.APPLICATION_XML);
  124.     }
  125.    
  126.     public String createStudent() {
  127.         try {
  128.             addHeaderBASIC();
  129.             Response response = client.target(baseUri)
  130.                                     .path("students")
  131.                                     .request(MediaType.APPLICATION_XML)
  132.                                     .post(asXML(newStudent));
  133.            
  134.             if (response.getStatus() != 200) {
  135.                 // error
  136.             }
  137.            
  138.             newStudent.clear();
  139.             return "/admin/index?faces-redirect=true";
  140.         } catch (Exception e) {
  141.             logger.warning(e.getMessage());
  142.         }
  143.         return "/admin/students/create";
  144.     }
  145.    
  146.     public String updateStudent() {
  147.         try {
  148.             addHeaderBASIC();
  149.             Response response = client.target(baseUri)
  150.                                     .path("students")
  151.                                     .request(MediaType.APPLICATION_XML)
  152.                                     .put(asXML(currentStudent));
  153.            
  154.             if (response.getStatus() != 200) {
  155.                 // error
  156.             }
  157.            
  158.             currentStudent.clear();
  159.             return "/admin/index?faces-redirect=true";
  160.         } catch (Exception e) {
  161.             logger.warning(e.getMessage());
  162.         }
  163.         return "/admin/students/update";
  164.     }
  165.  
  166.     public String removeStudent(ActionEvent event) {
  167.         try {
  168.             addHeaderBASIC();
  169.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteStudentId");
  170.             String username = param.getValue().toString();
  171.            
  172.             client.target(baseUri).path("students/" + username).request().delete();
  173.             return "/admin/index?faces-redirect=true";
  174.         } catch (Exception e) {
  175.             logger.warning(e.getMessage());
  176.             return "/admin/index";
  177.         }
  178.     }
  179.    
  180.     public String removeClient(ActionEvent event) {
  181.         try {
  182.             addHeaderBASIC();
  183.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteClientId");
  184.             String username = param.getValue().toString();
  185.            
  186.             client.target(baseUri).path("clients/" + username).request().delete();
  187.             return "/admin/clients/list?faces-redirect=true";
  188.         } catch (Exception e) {
  189.             logger.warning(e.getMessage());
  190.             return "/admin/clients/list";
  191.         }
  192.     }
  193.    
  194.     public String removeConfiguration(ActionEvent event) {
  195.         try {
  196.             addHeaderBASIC();
  197.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteConfigurationId");
  198.             String code = param.getValue().toString();
  199.            
  200.             client.target(baseUri).path("configurations/" + code).request().delete();
  201.             return "/admin/configurations/list?faces-redirect=true";
  202.         } catch (Exception e) {
  203.             logger.warning(e.getMessage());
  204.             return "/admin/configurations/list";
  205.         }
  206.     }
  207.    
  208.     public String removeTemplate(ActionEvent event) {
  209.         try {
  210.             addHeaderBASIC();
  211.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteTemplateId");
  212.             String code = param.getValue().toString();
  213.            
  214.             client.target(baseUri).path("templates/" + code).request().delete();
  215.             return "/admin/templates/list?faces-redirect=true";
  216.         } catch (Exception e) {
  217.             logger.warning(e.getMessage());
  218.             return "/admin/templates/list";
  219.         }
  220.     }
  221.    
  222.     public String removeCompany(ActionEvent event) {
  223.         try {
  224.             addHeaderBASIC();
  225.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCompanyId");
  226.             String code = param.getValue().toString();
  227.            
  228.             client.target(baseUri).path("companys/" + code).request().delete();
  229.             return "/admin/companys/list?faces-redirect=true";
  230.         } catch (Exception e) {
  231.             logger.warning(e.getMessage());
  232.             return "/admin/companys/list";
  233.         }
  234.     }
  235.    
  236.     public String removeTemplateCharacteristic(ActionEvent event) {
  237.         try {
  238.             addHeaderBASIC();
  239.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCharacteristicId");
  240.             String key = param.getValue().toString();
  241.            
  242.            
  243.             return "/admin/templates/details?faces-redirect=true";
  244.         } catch (Exception e) {
  245.             logger.warning(e.getMessage());
  246.             return "/admin/templates/list";
  247.         }
  248.     }
  249.    
  250.     public String removeCourse(ActionEvent event){
  251.         try {
  252.            
  253.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteCourseId");
  254.             Integer code = (Integer) param.getValue();
  255.            
  256.             addHeaderBASIC();
  257.            
  258.             client.target(baseUri).path("courses/" + code).request().delete();
  259.            
  260.             return "/admin/index?faces-redirect=true";
  261.         } catch (Exception e) {
  262.             logger.warning(e.getMessage());
  263.             return "/admin/index";
  264.         }
  265.     }
  266.    
  267.     public String removeSoftware(ActionEvent event){
  268.         try {
  269.            
  270.             UIParameter param = (UIParameter) event.getComponent().findComponent("deleteSoftwareId");
  271.             Integer code = (Integer) param.getValue();
  272.            
  273.             addHeaderBASIC();
  274.            
  275.             client.target(baseUri).path("softwares/" + code).request().delete();
  276.            
  277.             return "/admin/softwares/list?faces-redirect=true";
  278.         } catch (Exception e) {
  279.             logger.warning(e.getMessage());
  280.             return "/admin/softwares/list";
  281.         }
  282.     }
  283.  
  284.     public List<CourseDTO> getAllCourses() {
  285.         try {
  286.             addHeaderBASIC();
  287.             return client.target(baseUri)
  288.                         .path("courses")
  289.                         .request(MediaType.APPLICATION_XML)
  290.                         .get(new GenericType<List<CourseDTO>>() {});
  291.         } catch (Exception e) {
  292.             logger.warning(e.getMessage());
  293.             return null;
  294.         }
  295.     }
  296.    
  297.     public List<SoftwareDTO> getAllSoftwares() {
  298.         try {
  299.             addHeaderBASIC();
  300.             return client.target(baseUri)
  301.                         .path("softwares")
  302.                         .request(MediaType.APPLICATION_XML)
  303.                         .get(new GenericType<List<SoftwareDTO>>() {});
  304.         } catch (Exception e) {
  305.             logger.warning(e.getMessage());
  306.             return null;
  307.         }
  308.     }
  309.    
  310.     public List<CompanyDTO> getAllCompanies(){
  311.         try {
  312.             addHeaderBASIC();
  313.             return client.target(baseUri)
  314.                         .path("companys")
  315.                         .request(MediaType.APPLICATION_XML)
  316.                         .get(new GenericType<List<CompanyDTO>>() {});
  317.         } catch (Exception e) {
  318.             logger.warning("Unexpected error. Try again latter!");
  319.             return null;
  320.         }
  321.     }
  322.    
  323.     public List<TemplateDTO> getAllTemplates(){
  324.         try {
  325.             addHeaderBASIC();
  326.             return client.target(baseUri)
  327.                         .path("templates")
  328.                         .request(MediaType.APPLICATION_XML)
  329.                         .get(new GenericType<List<TemplateDTO>>() {});
  330.         } catch (Exception e) {
  331.             logger.warning("Unexpected error. Try again latter!");
  332.             return null;
  333.         }
  334.     }
  335.  
  336.     public List<ConfigurationDTO> getAllConfigurations(){
  337.         try {
  338.             addHeaderBASIC();
  339.             return client.target(baseUri)
  340.                         .path("configurations")
  341.                         .request(MediaType.APPLICATION_XML)
  342.                         .get(new GenericType<List<ConfigurationDTO>>() {});
  343.         } catch (Exception e) {
  344.             logger.warning("Unexpected error. Try again latter!");
  345.             return null;
  346.         }
  347.     }
  348.    
  349.     public List<ClientDTO> getAllClients(){
  350.         try {
  351.             addHeaderBASIC();
  352.             return client.target(baseUri)
  353.                         .path("clients")
  354.                         .request(MediaType.APPLICATION_XML)
  355.                         .get(new GenericType<List<ClientDTO>>() {});
  356.         } catch (Exception e) {
  357.             logger.warning("Unexpected error. Try again latter!");
  358.             return null;
  359.         }
  360.     }
  361.    
  362.     public String uploadDocument() {
  363.         try {
  364.             document = new DocumentDTO(uploadManager.getCompletePathFile(), uploadManager.getFilename(), uploadManager.getFile().getContentType());
  365.  
  366.             client.target(baseUri)
  367.                     .path("/configurations/addDocument")
  368.                     .path(currentConfiguration.getCode().toString())
  369.                     .request(MediaType.APPLICATION_XML)
  370.                     .put(Entity.xml(document));
  371.  
  372.         } catch (Exception e) {
  373.             FacesExceptionHandler.handleException(e, "Unexpected error! Try again latter!", logger);
  374.             return null;
  375.         }
  376.  
  377.         return "/admin/configurations/details?faces-redirect=true";
  378.     }
  379.  
  380.     public Collection<DocumentDTO> getDocuments() {
  381.         Collection<DocumentDTO> documents = null;
  382.         try {
  383.             documents = client.target(baseUri)
  384.                     .path("/configurations/documents")
  385.                     .path(currentConfiguration.getCode().toString())
  386.                     .request(MediaType.APPLICATION_XML)
  387.                     .get(new GenericType<Collection<DocumentDTO>>() {
  388.                     });
  389.         } catch (Exception e) {
  390.             FacesExceptionHandler.handleException(e, "Unexpected error! Try again latter!", logger);
  391.         }
  392.         return documents;
  393.     }
  394.  
  395.     public UIComponent getComponent() {
  396.         return component;
  397.     }
  398.  
  399.     public void setComponent(UIComponent component) {
  400.         this.component = component;
  401.     }
  402.  
  403.     public StudentDTO getNewStudent() {
  404.         return newStudent;
  405.     }
  406.  
  407.     public void setNewStudent(StudentDTO newStudent) {
  408.         this.newStudent = newStudent;
  409.     }
  410.  
  411.     public StudentDTO getCurrentStudent() {
  412.         return currentStudent;
  413.     }
  414.  
  415.     public void setCurrentStudent(StudentDTO currentStudent) {
  416.         this.currentStudent = currentStudent;
  417.     }
  418.  
  419.     public Client getClient() {
  420.         return client;
  421.     }
  422.  
  423.     public void setClient(Client client) {
  424.         this.client = client;
  425.     }
  426.  
  427.     public ConfigurationDTO getNewConfiguration() {
  428.         return newConfiguration;
  429.     }
  430.  
  431.     public void setNewConfiguration(ConfigurationDTO newConfiguration) {
  432.         this.newConfiguration = newConfiguration;
  433.     }
  434.  
  435.     public ConfigurationDTO getCurrentConfiguration() {
  436.         return currentConfiguration;
  437.     }
  438.  
  439.     public void setCurrentConfiguration(ConfigurationDTO currentConfiguration) {
  440.         this.currentConfiguration = currentConfiguration;
  441.     }
  442.  
  443.     public TemplateDTO getNewTemplate() {
  444.         return newTemplate;
  445.     }
  446.  
  447.     public void setNewTemplate(TemplateDTO newTemplate) {
  448.         this.newTemplate = newTemplate;
  449.     }
  450.  
  451.     public TemplateDTO getCurrentTemplate() {
  452.         return currentTemplate;
  453.     }
  454.  
  455.     public void setCurrentTemplate(TemplateDTO currentTemplate) {
  456.         this.currentTemplate = currentTemplate;
  457.     }
  458.  
  459.     public ClientDTO getNewClient() {
  460.         return newClient;
  461.     }
  462.  
  463.     public void setNewClient(ClientDTO newClient) {
  464.         this.newClient = newClient;
  465.     }
  466.  
  467.     public ClientDTO getCurrentClient() {
  468.         return currentClient;
  469.     }
  470.  
  471.     public void setCurrentClient(ClientDTO currentClient) {
  472.         this.currentClient = currentClient;
  473.     }
  474.  
  475.     public SoftwareDTO getNewSoftware() {
  476.         return newSoftware;
  477.     }
  478.  
  479.     public void setNewSoftware(SoftwareDTO newSoftware) {
  480.         this.newSoftware = newSoftware;
  481.     }
  482.  
  483.     public SoftwareDTO getCurrentSoftware() {
  484.         return currentSoftware;
  485.     }
  486.  
  487.     public void setCurrentSoftware(SoftwareDTO currentSoftware) {
  488.         this.currentSoftware = currentSoftware;
  489.     }
  490.  
  491.     public CompanyDTO getNewCompany() {
  492.         return newCompany;
  493.     }
  494.  
  495.     public void setNewCompany(CompanyDTO newCompany) {
  496.         this.newCompany = newCompany;
  497.     }
  498.  
  499.     public CompanyDTO getCurrentCompany() {
  500.         return currentCompany;
  501.     }
  502.  
  503.     public void setCurrentCompany(CompanyDTO currentCompany) {
  504.         this.currentCompany = currentCompany;
  505.     }
  506.  
  507.     public DocumentDTO getDocument() {
  508.         return document;
  509.     }
  510.  
  511.     public void setDocument(DocumentDTO document) {
  512.         this.document = document;
  513.     }
  514.  
  515.     public String getFilePath() {
  516.         return filePath;
  517.     }
  518.  
  519.     public void setFilePath(String filePath) {
  520.         this.filePath = filePath;
  521.     }
  522.  
  523.     public UserManager getUserManager() {
  524.         return userManager;
  525.     }
  526.  
  527.     public void setUserManager(UserManager userManager) {
  528.         this.userManager = userManager;
  529.     }
  530.  
  531.     public UploadManager getUploadManager() {
  532.         return uploadManager;
  533.     }
  534.  
  535.     public void setUploadManager(UploadManager uploadManager) {
  536.         this.uploadManager = uploadManager;
  537.     }
  538.  
  539.     public String getNewCharacteristicKey() {
  540.         return newCharacteristicKey;
  541.     }
  542.  
  543.     public void setNewCharacteristicKey(String newCharacteristicKey) {
  544.         this.newCharacteristicKey = newCharacteristicKey;
  545.     }
  546.  
  547.     public String getNewCharacteristicValue() {
  548.         return newCharacteristicValue;
  549.     }
  550.  
  551.     public void setNewCharacteristicValue(String newCharacteristicValue) {
  552.         this.newCharacteristicValue = newCharacteristicValue;
  553.     }
  554.  
  555.     public String getNewCharacteristicJSONString() {
  556.         return newCharacteristicJSONString;
  557.     }
  558.  
  559.     public void setNewCharacteristicJSONString(String newCharacteristicJSONString) {
  560.         this.newCharacteristicJSONString = newCharacteristicJSONString;
  561.     }
  562.  
  563.     public String getCurrentCharacteristicJSONString() {
  564.         return currentCharacteristicJSONString;
  565.     }
  566.  
  567.     public void setCurrentCharacteristicJSONString(String currentCharacteristicJSONString) {
  568.         this.currentCharacteristicJSONString = currentCharacteristicJSONString;
  569.     }
  570.    
  571.    
  572. }
Add Comment
Please, Sign In to add comment