From 86557eaefc7314671c6ac5c9d6ba15a6b1951ec1 Mon Sep 17 00:00:00 2001 From: Aleksandar Zivkovic Date: Mon, 26 Feb 2024 16:32:26 +0100 Subject: [PATCH] Add new Buttons and Forms, show Hardware in grid Known-Issues: Not all Forms have buttons Signed-off-by: Aleksandar Zivkovic --- .../samic/views/dashboard/DashboardView.java | 237 ++++++++++++++++-- 1 file changed, 214 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/samic/samic/views/dashboard/DashboardView.java b/src/main/java/com/samic/samic/views/dashboard/DashboardView.java index 3b0e70d..94b1970 100644 --- a/src/main/java/com/samic/samic/views/dashboard/DashboardView.java +++ b/src/main/java/com/samic/samic/views/dashboard/DashboardView.java @@ -1,26 +1,47 @@ package com.samic.samic.views.dashboard; import com.samic.samic.components.UIFactory; +import com.samic.samic.components.form.CPEForm; +import com.samic.samic.components.form.SFPForm; +import com.samic.samic.components.form.StorageForm; +import com.samic.samic.components.form.SupplyForm; +import com.samic.samic.components.form.UserForm; +import com.samic.samic.data.entity.CPE; +import com.samic.samic.data.entity.Producer; +import com.samic.samic.data.entity.Profile; +import com.samic.samic.data.entity.Role; +import com.samic.samic.data.entity.SFP; +import com.samic.samic.data.entity.Storage; import com.samic.samic.data.entity.StorageObject; +import com.samic.samic.data.entity.Supply; +import com.samic.samic.data.entity.User; import com.samic.samic.data.foundation.Guard; import com.samic.samic.security.AuthenticatedUser; import com.samic.samic.services.ServiceObjectType; import com.samic.samic.services.ServiceProducer; import com.samic.samic.services.ServiceReservation; +import com.samic.samic.services.ServiceStorage; import com.samic.samic.services.ServiceStorageObject; import com.samic.samic.views.MainLayout; +import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.contextmenu.MenuItem; +import com.vaadin.flow.component.contextmenu.SubMenu; +import com.vaadin.flow.component.dialog.Dialog; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.menubar.MenuBar; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextField; +import com.vaadin.flow.dom.Style.JustifyContent; import com.vaadin.flow.router.BeforeEnterEvent; import com.vaadin.flow.router.BeforeEnterObserver; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.router.RouteAlias; import jakarta.annotation.security.PermitAll; - -import java.util.*; +import java.util.HashMap; +import java.util.List; import java.util.concurrent.atomic.AtomicInteger; ; @@ -35,20 +56,44 @@ public class DashboardView extends VerticalLayout implements BeforeEnterObserver private final ServiceStorageObject storageObjectService; private final AuthenticatedUser authenticatedUser; private final ServiceObjectType serviceObjectType; + private final MenuBar menuBar = new MenuBar(); + private final CPEForm cpeForm; + private final SFPForm sfpForm; + private final SupplyForm supplyForm; + private final UserForm userForm; + private final StorageForm storageForm; + private final ServiceObjectType objectTypeService; + private final ServiceStorage stoageService; + private final ServiceProducer producerService; + private final ComboBox storageComboBox = new ComboBox<>("Lager"); + private final ComboBox producerComboBox = new ComboBox<>("Hersteller"); + Grid reservationGrid = new Grid<>(StorageObject.class, false); Grid hardwareGrid = new Grid<>(StorageObject.class, false); public DashboardView(ServiceReservation reservationService, - ServiceStorageObject storageObjectService, AuthenticatedUser authenticatedUser, - ServiceProducer producerService, ServiceStorageObject storageObjectService1, ServiceObjectType serviceObjectType) { + ServiceStorageObject storageObjectService, AuthenticatedUser authenticatedUser, + ServiceProducer producerService, ServiceStorageObject storageObjectService1, + ServiceObjectType serviceObjectType, + CPEForm cpeForm, SFPForm sfpForm, SupplyForm supplyForm, UserForm userForm, + ServiceObjectType objectTypeService, ServiceStorage stoageService, + ServiceProducer producerService1, StorageForm storageForm) { this.reservationService = reservationService; this.storageObjectService = storageObjectService; this.authenticatedUser = authenticatedUser; - this.serviceObjectType = serviceObjectType; + this.serviceObjectType = serviceObjectType; + this.cpeForm = cpeForm; + this.sfpForm = sfpForm; + this.supplyForm = supplyForm; + this.userForm = userForm; + this.objectTypeService = objectTypeService; + this.stoageService = stoageService; + this.producerService = producerService1; + this.storageForm = storageForm; - initUI(); + initUI(); } private void initUI() { @@ -61,7 +106,7 @@ private void initUI() { private void initStats() { // STATS - Container VerticalLayout statsContainer = UIFactory.rootComponentContainer("Statistiken"); - HorizontalLayout statsComponents = UIFactory.childContainer(JustifyContentMode.EVENLY); + HorizontalLayout statsComponents = UIFactory.childContainer(JustifyContentMode.START); HashMap> map = new HashMap<>(); map.put("Gerät Typ1", List.of(100, 50)); @@ -71,7 +116,8 @@ private void initStats() { storageObjectService.findAmountOfObjectType2().forEach( (key, value) -> { AtomicInteger objectTypeMin = new AtomicInteger(); - serviceObjectType.findObjectTypeByNameOptional(key).ifPresent(o -> objectTypeMin.set(o.getMinValue().intValue())); + serviceObjectType.findObjectTypeByNameOptional(key) + .ifPresent(o -> objectTypeMin.set(o.getMinValue().intValue())); Span sp = new Span(key + " " + value + "/" + value); if (value / objectTypeMin.get() > 2) { @@ -88,19 +134,163 @@ private void initStats() { } private void initQuickAccess() { - // CSS porperties - HashMap cssValues = new HashMap<>(Map.of("flex", "1 250px")); - // QUICK_ACCESS - PUT TOGETHER UI - add( - UIFactory.rootComponentContainer( - "Quick-Access", - UIFactory.childContainer( - JustifyContentMode.BETWEEN, - UIFactory.btnPrimary("Lagerobjekt erfassen", cssValues), - UIFactory.btnPrimary("Reservierung hinzufügen", cssValues), - UIFactory.btnPrimary("Lagerobjekt aufnehmen", cssValues), - UIFactory.btnPrimary("Lager hinzufügen", cssValues), - UIFactory.btnPrimary("Benutzer hinzufügen", cssValues)))); + menuBar.setOpenOnHover(true); + menuBar.addItem("Lagerobjekt aufnehmen", onClick -> onGetDevice()); + menuBar.setWidth("100%"); + menuBar.getStyle().setJustifyContent(JustifyContent.SPACE_BETWEEN); + + MenuItem addSo = menuBar.addItem("Lagerobjekt erfassen"); + SubMenu addSoSubmenu = addSo.getSubMenu(); + addSoSubmenu.addItem("CPE", onClick -> onAddCPE()); + addSoSubmenu.addItem("SFP", onClick -> onAddSFP()); + addSoSubmenu.addItem("Verbrauchsmaterial", onClick -> onAddSupply()); + + if (authenticatedUser.getUser().isPresent() + && authenticatedUser.getUser().get().getRole() == Role.MANAGMENT) { + menuBar.addItem("Lager hinzufügen", onClick -> { + Dialog dialog = new Dialog(); + dialog.add(storageForm); + supplyForm.setSupplyBeans(StorageObject.builder().supply(Supply.builder().build()).build()); + dialog.open(); + }); + menuBar.addItem("Benutzer hinzufügen", onClick -> { + Dialog dialog = new Dialog(); + dialog.add(userForm); + userForm.setBean(User.builder().profile(Profile.builder().build()).build()); + dialog.open(); + }); + + add( + UIFactory.rootComponentContainer( + "Quick-Access", UIFactory.childContainer(JustifyContentMode.START, menuBar))); + } + } + + private void onAddSupply() { + supplyForm.setSupplyBeans(StorageObject.builder().supply(Supply.builder().build()).build()); + createDialog(supplyForm).open(); + } + + private void onAddSFP() { + sfpForm.setSFPBeans(objectTypeService.findAll().toList(), + StorageObject.builder().sfp(SFP.builder().build()).build()); + createDialog(sfpForm).open(); + } + + private Dialog createDialog(SFPForm sfpForm) { + Dialog dialog = new Dialog(); + + dialog.setCloseOnOutsideClick(false); + storageComboBox.setItems(stoageService.findAll().toList()); + storageComboBox.setItemLabelGenerator(Storage::getName); + storageComboBox.setAllowCustomValue(false); + producerComboBox.setItems(producerService.findAll().toList()); + producerComboBox.setAllowCustomValue(false); + producerComboBox.setItemLabelGenerator(Producer::getName); + dialog.add(producerComboBox, storageComboBox, sfpForm); + + dialog.add(UIFactory.btnPrimary("Speichern", e -> { + dialog.close(); + onSave(sfpForm.isValid() ? sfpForm.saveStorageObject() : null); + }), UIFactory.btnPrimaryError("Abbrechen", e -> dialog.close())); + + return dialog; + + } + + private Dialog createDialog(SupplyForm supplyForm) { + Dialog dialog = new Dialog(); + + dialog.setCloseOnOutsideClick(false); + dialog.add(supplyForm); + + dialog.add(UIFactory.btnPrimary("Speichern", e -> { + dialog.close(); + onSave(supplyForm.isValid() ? supplyForm.saveStorageObject() : null); + }), UIFactory.btnPrimaryError("Abbrechen", e -> dialog.close())); + + return dialog; + + } + + private void onAddCPE() { + cpeForm.setCPEBeans(objectTypeService.findAll().toList(), + StorageObject.builder().cpe(CPE.builder().build()).build()); + createDialog(cpeForm).open(); + } + + private void onGetDevice() { + createDialog(new TextField("Lager ID")).open(); + } + + private Dialog createDialog(CPEForm form) { + Dialog dialog = new Dialog(); + + dialog.setCloseOnOutsideClick(false); + storageComboBox.setItems(stoageService.findAll().toList()); + storageComboBox.setItemLabelGenerator(Storage::getName); + storageComboBox.setAllowCustomValue(false); + producerComboBox.setItems(producerService.findAll().toList()); + producerComboBox.setAllowCustomValue(false); + producerComboBox.setItemLabelGenerator(Producer::getName); + dialog.add(producerComboBox, storageComboBox, form); + + dialog.add(UIFactory.btnPrimary("Speichern", e -> { + dialog.close(); + onSave(cpeForm.isValid() ? cpeForm.saveStorageObject() : null); + }), UIFactory.btnPrimaryError("Abbrechen", e -> dialog.close())); + + return dialog; + + } + + private Dialog createDialog(TextField field) { + Dialog dialog = new Dialog(); + dialog.add(field); + dialog.setCloseOnOutsideClick(false); + + dialog.add(UIFactory.btnPrimary("Speichern", e -> { + dialog.close(); + onSetUser(field.getValue()); + }), UIFactory.btnPrimaryError("Abbrechen", e -> dialog.close())); + + return dialog; + + } + + private void onSetUser(String id) { + StorageObject storageObject = storageObjectService.findStorageObjectById(Long.valueOf(id)); + storageObject.setStoredAtUser(authenticatedUser.getUser().get()); + UIFactory.notificationSuccess("Lagerobjekt umgebucht").open(); + storageObjectService.saveStorageObject(storageObject); + hardwareGrid.getDataProvider().refreshAll(); + } + + + private void onSave(StorageObject storageObject) { + if (storageObject == null) { + UIFactory.notificationError("Lagerobjekt konnte nicht gespeichert werden").open(); + return; + } + + storageObject.setStorage(storageComboBox.getValue()); + if (storageObject.getCpe() != null) { + storageObject.getCpe().setProducer(producerComboBox.getValue()); + } else if (storageObject.getSfp() != null) { + storageObject.getSfp().setProducer(producerComboBox.getValue()); + } + + var persisted = storageObjectService.saveStorageObject(storageObject); + if (storageObject.getObjectTypeName() != null) { + UIFactory.notificationInfoNoDuration("LagerID: " + persisted.getId().toString() + " Ger" + + "ätetyp: " + persisted.getObjectTypeName().getName()).open(); + UIFactory.notificationSuccess("Lagerobjekt erfolgreich gespeichert").open(); + + } else { + UIFactory.notificationInfoNoDuration("LagerID: " + persisted.getId().toString()).open(); + UIFactory.notificationSuccess("Lagerobjekt erfolgreich gespeichert").open(); + } + hardwareGrid.getDataProvider().refreshAll(); } private void initHardware() { @@ -119,7 +309,9 @@ private void initHardware() { } private void initHardwareData() { - //hardwareGrid.setItems( query -> storageObjectService.findStorageObjectByGivenUser(authenticatedUser.getUser().get())); + hardwareGrid.setItems( + storageObjectService.findStorageObjectByGivenUser(authenticatedUser.getUser().get()) + .toList()); } private void initReservation() { @@ -162,7 +354,6 @@ public Stream fetch(Query query){ } - @Override public void beforeEnter(BeforeEnterEvent beforeEnterEvent) { //VaadinSession.getCurrent().setErrorHandler(new SamicErrorHandler());