Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.50 KB | None | 0 0
  1. package project.controller;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. import java.util.Date;
  13. import java.util.List;
  14. import java.util.PropertyResourceBundle;
  15. import java.util.ResourceBundle;
  16.  
  17. import javax.servlet.RequestDispatcher;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.annotation.MultipartConfig;
  20. import javax.servlet.annotation.WebServlet;
  21. import javax.servlet.http.HttpServlet;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.servlet.http.HttpSession;
  25. import javax.servlet.http.Part;
  26.  
  27. import org.json.JSONArray;
  28. //import org.apache.tomcat.util.http.fileupload.UploadContext;
  29. import org.json.JSONObject;
  30.  
  31. import com.google.gson.Gson;
  32. import com.google.gson.GsonBuilder;
  33. import com.google.gson.JsonArray;
  34. import com.google.gson.JsonElement;
  35. import com.google.gson.JsonObject;
  36.  
  37. import beans.BlogBean;
  38. import beans.ConnectionsBean;
  39. import beans.FileBean;
  40. import beans.StudentBean;
  41. import daoimpl.BlogDaoImpl;
  42. import daoimpl.ConnectionDaoImpl;
  43. import daoimpl.FacultyDaoImpl;
  44. import daoimpl.FileDaoImpl;
  45. import daoimpl.LikeDaoImpl;
  46. import daoimpl.PostDaoImpl;
  47. import daoimpl.StudentDaoImpl;
  48. import dto.Blog;
  49. import dto.Event;
  50. import dto.Faculty;
  51. import dto.Like;
  52. import dto.News;
  53. import dto.Post;
  54. import dto.StudentPost;
  55. import service.EventReader;
  56. import service.NewsReader;
  57. import dto.Student;
  58.  
  59. @WebServlet("/Controller")
  60. @MultipartConfig
  61. public class Controller extends HttpServlet {
  62.  
  63. private static final long serialVersionUID = 1L;
  64.  
  65. private StudentDaoImpl sdi = new StudentDaoImpl();
  66. private FacultyDaoImpl fdi = new FacultyDaoImpl();
  67. private PostDaoImpl pdi = new PostDaoImpl();
  68. private LikeDaoImpl ldi = new LikeDaoImpl();
  69. private FileDaoImpl fileDaoImpl = new FileDaoImpl();
  70. private BlogDaoImpl bdi = new BlogDaoImpl();
  71. private ConnectionDaoImpl cdi = new ConnectionDaoImpl();
  72. private EventReader er = new EventReader();
  73. private NewsReader nr = new NewsReader();
  74. int i = 0;
  75.  
  76. @Override
  77. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  78.  
  79. req.setCharacterEncoding("UTF-8");
  80. resp.setContentType("text/html; charset=UTF-8");
  81. resp.setCharacterEncoding("UTF-8");
  82.  
  83. String address = "/login.jsp";
  84. String action = req.getParameter("action");
  85. HttpSession session = req.getSession();
  86.  
  87. StudentBean sb2 = (StudentBean) session.getAttribute("studentBean");
  88.  
  89.  
  90. if(sb2!=null) {
  91.  
  92. if (action == null || "".equals(action) || "login".equals(action)) {
  93.  
  94. address = "/WEB-INF/pages/main.jsp";
  95. FileBean fileBean = new FileBean();
  96. req.setAttribute("fileBean", fileBean);
  97. BlogBean blogBean = new BlogBean();
  98. req.setAttribute("blogBean", blogBean);
  99.  
  100. }
  101. else if("connNtf".equals(action)) {
  102.  
  103. resp.getOutputStream().println(cdi.getNumOfNewReq(sb2.getStudent().getId()));
  104. return;
  105. }
  106. else if("get_posts".equals(action)) {
  107.  
  108. List<Post> posts = getAllPosts(sb2.getStudent());
  109. List<JsonElement> jsonElements = new ArrayList<JsonElement>();
  110. Gson gson = new Gson();
  111.  
  112. for(Post post : posts) {
  113. JsonElement je = gson.toJsonTree(post);
  114.  
  115. if(post instanceof StudentPost) {
  116.  
  117. StudentPost sp = (StudentPost) post;
  118. Student s = sdi.getStudentyById(sp.getStudentId());
  119. String url = s.getImage();
  120. if(url == null || "".equals(url))
  121. url = "https://image.flaticon.com/icons/svg/17/17004.svg";
  122. else {
  123. url = "/UniShare" + url;
  124. }
  125. je.getAsJsonObject().addProperty("user_img", url);
  126. je.getAsJsonObject().addProperty("student_name", s.getName() + " " + s.getSurname());
  127. }
  128.  
  129. jsonElements.add(je);
  130. }
  131.  
  132. String json = gson.toJson(jsonElements);
  133. resp.getOutputStream().println(json);
  134. return;
  135.  
  136. }
  137. else if ("logout".equals(action)) {
  138. address = "/login.jsp";
  139. //StudentBean sb = (StudentBean) session.getAttribute("studentBean");
  140. sb2.logout();
  141. session.invalidate();
  142.  
  143. } else if ("toUpdate".equals(action)) {
  144.  
  145. address = "/WEB-INF/pages/updateProfile.jsp";
  146.  
  147. } else if (action.equals("update")) {
  148.  
  149. String name = req.getParameter("name");
  150. String surname = req.getParameter("surname");
  151. String password = req.getParameter("password");
  152. String username = req.getParameter("username");
  153. String mail = req.getParameter("mail");
  154. Faculty faculty = fdi.getFacultyByName(req.getParameter("faculty"));
  155. String fileName = req.getParameter("file_name");
  156.  
  157. System.out.println(fileName.equals(""));
  158.  
  159. String res = areRegistrationParamValid(name, surname, username, password, mail);
  160.  
  161. if("".equals(res) && faculty!=null) {
  162.  
  163. System.out.println("Udje li ovde");
  164.  
  165.  
  166.  
  167. sb2.getStudent().setName(name);
  168. sb2.getStudent().setSurname(surname);
  169. sb2.getStudent().setPassword(password);
  170. sb2.getStudent().setUsername(username);
  171. sb2.getStudent().setMail(mail);
  172.  
  173. sb2.getStudent().setDescription(req.getParameter("description"));
  174. sb2.getStudent().setStudyProgram(req.getParameter("studyProgram"));
  175.  
  176. sb2.getStudent().setFaculty(faculty);
  177. sb2.getStudent().setFacultyYear(Integer.parseInt(req.getParameter("facultyYear")));
  178.  
  179. Part filePart = req.getPart("file");
  180.  
  181. try {
  182.  
  183. // file saving
  184. InputStream fileContent = filePart.getInputStream();
  185. byte[] buffer = new byte[fileContent.available()];
  186. fileContent.read(buffer);
  187. String webRootPath = getWebRootPath();
  188. System.out.println("fileName: " + fileName);
  189. String uploadPath = "";
  190. if(fileName == null || "".equals(fileName) || " ".equals(fileName)) {
  191. uploadPath = webRootPath + "assets\\img\\userImg\\user-picture.png";
  192. sb2.getStudent().setImage("/assets/img/userImg/user-picture.png");
  193. }
  194. else {
  195. uploadPath = webRootPath + "assets\\img\\userImg\\" + username + "_" + fileName;
  196.  
  197. sb2.getStudent().setImage("/assets/img/userImg/" + username + "_" + fileName);
  198.  
  199. File targetFile = new File(uploadPath);
  200.  
  201. OutputStream outStream = new FileOutputStream(targetFile);
  202. outStream.write(buffer);
  203. outStream.close();
  204. }
  205. } catch (IOException ioe) {
  206. // TODO: handle exception
  207. ioe.printStackTrace();
  208. }
  209. //sb2.update();
  210.  
  211. address = "/WEB-INF/pages/updateProfile.jsp";
  212.  
  213.  
  214. if(!sb2.update()) {
  215.  
  216. req.setAttribute("updateNotification", "Error updating your profile");
  217. }
  218.  
  219. }
  220. else {
  221. if("".equals(res))
  222. res = "Please choose faculty";
  223. req.setAttribute("updateNotification", res);
  224. address = "/WEB-INF/pages/updateProfile.jsp";
  225. }
  226.  
  227. } else if (action.equals("like")) {
  228. BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
  229. String json = "";
  230.  
  231. if(br != null){
  232. json = br.readLine();
  233. }
  234. br.close();
  235.  
  236. JSONObject obj = new JSONObject(json);
  237. int postId = obj.getInt("postId");
  238. int rate = obj.getInt("rate");
  239. int studentId = ((StudentBean)session.getAttribute("studentBean")).getStudent().getId();
  240.  
  241. Like like = new Like();
  242. like.setPostId(postId);
  243. like.setStudentId(studentId);
  244. like.setType(rate);
  245.  
  246. StudentPost post = pdi.getById(postId);
  247.  
  248. if(ldi.isLiked(like)) {
  249. // ako takav lajk postoji skini ga
  250. ldi.removeLike(like);
  251.  
  252. String result = "";
  253. if(rate == 0) {
  254. post.setNumberOfLikes(post.getNumberOfLikes() - 1);
  255. result = "like-removed";
  256. }
  257. else {
  258. post.setNumberOfDislikes(post.getNumberOfDislikes() - 1);
  259. result = "dislike-removed";
  260. }
  261. pdi.updatePostRate(post);
  262.  
  263. resp.getWriter().write(result);
  264. return;
  265. }
  266.  
  267. // da li postoji suprotan lajk
  268. like.setType(Math.abs(like.getType() - 1));
  269.  
  270. if(ldi.isLiked(like)) {
  271. //ako da skini ga
  272. ldi.removeLike(like);
  273.  
  274. like.setType(Math.abs(like.getType() - 1));
  275. ldi.insertLike(like);
  276. String result = "";
  277. if(like.getType() == 0) {
  278. post.setNumberOfLikes(post.getNumberOfLikes() + 1);
  279. post.setNumberOfDislikes(post.getNumberOfDislikes() - 1);
  280. result = "dislike-removed-like-added";
  281. }
  282. else {
  283. post.setNumberOfLikes(post.getNumberOfLikes() - 1);
  284. post.setNumberOfDislikes(post.getNumberOfDislikes() + 1);
  285. result = "like-removed-dislike-added";
  286. }
  287. pdi.updatePostRate(post);
  288. resp.getWriter().write(result);
  289.  
  290. return;
  291. }
  292.  
  293. // ako ne postoji suprotan onda samo lajkaj
  294.  
  295. like = new Like();
  296. like.setPostId(postId);
  297. like.setStudentId(studentId);
  298. like.setType(rate);
  299.  
  300. ldi.insertLike(like);
  301.  
  302. String result = "";
  303. if(rate == 0) {
  304. result = "liked";
  305. post.setNumberOfLikes(post.getNumberOfLikes() + 1);
  306. }
  307. else {
  308. result = "disliked";
  309. post.setNumberOfDislikes(post.getNumberOfDislikes() + 1);
  310. }
  311. pdi.updatePostRate(post);
  312. resp.getWriter().write(result);
  313. return;
  314.  
  315. } else if (action.equals("post")) {
  316. int studentId = Integer.parseInt(req.getParameter("studentId"));
  317. long dateCreated = Long.parseLong(req.getParameter("dateCreated"));
  318. String description = req.getParameter("description");
  319. String linkPost = req.getParameter("linkPostText");
  320.  
  321. if("".equals(description) && "".equals(linkPost)) {
  322. resp.setStatus(403);
  323. resp.setContentType("text/plain");
  324. resp.getWriter().write("Invalid post data");
  325. return;
  326. }
  327.  
  328. StudentPost post = new StudentPost();
  329. post.setDescription(description);
  330. post.setNumberOfLikes(0);
  331. post.setNumberOfDislikes(0);
  332. post.setStudentId(studentId);
  333. post.setDatePosted(new Date(dateCreated));
  334.  
  335. String typeOfPost = "0";
  336. if(linkPost.equals(""))
  337. typeOfPost = "0";
  338. else if(linkPost.contains("youtube.com")) {
  339. typeOfPost = "2";
  340. linkPost = linkPost.replace("watch?v=", "embed/");
  341. }
  342. else {
  343. typeOfPost = "1";
  344. }
  345.  
  346. post.setTypeOfPost(typeOfPost);
  347. post.setLinkPost(linkPost);
  348.  
  349.  
  350. PostDaoImpl pdi = new PostDaoImpl();
  351. pdi.insertPost(post);
  352.  
  353. Student s = sdi.getStudentyById(studentId);
  354. String url = s.getImage();
  355. if(url == null || "".equals(url))
  356. url = "https://image.flaticon.com/icons/svg/17/17004.svg";
  357. else {
  358. url = "/UniShare" + url;
  359. }
  360. Gson gson = new Gson();
  361. JsonElement je = gson.toJsonTree(post);
  362. je.getAsJsonObject().addProperty("user_img", url);
  363. je.getAsJsonObject().addProperty("student_name", s.getName() + " " + s.getSurname());
  364.  
  365. String json = gson.toJson(je);
  366. resp.getOutputStream().println(json);
  367.  
  368. return;
  369.  
  370.  
  371. } else if (action.equals("addFile")) {
  372.  
  373.  
  374. StudentBean studentBean = (StudentBean) session.getAttribute("studentBean");
  375. int studentId = studentBean.getStudent().getId();
  376. String description = req.getParameter("fileDescription");
  377. String fileName = req.getParameter("file_name");
  378.  
  379. Part filePart = req.getPart("file");
  380.  
  381. InputStream fileContent = filePart.getInputStream();
  382.  
  383. if("".equals(description) || description == null) {
  384. req.setAttribute("addFileNtf", "Please add description");
  385.  
  386. }
  387. else {
  388.  
  389. if("".equals(fileName)) {
  390.  
  391. req.setAttribute("addFileNtf", "Please choose file");
  392.  
  393. }
  394. else {
  395.  
  396. try {
  397. byte[] buffer = new byte[fileContent.available()];
  398. fileContent.read(buffer);
  399.  
  400. String root = getWebRootPath();
  401.  
  402. String uploadPath = root + "assets\\files\\" + fileName;
  403. File targetFile = new File(uploadPath);
  404.  
  405. OutputStream outStream = new FileOutputStream(targetFile);
  406. outStream.write(buffer);
  407. outStream.close();
  408.  
  409. } catch (IOException e) {
  410. // TODO: handle exception
  411. e.printStackTrace();
  412. }
  413.  
  414. dto.File file = new dto.File();
  415. if("".equals(description)) {
  416. file.setDescription("No file description");
  417. }
  418. else
  419. file.setDescription(description);
  420.  
  421. file.setPath("/assets/files/" + fileName);
  422. file.setStudentId(studentId);
  423.  
  424. if(!fileDaoImpl.insertFile(file)) {
  425. req.setAttribute("addFileNtf", "Err saving Your file on server");
  426. }
  427. else {
  428. req.setAttribute("addFileNtf", "Succesful");
  429. }
  430.  
  431. }
  432.  
  433. }
  434. address = "/WEB-INF/pages/main.jsp";
  435. FileBean fileBean = new FileBean();
  436.  
  437. req.setAttribute("fileBean", fileBean);
  438. BlogBean blogBean = new BlogBean();
  439.  
  440. req.setAttribute("blogBean", blogBean);
  441.  
  442.  
  443. } else if (action.equals("addBlog")) {
  444.  
  445. StudentBean studentBean = (StudentBean) session.getAttribute("studentBean");
  446. int studentId = studentBean.getStudent().getId();
  447. String title = req.getParameter("title");
  448. String blogDescription = req.getParameter("blogDescription");
  449.  
  450. if("".equals(title) || "".equals(blogDescription)) {
  451. req.setAttribute("addBlogNtf", "Please add title and description of your blog.");
  452. }
  453. else {
  454. req.setAttribute("addBlogNtf", "Succesful");
  455. Blog blog = new Blog();
  456.  
  457. blog.setDateCreated(new Date(System.currentTimeMillis()));
  458. blog.setContent(blogDescription);
  459. blog.setStudentId(studentId);
  460. blog.setTitle(title);
  461.  
  462. bdi.insertBlog(blog);
  463.  
  464. }
  465. address = "/WEB-INF/pages/main.jsp";
  466. FileBean fileBean = new FileBean();
  467.  
  468. req.setAttribute("fileBean", fileBean);
  469. BlogBean blogBean = new BlogBean();
  470.  
  471. req.setAttribute("blogBean", blogBean);
  472. return;
  473.  
  474. } else if (action.equals("addComment")) {
  475.  
  476.  
  477.  
  478. int studentId = sb2.getStudent().getId();
  479. String blogComment = req.getParameter("blogComment");
  480.  
  481. if(studentId == 0 || blogComment == null || "".equals(blogComment)) {
  482. JsonObject json = new JsonObject();
  483. json.addProperty("Error", "Error");
  484. json.addProperty("msg", "Please add text");
  485. resp.getOutputStream().println(json.toString());
  486. return;
  487. }
  488.  
  489. bdi.addComment(req.getParameter("blogId"), blogComment, studentId);
  490.  
  491. System.out.println(req.getParameter("blogId"));
  492. System.out.println(blogComment);
  493. System.out.println(studentId);
  494.  
  495. String url = sb2.getStudent().getImage();
  496. if(url == null || "".equals(url))
  497. url = "https://image.flaticon.com/icons/svg/17/17004.svg";
  498. else {
  499. url = "/UniShare" + url;
  500. }
  501.  
  502.  
  503. JsonObject json = new JsonObject();
  504. json.addProperty("username", sb2.getStudent().getUsername());
  505. json.addProperty("imageUrl", url);
  506. json.addProperty("blogId", req.getParameter("blogId"));
  507. json.addProperty("comment", blogComment);
  508.  
  509. resp.getOutputStream().println(json.toString());
  510. return ;
  511.  
  512. } else if(action.equals("connections")) {
  513. ConnectionsBean cb = new ConnectionsBean();
  514. cb.setStudent(((StudentBean) session.getAttribute("studentBean")).getStudent());
  515. req.setAttribute("connectionBean", cb);
  516. address = "/WEB-INF/pages/connection.jsp";
  517. } else if(action.equals("get_students")) {
  518.  
  519. List<Student> ret = sdi.getAllStudentsByFacultyId(Integer.parseInt(req.getParameter("facultyId")));
  520. ret.remove(sb2.getStudent());
  521. List<Student> connectedStudents = sdi.getAllStudentsConnected(sb2.getStudent());
  522. List<Integer> studentRequests = cdi.getRequestsSent(sb2.getStudent().getId());
  523.  
  524. JSONArray jsonArray = new JSONArray();
  525.  
  526. for(Student student : ret) {
  527.  
  528.  
  529. String url = student.getImage();
  530. if(url == null || "".equals(url))
  531. url = "https://image.flaticon.com/icons/svg/17/17004.svg";
  532. else {
  533. url = "/UniShare" + url;
  534. }
  535.  
  536. //String username = student.getUsername();
  537. int type= -1;
  538. if(studentRequests.contains(student.getId()))
  539. type = 0;
  540. else if(connectedStudents.contains(student))
  541. type = 1;
  542. else
  543. type = 2;
  544.  
  545. JSONObject json = new JSONObject();
  546. json.put("id", student.getId());
  547. json.put("username", student.getName() + " " + student.getSurname());
  548. json.put("type", type);
  549. json.put("url", url);
  550.  
  551.  
  552. jsonArray.put(json);
  553.  
  554. }
  555.  
  556. resp.getOutputStream().println(jsonArray.toString());
  557. return;
  558.  
  559. }
  560. else if(action.equals("sendConnectionRequest")) {
  561. address = "/WEB-INF/pages/connection.jsp";
  562. BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
  563. String json = "";
  564. if(br != null){
  565. json = br.readLine();
  566. //System.out.println(json);
  567. }
  568.  
  569. JSONObject obj = new JSONObject(json);
  570. int senderId = obj.getInt("senderId");
  571. int recieverId = obj.getInt("recieverId");
  572. if(cdi.insertConnection(senderId, recieverId, 1))
  573. resp.setStatus(200);
  574. else {
  575. System.out.println("ERR");
  576. }
  577. return;
  578.  
  579. } else if(action.equals("acceptConnectionRequest")) {
  580. address = "/WEB-INF/pages/connection.jsp";
  581. BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
  582. String json = "";
  583. if(br != null){
  584. json = br.readLine();
  585. //System.out.println(json);
  586. }
  587.  
  588. JSONObject obj = new JSONObject(json);
  589. int senderId = obj.getInt("senderId");
  590. int accepterId = Integer.parseInt(obj.getString("accepterId"));
  591. int accept = obj.getInt("accept");
  592.  
  593.  
  594.  
  595. if(accept==1) {
  596. cdi.acceptConnection(senderId, accepterId);
  597. }
  598. else {
  599. cdi.deleteConnection(senderId, accepterId);
  600. }
  601. resp.setStatus(200);
  602. return;
  603. } else if(action.equals("deleteConnection")) {
  604. address = "/WEB-INF/pages/connection.jsp";
  605. BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
  606. String json = "";
  607. if(br != null){
  608. json = br.readLine();
  609. //System.out.println(json);
  610. }
  611.  
  612. JSONObject obj = new JSONObject(json);
  613. int senderId = obj.getInt("senderId");
  614. int accepterId = obj.getInt("accepterId");
  615.  
  616. cdi.deleteConnection(senderId, accepterId);
  617. cdi.deleteConnection(accepterId, senderId);
  618. resp.setStatus(200);
  619. return;
  620. } else if(action.equals("viewProfile")) {
  621. address = "/WEB-INF/pages/profile.jsp";
  622. int userId = Integer.parseInt(req.getParameter("userId"));
  623. session.setAttribute("userId", userId);
  624. }
  625. }
  626.  
  627. else {
  628. if ("login".equals(action)) {
  629.  
  630. String username = req.getParameter("username");
  631. String password = req.getParameter("password");
  632. if(areLoginParamValid(username, password)) {
  633.  
  634. StudentBean studentBean = new StudentBean();
  635.  
  636. if(studentBean.login(username, password)) {
  637. //setStudentBean(studentBean);
  638. session.setAttribute("studentBean", studentBean);
  639. address = "/WEB-INF/pages/main.jsp";
  640. FileBean fileBean = new FileBean();
  641.  
  642. req.setAttribute("fileBean", fileBean);
  643. BlogBean blogBean = new BlogBean();
  644.  
  645. req.setAttribute("blogBean", blogBean);
  646. }
  647. else {
  648. Student s = studentBean.getStudent();
  649.  
  650. if( s != null ) {
  651. if(studentBean.getStudent().isDisabled())
  652. req.setAttribute("loginNotification", "Account blocked");
  653.  
  654. }else
  655. req.setAttribute("loginNotification", "Invalid username or password");
  656. }
  657. }
  658. }
  659. else if ("registration".equals(action)) {
  660.  
  661. String name = req.getParameter("name");
  662. String surname = req.getParameter("surname");
  663. String password = req.getParameter("password");
  664. String username = req.getParameter("username");
  665. String mail = req.getParameter("mail");
  666.  
  667. String res = areRegistrationParamValid(name, surname, username, password, mail);
  668.  
  669. if("".equals(res)) {
  670.  
  671. StudentBean studentBean = new StudentBean();
  672.  
  673. Student student = new Student();
  674.  
  675. student.setName(name);
  676. student.setSurname(surname);
  677. student.setPassword(password);
  678. student.setUsername(username);
  679. student.setMail(mail);
  680. student.setImage("/assets/img/userImg/user-picture.png");
  681.  
  682. studentBean.setStudent(student);
  683.  
  684. String insertionResult = studentBean.add(student);
  685.  
  686. studentBean.login(username, password);
  687.  
  688. if("success".equals(insertionResult)) {
  689. session.setAttribute("studentBean", studentBean);
  690. address = "/WEB-INF/pages/updateProfile.jsp";
  691. }
  692. else {
  693. req.setAttribute("registrationNotification", insertionResult);
  694. address = "/login.jsp";
  695. }
  696.  
  697. }
  698. else {
  699. req.setAttribute("registrationNotification", res);
  700. }
  701.  
  702.  
  703. }
  704. }
  705.  
  706. RequestDispatcher dispatcher = req.getRequestDispatcher(address);
  707. dispatcher.forward(req, resp);
  708.  
  709. }
  710.  
  711.  
  712. @Override
  713. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  714. doGet(req, resp);
  715. }
  716.  
  717.  
  718. private boolean areLoginParamValid(String username, String password) {
  719. if("".equals(username) || "".equals(password))
  720. return false;
  721. return true;
  722. }
  723.  
  724. private String areRegistrationParamValid(String name, String surname, String username, String password, String mail) {
  725.  
  726. if("".equals(name) || name.length() < 2)
  727. return "Invalid name";
  728.  
  729. if("".equals(surname) || surname.length() < 2)
  730. return "Invalid surname";
  731.  
  732. if("".equals(username))
  733. if( username.length() < 4)
  734. return "Invalid username";
  735. else if(sdi.doesUsernameExist(username)) {
  736. return "Username already exist";
  737. }
  738.  
  739. if("".equals(password) || password.length() < 4)
  740. return "Invalid password";
  741.  
  742. if("".equals(mail) || mail.length() < 5)
  743. return "Invalid mail";
  744.  
  745. return "";
  746. }
  747.  
  748. private String getWebRootPath() {
  749. ResourceBundle bundle =
  750. PropertyResourceBundle.getBundle("daoimpl.ConnectionPool");
  751. String result = bundle.getString("webRootFolder");
  752. return result;
  753. }
  754.  
  755. private ArrayList<Post> getAllPosts(Student student) {
  756.  
  757. ArrayList<StudentPost> posts = (ArrayList<StudentPost>) pdi.getAllByStudentId(student.getId());
  758. List<Student> studentsConnected = sdi.getAllStudentsConnected(student);
  759. List<News> news = nr.getAll();
  760. news.forEach(n -> n.setType("news"));
  761. List<Event> events = er.getAll();
  762. events.forEach(e -> e.setType("event"));
  763.  
  764.  
  765. for(Student s : studentsConnected) {
  766.  
  767. posts.addAll(pdi.getAllByStudentId(s.getId()));
  768. }
  769. posts.forEach(s -> s.setType("studentPost"));
  770.  
  771. List<Post> allPosts = new ArrayList<>();
  772. allPosts.addAll(news);
  773. allPosts.addAll(events);
  774. allPosts.addAll(posts);
  775.  
  776. if(allPosts.size() > 5) {
  777. Collections.sort(allPosts, (p1, p2) -> (p2.getNumberOfLikes() - p2.getNumberOfDislikes()) - (p1.getNumberOfLikes() - p1.getNumberOfDislikes()));
  778. return (ArrayList<Post>) allPosts;
  779. }
  780. else
  781. return (ArrayList<Post>) allPosts;
  782.  
  783.  
  784.  
  785. }
  786. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement