From: elgekko Date: Mon, 24 Apr 2023 20:37:12 +0000 (+0200) Subject: Associates editor, page caption X-Git-Url: http://git.useribm.hu/?a=commitdiff_plain;h=4ea52567b74f9a42b4b281d0c3e36e1a69863b03;p=sly-crm.git Associates editor, page caption --- diff --git a/lis-db/src/main/java/hu/user/lis/db/Associate.java b/lis-db/src/main/java/hu/user/lis/db/Associate.java index a7b66e8..dcabbe8 100644 --- a/lis-db/src/main/java/hu/user/lis/db/Associate.java +++ b/lis-db/src/main/java/hu/user/lis/db/Associate.java @@ -10,6 +10,8 @@ import lombok.*; public class Associate { String id; String name; + String login; + String password; double hourlyRate; boolean active; } diff --git a/lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java b/lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java index e112dcd..5127581 100644 --- a/lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java +++ b/lis-services/src/main/java/hu/user/lis/services/data/AssociateServiceImpl.java @@ -55,11 +55,12 @@ public class AssociateServiceImpl implements AssociateService { private List generate() { List result = new ArrayList<>(); - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 10; i++) { String id = RandomStringUtils.random(8, "0123456789abcdef"); String name = dataGeneratorService.faker().name().fullName(); - - Associate entity = Associate.builder().active(true).id(id).name(name).build(); + String login = "user" + (i + 1); + String password = "password"; + Associate entity = Associate.builder().active(true).id(id).name(name).login(login).password(password).build(); result.add(entity); } return result; diff --git a/lis-ui/src/main/java/hu/user/lis/ui/config/ResourceConfigurer.java b/lis-ui/src/main/java/hu/user/lis/ui/config/ResourceConfigurer.java index 31b63db..ffa5562 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/config/ResourceConfigurer.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/config/ResourceConfigurer.java @@ -14,4 +14,9 @@ public class ResourceConfigurer { public String projects() { return "index"; } + + @GetMapping("/associates") + public String associates() { + return "index"; + } } diff --git a/lis-ui/src/main/java/hu/user/lis/ui/data/AssociatesDataModel.java b/lis-ui/src/main/java/hu/user/lis/ui/data/AssociatesDataModel.java new file mode 100644 index 0000000..b5b2616 --- /dev/null +++ b/lis-ui/src/main/java/hu/user/lis/ui/data/AssociatesDataModel.java @@ -0,0 +1,110 @@ +package hu.user.lis.ui.data; + +import hu.user.lis.db.Associate; +import hu.user.lis.services.data.AssociateService; +import lombok.Getter; +import lombok.extern.log4j.Log4j2; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import org.zkoss.bind.BindUtils; +import org.zkoss.zul.FieldComparator; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +@Component +@Log4j2 +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class AssociatesDataModel extends CachedDataModel { + @Getter + @Autowired + AssociateService associateService; + private String partialName; + private boolean listAll; + private boolean filterShowInActive; + private boolean filterShowActive; + + + private boolean canExecuteSearch() { + boolean result = listAll || filterShowActive || filterShowInActive || + StringUtils.isNotBlank(partialName); + log.info("Can execute search: {}", result); + return result; + } + + private boolean filter(Associate associate) { + if (listAll) { + return true; + } + + boolean result = true; + if (StringUtils.isNotBlank(partialName)) { + if (!associate.getName().toLowerCase().startsWith(partialName.toLowerCase())) { + result = false; + } + } + if (!filterShowActive && associate.isActive()) { + result = false; + } + + if (!filterShowInActive && !associate.isActive()) { + result = false; + } + + return result; + } + + @Override + protected List getResultSet(long offset, int limit, FieldComparator sortComparator) { + List result = null; + if (canExecuteSearch()) { + result = associateService.getAll().stream() + .filter(s -> filter(s)) + .sorted(Comparator.comparing(Associate::getName, String.CASE_INSENSITIVE_ORDER)) + .collect(Collectors.toList()); + } + return result; + } + + @Override + public int getResultSetCount() { + int result = 0; + if (canExecuteSearch()) { + result = (int) associateService.getAll().stream() + .filter(s -> filter(s)) + .count(); + } + return result; + } + + public void search(String partialName) { + log.info("Searching ssociate using filters: name LIKE {}", + partialName); + listAll = false; + this.partialName = partialName; + super.reset(); + BindUtils.postNotifyChange(null, null, this, "*"); + } + + + public void search(boolean filterShowActive, boolean filterShowInActive) { + log.info("Searching partner using filters: filterShowActive {}, filterShowInActive {}", + filterShowActive, filterShowInActive); + this.filterShowActive = filterShowActive; + this.filterShowInActive = filterShowInActive; + listAll = false; + super.reset(); + BindUtils.postNotifyChange(null, null, this, "*"); + } + + public void listAll() { + log.info("List all associates"); + listAll = true; + super.reset(); + BindUtils.postNotifyChange(null, null, this, "*"); + } +} diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/AssociateEditorModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/AssociateEditorModel.java new file mode 100644 index 0000000..1a6ec86 --- /dev/null +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/AssociateEditorModel.java @@ -0,0 +1,86 @@ +package hu.user.lis.ui.view; + +import hu.user.lis.db.Associate; +import hu.user.lis.services.data.AssociateService; +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; +import org.zkoss.zk.ui.select.annotation.VariableResolver; +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 AssociateEditorModel extends AbstractValidator { + @WireVariable + AssociateService associateServiceImpl; + private Associate formDocument; + private Associate origDocument; + private boolean formInvalid = true; + + @Init + public void init() { + log.info("Initialized"); + origDocument = (Associate) Executions.getCurrent().getArg().get("origDocument"); + formDocument = (Associate) Executions.getCurrent().getArg().get("formDocument"); + } + + @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); + } + + @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); + updateFormInvalid(false); + try { + Associate newData = associateServiceImpl.copy(formDocument, property, value); + if (!Objects.isNull(origDocument) && associateServiceImpl.toString(origDocument).equals(associateServiceImpl.toString(newData))) { + log.info("Document not changed"); + updateFormInvalid(true); + return; + } + if (StringUtils.isBlank(newData.getName()) || + StringUtils.isBlank(newData.getLogin()) || + StringUtils.isBlank(newData.getPassword()) || + newData.getHourlyRate() < 1 + ) { + log.info("Document is not valid"); + updateFormInvalid(true); + } + + + } catch (Exception e) { + log.catching(e); + } + } + + private void updateFormInvalid(boolean invalid) { + setFormInvalid(invalid); + BindUtils.postNotifyChange(this, "formInvalid"); + } +} diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/AssociatesViewModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/AssociatesViewModel.java new file mode 100644 index 0000000..90d8cdf --- /dev/null +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/AssociatesViewModel.java @@ -0,0 +1,155 @@ +package hu.user.lis.ui.view; + +import com.fasterxml.jackson.core.JsonProcessingException; +import hu.user.lis.db.Associate; +import hu.user.lis.ui.data.AssociatesDataModel; +import lombok.Getter; +import lombok.extern.log4j.Log4j2; +import org.zkoss.bind.annotation.*; +import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.Executions; +import org.zkoss.zk.ui.select.Selectors; +import org.zkoss.zk.ui.select.annotation.VariableResolver; +import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zk.ui.util.Clients; +import org.zkoss.zkplus.spring.DelegatingVariableResolver; +import org.zkoss.zul.Window; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +@Log4j2 +@VariableResolver(DelegatingVariableResolver.class) +public class AssociatesViewModel { + @WireVariable + @Getter + AssociatesDataModel associatesDataModel; + @Getter + private Associate selectedEntity; + private boolean filterShowInActive; + private boolean filterShowActive; + private boolean filterShowBoth; + + @AfterCompose + public void onAfterCompose(@ContextParam(ContextType.VIEW) Component view) { + Selectors.wireComponents(view, this, false); + Selectors.wireEventListeners(view, this); + } + + @Init + public void init() { + setFilterShowActive(true); + log.info("Initialized"); + Clients.evalJavaScript("pushNav('/associates')"); + } + + private void refresh() { + associatesDataModel.clearSelection(); + selectedEntity = null; + if (filterShowBoth) { + associatesDataModel.search(true, true); + } else { + associatesDataModel.search(filterShowActive, filterShowInActive); + } + } + + @Command + @NotifyChange("selectedPartner") + public void search() { + } + + @Command + @NotifyChange("selectedPartner") + public void onListSelection() { + selectedEntity = null; + Set selections = associatesDataModel.getSelection(); + if (selections.size() == 1) { + selectedEntity = selections.iterator().next(); + log.info("Selected {}", selectedEntity); + } + } + + @Command + public void onAfterRenderList() { + } + + @Command + public void onAdd() { + String page = "~./associate-editor.zul"; + Associate newEntity = associatesDataModel.getAssociateService().createNew(); + Window editorWindow = (Window) Executions.createComponents(page, null, + Collections.singletonMap("formDocument", newEntity)); + editorWindow.addEventListener("onClose", e -> { + if (e.getData() != null) { + log.info("Associate popup result {}", e.getData()); + associatesDataModel.getAssociateService().add(newEntity); + associatesDataModel.clearSelection(); + refresh(); + associatesDataModel.addToSelection(newEntity); + selectedEntity = newEntity; + } + }); + editorWindow.doModal(); + } + + @Command + public void onEdit() throws JsonProcessingException { + String page = "~./associate-editor.zul"; + Associate editEntity = associatesDataModel.getAssociateService().copy(selectedEntity); + Map arg = new HashMap<>(); + arg.put("origDocument", selectedEntity); + arg.put("formDocument", editEntity); + Window editorWindow = (Window) Executions.createComponents(page, null, arg); + editorWindow.addEventListener("onClose", e -> { + if (e.getData() != null) { + log.info("Partner popup result {}", e.getData()); + Associate modifiedEntity = (Associate) e.getData(); + associatesDataModel.clearSelection(); + associatesDataModel.getAssociateService().replace(selectedEntity, modifiedEntity); + refresh(); + associatesDataModel.addToSelection(modifiedEntity); + selectedEntity = modifiedEntity; + } + }); + editorWindow.doModal(); + } + + + public boolean isFilterShowInActive() { + return filterShowInActive; + } + + @NotifyChange({"filterShowActive", "filterShowInActive", "filterShowBoth"}) + public void setFilterShowInActive(boolean filterShowInActive) { + this.filterShowBoth = false; + this.filterShowActive = false; + this.filterShowInActive = filterShowInActive; + refresh(); + } + + public boolean isFilterShowActive() { + return filterShowActive; + } + + @NotifyChange({"filterShowActive", "filterShowInActive", "filterShowBoth"}) + public void setFilterShowActive(boolean filterShowActive) { + this.filterShowBoth = false; + this.filterShowInActive = false; + this.filterShowActive = filterShowActive; + refresh(); + } + + public boolean isFilterShowBoth() { + return this.filterShowBoth; + } + + @NotifyChange({"filterShowActive", "filterShowInActive", "filterShowBoth"}) + public void setFilterShowBoth(boolean filterShowBoth) { + this.filterShowActive = false; + this.filterShowInActive = false; + this.filterShowBoth = filterShowBoth; + refresh(); + } +} diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/IndexViewModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/IndexViewModel.java index 35b3686..7034e54 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/view/IndexViewModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/IndexViewModel.java @@ -29,6 +29,7 @@ public class IndexViewModel implements EventListener { public static final String PROJECT_EDITOR_PAGE = "~./project-editor.zul"; private static final String PARTNERS_LIST = "~./partners.zul"; private static final String PROJECTS_LIST = "~./projects.zul"; + private static final String ASSOCIATES_LIST = "~./associates.zul"; @WireVariable BuildProperties buildProperties; @WireVariable @@ -36,7 +37,8 @@ public class IndexViewModel implements EventListener { String searchPhrase; String page; private Map navigation = ImmutableMap.of( - "/projects", PROJECTS_LIST + "/projects", PROJECTS_LIST, + "/accociates", ASSOCIATES_LIST ); @Init diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/PartnersViewModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/PartnersViewModel.java index 0ad8556..f96ced4 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/view/PartnersViewModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/PartnersViewModel.java @@ -12,6 +12,7 @@ import org.zkoss.zk.ui.select.Selectors; import org.zkoss.zk.ui.select.annotation.VariableResolver; import org.zkoss.zk.ui.select.annotation.Wire; import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zk.ui.util.Clients; import org.zkoss.zkplus.spring.DelegatingVariableResolver; import org.zkoss.zul.Listbox; import org.zkoss.zul.Window; @@ -43,8 +44,9 @@ public class PartnersViewModel extends AsyncBaseModel { @Init public void init() { - log.info("Initialized"); setFilterShowActive(true); + Clients.evalJavaScript("pushNav('/')"); + log.info("Initialized"); } private void refresh() { diff --git a/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectsViewModel.java b/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectsViewModel.java index 6c27919..90eb386 100644 --- a/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectsViewModel.java +++ b/lis-ui/src/main/java/hu/user/lis/ui/view/ProjectsViewModel.java @@ -14,6 +14,7 @@ import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.select.Selectors; import org.zkoss.zk.ui.select.annotation.VariableResolver; import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zk.ui.util.Clients; import org.zkoss.zkplus.spring.DelegatingVariableResolver; import java.util.Map; @@ -44,6 +45,7 @@ public class ProjectsViewModel extends AsyncBaseModel implements EventListener { public void init() { setFilterShowActive(true); eventBus.register(this); + Clients.evalJavaScript("pushNav('/projects')"); log.info("Initialized"); } diff --git a/lis-ui/src/main/resources/web/associate-editor.zul b/lis-ui/src/main/resources/web/associate-editor.zul new file mode 100644 index 0000000..cb21931 --- /dev/null +++ b/lis-ui/src/main/resources/web/associate-editor.zul @@ -0,0 +1,51 @@ + + + + + + +
+ + + + + + + +
+ + +