Báze pro UI. Přidaná podpora validace. Podpora filtrování pře LambdaJ.

multitenant
Josef Rokos 11 years ago
parent 2ca878f52d
commit c5c8aacd31

@ -232,6 +232,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.googlecode.lambdaj</groupId>
<artifactId>lambdaj</artifactId>
<version>2.2</version>
</dependency>
<!-- Test -->
<dependency>

@ -13,6 +13,6 @@ public interface BaseDao<T> {
public List<T> getAll();
public Query getQuery(String query);
public List<T> execQuery(String query);
public String getTableName();
public String getEntityName();
}

@ -0,0 +1,18 @@
package info.bukova.isspst.dao;
@SuppressWarnings("serial")
public class IntegrityException extends RuntimeException {
public IntegrityException(String message) {
super( message );
}
public IntegrityException(Throwable root) {
super( root );
}
public IntegrityException(String message, Throwable root) {
super( message, root );
}
}

@ -33,7 +33,8 @@ public abstract class BaseDaoJPA<T> implements BaseDao<T> {
@SuppressWarnings("unchecked")
@Override
public T getById(int id) {
Query q = sessionFactory.getCurrentSession().createQuery("from " + getTableName() + " e where ID = :id");
sessionFactory.getCurrentSession().enableFetchProfile("fetch-all");
Query q = sessionFactory.getCurrentSession().createQuery("from " + getEntityName() + " e where ID = :id");
q.setInteger("id", id);
return (T) q.uniqueResult();
}
@ -52,7 +53,7 @@ public abstract class BaseDaoJPA<T> implements BaseDao<T> {
@SuppressWarnings("unchecked")
@Override
public List<T> getAll() {
return sessionFactory.getCurrentSession().createQuery("from " + getTableName()).list();
return sessionFactory.getCurrentSession().createQuery("from " + getEntityName()).list();
}
}

@ -6,8 +6,8 @@ import info.bukova.isspst.data.Role;
public class RoleDaoJPA extends BaseDaoJPA<Role> implements RoleDao {
@Override
public String getTableName() {
return "ROLE";
public String getEntityName() {
return "Role";
}
}

@ -7,8 +7,8 @@ public class UserDaoJPA extends BaseDaoJPA<User> implements UserDao {
@Override
public String getTableName() {
return "USER";
public String getEntityName() {
return "User";
}
}

@ -10,5 +10,7 @@ public interface DataModel {
public Date getCreated();
public void setModified(Date modified);
public Date getModified();
public boolean isValid();
public void setValid(boolean valid);
}

@ -7,6 +7,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.security.core.GrantedAuthority;
@ -23,7 +24,7 @@ public class Role implements GrantedAuthority, DataModel {
@Column(name="ID")
@GeneratedValue
private int id;
@Column(name="AUTHORITY")
@Column(name="AUTHORITY", unique=true)
private String authority;
@Column(name="DESCRIPTION")
private String description;
@ -31,6 +32,8 @@ public class Role implements GrantedAuthority, DataModel {
private Date created;
@Column(name="MODIFIED")
private Date modified;
@Transient
private boolean valid;
@Override
public String getAuthority() {
@ -73,4 +76,12 @@ public class Role implements GrantedAuthority, DataModel {
this.modified = modified;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}

@ -1,16 +1,19 @@
package info.bukova.isspst.data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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 javax.persistence.Transient;
import org.springframework.security.core.userdetails.UserDetails;
@ -27,19 +30,25 @@ public class User implements UserDetails, DataModel {
@Column(name="ID")
@GeneratedValue
private int id;
@Column(name="USERNAME")
@Column(name="USERNAME", unique=true)
private String username;
@Column(name="PASSWORD")
private String password;
@Column(name="ENABLED")
private boolean enabled;
@OneToMany @JoinTable(name="USER_ROLE")
@OneToMany(fetch=FetchType.EAGER) @JoinTable(name="USER_ROLE")
@MapKeyColumn(name="ROLE_ID")
private List<Role> authorities;
@Column(name="CREATED")
private Date created;
@Column(name="MODIFIED")
private Date modified;
@Transient
private boolean valid;
public User() {
authorities = new ArrayList<Role>();
}
@Override
public List<Role> getAuthorities() {
@ -120,4 +129,12 @@ public class User implements UserDetails, DataModel {
this.modified = modified;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}

@ -0,0 +1,10 @@
package info.bukova.isspst.filters;
import org.hamcrest.TypeSafeMatcher;
public interface Filter<T> {
public TypeSafeMatcher<T> matcher();
public String queryString();
}

@ -1,16 +1,28 @@
package info.bukova.isspst.services;
import static ch.lambdaj.Lambda.filter;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.springframework.transaction.annotation.Transactional;
import info.bukova.isspst.dao.BaseDao;
import info.bukova.isspst.dao.IntegrityException;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.filters.Filter;
public abstract class AbstractService<T extends DataModel> implements Service<T> {
protected BaseDao<T> dao;
private Validator validator;
public void setDao(BaseDao<T> dao) {
this.dao = dao;
@ -33,22 +45,73 @@ public abstract class AbstractService<T extends DataModel> implements Service<T>
@Override
@Transactional
public void delete(T entity) {
dao.delete(entity);
try {
dao.delete(entity);
} catch (HibernateException e) {
throw new IntegrityException(e);
}
}
@Override
public void validate(T entity) {
if (validator == null) {
entity.setValid(true);
return;
}
Set<ConstraintViolation<T>> violations = validator.validate(entity);
if (!violations.isEmpty()) {
ValidationException ex = new ValidationException();
for(ConstraintViolation<T> v : violations) {
String errKye = v.getPropertyPath().toString();
ex.addMessage(errKye.isEmpty() ? "CLASS_ERR" : errKye, v.getMessage());
}
throw ex;
}
entity.setValid(true);
}
@Override
public List<T> filterList(List<T> sourceList, Filter<T> filter) {
return filter(filter.matcher(), sourceList);
}
@Override
@Transactional
public T getById(int id) {
return dao.getById(id);
}
@Override
@Transactional
public List<T> getAll() {
return dao.getAll();
}
@Override
@Transactional
public List<T> execQuery(String query) {
return dao.execQuery(query);
}
@SuppressWarnings("unchecked")
@Override
@Transactional
public T selectSingle(String query) {
return null;
try {
Query q = dao.getQuery(query);
return (T) q.uniqueResult();
} catch (NonUniqueResultException e) {
return null;
}
}
public void setValidator(Validator validator) {
this.validator = validator;
}
}

@ -4,4 +4,6 @@ import info.bukova.isspst.data.Role;
public interface RoleService extends Service<Role> {
public Role getRoleByAuthority(String authority);
}

@ -1,7 +1,15 @@
package info.bukova.isspst.services;
import org.springframework.transaction.annotation.Transactional;
import info.bukova.isspst.data.Role;
public class RoleServiceImpl extends AbstractService<Role> implements RoleService {
@Override
@Transactional
public Role getRoleByAuthority(String authority) {
return this.selectSingle("from Role where authority = '" + authority + "'");
}
}

@ -1,5 +1,7 @@
package info.bukova.isspst.services;
import info.bukova.isspst.filters.Filter;
import java.util.List;
public interface Service<T> {
@ -7,9 +9,11 @@ public interface Service<T> {
public void add(T entity);
public void update(T entity);
public void delete(T entity);
public void validate(T entity) throws ValidationException;
public List<T> getAll();
public List<T> execQuery(String query);
public T selectSingle(String query);
public T getById(int id);
public List<T> filterList(List<T> sourceList, Filter<T> filter);
}

@ -5,5 +5,7 @@ import info.bukova.isspst.data.User;
public interface UserService extends Service<User> {
public void setPassword(User user, String password);
public boolean hasRole(User user, String authority);
public void test();
}

@ -1,12 +1,15 @@
package info.bukova.isspst.services;
import org.hibernate.Query;
import org.springframework.security.access.annotation.Secured;
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.Constants;
import info.bukova.isspst.data.Role;
import info.bukova.isspst.data.User;
public class UserServiceImpl extends AbstractService<User> implements UserService, UserDetailsService {
@ -37,5 +40,21 @@ public class UserServiceImpl extends AbstractService<User> implements UserServic
user.setPassword(encoder.encodePassword(password, user.getUsername()));
}
@Override
public boolean hasRole(User user, String authority) {
for (Role r : user.getAuthorities()) {
if (r.getAuthority().equals(authority)) {
return true;
}
}
return false;
}
@Override
@Secured(Constants.ROLE_ADMIN)
public void test() {
System.out.println("pokus secured");
}
}

@ -0,0 +1,28 @@
package info.bukova.isspst.services;
import java.util.HashMap;
import java.util.Map;
public class ValidationException extends RuntimeException {
private static final long serialVersionUID = -2251676786418660137L;
private Map<String, String> messages;
public ValidationException() {
messages = new HashMap<String, String>();
}
public void addMessage(String property, String message) {
messages.put(property, message);
}
public Map<String, String> getMessages() {
return messages;
}
public void setMessages(Map<String, String> messages) {
this.messages = messages;
}
}

@ -0,0 +1,61 @@
package info.bukova.isspst.ui;
import info.bukova.isspst.dao.IntegrityException;
import info.bukova.isspst.services.Service;
import info.bukova.isspst.services.ValidationException;
import java.util.Map;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
public class FormViewModel<T> {
private T dataBean;
private Map<String, String> errMessages;
private Service<T> service;
@Init
public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service<T> service) {
this.dataBean = selected;
this.service = service;
}
public T getDataBean() {
return dataBean;
}
@Command
@NotifyChange("errMessages")
public void save(@BindingParam("window") Window win) {
try {
service.update(dataBean);
win.detach();
} catch (ValidationException e) {
errMessages = e.getMessages();
String classErr = errMessages.get("CLASS_ERR");
if (classErr == null) {
classErr = "";
}
Messagebox.show("Chyba validace", "Error", Messagebox.OK, Messagebox.ERROR);
} catch (IntegrityException e) {
e.printStackTrace();
}
}
public Map<String, String> getErrMessages() {
return errMessages;
}
protected void setErrMesages(Map<String, String> msgs) {
this.errMessages = msgs;
}
}

@ -0,0 +1,263 @@
package info.bukova.isspst.ui;
import info.bukova.isspst.dao.IntegrityException;
import info.bukova.isspst.data.DataModel;
import info.bukova.isspst.filters.Filter;
import info.bukova.isspst.services.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
public class ListViewModel<T extends DataModel> {
private boolean confirmDelete = false;
private boolean filter = false;
private Window editWin;
private T dataBean;
//private T filterTemplate;
private Filter<T> dataFilter;
private T editBean;
private List<T> dataList;
private List<T> fullList;
private List<T> tmpList;
private int selIndex = -1;
private String sortCol;
private boolean sortDesc = true;
private boolean newRec = false;
private boolean fullFill = false;
protected Service<T> service;
protected Class<T> dataClass;
protected String formZul;
public List<T> getDataList() {
if (dataList == null) {
dataList = new ArrayList<T>();
loadFromDb();
}
return dataList;
}
public void setDataBean(T data) {
this.dataBean = data;
}
public void setDataFilter(Filter<T> dataFilter) {
this.dataFilter = dataFilter;
}
public T getDataBean() {
return dataBean;
}
public boolean getConfirmDelete() {
return confirmDelete;
}
public boolean getFilter() {
return this.filter;
}
public boolean getFullFill() {
return fullFill;
}
protected void newRecMode() {
newRec = true;
}
protected void setEditBean(T edit) {
this.editBean = edit;
}
@Command
@NotifyChange({"filter", "dataList", "dataBean"})
public void filter() {
filter = !filter;
if (!filter) {
dataList = fullList;
dataBean = null;
selIndex = -1;
} else {
doFilter();
dataBean = null;
}
}
@Command
@NotifyChange("dataList")
public void doFilter() {
if (dataFilter == null) {
return;
}
List<T> result = service.filterList(fullList, dataFilter);
selIndex = -1;
dataList = result;
}
@Command
public void addNew() {
try {
newRecMode();
editBean = dataClass.newInstance();
showForm();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Command
public void edit() {
int index = dataList.indexOf(dataBean);
if (index != -1) {
selIndex = index;
}
editBean = service.getById(dataBean.getId());
showForm();
}
@Command
@NotifyChange("confirmDelete")
public void delObject() {
confirmDelete = true;
}
@Command
@NotifyChange({"dataList", "dataBean"})
public void delete() {
try {
service.delete(dataBean);
dataList.remove(dataBean);
dataBean = null;
} catch (IntegrityException e) {
Messagebox.show("Error while deleting object", "Error", Messagebox.OK, Messagebox.ERROR);
}
confirmDelete = false;
}
@GlobalCommand
@NotifyChange({"dataList", "dataBean"})
public void refresh() {
if (editBean != null && !editBean.isValid()) {
return;
}
if (!filter && newRec) {
dataList.add(editBean);
if (dataList != fullList) {
fullList.add(editBean);
}
} else if (newRec) {
fullList.add(editBean);
}
dataBean = editBean;
if (!newRec) {
dataList.set(selIndex, editBean);
}
}
@GlobalCommand
@NotifyChange({"dataList", "dataBean", "fullFill"})
public void reload() {
dataBean = null;
dataList.clear();
loadFromDbSync();
}
@Command
@NotifyChange("selIndex")
public void afterRender() {
if (editBean != null && !editBean.isValid()) {
return;
}
if (selIndex > dataList.size() -1) {
selIndex = -1;
}
if (newRec) {
selIndex = dataList.size() -1;
newRec = false;
}
}
@Command
@NotifyChange("dataBean")
public void onSort(@BindingParam("column") String column) {
if (sortCol == null || this.sortCol.equals(column))
this.sortDesc = !this.sortDesc;
this.sortCol = column;
selIndex = -1;
dataBean = null;
}
public int getSelIndex() {
return this.selIndex;
}
@Command
@NotifyChange({"dataList", "fullFill"})
public void fullFill() {
if (fullFill && dataList.isEmpty()) {
dataList.addAll(tmpList);
fullList = dataList;
}
}
private void loadFromDb() {
Thread fillThread = new Thread(new Runnable() {
@Override
public void run() {
tmpList = service.getAll();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
fullFill = true;
}
});
fillThread.start();
}
private void loadFromDbSync() {
dataList.addAll(service.getAll());
}
protected void showForm() {
Map<String, Object> arg = new HashMap<String, Object>();
arg.put("selected", editBean);
arg.put("service", service);
editWin = (Window) Executions.createComponents(formZul, null, arg);
editWin.doModal();
}
protected List<T> getFullList() {
return fullList;
}
protected void setFullFill(boolean fullFill) {
this.fullFill = fullFill;
}
}

@ -0,0 +1,18 @@
package info.bukova.isspst.ui;
import org.zkoss.bind.annotation.Command;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import info.bukova.isspst.services.UserService;
public class TestVM {
@WireVariable
private UserService userService;
@Command
public void test() {
userService.test();
}
}

@ -38,7 +38,7 @@
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
@ -50,10 +50,13 @@
</bean>
<!-- Security -->
<security:global-method-security secured-annotations="enabled" />
<security:http auto-config="true">
<security:intercept-url pattern="/app/**" access="ROLE_USER"/>
<security:intercept-url pattern="/api/remote/**" access="ROLE_USER"/>
<security:form-login login-page="/login.zhtml"/>
<security:intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<security:form-login login-page="/login.zhtml"
authentication-failure-handler-ref="loginFail"/>
<security:http-basic/>
<security:logout invalidate-session="true"/>
</security:http>
@ -73,6 +76,8 @@
</bean>
<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"/>
<bean id="loginFail" class="info.bukova.isspst.LoginFailHandler"/>
<!-- DAO -->
<bean id="userDao" class="info.bukova.isspst.dao.jpa.UserDaoJPA">
@ -86,6 +91,11 @@
<!-- Business logic -->
<bean id="userService" class="info.bukova.isspst.services.UserServiceImpl">
<property name="dao" ref="userDao"/>
<property name="encoder" ref="passwordEncoder"/>
</bean>
<bean id="roleService" class="info.bukova.isspst.services.RoleServiceImpl">
<property name="dao" ref="roleDao"/>
</bean>
</beans>

@ -18,6 +18,10 @@
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>info.bukova.isspst.DbInitListener</listener-class>
</listener>
<!-- ZK -->
<listener>
<description>ZK listener for session cleanup</description>
@ -109,7 +113,7 @@
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/api/</url-pattern>
</servlet-mapping>
<mime-mapping>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Objednávky</title>
</head>
<body>
administrace
</body>
</html>

@ -0,0 +1,10 @@
<?page title="Okno" contentType="text/html;charset=UTF-8"?>
<zk>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<window title="Okno" border="normal" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('info.bukova.isspst.ui.TestVM')">
<label value="Pokus pokus"/>
<button label="metoda" onClick="@command('test')"/>
</window>
<include src="../app/testApp.zul"/>
</zk>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Objednávky</title>
</head>
<body>
aplikace
</body>
</html>

@ -0,0 +1,6 @@
<?page title="Test" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Test" border="normal">
New Content Here!
</window>
</zk>

@ -0,0 +1,15 @@
<html xmlns="native" xmlns:u="zul" xmlns:zk="zk">
<head>
<title>Objednávky</title>
</head>
<body style="height: 100%; padding: 0 5px;">
<div style="height: 15%"/>
<div align="center">
<u:include src="login.zul"/>
</div>
</body>
</html>

@ -0,0 +1,33 @@
<?page title="Přihlášení" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Přihlášení" border="normal" width="300px">
<html style="font-family:arial,sans-serif;font-size:12px;">
<![CDATA[
<form name='loginForm' action="j_spring_security_check"
method='POST'>
<table>
<tr>
<td>Uživatel:</td>
<td><input type='text' name='j_username' value=''/>
</td>
</tr>
<tr>
<td>Heslo:</td>
<td><input type='password' name='j_password' />
</td>
</tr>
<tr>
<td colspan='2'><input name="submit" type="submit"
value="Přihlásit" />
</td>
</tr>
</table>
</form>
]]>
</html>
</window>
</zk>
Loading…
Cancel
Save