From b71b9bde727b8b27439886e543dbd275b258226e Mon Sep 17 00:00:00 2001 From: elgekko Date: Tue, 18 Apr 2023 10:39:00 +0200 Subject: [PATCH] Partners, Projects list improved, form validator for empty check done --- .../lis/services/data/ProjectService.java | 4 ++ .../lis/services/data/ProjectServiceImpl.java | 29 +++++++++++++++ ...del.java => PartnerSelectorDataModel.java} | 2 +- .../user/lis/ui/view/PartnerEditorModel.java | 29 +++++++++++++-- .../user/lis/ui/view/ProjectEditorModel.java | 37 +++++++++++++++---- lis-ui/src/main/resources/web/index.zul | 6 +++ .../main/resources/web/partner-selector.zul | 6 +-- lis-ui/src/main/resources/web/partner.zul | 11 +++--- lis-ui/src/main/resources/web/project.zul | 13 +++++-- 9 files changed, 113 insertions(+), 24 deletions(-) rename lis-ui/src/main/java/hu/user/lis/ui/data/{PartnersSelectorDataModel.java => PartnerSelectorDataModel.java} (96%) diff --git a/lis-services/src/main/java/hu/user/lis/services/data/ProjectService.java b/lis-services/src/main/java/hu/user/lis/services/data/ProjectService.java index 261ef91..c166af5 100644 --- a/lis-services/src/main/java/hu/user/lis/services/data/ProjectService.java +++ b/lis-services/src/main/java/hu/user/lis/services/data/ProjectService.java @@ -15,4 +15,8 @@ public interface ProjectService { Project copy(Project sourceEntity) throws JsonProcessingException; void replace(Project targetEntity, Project replacementEntity); + + boolean isInvalid(Project entity); + + String toString(Project entity); } diff --git a/lis-services/src/main/java/hu/user/lis/services/data/ProjectServiceImpl.java b/lis-services/src/main/java/hu/user/lis/services/data/ProjectServiceImpl.java index 3850429..68df25a 100644 --- a/lis-services/src/main/java/hu/user/lis/services/data/ProjectServiceImpl.java +++ b/lis-services/src/main/java/hu/user/lis/services/data/ProjectServiceImpl.java @@ -4,14 +4,18 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import hu.user.lis.db.Partner; import hu.user.lis.db.Project; +import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; +import java.util.Objects; @Service +@Log4j2 public class ProjectServiceImpl implements ProjectService { private List entities; @Autowired @@ -75,4 +79,29 @@ public class ProjectServiceImpl implements ProjectService { } return result; } + + @Override + public boolean isInvalid(Project entity) { + if (StringUtils.isBlank(entity.getName())) { + return true; + } + if (StringUtils.isBlank(entity.getContactName())) { + return true; + } + if (Objects.isNull(entity.getPartner())) { + return true; + } + return false; + } + + @Override + public String toString(Project entity) { + ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.writeValueAsString(entity); + } catch (JsonProcessingException e) { + log.catching(e); + } + return null; + } } diff --git a/lis-ui/src/main/java/hu/user/lis/ui/data/PartnersSelectorDataModel.java b/lis-ui/src/main/java/hu/user/lis/ui/data/PartnerSelectorDataModel.java similarity index 96% rename from lis-ui/src/main/java/hu/user/lis/ui/data/PartnersSelectorDataModel.java rename to lis-ui/src/main/java/hu/user/lis/ui/data/PartnerSelectorDataModel.java index 6867013..efb951a 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/data/PartnersSelectorDataModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/data/PartnerSelectorDataModel.java @@ -15,7 +15,7 @@ import java.util.stream.Collectors; @Component @Log4j2 -public class PartnersSelectorDataModel extends CachedDataModel { +public class PartnerSelectorDataModel extends CachedDataModel { static private final int SEARCH_LIMIT = 10; private String partialName; @Autowired diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/PartnerEditorModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/PartnerEditorModel.java index 1ee5dbd..8aea7e5 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/view/PartnerEditorModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/PartnerEditorModel.java @@ -4,9 +4,14 @@ import hu.user.lis.db.Partner; import lombok.Getter; import lombok.Setter; import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; +import org.zkoss.bind.BindUtils; +import org.zkoss.bind.ValidationContext; import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; +import org.zkoss.bind.validator.AbstractValidator; +import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Events; @@ -14,23 +19,39 @@ import org.zkoss.zk.ui.select.annotation.VariableResolver; import org.zkoss.zkplus.spring.DelegatingVariableResolver; import org.zkoss.zul.Window; +import java.util.Objects; + @Log4j2 @Getter @Setter @VariableResolver(DelegatingVariableResolver.class) -public class PartnerEditorModel { - private Partner selectedPartner; +public class PartnerEditorModel extends AbstractValidator { + private Partner formDocument; + private boolean formInvalid; @Init public void init() { log.info("Initialized"); - selectedPartner = (Partner) Executions.getCurrent().getArg().get("formDocument"); + formDocument = (Partner) Executions.getCurrent().getArg().get("formDocument"); } @Command public void onCloseWindow(@BindingParam("target") Window target, @BindingParam("select") boolean select) { - Event closeEvent = new Event("onClose", target, select ? selectedPartner : null); + if (select && formInvalid) { + return; + } + Event closeEvent = new Event("onClose", target, select ? formDocument : null); Events.postEvent(closeEvent); } + @Override + public void validate(ValidationContext ctx) { + Component target = ctx.getBindContext().getComponent(); + String property = ctx.getProperty().getProperty(); + Object value = ctx.getProperty().getValue(); + log.info("Validating caused by {} {} {}", target.getId(), property, value); + boolean invalid = Objects.isNull(value) || StringUtils.isBlank(Objects.toString(value)); + setFormInvalid(invalid); + BindUtils.postNotifyChange(this, "formInvalid"); + } } diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectEditorModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectEditorModel.java index 4cfaf16..804d145 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectEditorModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectEditorModel.java @@ -2,13 +2,18 @@ package hu.user.lis.ui.view; import hu.user.lis.db.Partner; import hu.user.lis.db.Project; -import hu.user.lis.ui.data.PartnersSelectorDataModel; +import hu.user.lis.ui.data.PartnerSelectorDataModel; +import hu.user.lis.ui.data.ProjectsDataModel; import lombok.Getter; import lombok.Setter; import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; import org.zkoss.bind.BindContext; import org.zkoss.bind.BindUtils; +import org.zkoss.bind.ValidationContext; import org.zkoss.bind.annotation.*; +import org.zkoss.bind.validator.AbstractValidator; +import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Events; @@ -19,26 +24,34 @@ import org.zkoss.zk.ui.select.annotation.WireVariable; import org.zkoss.zkplus.spring.DelegatingVariableResolver; import org.zkoss.zul.Window; +import java.util.Objects; + @Log4j2 @Getter @Setter @VariableResolver(DelegatingVariableResolver.class) -public class ProjectEditorModel { +public class ProjectEditorModel extends AbstractValidator { private Project formDocument; private Partner selectedPartner; @WireVariable - PartnersSelectorDataModel partnersSelectorDataModel; + private PartnerSelectorDataModel partnerSelectorDataModel; + @WireVariable + private ProjectsDataModel projectsDataModel; + private boolean formInvalid; @Init public void init() { log.info("Initialized"); formDocument = (Project) Executions.getCurrent().getArg().get("formDocument"); selectedPartner = formDocument.getPartner(); - partnersSelectorDataModel.clearSelection(); + partnerSelectorDataModel.clearSelection(); } @Command public void onCloseWindow(@BindingParam("target") Window target, @BindingParam("select") boolean select) { + if (select && formInvalid) { + return; + } Event closeEvent = new Event("onClose", target, select ? formDocument : null); Events.postEvent(closeEvent); } @@ -47,14 +60,14 @@ public class ProjectEditorModel { public void onPartnerBandChanging(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx) { InputEvent event = (InputEvent) ctx.getTriggerEvent(); log.info("onPartnerBandChanging: {}", event.getValue()); - partnersSelectorDataModel.search(event.getValue()); + partnerSelectorDataModel.search(event.getValue()); } @Command public void onPartnerBandOpen(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx) { OpenEvent event = (OpenEvent) ctx.getTriggerEvent(); log.info("onPartnerBandOpen: {}", event.isOpen()); - partnersSelectorDataModel.search(null); + partnerSelectorDataModel.search(null); } @Command @@ -64,7 +77,7 @@ public class ProjectEditorModel { suppliersWindow.addEventListener("onClose", e -> { log.info("Suppliers popup result {}", e.getData()); if (e.getData() != null) { - partnersSelectorDataModel.clearSelection(); + partnerSelectorDataModel.clearSelection(); //setSelectedSupplierId(((Supplier) e.getData()).getId()); BindUtils.postNotifyChange(null, null, this, "selectedSupplierId"); } @@ -79,4 +92,14 @@ public class ProjectEditorModel { formDocument.setPartner(selectedPartner); } + @Override + public void validate(ValidationContext ctx) { + Component target = ctx.getBindContext().getComponent(); + String property = ctx.getProperty().getProperty(); + Object value = ctx.getProperty().getValue(); + log.info("Validating caused by {} {} {}", target.getId(), property, value); + boolean invalid = Objects.isNull(value) || StringUtils.isBlank(Objects.toString(value)); + setFormInvalid(invalid); + BindUtils.postNotifyChange(this, "formInvalid"); + } } diff --git a/lis-ui/src/main/resources/web/index.zul b/lis-ui/src/main/resources/web/index.zul index 43385bf..100e7ce 100644 --- a/lis-ui/src/main/resources/web/index.zul +++ b/lis-ui/src/main/resources/web/index.zul @@ -1,6 +1,12 @@ + diff --git a/lis-ui/src/main/resources/web/partner-selector.zul b/lis-ui/src/main/resources/web/partner-selector.zul index 9a758ac..7eb2f5e 100644 --- a/lis-ui/src/main/resources/web/partner-selector.zul +++ b/lis-ui/src/main/resources/web/partner-selector.zul @@ -32,8 +32,8 @@ @@ -47,6 +47,6 @@ -