From 3fdfa76798d4bec91571f2f473b03fc65bf334bb Mon Sep 17 00:00:00 2001 From: Franta Pribyl Date: Mon, 1 Sep 2014 10:35:19 +0200 Subject: [PATCH] =?UTF-8?q?V=20p=C5=99=C3=ADpad=C4=9B=20zad=C3=A1n=C3=AD?= =?UTF-8?q?=20duplicitn=C3=AD=20hodnoty=20se=20zpracuj=C3=AD=20pot=C5=99eb?= =?UTF-8?q?n=C3=A9=20v=C3=BDjimky=20a=20zobraz=C3=AD=20se=20v=20messagebox?= =?UTF-8?q?u=20hl=C3=A1=C5=A1en=C3=AD=20o=20d=C5=AFvodu=20chyby.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #127 --- .../info/bukova/isspst/ui/FormViewModel.java | 122 +++++++++++++----- .../WEB-INF/locales/zk-label.properties | 10 +- 2 files changed, 96 insertions(+), 36 deletions(-) diff --git a/src/main/java/info/bukova/isspst/ui/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index f7adbd5f..b6dd3e98 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -5,9 +5,11 @@ import info.bukova.isspst.data.DataModel; import info.bukova.isspst.services.Service; import info.bukova.isspst.services.ValidationException; +import java.sql.SQLException; import java.util.Map; import org.hibernate.exception.ConstraintViolationException; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.security.access.AccessDeniedException; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; @@ -17,36 +19,43 @@ 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; private Service service; private boolean newRec; private ServiceConstraint constraint; - + @Init - public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) { + public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) + { this.dataBean = selected; this.service = service; constraint = new ServiceConstraint(); constraint.setDataBean(selected); constraint.setService(service); - if (selected.getId() == 0 && selected.getCreated() == null) { + if (selected.getId() == 0 && selected.getCreated() == null) + { newRec = true; - } else { + } + else + { newRec = false; } } - - public ServiceConstraint getConstriant() { + + public ServiceConstraint getConstriant() + { return constraint; } - - public T getDataBean() { + + public T getDataBean() + { return dataBean; } - + public boolean isNewRec() { return this.newRec; @@ -59,52 +68,103 @@ public class FormViewModel { @Command @NotifyChange("errMessages") - public void save(@BindingParam("window") Window win) { - try { - if (newRec) { + public void save(@BindingParam("window") Window win) + { + try + { + if (newRec) + { doAdd(); - } else { + } + else + { doSave(); } win.detach(); - } catch (ValidationException e) { + } + catch (ValidationException e) + { errMessages = e.getMessages(); String classErr = errMessages.get("CLASS_ERR"); - - if (classErr == null) { + + if (classErr == null) + { classErr = ""; } - + Messagebox.show(StringUtils.localize("DbValidationError"), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR); - } catch (AccessDeniedException e) { + } + catch (AccessDeniedException e) + { Messagebox.show(StringUtils.localize("ErrorRights"), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR); - } catch (ConstraintViolationException e) { + } + catch (ConstraintViolationException e) + { Messagebox.show(StringUtils.localizeDbErr(e.getSQLException().getMessage()), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR); dataBean.setValid(false); - } catch (Exception e) { + } + catch (DataIntegrityViolationException e) + { + Throwable actualException = e; + Throwable previousException = actualException.getCause(); + String msg = actualException.getLocalizedMessage(); + boolean printStackTree = true; + + if (previousException != null) + { + if (previousException instanceof ConstraintViolationException) + { + ConstraintViolationException cve = (ConstraintViolationException) previousException; + SQLException sqle = cve.getSQLException(); + int errCode = sqle.getErrorCode(); + + if (errCode == 1062) + { + printStackTree = false; + msg = sqle.getMessage(); + msg = StringUtils.localizeDbErr(msg); + } + } + } + + if (printStackTree) + { + e.printStackTrace(); + } + + Messagebox.show(StringUtils.localize("DbSaveError") + "\n\n" + msg, StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR); + dataBean.setValid(false); + } + catch (Exception e) + { e.printStackTrace(); Messagebox.show(StringUtils.localize("DbSaveError"), StringUtils.localize("Error"), Messagebox.OK, Messagebox.ERROR); dataBean.setValid(false); } } - public Map getErrMessages() { + public Map getErrMessages() + { return errMessages; } - - protected void setErrMesages(Map msgs) { + + protected void setErrMesages(Map msgs) + { this.errMessages = msgs; } - - protected void doSave() { + + protected void doSave() + { service.update(dataBean); } - - protected void doAdd() { + + protected void doAdd() + { service.add(dataBean); } - - public boolean isCanSave() { + + public boolean isCanSave() + { return true; } diff --git a/src/main/webapp/WEB-INF/locales/zk-label.properties b/src/main/webapp/WEB-INF/locales/zk-label.properties index 75dec75f..18a249c0 100644 --- a/src/main/webapp/WEB-INF/locales/zk-label.properties +++ b/src/main/webapp/WEB-INF/locales/zk-label.properties @@ -117,11 +117,11 @@ UsersGridColumnPersonalID=Osobní číslo UsersGridColumnFirstName=Jméno UsersGridColumnSureName=Příjmení -AgendaMaterial=Materiál -MaterialFormTitle=Materiál +AgendaMaterial=Skupiny materiálu +MaterialFormTitle=Skupina materiálu -AgendaServices=Služby -ServiceFormTitle=Služba +AgendaServices=Skupiny služeb +ServiceFormTitle=Skupina služeb AgendaWorkgroups=Střediska / komise WorkgroupFormTitle=Pracvní skupina @@ -239,7 +239,7 @@ ErrorRights=K vykobání této operace nemáte dostatečná oprávnění DbDeleteRecord=Smazat záznam DbReallyDelete=Opravdu chcete smazat tento záznam? -DbDuplicateEntry=Chyba při ukládání záznamu: Duplicitní záznam +DbDuplicateEntry=Duplicitní hodnota DbForKey= pro klíč DbCannotDelete=Chyba při mazání záznamu: Záznam nelze smazat protože na něj odkazuje jiný záznam DbSaveError=Chyba při ukládání záznamu