From 9abe1ab6a4f9601a611e373d433a5a715890d4ea Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Mon, 5 May 2014 21:06:15 +0200 Subject: [PATCH 1/6] =?UTF-8?q?Toolbar=20p=C5=99em=C3=ADst=C4=9Bn=20do=20s?= =?UTF-8?q?amostatn=C3=A9ho=20souboru.=20M=C3=ADrn=C3=A1=20=C3=BAprava=20l?= =?UTF-8?q?ayoutu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/webapp/admin/users/users.zul | 12 +++--------- src/main/webapp/app/page.css | 17 +++++++++++++---- src/main/webapp/app/template.zhtml | 3 +++ src/main/webapp/app/toolbar.zul | 9 +++++++++ 4 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 src/main/webapp/app/toolbar.zul diff --git a/src/main/webapp/admin/users/users.zul b/src/main/webapp/admin/users/users.zul index b7f4f7e0..49a93ba1 100644 --- a/src/main/webapp/admin/users/users.zul +++ b/src/main/webapp/admin/users/users.zul @@ -2,16 +2,10 @@ - - - - - - - - + viewModel="@id('vm') @init('info.bukova.isspst.ui.UsersList')" height="570px"> + + diff --git a/src/main/webapp/app/page.css b/src/main/webapp/app/page.css index 9996d2b4..c633edb5 100644 --- a/src/main/webapp/app/page.css +++ b/src/main/webapp/app/page.css @@ -1,10 +1,15 @@ -body { +/*body { font-family: sans-serif; +}*/ + +#container { + min-height:100%; + position:relative; } #header{ width: auto; - height:70px; + height:40px; border: 1px solid #ccc; padding:10px; } @@ -23,13 +28,17 @@ body { #maincolumn{ padding:10px; + padding-bottom:20px; /* Height of the footer */ margin: 0px 0px 0px 160px; - border: 1px solid #ccc; } #footer{ clear:both; - width:auto; + position:absolute; + bottom:0; + width:100%; + height:20px; /* Height of the footer */ + border: 1px solid #ccc; } /* diff --git a/src/main/webapp/app/template.zhtml b/src/main/webapp/app/template.zhtml index a1cd3ab8..22fa7376 100644 --- a/src/main/webapp/app/template.zhtml +++ b/src/main/webapp/app/template.zhtml @@ -2,6 +2,7 @@ +
@@ -11,11 +12,13 @@
+
+ \ No newline at end of file diff --git a/src/main/webapp/app/toolbar.zul b/src/main/webapp/app/toolbar.zul new file mode 100644 index 00000000..7c4684e0 --- /dev/null +++ b/src/main/webapp/app/toolbar.zul @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file From 202825322a35986eb4c89204d172eb2e6ae18371 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sat, 10 May 2014 23:46:07 +0200 Subject: [PATCH 2/6] =?UTF-8?q?Opravy=20b=C3=A1zov=C3=BDch=20t=C5=99=C3=AD?= =?UTF-8?q?d.=20Toolbar=20v=20samostatn=C3=A9m=20souboru.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../isspst/services/AbstractService.java | 25 +++--- .../info/bukova/isspst/ui/FormViewModel.java | 21 +++-- .../info/bukova/isspst/ui/ListViewModel.java | 79 ++++++++++++------ src/main/webapp/WEB-INF/jdbc.properties | 2 +- src/main/webapp/app/form.css | 51 +++++++++++ src/main/webapp/app/toolbar.zul | 2 +- src/main/webapp/img/save.png | Bin 0 -> 719 bytes 7 files changed, 135 insertions(+), 45 deletions(-) create mode 100644 src/main/webapp/app/form.css create mode 100644 src/main/webapp/img/save.png diff --git a/src/main/java/info/bukova/isspst/services/AbstractService.java b/src/main/java/info/bukova/isspst/services/AbstractService.java index 163873f6..4119d093 100644 --- a/src/main/java/info/bukova/isspst/services/AbstractService.java +++ b/src/main/java/info/bukova/isspst/services/AbstractService.java @@ -1,6 +1,9 @@ package info.bukova.isspst.services; import static ch.lambdaj.Lambda.filter; +import info.bukova.isspst.dao.BaseDao; +import info.bukova.isspst.data.DataModel; +import info.bukova.isspst.filters.Filter; import java.util.Date; import java.util.List; @@ -9,16 +12,10 @@ 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 implements Service { protected BaseDao dao; @@ -34,22 +31,22 @@ public abstract class AbstractService implements Service entity.setCreated(new Date()); dao.add(entity); } - + @Override @Transactional public void update(T entity) { - entity.setModified(new Date()); - dao.modify(entity); + if (entity.getCreated() == null) { + add(entity); + } else { + entity.setModified(new Date()); + dao.modify(entity); + } } @Override @Transactional public void delete(T entity) { - try { - dao.delete(entity); - } catch (HibernateException e) { - throw new IntegrityException(e); - } + dao.delete(entity); } @Override diff --git a/src/main/java/info/bukova/isspst/ui/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index 48e01e56..c4bd0e8e 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -1,11 +1,12 @@ package info.bukova.isspst.ui; -import info.bukova.isspst.dao.IntegrityException; +import info.bukova.isspst.data.DataModel; import info.bukova.isspst.services.Service; import info.bukova.isspst.services.ValidationException; import java.util.Map; +import org.springframework.dao.DataIntegrityViolationException; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.ExecutionArgParam; @@ -14,7 +15,7 @@ import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Window; -public class FormViewModel { +public class FormViewModel { private T dataBean; private Map errMessages; @@ -34,7 +35,7 @@ public class FormViewModel { @NotifyChange("errMessages") public void save(@BindingParam("window") Window win) { try { - service.update(dataBean); + doSave(); win.detach(); } catch (ValidationException e) { errMessages = e.getMessages(); @@ -44,9 +45,11 @@ public class FormViewModel { classErr = ""; } - Messagebox.show("Chyba validace", "Error", Messagebox.OK, Messagebox.ERROR); - } catch (IntegrityException e) { + Messagebox.show("Chyba validace", "Chyba", Messagebox.OK, Messagebox.ERROR); + } catch (DataIntegrityViolationException e) { + dataBean.setCreated(null); e.printStackTrace(); + Messagebox.show("Chyba při ukládání záznamu", "Chyba", Messagebox.OK, Messagebox.ERROR); } } @@ -57,5 +60,13 @@ public class FormViewModel { protected void setErrMesages(Map msgs) { this.errMessages = msgs; } + + protected void doSave() { + service.update(dataBean); + } + + public boolean isCanSave() { + return true; + } } diff --git a/src/main/java/info/bukova/isspst/ui/ListViewModel.java b/src/main/java/info/bukova/isspst/ui/ListViewModel.java index bdc32a95..bdfbff3a 100644 --- a/src/main/java/info/bukova/isspst/ui/ListViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/ListViewModel.java @@ -1,6 +1,5 @@ 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; @@ -10,11 +9,15 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.springframework.dao.DataIntegrityViolationException; +import org.zkoss.bind.BindUtils; 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.zk.ui.event.Event; +import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Window; @@ -125,6 +128,7 @@ public class ListViewModel { @Command public void edit() { int index = dataList.indexOf(dataBean); + newRec = false; if (index != -1) { selIndex = index; } @@ -141,20 +145,46 @@ public class ListViewModel { @Command @NotifyChange({"dataList", "dataBean"}) public void delete() { + Messagebox.show("Opravdu smazat?", "Smazat záznam", Messagebox.YES|Messagebox.NO, + Messagebox.QUESTION, new EventListener() { + + @Override + public void onEvent(Event evt) throws Exception { + if (((Integer)evt.getData()).intValue() == Messagebox.YES) { + try { + service.delete(dataBean); + dataList.remove(dataBean); + dataBean = null; + BindUtils.postNotifyChange(null, null, ListViewModel.this, "dataList"); + BindUtils.postNotifyChange(null, null, ListViewModel.this, "dataBean"); + } catch (DataIntegrityViolationException e) { + Messagebox.show("Chyba při mazání záznamu", "Chyba", Messagebox.OK, Messagebox.ERROR); + } + } + } + }); + + confirmDelete = false; + } + + @NotifyChange({"dataList", "dataBean"}) + private void onDelete() { try { service.delete(dataBean); dataList.remove(dataBean); dataBean = null; - } catch (IntegrityException e) { - Messagebox.show("Error while deleting object", "Error", Messagebox.OK, Messagebox.ERROR); + } catch (DataIntegrityViolationException e) { + Messagebox.show("Chyba při mazání záznamu", "Chyba", Messagebox.OK, Messagebox.ERROR); } - confirmDelete = false; } @GlobalCommand @NotifyChange({"dataList", "dataBean"}) public void refresh() { - if (editBean != null && !editBean.isValid()) { + /*if (editBean != null && !editBean.isValid()) { + return; + }*/ + if (editBean.getCreated() == null) { return; } if (!filter && newRec) { @@ -220,28 +250,29 @@ public class ListViewModel { } } - 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 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()); + fullList = dataList; } protected void showForm() { diff --git a/src/main/webapp/WEB-INF/jdbc.properties b/src/main/webapp/WEB-INF/jdbc.properties index 3483bc97..658408f0 100644 --- a/src/main/webapp/WEB-INF/jdbc.properties +++ b/src/main/webapp/WEB-INF/jdbc.properties @@ -1,5 +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.databaseurl=jdbc:mysql://127.0.0.1:3306/isspst?characterEncoding=utf8 jdbc.username=root jdbc.password=xsacfgd \ No newline at end of file diff --git a/src/main/webapp/app/form.css b/src/main/webapp/app/form.css new file mode 100644 index 00000000..4bd4c0c0 --- /dev/null +++ b/src/main/webapp/app/form.css @@ -0,0 +1,51 @@ +.nicebutton { +font-family: Arial; +color: #050005; +font-size: 12px; +padding-top: 3px; +padding-right: 10px; +padding-bottom: 3px; +padding-left: 10px; +margin: 4px; +text-decoration: none; +-webkit-border-radius: 5px; +-moz-border-radius: 5px; +border-radius: 5px; +-webkit-box-shadow: 0px 1px 3px #666666; +-moz-box-shadow: 0px 1px 3px #666666; +box-shadow: 0px 1px 3px #666666; +text-shadow: 1px 1px 2px #666666; +border: solid #8abaed 1px; +background: -webkit-gradient(linear, 0 0, 0 100%, from(#ffffff), to(#efefef)); +background: -moz-linear-gradient(top, #ffffff, #efefef); +-ms-filter: progid:DXImageTransform.Microsoft.gradient(startColorStr=#ffffff, endColorStr=#efefef); +filter: progid:DXImageTransform.Microsoft.gradient(startColorStr=#ffffff, endColorStr=#efefef); +display:inline-block; /* IE is so silly */ +} +.nicebutton:hover { +background: #c5e8fa; +} +.nicebutton:disabled { +font-family: Arial; +color: #c4c4c4; +font-size: 12px; +padding-top: 3px; +padding-right: 10px; +padding-bottom: 3px; +padding-left: 10px; +margin: 5px; +text-decoration: none; +-webkit-border-radius: 5px; +-moz-border-radius: 5px; +border-radius: 5px; +-webkit-box-shadow: 0px 1px 3px #666666; +-moz-box-shadow: 0px 1px 3px #666666; +text-shadow: 0px 0px 0px #9e9e9e; +box-shadow: 0px 1px 3px #666666; +border: solid #d91c71 0px; +background: #ebebeb; +} + +.error { + color: red; +} \ No newline at end of file diff --git a/src/main/webapp/app/toolbar.zul b/src/main/webapp/app/toolbar.zul index 7c4684e0..2c0497d1 100644 --- a/src/main/webapp/app/toolbar.zul +++ b/src/main/webapp/app/toolbar.zul @@ -3,7 +3,7 @@ - +
\ No newline at end of file diff --git a/src/main/webapp/img/save.png b/src/main/webapp/img/save.png new file mode 100644 index 0000000000000000000000000000000000000000..024475d50ee264b491a100d646b2269d8d98ee01 GIT binary patch literal 719 zcmV;=0xA1{-&eKL`K-0$fQ% zK~y-)jgq@-6j2n$e`jXy&g{;*W=T}sBoZT0@R251sEA@JvyH^c|G-8N5km^A;GcvCkH*F+};(rcXmQatDXe&GE(h9!ck z{wg3uVaUyE*Z+$^Zcm~=2^2^aa-Jcmec_K9C}tO?B!yAgCxwI++4#LbjuiMN>jha=uGg4^}Ox z7^a`x<-*u`jB)WY9vNd$Ac|w+C}Lz}gw7+QXw+b4wuY?&0uH3VX%gcYn@bnR_%LsC zXrPC3xlHgapuN2V)rbU(d^UsUW@v?tbh#8+Nz`+*_}L5__MgNdrrBx|M&W8@wuSFH zSeBs6QCeXbhdBe5fa`i>^F{RORWMnattJ4*m{h>A@mzy#+bd<+X7j#BmD&1nZ}C-T zeWAeCo?dqE-b2pMQTaU0LT!PWFJF24<_$~P?F6-Xs?~tGxq$hvpT_ BM3w*m literal 0 HcmV?d00001 From 6462fb3806d4a35adff1998d6fbd343bbbf6cb08 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sun, 11 May 2014 00:54:42 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Agenda=20na=20spr=C3=A1vu=20u=C5=BEivatel?= =?UTF-8?q?=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/info/bukova/isspst/Constants.java | 21 ++++- .../info/bukova/isspst/DbInitListener.java | 37 ++++---- .../info/bukova/isspst/HomeController.java | 2 +- .../java/info/bukova/isspst/StringUtils.java | 13 +++ .../java/info/bukova/isspst/data/Role.java | 18 ++++ .../java/info/bukova/isspst/data/User.java | 65 ++++++++++++-- .../bukova/isspst/filters/UserFilter.java | 57 ++++++++++++ .../bukova/isspst/services/UserService.java | 11 --- .../services/{ => users}/RoleService.java | 3 +- .../services/{ => users}/RoleServiceImpl.java | 3 +- .../isspst/services/users/UserService.java | 14 +++ .../services/{ => users}/UserServiceImpl.java | 15 ++-- .../info/bukova/isspst/ui/ListViewModel.java | 18 +++- .../java/info/bukova/isspst/ui/UserForm.java | 14 --- .../bukova/isspst/ui/users/RoleCheck.java | 43 +++++++++ .../info/bukova/isspst/ui/users/UserForm.java | 90 +++++++++++++++++++ .../bukova/isspst/ui/users/UserRoles.java | 24 +++++ .../isspst/ui/{ => users}/UsersList.java | 7 +- .../webapp/WEB-INF/spring/root-context.xml | 4 +- src/main/webapp/admin/users/userForm.zul | 32 ++++++- src/main/webapp/admin/users/users.zul | 30 ++++++- 21 files changed, 447 insertions(+), 74 deletions(-) create mode 100644 src/main/java/info/bukova/isspst/StringUtils.java create mode 100644 src/main/java/info/bukova/isspst/filters/UserFilter.java delete mode 100644 src/main/java/info/bukova/isspst/services/UserService.java rename src/main/java/info/bukova/isspst/services/{ => users}/RoleService.java (62%) rename src/main/java/info/bukova/isspst/services/{ => users}/RoleServiceImpl.java (78%) create mode 100644 src/main/java/info/bukova/isspst/services/users/UserService.java rename src/main/java/info/bukova/isspst/services/{ => users}/UserServiceImpl.java (79%) delete mode 100644 src/main/java/info/bukova/isspst/ui/UserForm.java create mode 100644 src/main/java/info/bukova/isspst/ui/users/RoleCheck.java create mode 100644 src/main/java/info/bukova/isspst/ui/users/UserForm.java create mode 100644 src/main/java/info/bukova/isspst/ui/users/UserRoles.java rename src/main/java/info/bukova/isspst/ui/{ => users}/UsersList.java (60%) diff --git a/src/main/java/info/bukova/isspst/Constants.java b/src/main/java/info/bukova/isspst/Constants.java index 2d1b3923..26c5ca28 100644 --- a/src/main/java/info/bukova/isspst/Constants.java +++ b/src/main/java/info/bukova/isspst/Constants.java @@ -1,10 +1,29 @@ package info.bukova.isspst; +import info.bukova.isspst.data.Role; + public class Constants { public final static String DEF_ADMIN = "admin"; public final static String DEF_ADMIN_PASSWD = "admin"; public final static String ROLE_USER = "ROLE_USER"; public final static String ROLE_ADMIN = "ROLE_ADMIN"; - public final static String ROLES[] = {ROLE_USER, ROLE_ADMIN}; + public final static String ROLE_DIRECTOR = "ROLE_DIRECTOR"; + public final static String ROLE_MANAGER = "ROLE_MANAGER"; + public final static String ROLE_PRINCIPAL = "ROLE_PRINCIPAL"; + public final static String ROLE_ACCOUNTANT = "ROLE_ACCOUNTANT"; + public final static String ROLE_SUPPLIER = "ROLE_SUPPLIER"; + public final static String ROLE_TECHNICIAN = "ROLE_TECHNICIAN"; + public final static String ROLE_LEADER = "ROLE_LEADER"; + public final static Role ROLES[] = { + new Role(ROLE_ADMIN, "Administrátor"), + new Role(ROLE_DIRECTOR, "Ředitel"), + new Role(ROLE_MANAGER, "Správce"), + new Role(ROLE_PRINCIPAL, "Příkazce"), + new Role(ROLE_ACCOUNTANT, "Účetní"), + new Role(ROLE_SUPPLIER, "Zásobovač"), + new Role(ROLE_TECHNICIAN, "Technik"), + new Role(ROLE_LEADER, "Vedoucí"), + new Role(ROLE_USER, "Uživatel") + }; } diff --git a/src/main/java/info/bukova/isspst/DbInitListener.java b/src/main/java/info/bukova/isspst/DbInitListener.java index ada4fa8a..da5f8974 100644 --- a/src/main/java/info/bukova/isspst/DbInitListener.java +++ b/src/main/java/info/bukova/isspst/DbInitListener.java @@ -2,14 +2,15 @@ package info.bukova.isspst; import info.bukova.isspst.data.Role; import info.bukova.isspst.data.User; -import info.bukova.isspst.services.RoleService; -import info.bukova.isspst.services.UserService; +import info.bukova.isspst.services.users.RoleService; +import info.bukova.isspst.services.users.UserService; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; @@ -37,16 +38,10 @@ public class DbInitListener implements ServletContextListener { } private void checkRoles() { - Role r = null; - - for (String auth : Constants.ROLES) + for (Role role : Constants.ROLES) { - r = roleService.getRoleByAuthority(auth); - if (r == null) { - r = new Role(); - r.setAuthority(auth); - r.setDescription("---"); - roleService.add(r); + if (roleService.getRoleByAuthority(role.getAuthority()) == null) { + roleService.add(role); } } } @@ -62,12 +57,20 @@ public class DbInitListener implements ServletContextListener { } if (!adminExists) { - User u = new User(); - u.setUsername(Constants.DEF_ADMIN); - u.addAuthority(roleService.getRoleByAuthority(Constants.ROLE_ADMIN)); - u.setEnabled(true); - userService.setPassword(u, Constants.DEF_ADMIN_PASSWD); - userService.add(u); + User u = null; + try { + u = (User) userService.loadUserByUsername(Constants.DEF_ADMIN); + u.addAuthority(roleService.getRoleByAuthority(Constants.ROLE_ADMIN)); + u.setEnabled(true); + userService.update(u); + } catch(UsernameNotFoundException e) { + u = new User(); + u.setUsername(Constants.DEF_ADMIN); + userService.setPassword(u, Constants.DEF_ADMIN_PASSWD); + u.addAuthority(roleService.getRoleByAuthority(Constants.ROLE_ADMIN)); + u.setEnabled(true); + userService.add(u); + } } } diff --git a/src/main/java/info/bukova/isspst/HomeController.java b/src/main/java/info/bukova/isspst/HomeController.java index 0d5b9736..fb3ef412 100644 --- a/src/main/java/info/bukova/isspst/HomeController.java +++ b/src/main/java/info/bukova/isspst/HomeController.java @@ -1,7 +1,7 @@ package info.bukova.isspst; import info.bukova.isspst.data.User; -import info.bukova.isspst.services.UserService; +import info.bukova.isspst.services.users.UserService; import java.text.DateFormat; import java.util.Date; diff --git a/src/main/java/info/bukova/isspst/StringUtils.java b/src/main/java/info/bukova/isspst/StringUtils.java new file mode 100644 index 00000000..2720016f --- /dev/null +++ b/src/main/java/info/bukova/isspst/StringUtils.java @@ -0,0 +1,13 @@ +package info.bukova.isspst; + +public class StringUtils { + + public static String nullStr(String str) { + return str == null ? "" : str; + } + + public static String not0ToStr(long i) { + return i == 0 ? "" : String.valueOf(i); + } + +} diff --git a/src/main/java/info/bukova/isspst/data/Role.java b/src/main/java/info/bukova/isspst/data/Role.java index 6ecc2710..b957606f 100644 --- a/src/main/java/info/bukova/isspst/data/Role.java +++ b/src/main/java/info/bukova/isspst/data/Role.java @@ -34,6 +34,15 @@ public class Role implements GrantedAuthority, DataModel { private Date modified; @Transient private boolean valid; + + public Role(String authority, String description) { + this.authority = authority; + this.description = description; + } + + public Role() { + + } @Override public String getAuthority() { @@ -83,5 +92,14 @@ public class Role implements GrantedAuthority, DataModel { public void setValid(boolean valid) { this.valid = valid; } + + @Override + public boolean equals(Object o) { + if ((o instanceof Role) && ((Role)o).getId() == this.id) { + return true; + } else { + return false; + } + } } diff --git a/src/main/java/info/bukova/isspst/data/User.java b/src/main/java/info/bukova/isspst/data/User.java index 5091b7e9..fc3c20d7 100644 --- a/src/main/java/info/bukova/isspst/data/User.java +++ b/src/main/java/info/bukova/isspst/data/User.java @@ -9,9 +9,9 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.JoinColumn; import javax.persistence.JoinTable; -import javax.persistence.MapKeyColumn; -import javax.persistence.OneToMany; +import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Transient; @@ -36,9 +36,18 @@ public class User implements UserDetails, DataModel { private String password; @Column(name="ENABLED") private boolean enabled; - private String fullName; - @OneToMany(fetch=FetchType.EAGER) @JoinTable(name="USER_ROLE") - @MapKeyColumn(name="ROLE_ID") + @Column(name="FIRST_NAME") + private String firstName; + @Column(name="LAST_NAME") + private String lastName; + @Column(name="PERSONAL_NUMBER") + private String personalNumber; + @Column(name="EMAIL") + private String email; + @Column(name="NOTIFY") + private boolean notify; + @ManyToMany(fetch=FetchType.EAGER) + @JoinTable(name="USER_ROLE", joinColumns={@JoinColumn(name="USER_ID")}, inverseJoinColumns={@JoinColumn(name="ROLE_ID")}) private List authorities; @Column(name="CREATED") private Date created; @@ -139,11 +148,51 @@ public class User implements UserDetails, DataModel { } public String getFullName() { - return fullName; + String ret = ""; + if (firstName != null && !firstName.isEmpty()) { + ret = firstName + " "; + } + return ret + lastName == null ? "" : lastName; } - public void setFullName(String fullName) { - this.fullName = fullName; + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getPersonalNumber() { + return personalNumber; + } + + public void setPersonalNumber(String personalNumber) { + this.personalNumber = personalNumber; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public boolean isNotify() { + return notify; + } + + public void setNotify(boolean notify) { + this.notify = notify; } } diff --git a/src/main/java/info/bukova/isspst/filters/UserFilter.java b/src/main/java/info/bukova/isspst/filters/UserFilter.java new file mode 100644 index 00000000..c613510c --- /dev/null +++ b/src/main/java/info/bukova/isspst/filters/UserFilter.java @@ -0,0 +1,57 @@ +package info.bukova.isspst.filters; + +import info.bukova.isspst.data.User; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.TypeSafeMatcher; + +import static info.bukova.isspst.StringUtils.nullStr; + +public class UserFilter implements Filter { + + private User condUser; + + public UserFilter(User condition) { + this.condUser = condition; + } + + private static class UserMatcher extends TypeSafeMatcher { + + private User condUser; + + public UserMatcher(User condition) { + this.condUser = condition; + } + + @Override + public void describeTo(Description description) { + description.appendText("user matcher"); + } + + @Override + public boolean matchesSafely(User item) { + return nullStr(item.getUsername()).toLowerCase().contains(nullStr(condUser.getUsername()).toLowerCase()) + && nullStr(item.getFirstName()).toLowerCase().contains(nullStr(condUser.getFirstName()).toLowerCase()) + && nullStr(item.getLastName()).toLowerCase().contains(nullStr(condUser.getLastName()).toLowerCase()) + && nullStr(item.getPersonalNumber()).toLowerCase().contains(nullStr(condUser.getPersonalNumber()).toLowerCase()); + } + + @Factory + public UserMatcher matchUser(User cond) { + return new UserMatcher(cond); + } + + } + + @Override + public TypeSafeMatcher matcher() { + return new UserMatcher(condUser); + } + + @Override + public String queryString() { + return null; + } + +} diff --git a/src/main/java/info/bukova/isspst/services/UserService.java b/src/main/java/info/bukova/isspst/services/UserService.java deleted file mode 100644 index de8af3b7..00000000 --- a/src/main/java/info/bukova/isspst/services/UserService.java +++ /dev/null @@ -1,11 +0,0 @@ -package info.bukova.isspst.services; - -import info.bukova.isspst.data.User; - -public interface UserService extends Service { - - public void setPassword(User user, String password); - public boolean hasRole(User user, String authority); - public void test(); - -} diff --git a/src/main/java/info/bukova/isspst/services/RoleService.java b/src/main/java/info/bukova/isspst/services/users/RoleService.java similarity index 62% rename from src/main/java/info/bukova/isspst/services/RoleService.java rename to src/main/java/info/bukova/isspst/services/users/RoleService.java index f85e27ca..e20ca4f2 100644 --- a/src/main/java/info/bukova/isspst/services/RoleService.java +++ b/src/main/java/info/bukova/isspst/services/users/RoleService.java @@ -1,6 +1,7 @@ -package info.bukova.isspst.services; +package info.bukova.isspst.services.users; import info.bukova.isspst.data.Role; +import info.bukova.isspst.services.Service; 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/users/RoleServiceImpl.java similarity index 78% rename from src/main/java/info/bukova/isspst/services/RoleServiceImpl.java rename to src/main/java/info/bukova/isspst/services/users/RoleServiceImpl.java index 0fd81feb..b538fe79 100644 --- a/src/main/java/info/bukova/isspst/services/RoleServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/users/RoleServiceImpl.java @@ -1,8 +1,9 @@ -package info.bukova.isspst.services; +package info.bukova.isspst.services.users; import org.springframework.transaction.annotation.Transactional; import info.bukova.isspst.data.Role; +import info.bukova.isspst.services.AbstractService; public class RoleServiceImpl extends AbstractService implements RoleService { diff --git a/src/main/java/info/bukova/isspst/services/users/UserService.java b/src/main/java/info/bukova/isspst/services/users/UserService.java new file mode 100644 index 00000000..466ea95e --- /dev/null +++ b/src/main/java/info/bukova/isspst/services/users/UserService.java @@ -0,0 +1,14 @@ +package info.bukova.isspst.services.users; + +import org.springframework.security.core.userdetails.UserDetailsService; + +import info.bukova.isspst.data.User; +import info.bukova.isspst.services.Service; + +public interface UserService extends UserDetailsService, Service { + + public void setPassword(User user, String password); + public boolean hasRole(User user, String authority); + public void saveWithPwd(User user, String password); + +} diff --git a/src/main/java/info/bukova/isspst/services/UserServiceImpl.java b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java similarity index 79% rename from src/main/java/info/bukova/isspst/services/UserServiceImpl.java rename to src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java index af5a3c75..f2f1531c 100644 --- a/src/main/java/info/bukova/isspst/services/UserServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/users/UserServiceImpl.java @@ -1,18 +1,16 @@ -package info.bukova.isspst.services; +package info.bukova.isspst.services.users; 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; +import info.bukova.isspst.services.AbstractService; -public class UserServiceImpl extends AbstractService implements UserService, UserDetailsService { +public class UserServiceImpl extends AbstractService implements UserService { private PasswordEncoder encoder; @@ -51,9 +49,10 @@ public class UserServiceImpl extends AbstractService implements UserServic } @Override - @Secured(Constants.ROLE_ADMIN) - public void test() { - System.out.println("pokus secured"); + @Transactional + public void saveWithPwd(User user, String password) { + this.setPassword(user, password); + this.update(user); } diff --git a/src/main/java/info/bukova/isspst/ui/ListViewModel.java b/src/main/java/info/bukova/isspst/ui/ListViewModel.java index bdfbff3a..12e948d2 100644 --- a/src/main/java/info/bukova/isspst/ui/ListViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/ListViewModel.java @@ -27,8 +27,7 @@ public class ListViewModel { private boolean filter = false; private Window editWin; private T dataBean; - //private T filterTemplate; - private Filter dataFilter; + private T filterTemplate; private T editBean; private List dataList; private List fullList; @@ -42,6 +41,7 @@ public class ListViewModel { protected Service service; protected Class dataClass; protected String formZul; + protected Filter dataFilter; public List getDataList() { if (dataList == null) { @@ -60,6 +60,20 @@ public class ListViewModel { this.dataFilter = dataFilter; } + public T getFilterTemplate() { + if (filterTemplate == null) { + try { + filterTemplate = dataClass.newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + + return filterTemplate; + } + public T getDataBean() { return dataBean; } diff --git a/src/main/java/info/bukova/isspst/ui/UserForm.java b/src/main/java/info/bukova/isspst/ui/UserForm.java deleted file mode 100644 index 07116ed4..00000000 --- a/src/main/java/info/bukova/isspst/ui/UserForm.java +++ /dev/null @@ -1,14 +0,0 @@ -package info.bukova.isspst.ui; - -import org.zkoss.bind.annotation.Init; - -import info.bukova.isspst.data.User; - -public class UserForm extends FormViewModel { - - @Init(superclass = true) - public void init() { - - } - -} diff --git a/src/main/java/info/bukova/isspst/ui/users/RoleCheck.java b/src/main/java/info/bukova/isspst/ui/users/RoleCheck.java new file mode 100644 index 00000000..a564be95 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/users/RoleCheck.java @@ -0,0 +1,43 @@ +package info.bukova.isspst.ui.users; + +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; + +public class RoleCheck { + + private Role role; + private User user; + private boolean checked; + + public RoleCheck(User user, Role role) { + this.user = user; + this.role = role; + if (user.getAuthorities().contains(role)) { + checked = true; + } else { + checked = false; + } + } + + public Role getRole() { + return role; + } + + public void setRole(Role role) { + this.role = role; + } + + public boolean isChecked() { + return checked; + } + + public void setChecked(boolean checked) { + this.checked = checked; + if (checked && !user.getAuthorities().contains(role)) { + user.addAuthority(role); + } else { + user.getAuthorities().remove(role); + } + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/users/UserForm.java b/src/main/java/info/bukova/isspst/ui/users/UserForm.java new file mode 100644 index 00000000..2ebdb55c --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/users/UserForm.java @@ -0,0 +1,90 @@ +package info.bukova.isspst.ui.users; + +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.zkoss.bind.annotation.Command; +import org.zkoss.bind.annotation.Init; +import org.zkoss.bind.annotation.NotifyChange; +import org.zkoss.zk.ui.select.annotation.WireVariable; + +import info.bukova.isspst.data.User; +import info.bukova.isspst.services.users.RoleService; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.ui.FormViewModel; + +public class UserForm extends FormViewModel { + + @WireVariable + private RoleService roleService; + @WireVariable + private UserService userService; + private String retPasswd = ""; + private String password = ""; + private UserRoles userRoles; + private boolean loginFree; + + @Init(superclass = true) + public void init() { + userRoles = new UserRoles(getDataBean(), roleService.getAll()); + loginFree = true; + } + + public UserRoles getUserRoles() { + return userRoles; + } + + public String getRetPasswd() { + return retPasswd; + } + + @NotifyChange({"canSave", "pwMatches"}) + public void setRetPasswd(String retPasswd) { + this.retPasswd = retPasswd; + } + + public String getPassword() { + return password; + } + + @NotifyChange({"canSave", "pwMatches"}) + public void setPassword(String password) { + this.password = password; + } + + @Command + @NotifyChange({"loginFree", "canSave"}) + public void checkLogin() { + try { + userService.loadUserByUsername(getDataBean().getUsername()); + loginFree = false; + } catch(UsernameNotFoundException e) { + loginFree = true; + } + } + + public boolean isPwMatches() { + return password.equals(retPasswd); + } + + public boolean isLoginFree() { + return loginFree || isEdit(); + } + + public boolean isEdit() { + return getDataBean().getCreated() != null; + } + + @Override + protected void doSave() { + if (!password.isEmpty()) { + userService.saveWithPwd(getDataBean(), this.password); + } else { + super.doSave(); + } + } + + @Override + public boolean isCanSave() { + return password.equals(retPasswd) && isLoginFree() && getDataBean().getUsername() != null && !getDataBean().getUsername().isEmpty(); + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/users/UserRoles.java b/src/main/java/info/bukova/isspst/ui/users/UserRoles.java new file mode 100644 index 00000000..7ff5fbe9 --- /dev/null +++ b/src/main/java/info/bukova/isspst/ui/users/UserRoles.java @@ -0,0 +1,24 @@ +package info.bukova.isspst.ui.users; + +import info.bukova.isspst.data.Role; +import info.bukova.isspst.data.User; + +import java.util.ArrayList; +import java.util.List; + +public class UserRoles { + + private List roleChecks; + + public UserRoles(User user, List roles) { + roleChecks = new ArrayList(); + for (Role r : roles) { + roleChecks.add(new RoleCheck(user, r)); + } + } + + public List getRoleChecks() { + return roleChecks; + } + +} diff --git a/src/main/java/info/bukova/isspst/ui/UsersList.java b/src/main/java/info/bukova/isspst/ui/users/UsersList.java similarity index 60% rename from src/main/java/info/bukova/isspst/ui/UsersList.java rename to src/main/java/info/bukova/isspst/ui/users/UsersList.java index c5ca0c0f..047f22ff 100644 --- a/src/main/java/info/bukova/isspst/ui/UsersList.java +++ b/src/main/java/info/bukova/isspst/ui/users/UsersList.java @@ -1,10 +1,12 @@ -package info.bukova.isspst.ui; +package info.bukova.isspst.ui.users; import org.zkoss.bind.annotation.Init; import org.zkoss.zk.ui.select.annotation.WireVariable; import info.bukova.isspst.data.User; -import info.bukova.isspst.services.UserService; +import info.bukova.isspst.filters.UserFilter; +import info.bukova.isspst.services.users.UserService; +import info.bukova.isspst.ui.ListViewModel; public class UsersList extends ListViewModel { @@ -16,6 +18,7 @@ public class UsersList extends ListViewModel { service = userService; dataClass = User.class; formZul = "userForm.zul"; + dataFilter = new UserFilter(getFilterTemplate()); } } diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index 39249fd2..2e95a6b7 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -89,12 +89,12 @@ - + - + diff --git a/src/main/webapp/admin/users/userForm.zul b/src/main/webapp/admin/users/userForm.zul index 635a212d..dd16454d 100644 --- a/src/main/webapp/admin/users/userForm.zul +++ b/src/main/webapp/admin/users/userForm.zul @@ -1,6 +1,34 @@ - - + +