19import java.util.List;
20import java.util.Properties;
21
22@Controller 23public class UserWeb { 24 private static final String PROPERTY_PASSWORD = "password"; 25 private static final String PROPERTY_LOGIN = "login"; 26 private static final String PROPERTY_NAME = "name"; 27 private static final String PROPERTY_SURNAME = "surname"; 28 private static final String PROPERTY_EMAIL = "email"; 29 private static final String PROPERTY_STUDENT_NUMBER = "studentNumber"; 30 protected static final String STATUS_SUCCESS = "Success"; 31 private static final 32 SessionFactory factory = new Configuration().configure().buildSessionFactory(); 33 UserHibernateController userHibControl = new UserHibernateController(factory); 34 35 @RequestMapping(value = "user/getUser", method = RequestMethod.GET) 36 @ResponseStatus(value = HttpStatus.OK) 37 @ResponseBody 38 public String getUserById(@RequestParam("id") int id){ 39 return userHibControl.getUserById(id).toString(); 40 } 41 42 @RequestMapping(value = "user/userLogin", method = RequestMethod.POST) 43 @ResponseStatus(value = HttpStatus.OK) 44 @ResponseBody 45 private String getCurrentUser(@RequestBody String request){ 46 Connection connection = DbUtils.connectToDb(); 47 Gson gson = new Gson(); 48 Properties properties = gson.fromJson(request, Properties.class); 49 User user = userHibControl.getAllUsers().stream().filter(c -> c.getLogin().equals(properties.getProperty(PROPERTY_LOGIN))).filter(c -> c.getPassword().equals(properties.getProperty(PROPERTY_PASSWORD))).findFirst().orElse(null); 50 GsonBuilder gsonBuilder = new GsonBuilder(); 51 if(user instanceof Moderator){ 52 gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer()).registerTypeAdapter(Moderator.class, new ModeratorSerializer()); 53 } 54 else gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer()).registerTypeAdapter(Student.class, new StudentSerializer()); 55 Gson parser = gsonBuilder.create(); 56 DbUtils.disconnectFromDb(connection); 57 if(user != null) 58 return parser.toJson(user); 59 else return null; 60 } 61 62 @RequestMapping(value = "user/createStudent", method = RequestMethod.POST) 63 @ResponseStatus(value = HttpStatus.OK) 64 @ResponseBody 65 public String createStudent(@RequestBody String request){ 66 Gson gson = new Gson(); 67 Properties properties = gson.fromJson(request, Properties.class); 68 Student student = new Student(properties.getProperty(PROPERTY_LOGIN), properties.getProperty(PROPERTY_PASSWORD), properties.getProperty(PROPERTY_NAME), properties.getProperty(PROPERTY_SURNAME), properties.getProperty(PROPERTY_EMAIL), properties.getProperty(PROPERTY_STUDENT_NUMBER)); 69 userHibControl.createUser(student); 70 return STATUS_SUCCESS; 71 } 72 73 @RequestMapping(value = "user/getAllUsers", method = RequestMethod.GET) 74 @ResponseBody 75 public String getAllUsers(){ 76 List<User> users = userHibControl.getAllUsers(); 77 return users.toString(); 78 } 79 80 @RequestMapping(value = "user/getAllStudents", method = RequestMethod.GET) 81 @ResponseBody 82 public String getAllStudents(){ 83 List<Student> students = userHibControl.getAllStudents(); 84 GsonBuilder gson = new GsonBuilder(); 85 gson.registerTypeAdapter(Student.class, new StudentSerializer()); 86 Gson parser = gson.create(); 87 return parser.toJson(students); 88 } 89 90 @RequestMapping(value = "user/getAllModerators", method = RequestMethod.GET) 91 @ResponseBody 92 public String getAllModerators(){ 93 List<Moderator> moderators = userHibControl.getAllModerators(); 94 GsonBuilder gson = new GsonBuilder(); 95 gson.registerTypeAdapter(Moderator.class, new ModeratorSerializer()); 96 Gson parser = gson.create(); 97 return parser.toJson(moderators); 98 } 99100 @RequestMapping(value = "user/deleteUser", method = RequestMethod.DELETE)101 @ResponseBody102 public String deleteUser(@RequestParam("id") int id){103 userHibControl.removeUser(id);104 return STATUS_SUCCESS;105 }106107 @RequestMapping(value = "user/createMod", method = RequestMethod.POST)108 @ResponseStatus(value = HttpStatus.OK)109 @ResponseBody110 public String createMod(@RequestBody String request){111 Gson gson = new Gson();112 Properties properties = gson.fromJson(request, Properties.class);113 Moderator mod = new Moderator(properties.getProperty(PROPERTY_LOGIN), properties.getProperty(PROPERTY_PASSWORD), properties.getProperty(PROPERTY_NAME), properties.getProperty(PROPERTY_SURNAME), properties.getProperty(PROPERTY_EMAIL));114 userHibControl.createUser(mod);115 return STATUS_SUCCESS;116 }117118 @RequestMapping(value = "user/updateStudent", method = RequestMethod.PUT)119 @ResponseBody120 public String updateStudent(@RequestBody String request){121 Gson gson = new Gson();122 Properties properties = gson.fromJson(request, Properties.class);123 Student student = new Student(properties.getProperty(PROPERTY_LOGIN), properties.getProperty(PROPERTY_PASSWORD), properties.getProperty(PROPERTY_NAME), properties.getProperty(PROPERTY_SURNAME), properties.getProperty(PROPERTY_EMAIL), properties.getProperty(PROPERTY_STUDENT_NUMBER));124 userHibControl.editUser(student);125 return STATUS_SUCCESS;126 }127128 @RequestMapping(value = "user/updateMod", method = RequestMethod.PUT)129 @ResponseStatus(value = HttpStatus.OK)130 @ResponseBody131 public String updateMod(@RequestBody String request){132 Gson gson = new Gson();133 Properties properties = gson.fromJson(request, Properties.class);134 Moderator mod = new Moderator(properties.getProperty(PROPERTY_LOGIN), properties.getProperty(PROPERTY_PASSWORD), properties.getProperty(PROPERTY_NAME), properties.getProperty(PROPERTY_SURNAME), properties.getProperty(PROPERTY_EMAIL));135 userHibControl.editUser(mod);136 return STATUS_SUCCESS;137 }138}
14
15import static com.coursemngsys.coursemanagementsystem.webcontrollers.UserWeb.STATUS_SUCCESS;
16
17@Controller18public class FolderWeb {19 SessionFactory factory = new Configuration().configure().buildSessionFactory();20 FolderHibernateController folderHibControl = new FolderHibernateController(factory);2122 @RequestMapping(value = "folder/getFolder", method = RequestMethod.GET)23 @ResponseStatus(value = HttpStatus.OK)24 @ResponseBody25 public String getCourseById(@RequestParam("id") int id){26 return folderHibControl.getFolderById(id).toString();27 }2829 @RequestMapping(value = "folder/createFolder", method = RequestMethod.POST)30 @ResponseStatus(value = HttpStatus.OK)31 @ResponseBody32 public String createFolder(@RequestBody String request){33 Gson gson = new Gson();34 Properties properties = gson.fromJson(request, Properties.class);35 Folder folder = new Folder(properties.getProperty("name"));36 folderHibControl.createFolder(folder);37 return STATUS_SUCCESS;38 }3940 @RequestMapping(value = "folder/getAllFolders", method = RequestMethod.GET)41 @ResponseBody42 public String getAllFolders(){43 List<Folder> folders = folderHibControl.getAllFolders();44 return folders.toString();45 }4647 @RequestMapping(value = "folder/deleteFolder", method = RequestMethod.DELETE)48 @ResponseBody49 public String deleteFolder(@RequestParam("id") int id){50 folderHibControl.removeFolder(id);51 return STATUS_SUCCESS;52 }5354 @RequestMapping(value = "folder/updateFolder", method = RequestMethod.PUT)55 @ResponseBody56 public String updateFolder(@RequestBody String request){57 Gson gson = new Gson();58 Properties properties = gson.fromJson(request, Properties.class);59 Folder folder = new Folder(properties.getProperty("name"));60 folderHibControl.editFolder(folder);61 return STATUS_SUCCESS;62 }63}
14
15import static com.coursemngsys.coursemanagementsystem.webcontrollers.UserWeb.STATUS_SUCCESS;
16
17@Controller18public class FileWeb {19 SessionFactory factory = new Configuration().configure().buildSessionFactory();20 FileHibernateController fileHibControl = new FileHibernateController(factory);2122 @RequestMapping(value = "file/getFile", method = RequestMethod.GET)23 @ResponseStatus(value = HttpStatus.OK)24 @ResponseBody25 public String getFileById(@RequestParam("id") int id){26 return fileHibControl.getFileById(id).toString();27 }2829 @RequestMapping(value = "file/createFile", method = RequestMethod.POST)30 @ResponseStatus(value = HttpStatus.OK)31 @ResponseBody32 public String createFile(@RequestBody String request){33 Gson gson = new Gson();34 Properties properties = gson.fromJson(request, Properties.class);35 File file = new File(properties.getProperty("name"),properties.getProperty("location"));36 fileHibControl.createFile(file);37 return STATUS_SUCCESS;38 }3940 @RequestMapping(value = "file/getAllFiles", method = RequestMethod.GET)41 @ResponseBody42 public String getAllFiles(){43 List<File> files = fileHibControl.getAllFiles();44 return files.toString();45 }4647 @RequestMapping(value = "file/deleteFile", method = RequestMethod.DELETE)48 @ResponseBody49 public String deleteFile(@RequestParam("id") int id){50 fileHibControl.removeFile(id);51 return STATUS_SUCCESS;52 }5354 @RequestMapping(value = "file/updateFile", method = RequestMethod.PUT)55 @ResponseBody56 public String updateFile(@RequestBody String request){57 Gson gson = new Gson();58 Properties properties = gson.fromJson(request, Properties.class);59 File file = new File(properties.getProperty("name"),properties.getProperty("location"));60 fileHibControl.editFile(file);61 return STATUS_SUCCESS;62 }63}
18
19import static com.coursemngsys.coursemanagementsystem.webcontrollers.UserWeb.STATUS_SUCCESS;
20
21@Controller22public class CourseWeb {23 SessionFactory factory = new Configuration().configure().buildSessionFactory();24 CourseHibernateController courseHibControl = new CourseHibernateController(factory);2526 @RequestMapping(value = "course/getCourse", method = RequestMethod.GET)27 @ResponseStatus(value = HttpStatus.OK)28 @ResponseBody29 public String getCourseById(@RequestParam("id") int id){30 Course course = courseHibControl.getCourseById(id);31 GsonBuilder gson = new GsonBuilder();32 gson.registerTypeAdapter(LocalDate.class, new LocalDateSerializer()).registerTypeAdapter(Course.class, new CourseSerializer());33 Gson parser = gson.create();34 return parser.toJson(course);35 }3637 @RequestMapping(value = "course/createCourse", method = RequestMethod.POST)38 @ResponseStatus(value = HttpStatus.OK)39 @ResponseBody40 public String createCourse(@RequestBody String request){41 Gson gson = new Gson();42 Properties properties = gson.fromJson(request, Properties.class);43 Course course = new Course(properties.getProperty("title"), properties.getProperty("description"), LocalDate.parse(properties.getProperty("startDate")),LocalDate.parse(properties.getProperty("endDate")));44 courseHibControl.createCourse(course);45 return STATUS_SUCCESS;46 }4748 @RequestMapping(value = "course/getAllCourses", method = RequestMethod.GET)49 @ResponseBody50 public String getAllCourses(){51 List<Course> courses = courseHibControl.getAllCourses();52 GsonBuilder gson = new GsonBuilder();53 gson.registerTypeAdapter(Course.class, new CourseSerializer());54 Gson parser = gson.create();55 return parser.toJson(courses);56 }5758 @RequestMapping(value = "course/deleteCourse", method = RequestMethod.DELETE)59 @ResponseBody60 public String deleteCourse(@RequestParam("id") int id){61 courseHibControl.removeCourse(id);62 return STATUS_SUCCESS;63 }6465 @RequestMapping(value = "course/updateCourse", method = RequestMethod.PUT)66 @ResponseBody67 public String updateCourse(@RequestBody String request){68 Gson gson = new Gson();69 Properties properties = gson.fromJson(request, Properties.class);70 Course course = new Course(Integer.parseInt(properties.getProperty("id")), properties.getProperty("title"), properties.getProperty("description"));71 //, LocalDate.parse(properties.getProperty("startDate")),LocalDate.parse(properties.getProperty("endDate"))72 courseHibControl.editCourse(course);73 return STATUS_SUCCESS;74 }75}
14import java.util.logging.Level;
15import java.util.logging.Logger;
16
17public class UserHibernateController { 18 private SessionFactory factory = null; 19 private Logger logger = null; 20 21 public UserHibernateController(SessionFactory factory) { 22 this.factory = factory; 23 this.logger = Logger.getLogger(UserHibernateController.class.getName()); 24 } 25 26 private Session getSession() { 27 return factory.openSession(); 28 } 29 30 public void createUser(User user){ 31 Session session = null; 32 try{ 33 session = getSession(); 34 Transaction tx = session.beginTransaction(); 35 session.save(user); 36 tx.commit(); 37 } catch (Exception e){ 38 e.printStackTrace(); 39 } finally { 40 if(session != null){ 41 session.close(); 42 } 43 } 44 } 45 46 public void editUser(User user){ 47 Session session = null; 48 try{ 49 session = getSession(); 50 Transaction tx = session.beginTransaction(); 51 session.merge(user); 52 tx.commit(); 53 } catch (Exception e){ 54 e.printStackTrace(); 55 } finally { 56 if(session != null){ 57 session.close(); 58 } 59 } 60 } 61 62 public void removeUser(int id){ 63 Session session = null; 64 User user = null; 65 try{ 66 session = getSession(); 67 Transaction tx = session.beginTransaction(); 68 user = session.getReference(User.class, id); 69 session.remove(user); 70 tx.commit(); 71 } catch (Exception e){ 72 logger.log(Level.INFO,"No such user by given ID"); 73 } finally { 74 if(session != null){ 75 session.close(); 76 } 77 } 78 } 79 80 public User getUserById(int id){ 81 Session session = null; 82 User user = null; 83 try{ 84 session = getSession(); 85 Transaction tx = session.beginTransaction(); 86 user = session.find(User.class, id); 87 tx.commit(); 88 } catch (Exception e){ 89 logger.log(Level.INFO,"No such user by given ID"); 90 } 91 return user; 92 } 93 94 public List<User> getAllUsers(){ 95 Session session = null; 96 try { 97 session = getSession(); 98 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery(); 99 query.select(query.from(User.class));100 Query q = session.createQuery(query);101 return q.getResultList();102 }catch (Exception e){103 e.printStackTrace();104 }finally {105 if(session != null){106 session.close();107 }108 }109 return Collections.emptyList();110 }111112 public List<Student> getAllStudents(){113 Session session = null;114 try {115 session = getSession();116 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery();117 query.select(query.from(Student.class));118 Query q = session.createQuery(query);119 return q.getResultList();120 }catch (Exception e){121 e.printStackTrace();122 }finally {123 if(session != null){124 session.close();125 }126 }127 return Collections.emptyList();128 }129130 public List<Moderator> getAllModerators(){131 Session session = null;132 try {133 session = getSession();134 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery();135 query.select(query.from(Moderator.class));136 Query q = session.createQuery(query);137 return q.getResultList();138 }catch (Exception e){139 e.printStackTrace();140 }finally {141 if(session != null){142 session.close();143 }144 }145 return Collections.emptyList();146 }147}
11import java.util.Collections;
12import java.util.List;
13
14public class FolderHibernateController { 15 private SessionFactory factory = null; 16 17 public FolderHibernateController(SessionFactory factory) { 18 this.factory = factory; 19 } 20 21 private Session getSession() { 22 return factory.openSession(); 23 } 24 25 public void createFolder(Folder folder){ 26 Session session = null; 27 try{ 28 session = getSession(); 29 Transaction tx = session.beginTransaction(); 30 session.save(folder); 31 tx.commit(); 32 } catch (Exception e){ 33 e.printStackTrace(); 34 } finally { 35 if(session != null){ 36 session.close(); 37 } 38 } 39 } 40 41 public void editFolder(Folder folder){ 42 Session session = null; 43 try{ 44 session = getSession(); 45 Transaction tx = session.beginTransaction(); 46 session.update(folder); 47 tx.commit(); 48 } catch (Exception e){ 49 e.printStackTrace(); 50 } finally { 51 if(session != null){ 52 session.close(); 53 } 54 } 55 } 56 57 public void removeFolder(int id){ 58 Session session = null; 59 try{ 60 session = getSession(); 61 Transaction tx = session.beginTransaction(); 62 Folder folder = null; 63 try{ 64 folder = session.getReference(Folder.class, id); 65 folder.getId(); 66 } catch (Exception e){ 67 System.out.println("No such folder by given ID"); 68 } 69 session.remove(folder); 70 tx.commit(); 71 } catch (Exception e){ 72 e.printStackTrace(); 73 } finally { 74 if(session != null){ 75 session.close(); 76 } 77 } 78 } 79 80 public Folder getFolderById(int id){ 81 Session session = null; 82 Folder folder = null; 83 try{ 84 session = getSession(); 85 Transaction tx = session.beginTransaction(); 86 folder = session.find(Folder.class, id); 87 folder.getId(); 88 tx.commit(); 89 } catch (Exception e){ 90 System.out.println("No such folder by given ID"); 91 } 92 return folder; 93 } 94 95 public List<Folder> getAllFolders(){ 96 Session session = null; 97 try { 98 session = getSession(); 99 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery();100 query.select(query.from(Folder.class));101 Query q = session.createQuery(query);102 return q.getResultList();103 }catch (Exception e){104 e.printStackTrace();105 }finally {106 if(session != null){107 session.close();108 }109 }110 return Collections.emptyList();111 }112113}
11import java.util.Collections;
12import java.util.List;
13
14public class FileHibernateController { 15 private SessionFactory factory = null; 16 17 public FileHibernateController(SessionFactory factory) { 18 this.factory = factory; 19 } 20 21 private Session getSession() { 22 return factory.openSession(); 23 } 24 25 public void createFile(File file){ 26 Session session = null; 27 try{ 28 session = getSession(); 29 Transaction tx = session.beginTransaction(); 30 session.save(file); 31 tx.commit(); 32 } catch (Exception e){ 33 e.printStackTrace(); 34 } finally { 35 if(session != null){ 36 session.close(); 37 } 38 } 39 } 40 41 public void editFile(File file){ 42 Session session = null; 43 try{ 44 session = getSession(); 45 Transaction tx = session.beginTransaction(); 46 session.update(file); 47 tx.commit(); 48 } catch (Exception e){ 49 e.printStackTrace(); 50 } finally { 51 if(session != null){ 52 session.close(); 53 } 54 } 55 } 56 57 public void removeFile(int id){ 58 Session session = null; 59 try{ 60 session = getSession(); 61 Transaction tx = session.beginTransaction(); 62 File file = null; 63 try{ 64 file = session.getReference(File.class, id); 65 file.getId(); 66 } catch (Exception e){ 67 System.out.println("No such user by given ID"); 68 } 69 session.remove(file); 70 tx.commit(); 71 } catch (Exception e){ 72 e.printStackTrace(); 73 } finally { 74 if(session != null){ 75 session.close(); 76 } 77 } 78 } 79 80 public File getFileById(int id){ 81 Session session = null; 82 File file = null; 83 try{ 84 session = getSession(); 85 Transaction tx = session.beginTransaction(); 86 file = session.find(File.class, id); 87 file.getId(); 88 tx.commit(); 89 } catch (Exception e){ 90 System.out.println("No such user by given ID"); 91 } 92 return file; 93 } 94 95 public List<File> getAllFiles(){ 96 Session session = null; 97 try { 98 session = getSession(); 99 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery();100 query.select(query.from(File.class));101 Query q = session.createQuery(query);102 return q.getResultList();103 }catch (Exception e){104 e.printStackTrace();105 }finally {106 if(session != null){107 session.close();108 }109 }110 return Collections.emptyList();111 }112113}
10import java.util.Collections;
11import java.util.List;
12
13public class CourseHibernateController { 14 private SessionFactory factory; 15 16 17 public CourseHibernateController(SessionFactory factory) { 18 this.factory = factory; 19 } 20 21 private Session getSession() { 22 return factory.openSession(); 23 } 24 25 public void createCourse(Course course){ 26 Session session = null; 27 try{ 28 session = getSession(); 29 Transaction transaction = session.beginTransaction(); 30 31 session.save(course); 32 transaction.commit(); 33 } catch (Exception e){ 34 e.printStackTrace(); 35 } finally { 36 if(session != null){ 37 session.close(); 38 } 39 } 40 } 41 42 public void editCourse(Course course){ 43 Session session = null; 44 try{ 45 session = getSession(); 46 Transaction tx = session.beginTransaction(); 47 session.merge(course); 48 tx.commit(); 49 } catch (Exception e){ 50 e.printStackTrace(); 51 } finally { 52 if(session != null){ 53 session.close(); 54 } 55 } 56 } 57 58 public void removeCourse(int id){ 59 Session session = null; 60 try{ 61 session = getSession(); 62 Transaction tx = session.beginTransaction(); 63 Course course = null; 64 course = getCourseById(id); 65 session.remove(course); 66 tx.commit(); 67 } catch (Exception e){ 68 e.printStackTrace(); 69 } finally { 70 if(session != null){ 71 session.close(); 72 } 73 } 74 } 75 76 public Course getCourseById(int id){ 77 Session session = null; 78 Course course = null; 79 try{ 80 session = getSession(); 81 Transaction tx = session.beginTransaction(); 82 course = session.find(Course.class, id); 83 course.getId(); 84 tx.commit(); 85 } catch (Exception e){ 86 System.out.println("No such course by given ID"); 87 } 88 return course; 89 } 90 91 public List<Course> getAllCourses(){ 92 Session session = null; 93 try { 94 session = getSession(); 95 CriteriaQuery<Object> query = session.getCriteriaBuilder().createQuery(); 96 query.select(query.from(Course.class)); 97 Query q = session.createQuery(query); 98 return q.getResultList(); 99 }catch (Exception e){100 e.printStackTrace();101 }finally {102 if(session != null){103 session.close();104 }105 }106 return Collections.emptyList();107 }108}
26import java.util.Objects;
27import java.util.ResourceBundle;
28
29public class SignUpForm { 30 @FXML 31 private TextField loginField; 32 @FXML 33 private PasswordField passwordField; 34 @FXML 35 private RadioButton studentRadioButton; 36 @FXML 37 private RadioButton moderatorRadioButton; 38 @FXML 39 private TextField nameField; 40 @FXML 41 private TextField surnameField; 42 @FXML 43 private TextField emailField; 44 @FXML 45 private TextField repeatedPasswordField; 46 @FXML 47 private ToggleGroup userType; 48 private Connection connection; 49 50 public void createUser(ActionEvent actionEvent) throws IOException{ 51 DbUtils.createDb(); 52 connection = DbUtils.connectToDb(); 53 SessionFactory factory = new Configuration().configure().buildSessionFactory(); 54 UserHibernateController userHibControl = new UserHibernateController(factory); 55 56 if(loginField.getText() == "" || passwordField.getText() == "" || repeatedPasswordField.getText() == "" || nameField.getText() == "" || surnameField.getText() == "" || emailField.getText() == "") { 57 Alert.alertError("Some fields are empty."); 58 } else if(arePasswordsMatching(passwordField.getText(), repeatedPasswordField.getText())){ 59 if (studentRadioButton.isSelected()) { 60 String studentNumber = generateStudentNumber(); 61 Student student = new Student(loginField.getText(),passwordField.getText(),nameField.getText(),surnameField.getText(),emailField.getText(),studentNumber); 62 userHibControl.createUser(student); 63 } else { 64 Moderator moderator = new Moderator(loginField.getText(),passwordField.getText(),nameField.getText(),surnameField.getText(),emailField.getText()); 65 userHibControl.createUser(moderator); 66 } 67 Alert.alertMessage("User created successfully."); 68 returnToPrevious(); 69 } 70 else { 71 Alert.alertError("Passwords do not match."); 72 } 73 DbUtils.disconnectFromDb(connection); 74 } 75 76 private void returnToPrevious() throws IOException { 77 FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("login-window.fxml")); 78 Parent root = fxmlLoader.load(); 79 Scene scene = new Scene(root); 80 81 Stage stage = (Stage) loginField.getScene().getWindow(); 82 stage.setScene(scene); 83 stage.show(); 84 } 85 86 private String generateStudentNumber(){ 87 int min = 1000; 88 int max = 9999; 89 int randomStudentNr = (int)Math.floor(Math.random()*(max-min+1)+min); 90 return Year.now().getValue() + String.valueOf(randomStudentNr); 91 } 92 93 private boolean arePasswordsMatching(String password, String repeatedPassword){ 94 if(Objects.equals(password, repeatedPassword)){ 95 return true; 96 } 97 return false; 98 } 99100}
23import java.net.URL;
24import java.util.ResourceBundle;
25
26public class ModeratorCoursesWindow { 27 @FXML 28 private ListView<Course> coursesList; 29 @FXML 30 private TextField name; 31 @FXML 32 private TextField surname; 33 @FXML 34 private PasswordField password; 35 @FXML 36 private TextField email; 37 private Moderator moderator; 38 private SessionFactory factory = null; 39 40 public void getModerator(Moderator mod) { 41 this.moderator = mod; 42 fillList(); 43 } 44 45 public void fillList() { 46 ObservableList<Course> courses = FXCollections.observableArrayList(moderator.getModeratedCourses()); 47 coursesList.setItems(courses); 48 } 49 50 public void openNewCourseForm(ActionEvent actionEvent) throws IOException { 51 52 FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("new-course-form.fxml")); 53 Parent root = fxmlLoader.load(); 54 55 NewCourseForm newCourseForm = fxmlLoader.getController(); 56 newCourseForm.getData(moderator, this); 57 Scene scene = new Scene(root); 58 59 Stage stage = new Stage(); 60 stage.setScene(scene); 61 stage.show(); 62 } 63 64 public void setUserInfo(Event event) { 65 name.setText(moderator.getName()); 66 surname.setText(moderator.getSurname()); 67 email.setText(moderator.getEmail()); 68 } 69 70 public void updateUser(ActionEvent actionEvent) { 71 factory = new Configuration().configure().buildSessionFactory(); 72 UserHibernateController userHibControl = new UserHibernateController(factory); 73 moderator.setEmail(email.getText()); 74 if(!password.getText().equals("")) { 75 moderator.setPassword(password.getText()); 76 } 77 userHibControl.editUser(moderator); 78 Alert.alertMessage("User updated successfully."); 79 } 80 81 public void deleteUser(ActionEvent actionEvent) { 82 factory = new Configuration().configure().buildSessionFactory(); 83 UserHibernateController userHibControl = new UserHibernateController(factory); 84 userHibControl.removeUser(moderator.getId()); 85 Alert.alertMessage("User deleted successfully."); 86 } 87 88 public void selectCourse(MouseEvent mouseEvent) throws IOException { 89 FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("selected-course-window.fxml")); 90 Parent root = fxmlLoader.load(); 91 92 SelectedCourseWindow selectedCourseWindow = fxmlLoader.getController(); 93 selectedCourseWindow.getData(coursesList.getSelectionModel().getSelectedItem(), moderator, this); 94 Scene scene = new Scene(root); 95 96 Stage stage = new Stage(); 97 stage.setScene(scene); 98 stage.show(); 99 }100}
This class does not have any documentation.
Consider adding a documentation comment to explain its use.
While it may seem like the functionality of a class is perfectly obvious, any consumers of your API may not be able to pick up on certain details.
Bad Practice
Consider a case where the class given below can be instantiated and provides certain functionalities within each instance in a thread-safe manner, perhaps it is a rest API client.
If there is no documentation comment on the class, it is not immediately obvious that the class is thread safe. Thus, multiple instances of the class may be created to perform operations concurrently, using up both memory as well as OS resources like sockets. If it were known from the beginning that the class were thread safe, the user would not need to create unnecessary extra instances of SomeClass
.
class SomeClass {
// ...
}
Recommended
Make sure to add useful information regarding the usage or implementation of a particular declaration, so that anything about it which can't be understood from the name or some other cue is correctly conveyed.
/**
* Instances of this class are used to perform xyz action.
*
* This class is thread safe and the same instance can be used over multiple threads.
*/
class SomeClass {
// ...
}
Exceptions
This issue will not be reported for model entity classes. If there is any non-obvious behavior associated with a particular class however, do consider documenting it.