Skip to content

Commit

Permalink
add LagerobjektaufnehmenView, fix LagerobjekteErfassenView objekt lös…
Browse files Browse the repository at this point in the history
…chen beim verlassen der seite wenn nicht gespeichert werden soll
  • Loading branch information
anywaywayany committed Dec 10, 2023
1 parent 8b63e0b commit 2aa88f8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/samic/samic/views/MainLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.samic.samic.data.entity.User;
import com.samic.samic.security.AuthenticatedUser;
import com.samic.samic.views.dashboard.DashboardView;
import com.samic.samic.views.lagerobjekt_aufnehmen.LagerobjektAufnehmenView;
import com.samic.samic.views.lagerobjekt_erfassen.LagerobjektErfassenView;
import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
Expand Down Expand Up @@ -79,7 +80,12 @@ private SideNav createNavigation() {
LagerobjektErfassenView.class,
LineAwesomeIcon.DASHCUBE.create()));
}

if (accessChecker.hasAccess(LagerobjektAufnehmenView.class))
nav.addItem(
new SideNavItem(
"Lagerobjekt aufnehmen",
LagerobjektAufnehmenView.class,
LineAwesomeIcon.CART_ARROW_DOWN_SOLID.create()));
return nav;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.samic.samic.views.lagerobjekt_aufnehmen;

import com.samic.samic.components.UIFactory;
import com.samic.samic.data.entity.StorageObject;
import com.samic.samic.security.AuthenticatedUser;
import com.samic.samic.services.ServiceStorageObject;
import com.samic.samic.services.ServiceUser;
import com.samic.samic.views.MainLayout;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.converter.StringToLongConverter;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll;
import java.util.NoSuchElementException;


@PageTitle("Lagerobjekt aufnehmen")
@Route(value = "lagerobjektAufnehmen", layout = MainLayout.class)
@PermitAll
public class LagerobjektAufnehmenView extends VerticalLayout {

private final ServiceStorageObject storageObjectService;
private StorageObject storageObject;
private final TextField storageObjectID = new TextField("Lager ID");
private final Binder<StorageObject> binderStorageObject = new Binder<>(StorageObject.class, true);
private final AuthenticatedUser authenticatedUser;
private final ServiceUser userService;


LagerobjektAufnehmenView(ServiceStorageObject storageObjectService, AuthenticatedUser authenticatedUser, ServiceUser userService) {
this.storageObjectService = storageObjectService;
this.authenticatedUser = authenticatedUser;
this.userService = userService;
initUI();
}

private void initBinder() {
binderStorageObject.forField(storageObjectID).asRequired("Lager ID muss augefüllt werden").withConverter(
new StringToLongConverter("Id is not Long")).bind(StorageObject::getId, null);
binderStorageObject.setBean(storageObject);
}

private void initUI() {
add(UIFactory.rootComponentContainer("",
UIFactory.childContainer(JustifyContentMode.START, storageObjectID),
UIFactory.childContainer(JustifyContentMode.END,
UIFactory.btnPrimary("Aufnehmen", event -> onSave()),
UIFactory.btnPrimaryError("Abbrechen", event -> onCancel()))));
}

private void onCancel() {
UI.getCurrent().getPage().reload();
}

private void onSave() {
try {
var toEdit = storageObjectService.findStorageObjectById(Long.valueOf(storageObjectID.getValue()));
if (toEdit != null) {
if (toEdit.getStoredAtUser() == null) {
toEdit.setStoredAtUser(authenticatedUser.getUser().orElse(userService.findUserByID(authenticatedUser.getUser().get().getId())));
toEdit.setStorage(null);
storageObjectService.saveStorageObject(toEdit);
storageObjectID.setValue("");
UIFactory.NotificationSuccess("Lagerobjekt wurde aufgenommen").open();
} else {
UIFactory.NotificationError("Lagerobjekt von jemandem aufgenommen worden").open();
}
}
} catch (NoSuchElementException e) {
UIFactory.NotificationError("Konnte nicht aufgenommen werden, da das Lagerobjekt nicht existiert").open();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.BeforeLeaveEvent;
import com.vaadin.flow.router.BeforeLeaveObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import jakarta.annotation.security.PermitAll;
Expand All @@ -21,7 +23,7 @@
@PageTitle("Lagerobjekt erfassen")
@Route(value = "lagerobjektErfassen", layout = MainLayout.class)
@PermitAll
public class LagerobjektErfassenView extends VerticalLayout {
public class LagerobjektErfassenView extends VerticalLayout implements BeforeLeaveObserver {

private Storage storage;
private StorageObject storageObject;
Expand Down Expand Up @@ -175,4 +177,9 @@ private void onSave(Type selectedType, Storage value) {
this.storageObject = storageObjectService.saveStorageObject(StorageObject.builder().name("Temporary Name").build());
changeForm(selectedType, value);
}

@Override
public void beforeLeave(BeforeLeaveEvent beforeLeaveEvent) {
storageObjectService.deleteStorageObjectById(storageObject.getId());
}
}

0 comments on commit 2aa88f8

Please sign in to comment.