commit 2ca878f52dc56deaf2e1e2fdb22007dc52e9ad5d Author: Josef Rokos Date: Tue Apr 15 22:11:00 2014 +0200 Báze pro DAO a business logiku, nastavení Hibernate, nastavení security. diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..88db2ee2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,282 @@ + + + 4.0.0 + info.bukova + isspst + isspst + war + 1.0.0-BUILD-SNAPSHOT + + 1.6 + 7.0.1 + 3.1.1.RELEASE + 1.6.10 + 1.6.6 + + + + + ZK CE + ZK CE Repository + http://mavensync.zkoss.org/maven2 + + + + + + zkmaven + ZK Maven Plugin Repository + http://mavensync.zkoss.org/maven2/ + + + + + + + org.springframework + spring-context + ${org.springframework-version} + + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + ${org.springframework-version} + + + org.springframework + spring-orm + ${org.springframework-version} + + + org.springframework.security + spring-security-core + ${org.springframework-version} + + + org.springframework.security + spring-security-web + ${org.springframework-version} + + + org.springframework.security + spring-security-config + ${org.springframework-version} + + + + + + org.aspectj + aspectjrt + ${org.aspectj-version} + + + + + org.slf4j + slf4j-api + ${org.slf4j-version} + + + org.slf4j + jcl-over-slf4j + ${org.slf4j-version} + runtime + + + org.slf4j + slf4j-log4j12 + ${org.slf4j-version} + runtime + + + log4j + log4j + 1.2.15 + + + javax.mail + mail + + + javax.jms + jms + + + com.sun.jdmk + jmxtools + + + com.sun.jmx + jmxri + + + runtime + + + + cglib + cglib + 2.2 + + + + + javax.inject + javax.inject + 1 + + + + + javax.servlet + servlet-api + 2.5 + provided + + + javax.servlet.jsp + jsp-api + 2.1 + provided + + + javax.servlet + jstl + 1.2 + + + + + mysql + mysql-connector-java + 5.1.21 + + + commons-dbcp + commons-dbcp + 1.4 + + + + + org.hibernate + hibernate-core + 4.2.8.Final + + + org.hibernate + hibernate-validator + 4.3.0.Final + + + + + + org.zkoss.zk + zkbind + ${zk.version} + + + + org.zkoss.zk + zul + ${zk.version} + + + org.zkoss.zk + zkplus + ${zk.version} + + + org.zkoss.zk + zhtml + ${zk.version} + + + org.zkoss.zkforge + ckez + 3.6.0.0 + + + + org.zkoss.zk + zkspring-core + 3.1 + + + ch.qos.logback + logback-classic + + + + + + + junit + junit + 4.7 + test + + + + + + maven-eclipse-plugin + 2.9 + + + org.springframework.ide.eclipse.core.springnature + + + org.springframework.ide.eclipse.core.springbuilder + + true + true + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + + 1.6 + 1.6 + -Xlint:all + true + true + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + org.test.int1.Main + + + + + diff --git a/src/main/java/info/bukova/isspst/HomeController.java b/src/main/java/info/bukova/isspst/HomeController.java new file mode 100644 index 00000000..0d5b9736 --- /dev/null +++ b/src/main/java/info/bukova/isspst/HomeController.java @@ -0,0 +1,51 @@ +package info.bukova.isspst; + +import info.bukova.isspst.data.User; +import info.bukova.isspst.services.UserService; + +import java.text.DateFormat; +import java.util.Date; +import java.util.Locale; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Handles requests for the application home page. + */ +@Controller +public class HomeController { + + private static final Logger logger = LoggerFactory.getLogger(HomeController.class); + + @Autowired + private UserService userService; + + /** + * Simply selects the home view to render by returning its name. + */ + @RequestMapping(value = "/", method = RequestMethod.GET) + public String home(Locale locale, Model model) { + logger.info("Welcome home! The client locale is {}.", locale); + + Date date = new Date(); + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); + + String formattedDate = dateFormat.format(date); + + model.addAttribute("serverTime", formattedDate ); + + User user = new User(); + user.setUsername("pokus"); + + userService.add(user); + + return "home"; + } + +} diff --git a/src/main/java/info/bukova/isspst/dao/BaseDao.java b/src/main/java/info/bukova/isspst/dao/BaseDao.java new file mode 100644 index 00000000..de4f23bc --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/BaseDao.java @@ -0,0 +1,18 @@ +package info.bukova.isspst.dao; + +import java.util.List; + +import org.hibernate.Query; + +public interface BaseDao { + + public void add(T entity); + public void delete(T entity); + public void modify(T entity); + public T getById(int id); + public List getAll(); + public Query getQuery(String query); + public List execQuery(String query); + public String getTableName(); + +} diff --git a/src/main/java/info/bukova/isspst/dao/RoleDao.java b/src/main/java/info/bukova/isspst/dao/RoleDao.java new file mode 100644 index 00000000..77ad68d7 --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/RoleDao.java @@ -0,0 +1,7 @@ +package info.bukova.isspst.dao; + +import info.bukova.isspst.data.Role; + +public interface RoleDao extends BaseDao { + +} diff --git a/src/main/java/info/bukova/isspst/dao/UserDao.java b/src/main/java/info/bukova/isspst/dao/UserDao.java new file mode 100644 index 00000000..aac071e8 --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/UserDao.java @@ -0,0 +1,7 @@ +package info.bukova.isspst.dao; + +import info.bukova.isspst.data.User; + +public interface UserDao extends BaseDao { + +} diff --git a/src/main/java/info/bukova/isspst/dao/jpa/BaseDaoJPA.java b/src/main/java/info/bukova/isspst/dao/jpa/BaseDaoJPA.java new file mode 100644 index 00000000..c6cae75b --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/jpa/BaseDaoJPA.java @@ -0,0 +1,58 @@ +package info.bukova.isspst.dao.jpa; + +import java.util.List; + +import org.hibernate.Query; +import org.hibernate.SessionFactory; + +import info.bukova.isspst.dao.BaseDao; + +public abstract class BaseDaoJPA implements BaseDao { + + private SessionFactory sessionFactory; + + public void setSessionFactory(SessionFactory factory) { + this.sessionFactory = factory; + } + + @Override + public void add(T entity) { + sessionFactory.getCurrentSession().save(entity); + } + + @Override + public void delete(T entity) { + sessionFactory.getCurrentSession().delete(entity); + } + + @Override + public void modify(T entity) { + sessionFactory.getCurrentSession().update(entity); + } + + @SuppressWarnings("unchecked") + @Override + public T getById(int id) { + Query q = sessionFactory.getCurrentSession().createQuery("from " + getTableName() + " e where ID = :id"); + q.setInteger("id", id); + return (T) q.uniqueResult(); + } + + @Override + public Query getQuery(String query) { + return sessionFactory.getCurrentSession().createQuery(query); + } + + @SuppressWarnings("unchecked") + @Override + public List execQuery(String query) { + return sessionFactory.getCurrentSession().createQuery(query).list(); + } + + @SuppressWarnings("unchecked") + @Override + public List getAll() { + return sessionFactory.getCurrentSession().createQuery("from " + getTableName()).list(); + } + +} diff --git a/src/main/java/info/bukova/isspst/dao/jpa/RoleDaoJPA.java b/src/main/java/info/bukova/isspst/dao/jpa/RoleDaoJPA.java new file mode 100644 index 00000000..d57d3da6 --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/jpa/RoleDaoJPA.java @@ -0,0 +1,13 @@ +package info.bukova.isspst.dao.jpa; + +import info.bukova.isspst.dao.RoleDao; +import info.bukova.isspst.data.Role; + +public class RoleDaoJPA extends BaseDaoJPA implements RoleDao { + + @Override + public String getTableName() { + return "ROLE"; + } + +} diff --git a/src/main/java/info/bukova/isspst/dao/jpa/UserDaoJPA.java b/src/main/java/info/bukova/isspst/dao/jpa/UserDaoJPA.java new file mode 100644 index 00000000..1db8d65f --- /dev/null +++ b/src/main/java/info/bukova/isspst/dao/jpa/UserDaoJPA.java @@ -0,0 +1,14 @@ +package info.bukova.isspst.dao.jpa; + +import info.bukova.isspst.dao.UserDao; +import info.bukova.isspst.data.User; + +public class UserDaoJPA extends BaseDaoJPA implements UserDao { + + + @Override + public String getTableName() { + return "USER"; + } + +} diff --git a/src/main/java/info/bukova/isspst/data/DataModel.java b/src/main/java/info/bukova/isspst/data/DataModel.java new file mode 100644 index 00000000..70254945 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/DataModel.java @@ -0,0 +1,14 @@ +package info.bukova.isspst.data; + +import java.util.Date; + +public interface DataModel { + + public void setId(int id); + public int getId(); + public void setCreated(Date created); + public Date getCreated(); + public void setModified(Date modified); + public Date getModified(); + +} diff --git a/src/main/java/info/bukova/isspst/data/Role.java b/src/main/java/info/bukova/isspst/data/Role.java new file mode 100644 index 00000000..bad18950 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/Role.java @@ -0,0 +1,76 @@ +package info.bukova.isspst.data; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.springframework.security.core.GrantedAuthority; + +@Entity +@Table(name="ROLE") +public class Role implements GrantedAuthority, DataModel { + + /** + * + */ + private static final long serialVersionUID = 5677876140880991163L; + + @Id + @Column(name="ID") + @GeneratedValue + private int id; + @Column(name="AUTHORITY") + private String authority; + @Column(name="DESCRIPTION") + private String description; + @Column(name="CREATED") + private Date created; + @Column(name="MODIFIED") + private Date modified; + + @Override + public String getAuthority() { + return authority; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setAuthority(String authority) { + this.authority = authority; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Date getModified() { + return modified; + } + + public void setModified(Date modified) { + this.modified = modified; + } + +} diff --git a/src/main/java/info/bukova/isspst/data/User.java b/src/main/java/info/bukova/isspst/data/User.java new file mode 100644 index 00000000..fbedd064 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/User.java @@ -0,0 +1,123 @@ +package info.bukova.isspst.data; + +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinTable; +import javax.persistence.MapKeyColumn; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.springframework.security.core.userdetails.UserDetails; + +@Entity +@Table(name="USER") +public class User implements UserDetails, DataModel { + + /** + * + */ + private static final long serialVersionUID = 5421234421833765433L; + + @Id + @Column(name="ID") + @GeneratedValue + private int id; + @Column(name="USERNAME") + private String username; + @Column(name="PASSWORD") + private String password; + @Column(name="ENABLED") + private boolean enabled; + @OneToMany @JoinTable(name="USER_ROLE") + @MapKeyColumn(name="ROLE_ID") + private List authorities; + @Column(name="CREATED") + private Date created; + @Column(name="MODIFIED") + private Date modified; + + @Override + public List getAuthorities() { + return authorities; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setAuthorities(List authorities) { + this.authorities = authorities; + } + + public void addAuthority(Role role) { + this.authorities.add(role); + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public Date getModified() { + return modified; + } + + public void setModified(Date modified) { + this.modified = modified; + } + +} diff --git a/src/main/java/info/bukova/isspst/services/AbstractService.java b/src/main/java/info/bukova/isspst/services/AbstractService.java new file mode 100644 index 00000000..85caf381 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/AbstractService.java @@ -0,0 +1,54 @@ +package info.bukova.isspst.services; + +import java.util.Date; +import java.util.List; + +import org.springframework.transaction.annotation.Transactional; + +import info.bukova.isspst.dao.BaseDao; +import info.bukova.isspst.data.DataModel; + +public abstract class AbstractService implements Service { + + protected BaseDao dao; + + public void setDao(BaseDao dao) { + this.dao = dao; + } + + @Override + @Transactional + public void add(T entity) { + entity.setCreated(new Date()); + dao.add(entity); + } + + @Override + @Transactional + public void update(T entity) { + entity.setModified(new Date()); + dao.modify(entity); + } + + @Override + @Transactional + public void delete(T entity) { + dao.delete(entity); + } + + @Override + public List getAll() { + return dao.getAll(); + } + + @Override + public List execQuery(String query) { + return dao.execQuery(query); + } + + @Override + public T selectSingle(String query) { + return null; + } + +} diff --git a/src/main/java/info/bukova/isspst/services/RoleService.java b/src/main/java/info/bukova/isspst/services/RoleService.java new file mode 100644 index 00000000..4171ec00 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/RoleService.java @@ -0,0 +1,7 @@ +package info.bukova.isspst.services; + +import info.bukova.isspst.data.Role; + +public interface RoleService extends Service { + +} diff --git a/src/main/java/info/bukova/isspst/services/RoleServiceImpl.java b/src/main/java/info/bukova/isspst/services/RoleServiceImpl.java new file mode 100644 index 00000000..8d8e4c46 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/RoleServiceImpl.java @@ -0,0 +1,7 @@ +package info.bukova.isspst.services; + +import info.bukova.isspst.data.Role; + +public class RoleServiceImpl extends AbstractService implements RoleService { + +} diff --git a/src/main/java/info/bukova/isspst/services/Service.java b/src/main/java/info/bukova/isspst/services/Service.java new file mode 100644 index 00000000..54d7097c --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/Service.java @@ -0,0 +1,15 @@ +package info.bukova.isspst.services; + +import java.util.List; + +public interface Service { + + public void add(T entity); + public void update(T entity); + public void delete(T entity); + public List getAll(); + public List execQuery(String query); + public T selectSingle(String query); + + +} diff --git a/src/main/java/info/bukova/isspst/services/UserService.java b/src/main/java/info/bukova/isspst/services/UserService.java new file mode 100644 index 00000000..6cff5997 --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/UserService.java @@ -0,0 +1,9 @@ +package info.bukova.isspst.services; + +import info.bukova.isspst.data.User; + +public interface UserService extends Service { + + public void setPassword(User user, String password); + +} diff --git a/src/main/java/info/bukova/isspst/services/UserServiceImpl.java b/src/main/java/info/bukova/isspst/services/UserServiceImpl.java new file mode 100644 index 00000000..7f4e5f8a --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/UserServiceImpl.java @@ -0,0 +1,41 @@ +package info.bukova.isspst.services; + +import org.hibernate.Query; +import org.springframework.security.authentication.encoding.PasswordEncoder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.transaction.annotation.Transactional; + +import info.bukova.isspst.data.User; + +public class UserServiceImpl extends AbstractService implements UserService, UserDetailsService { + + private PasswordEncoder encoder; + + public void setEncoder(PasswordEncoder encoder) { + this.encoder = encoder; + } + + @Override + @Transactional + public UserDetails loadUserByUsername(String username) + throws UsernameNotFoundException { + + Query q = dao.getQuery("from User user where user.username = :username"); + q.setString("username", username); + User u = (User) q.uniqueResult(); + + if (u == null) + throw new UsernameNotFoundException("User " + username + " not found"); + + return u; + } + + @Override + public void setPassword(User user, String password) { + user.setPassword(encoder.encodePassword(password, user.getUsername())); + } + + +} diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml new file mode 100644 index 00000000..76d3d2d2 --- /dev/null +++ b/src/main/resources/hibernate.cfg.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml new file mode 100644 index 00000000..e4394694 --- /dev/null +++ b/src/main/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/jdbc.properties b/src/main/webapp/WEB-INF/jdbc.properties new file mode 100644 index 00000000..3483bc97 --- /dev/null +++ b/src/main/webapp/WEB-INF/jdbc.properties @@ -0,0 +1,5 @@ +jdbc.driverClassName=com.mysql.jdbc.Driver +jdbc.dialect=org.hibernate.dialect.MySQLDialect +jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/isspst +jdbc.username=root +jdbc.password=xsacfgd \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml b/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml new file mode 100644 index 00000000..a5b3da82 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml new file mode 100644 index 00000000..abab89a5 --- /dev/null +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + classpath:hibernate.cfg.xml + + + + ${jdbc.dialect} + true + create + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/views/home.jsp b/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 00000000..47833836 --- /dev/null +++ b/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,14 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ page session="false" %> + + + Home + + +

+ Hello world! +

+ +

The time on the server is ${serverTime}.

+ + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..767cbba3 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,189 @@ + + + + + + contextConfigLocation + /WEB-INF/spring/root-context.xml + + + + org.zkoss.spring.web.context.CoreContextListener + + + + + org.springframework.web.context.ContextLoaderListener + + + + + ZK listener for session cleanup + org.zkoss.zk.ui.http.HttpSessionListener + + + ZK loader for ZUML pages + zkLoader + org.zkoss.zk.ui.http.DHtmlLayoutServlet + + + + update-uri + /zkau + + + + 1 + + + zkLoader + *.zul + + + zkLoader + *.zhtml + + + + The asynchronous update engine for ZK + auEngine + org.zkoss.zk.au.http.DHtmlUpdateServlet + + + + + + auEngine + /zkau/* + + + + + appServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/spring/appServlet/servlet-context.xml + + 1 + + + + appServlet + / + + + + doc + application/vnd.ms-word + + + gif + image/gif + + + htm + text/html + + + html + text/html + + + jpeg + image/jpeg + + + jpg + image/jpeg + + + js + text/javascript + + + pdf + application/pdf + + + png + image/png + + + txt + text/plain + + + xls + application/vnd.ms-excel + + + xml + text/xml + + + zhtml + text/html + + + zul + text/html + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + + springSecurityFilterChain + /* + + + + index.zul + index.zhtml + index.html + index.htm + + + diff --git a/src/test/resources/log4j.xml b/src/test/resources/log4j.xml new file mode 100644 index 00000000..10476512 --- /dev/null +++ b/src/test/resources/log4j.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +