From 303599780d2758bad8b326ad2f8038e68cd968d9 Mon Sep 17 00:00:00 2001 From: "M.Filatov" Date: Sat, 3 Dec 2016 17:33:16 +0300 Subject: [PATCH] Added XML parser. Added user logout. --- .gitignore | 1 + pom.xml | 45 +++++++------- src/main/java/mvcapp/DB_Utils/DAO/DB_DAO.java | 9 --- .../mvcapp/DB_Utils/DAO/DB_PostgresImpl.java | 24 -------- .../mvcapp/DB_Utils/service/LoadService.java | 16 ----- .../controllers/HomeMenuController.java | 19 +++++- .../mvcapp/controllers/LoginController.java | 22 ++++++- .../dbutils/dbconnection/DataBaseDAO.java | 10 ++++ .../dbconnection/DataBasePostgresImpl.java | 27 +++++++++ .../mvcapp/dbutils/service/DBService.java | 22 +++++++ .../{Entities => entities}/Requirement.java | 12 ++-- .../mvcapp/parser/fileconnection/FileDAO.java | 13 ++++ .../mvcapp/parser/fileconnection/XmlImpl.java | 59 +++++++++++++++++++ .../mvcapp/parser/service/FileService.java | 24 ++++++++ src/main/resources/log4j.xml | 2 +- .../config/appServlet/servlet-context.xml | 57 ++++++++++++------ .../WEB-INF/config/application-security.xml | 16 ++--- .../webapp/WEB-INF/config/root-context.xml | 4 +- src/main/webapp/WEB-INF/jsp/home.jsp | 4 +- .../webapp/WEB-INF/jsp/load-completed.jsp | 17 ++++++ src/main/webapp/WEB-INF/jsp/load.jsp | 1 + src/main/webapp/WEB-INF/jsp/loading-error.jsp | 17 ++++++ src/main/webapp/WEB-INF/jsp/login-failed.jsp | 17 ++++++ src/main/webapp/WEB-INF/jsp/login.jsp | 2 +- src/main/webapp/WEB-INF/jsp/search.jsp | 1 + src/main/webapp/WEB-INF/web.xml | 1 - 26 files changed, 324 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/mvcapp/DB_Utils/DAO/DB_DAO.java delete mode 100644 src/main/java/mvcapp/DB_Utils/DAO/DB_PostgresImpl.java delete mode 100644 src/main/java/mvcapp/DB_Utils/service/LoadService.java create mode 100644 src/main/java/mvcapp/dbutils/dbconnection/DataBaseDAO.java create mode 100644 src/main/java/mvcapp/dbutils/dbconnection/DataBasePostgresImpl.java create mode 100644 src/main/java/mvcapp/dbutils/service/DBService.java rename src/main/java/mvcapp/{Entities => entities}/Requirement.java (84%) create mode 100644 src/main/java/mvcapp/parser/fileconnection/FileDAO.java create mode 100644 src/main/java/mvcapp/parser/fileconnection/XmlImpl.java create mode 100644 src/main/java/mvcapp/parser/service/FileService.java create mode 100644 src/main/webapp/WEB-INF/jsp/load-completed.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/loading-error.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/login-failed.jsp diff --git a/.gitignore b/.gitignore index c38fa4e..3f9378d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea *.iml +src/main/resources/jdbc.properties \ No newline at end of file diff --git a/pom.xml b/pom.xml index cd116f6..50fa9f6 100644 --- a/pom.xml +++ b/pom.xml @@ -8,6 +8,7 @@ FileAnalyser Maven Webapp http://maven.apache.org + 1.8 4.3.3.RELEASE 1.6.9 1.5.10 @@ -18,7 +19,12 @@ org.springframework spring-webmvc - 4.3.3.RELEASE + ${spring.version} + + + org.springframework + spring-orm + ${spring.version} @@ -38,32 +44,16 @@ 4.1.3.RELEASE - - - org.springframework - spring-jdbc - 4.3.4.RELEASE - - - + - postgresql - postgresql - 9.1-901-1.jdbc4 + org.hibernate + hibernate-core + 4.3.11.Final - - - - javax.persistence - persistence-api - 1.0 - - - - org.aspectj - aspectjrt - ${org.aspectj-version} + org.hibernate + hibernate-commons-annotations + 3.2.0.Final @@ -98,6 +88,13 @@ 1 + + + javax + javaee-api + 6.0 + + javax.servlet diff --git a/src/main/java/mvcapp/DB_Utils/DAO/DB_DAO.java b/src/main/java/mvcapp/DB_Utils/DAO/DB_DAO.java deleted file mode 100644 index b8c6c44..0000000 --- a/src/main/java/mvcapp/DB_Utils/DAO/DB_DAO.java +++ /dev/null @@ -1,9 +0,0 @@ -package mvcapp.DB_Utils.DAO; - -import mvcapp.Entities.Requirement; - -import java.util.List; - -public interface DB_DAO { - public void loadReqs(List reqs); -} diff --git a/src/main/java/mvcapp/DB_Utils/DAO/DB_PostgresImpl.java b/src/main/java/mvcapp/DB_Utils/DAO/DB_PostgresImpl.java deleted file mode 100644 index 2eed7a9..0000000 --- a/src/main/java/mvcapp/DB_Utils/DAO/DB_PostgresImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package mvcapp.DB_Utils.DAO; - -import mvcapp.Entities.Requirement; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.SimpleDriverDataSource; - -import java.util.List; - -public class DB_PostgresImpl implements DB_DAO { - public void loadReqs(List reqs){ - SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); - dataSource.setDriverClass(org.postgresql.Driver.class); - dataSource.setUrl("jdbc:postgresql://localhost:5432/NCEC"); - dataSource.setUsername("postgres"); - dataSource.setPassword("postgresfilm"); - - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - for (Requirement req:reqs) { - jdbcTemplate.update( - "INSERT INTO requirements(id, title, text, comment, done, 'time', 'date') values(?,?,?,?,?,?,?)", - req.getId(), req.getTitle(), req.getText(), req.getComment(), req.isDone(), req.getTime(), req.getDate()); - } - } -} diff --git a/src/main/java/mvcapp/DB_Utils/service/LoadService.java b/src/main/java/mvcapp/DB_Utils/service/LoadService.java deleted file mode 100644 index 63e4d63..0000000 --- a/src/main/java/mvcapp/DB_Utils/service/LoadService.java +++ /dev/null @@ -1,16 +0,0 @@ -package mvcapp.DB_Utils.service; - -import java.util.List; - -import mvcapp.DB_Utils.DAO.DB_DAO; -import mvcapp.Entities.Requirement; - -public class LoadService { - private DB_DAO db_dao; - public LoadService(DB_DAO db_dao){ - this.db_dao = db_dao; - } - public void loadReqs(List reqs) { - db_dao.loadReqs(reqs); - } -} \ No newline at end of file diff --git a/src/main/java/mvcapp/controllers/HomeMenuController.java b/src/main/java/mvcapp/controllers/HomeMenuController.java index cce08bd..eb3da57 100644 --- a/src/main/java/mvcapp/controllers/HomeMenuController.java +++ b/src/main/java/mvcapp/controllers/HomeMenuController.java @@ -1,19 +1,32 @@ package mvcapp.controllers; +import mvcapp.dbutils.service.DBService; +import mvcapp.entities.Requirement; +import mvcapp.parser.service.FileService; + import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import java.util.List; @Controller public class HomeMenuController { + @RequestMapping(value = "/search", method = RequestMethod.GET) + public String search() { + return "search"; + } + @RequestMapping(value = "/load", method = RequestMethod.GET) public String load() { return "load"; } - @RequestMapping(value = "/search", method = RequestMethod.GET) - public String search() { - return "search"; + @RequestMapping(value = "/load-file", method = RequestMethod.GET) + public String loadFile(String path) throws Exception { + + + return "load-completed"; } + } \ No newline at end of file diff --git a/src/main/java/mvcapp/controllers/LoginController.java b/src/main/java/mvcapp/controllers/LoginController.java index d15e119..3cc6a36 100644 --- a/src/main/java/mvcapp/controllers/LoginController.java +++ b/src/main/java/mvcapp/controllers/LoginController.java @@ -1,16 +1,36 @@ package mvcapp.controllers; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + @Controller public class LoginController { - @RequestMapping(value = {"/", "/login", "/loginfailed", "/logout"}, method = RequestMethod.GET) + @RequestMapping(value = {"/", "/login"}, method = RequestMethod.GET) public String login() { return "login"; } + @RequestMapping(value = "/loginfailed", method = RequestMethod.GET) + public String loginFailed() { + return "login-failed"; + } + + @RequestMapping(value="/logout", method = RequestMethod.GET) + public String logoutPage (HttpServletRequest request, HttpServletResponse response) { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + if (auth != null){ + new SecurityContextLogoutHandler().logout(request, response, auth); + } + return "login"; + } + @RequestMapping(value = "/home", method = RequestMethod.GET) public String home() { return "home"; diff --git a/src/main/java/mvcapp/dbutils/dbconnection/DataBaseDAO.java b/src/main/java/mvcapp/dbutils/dbconnection/DataBaseDAO.java new file mode 100644 index 0000000..bf768d1 --- /dev/null +++ b/src/main/java/mvcapp/dbutils/dbconnection/DataBaseDAO.java @@ -0,0 +1,10 @@ +package mvcapp.dbutils.dbconnection; + +import mvcapp.entities.Requirement; + +import java.sql.SQLException; +import java.util.List; + +public interface DataBaseDAO { + public void loadReqs(List reqs) throws SQLException; +} diff --git a/src/main/java/mvcapp/dbutils/dbconnection/DataBasePostgresImpl.java b/src/main/java/mvcapp/dbutils/dbconnection/DataBasePostgresImpl.java new file mode 100644 index 0000000..b1ade34 --- /dev/null +++ b/src/main/java/mvcapp/dbutils/dbconnection/DataBasePostgresImpl.java @@ -0,0 +1,27 @@ +package mvcapp.dbutils.dbconnection; + +import java.sql.SQLException; + +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import mvcapp.entities.Requirement; + +import java.util.List; + +@Component +public class DataBasePostgresImpl implements DataBaseDAO { + @Autowired + private SessionFactory sessionFactory; + + @Transactional + public void loadReqs(List reqs) throws SQLException{ + for (Requirement req: reqs) { + sessionFactory.getCurrentSession().save(req); + } + + } + +} diff --git a/src/main/java/mvcapp/dbutils/service/DBService.java b/src/main/java/mvcapp/dbutils/service/DBService.java new file mode 100644 index 0000000..73b3dbc --- /dev/null +++ b/src/main/java/mvcapp/dbutils/service/DBService.java @@ -0,0 +1,22 @@ +package mvcapp.dbutils.service; + +import java.sql.SQLException; +import java.util.List; + +import mvcapp.dbutils.dbconnection.DataBaseDAO; +import mvcapp.entities.Requirement; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class DBService { + @Autowired + private DataBaseDAO dataBase_dao; + + @Transactional + public void loadReqs(List reqs) throws SQLException{ + dataBase_dao.loadReqs(reqs); + } + +} \ No newline at end of file diff --git a/src/main/java/mvcapp/Entities/Requirement.java b/src/main/java/mvcapp/entities/Requirement.java similarity index 84% rename from src/main/java/mvcapp/Entities/Requirement.java rename to src/main/java/mvcapp/entities/Requirement.java index 9c66aa5..a6cbbd3 100644 --- a/src/main/java/mvcapp/Entities/Requirement.java +++ b/src/main/java/mvcapp/entities/Requirement.java @@ -1,18 +1,14 @@ -package mvcapp.Entities; +package mvcapp.entities; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; import java.util.Date; @Entity -@Table(name = "Requirements") +@Table(name = "requirements") public class Requirement { @Id @Column(name = "id") - @GeneratedValue + @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "title") diff --git a/src/main/java/mvcapp/parser/fileconnection/FileDAO.java b/src/main/java/mvcapp/parser/fileconnection/FileDAO.java new file mode 100644 index 0000000..49fb38f --- /dev/null +++ b/src/main/java/mvcapp/parser/fileconnection/FileDAO.java @@ -0,0 +1,13 @@ +package mvcapp.parser.fileconnection; + +import mvcapp.entities.Requirement; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.text.ParseException; +import java.util.List; + +public interface FileDAO { + public List parseReqs(String path) throws ParserConfigurationException, IOException, SAXException, ParseException; +} diff --git a/src/main/java/mvcapp/parser/fileconnection/XmlImpl.java b/src/main/java/mvcapp/parser/fileconnection/XmlImpl.java new file mode 100644 index 0000000..f880463 --- /dev/null +++ b/src/main/java/mvcapp/parser/fileconnection/XmlImpl.java @@ -0,0 +1,59 @@ +package mvcapp.parser.fileconnection; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import mvcapp.entities.Requirement; +import org.xml.sax.SAXException; + +public class XmlImpl implements FileDAO { + @Override + public List parseReqs(String path) throws ParserConfigurationException, IOException, SAXException, ParseException { + File xmlFile = new File(path); + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.parse(xmlFile); + document.getDocumentElement().normalize(); + + NodeList nodeList = document.getElementsByTagName(document.getDocumentElement().getChildNodes().item(1).getNodeName()); + + List reqs = new ArrayList<>(); + for(int tmp = 0; tmp < nodeList.getLength(); tmp++) + { + Node node = nodeList.item(tmp); + if(node.getNodeType() == Node.ELEMENT_NODE) + { + Requirement req = new Requirement(); + + Element element = (Element)node; + req.setId(Integer.valueOf(element.getElementsByTagName("id").item(0).getChildNodes().item(0).getNodeValue())); + req.setTitle(element.getElementsByTagName("title").item(0).getChildNodes().item(0).getNodeValue()); + req.setText(element.getElementsByTagName("text").item(0).getChildNodes().item(0).getNodeValue()); + req.setComment(element.getElementsByTagName("comment").item(0).getChildNodes().item(0).getNodeValue()); + String done = element.getElementsByTagName("done").item(0).getChildNodes().item(0).getNodeValue(); + req.setDone(done.equals("yes")); + String dateStr = (element.getElementsByTagName("date").item(0).getChildNodes().item(0).getNodeValue()); + SimpleDateFormat format = new SimpleDateFormat("dd.mm.yyyy"); + Date date = format.parse(dateStr); + req.setDate(date); + + reqs.add(req); + } + } + return reqs; + } +} diff --git a/src/main/java/mvcapp/parser/service/FileService.java b/src/main/java/mvcapp/parser/service/FileService.java new file mode 100644 index 0000000..7bab707 --- /dev/null +++ b/src/main/java/mvcapp/parser/service/FileService.java @@ -0,0 +1,24 @@ +package mvcapp.parser.service; + +import mvcapp.entities.Requirement; +import mvcapp.parser.fileconnection.FileDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; +import java.text.ParseException; +import java.util.List; + +@Service +public class FileService { + @Autowired + private FileDAO fileDAO; + + @Transactional + public List parseReqs(String path) throws ParserConfigurationException, SAXException, ParseException, IOException { + return fileDAO.parseReqs(path); + } +} diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index 4cbc21f..f021f60 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -37,5 +37,5 @@ - + diff --git a/src/main/webapp/WEB-INF/config/appServlet/servlet-context.xml b/src/main/webapp/WEB-INF/config/appServlet/servlet-context.xml index 4800605..620a9ad 100644 --- a/src/main/webapp/WEB-INF/config/appServlet/servlet-context.xml +++ b/src/main/webapp/WEB-INF/config/appServlet/servlet-context.xml @@ -1,21 +1,42 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - @@ -23,6 +44,4 @@ - - - + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/config/application-security.xml b/src/main/webapp/WEB-INF/config/application-security.xml index b77f75b..65988a1 100644 --- a/src/main/webapp/WEB-INF/config/application-security.xml +++ b/src/main/webapp/WEB-INF/config/application-security.xml @@ -2,22 +2,22 @@ xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-4.1.xsd + http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd"> - + - + diff --git a/src/main/webapp/WEB-INF/config/root-context.xml b/src/main/webapp/WEB-INF/config/root-context.xml index 2c85a9f..78ed7be 100644 --- a/src/main/webapp/WEB-INF/config/root-context.xml +++ b/src/main/webapp/WEB-INF/config/root-context.xml @@ -3,4 +3,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> - + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/home.jsp b/src/main/webapp/WEB-INF/jsp/home.jsp index c7c639a..e83c33d 100644 --- a/src/main/webapp/WEB-INF/jsp/home.jsp +++ b/src/main/webapp/WEB-INF/jsp/home.jsp @@ -1,3 +1,4 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> Login Page @@ -8,9 +9,8 @@

NCEC File Analyser

-

Menu:

- Log Out
+ ">Log out
Search
Load
diff --git a/src/main/webapp/WEB-INF/jsp/load-completed.jsp b/src/main/webapp/WEB-INF/jsp/load-completed.jsp new file mode 100644 index 0000000..ca7bbd7 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/load-completed.jsp @@ -0,0 +1,17 @@ + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Loading completed page + + + +

NCEC File Analyser

+ +
+ +

Your file was loaded succesfully

+ Home
+
+ + diff --git a/src/main/webapp/WEB-INF/jsp/load.jsp b/src/main/webapp/WEB-INF/jsp/load.jsp index 8c7999b..055ebd8 100644 --- a/src/main/webapp/WEB-INF/jsp/load.jsp +++ b/src/main/webapp/WEB-INF/jsp/load.jsp @@ -10,6 +10,7 @@

Load file:

+ Home
diff --git a/src/main/webapp/WEB-INF/jsp/loading-error.jsp b/src/main/webapp/WEB-INF/jsp/loading-error.jsp new file mode 100644 index 0000000..3f887d7 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/loading-error.jsp @@ -0,0 +1,17 @@ + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Loading error page + + + +

NCEC File Analyser

+ +
+ +

Your file is not supported. Use .xml files to load

+ Home
+
+ + diff --git a/src/main/webapp/WEB-INF/jsp/login-failed.jsp b/src/main/webapp/WEB-INF/jsp/login-failed.jsp new file mode 100644 index 0000000..8ca9f61 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/login-failed.jsp @@ -0,0 +1,17 @@ + +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Login error page + + + +

NCEC File Analyser

+ +
+ +

Your login or password is incorrect

+ Log in
+
+ + diff --git a/src/main/webapp/WEB-INF/jsp/login.jsp b/src/main/webapp/WEB-INF/jsp/login.jsp index aec5e1f..cea2461 100644 --- a/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/src/main/webapp/WEB-INF/jsp/login.jsp @@ -14,7 +14,7 @@ -
File path:
+
diff --git a/src/main/webapp/WEB-INF/jsp/search.jsp b/src/main/webapp/WEB-INF/jsp/search.jsp index 7de704c..ff502d2 100644 --- a/src/main/webapp/WEB-INF/jsp/search.jsp +++ b/src/main/webapp/WEB-INF/jsp/search.jsp @@ -9,6 +9,7 @@

Requierment Search:

+ Home
User:
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 7666687..1975e71 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -9,7 +9,6 @@ contextConfigLocation /WEB-INF/config/root-context.xml - /WEB-INF/config/application-security.xml
Parameter search