Skip to content

Commit

Permalink
Improvements and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuJLund committed Jun 4, 2024
1 parent 8480a25 commit 103236d
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 126 deletions.
2 changes: 1 addition & 1 deletion vaadincreate-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<dependency>
<groupId>com.vaadin.testbench</groupId>
<artifactId>vaadin-uiunittest</artifactId>
<version>5.0.0-alpha5</version>
<version>5.0.0-alpha6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import org.vaadin.tatu.vaadincreate.backend.data.Availability;
import org.vaadin.tatu.vaadincreate.backend.data.Category;
import org.vaadin.tatu.vaadincreate.backend.data.Product;
import org.vaadin.tatu.vaadincreate.crud.LockedBooks.BookEvent;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus.EventBusListener;
import org.vaadin.tatu.vaadincreate.i18n.HasI18N;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects.LockingEvent;
import org.vaadin.tatu.vaadincreate.util.Utils;

import com.vaadin.data.ValueContext;
Expand Down Expand Up @@ -43,10 +44,11 @@ public class BookGrid extends Grid<Product>
private static final String PRICE = "price";
private static final String PRODUCT_NAME = "product-name";
private static final String CANNOT_CONVERT = "cannot-convert";
private static final String EDITED_BY = "edited-by";

private Registration resizeReg;
private Label availabilityCaption;
private LockedBooks lockedBooks = LockedBooks.get();
private LockedObjects lockedBooks = LockedObjects.get();
private EventBus eventBus = EventBus.get();

private Product editedProduct;
Expand All @@ -62,7 +64,7 @@ public BookGrid() {
if (book.getId() == edited) {
return VaadinCreateTheme.BOOKVIEW_GRID_EDITED;
}
if (lockedBooks.lockedBooks().contains(book.getId())) {
if (lockedBooks.isLocked(Product.class, book.getId()) != null) {
return VaadinCreateTheme.BOOKVIEW_GRID_LOCKED;
}
return "";
Expand Down Expand Up @@ -189,7 +191,13 @@ public void attach() {
}

private void adjustColumns(int width) {
setDescriptionGenerator(null);
setDescriptionGenerator(book -> {
var user = lockedBooks.isLocked(Product.class, book.getId());
if (user != null) {
return getTranslation(EDITED_BY, user.getName());
}
return null;
});
getColumns().forEach(c -> c.setHidden(true));
if (width < 650) {
getColumn("name").setHidden(false).setWidth(300);
Expand Down Expand Up @@ -267,8 +275,8 @@ public void setEdited(Product product) {
@SuppressWarnings("unchecked")
@Override
public void eventFired(Object event) {
if (event instanceof BookEvent && isAttached()) {
var bookEvent = (BookEvent) event;
if (event instanceof LockingEvent && isAttached()) {
var bookEvent = (LockingEvent) event;
getUI().access(() -> {
ListDataProvider<Product> dataProvider = (ListDataProvider<Product>) getDataProvider();
dataProvider.getItems().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.vaadin.tatu.vaadincreate.backend.data.Category;
import org.vaadin.tatu.vaadincreate.backend.data.Product;
import org.vaadin.tatu.vaadincreate.backend.data.User.Role;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects;

/**
* This class provides an interface for the logical operations between the CRUD
Expand All @@ -35,7 +36,7 @@ public class BooksPresenter implements Serializable {
.getProductService();
private AccessControl accessControl = VaadinCreateUI.get()
.getAccessControl();
private LockedBooks lockedBooks = LockedBooks.get();
private LockedObjects lockedBooks = LockedObjects.get();
private Integer editing;

public BooksPresenter(BooksView simpleCrudView) {
Expand Down Expand Up @@ -85,7 +86,7 @@ public void cancelProduct() {

private void unlockBook() {
if (editing != null) {
lockedBooks.unlock(editing);
lockedBooks.unlock(Product.class, editing);
editing = null;
}
}
Expand All @@ -94,7 +95,7 @@ private void lockBook(Integer id) {
if (editing != null) {
unlockBook();
}
lockedBooks.lock(id);
lockedBooks.lock(Product.class, id, CurrentUser.get().get());
editing = id;
}

Expand Down Expand Up @@ -173,8 +174,8 @@ public void newProduct() {

public void rowSelected(Product product) {
if (accessControl.isUserInRole(Role.ADMIN)) {
if (product != null
&& lockedBooks.lockedBooks().contains(product.getId())) {
if (product != null && lockedBooks.isLocked(Product.class,
product.getId()) != null) {
view.clearSelection();
} else {
editProduct(product);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public void beforeLeave(ViewBeforeLeaveEvent event) {
var book = getSelectedRow();
if (book != null) {
getUI().access(() -> {
logger.debug("Set fragment: "+book.getId());
setFragmentParameter("" + book.getId());
});
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ private EventBusImpl() {
@Override
public void post(Object event) {
synchronized (eventListeners) {
logger.info("EventBus ({}) event fired for {} recipients.",
this.hashCode(), eventListeners.size());
logger.debug(
"EventBus event fired for {} recipients.",
eventListeners.size());
eventListeners.forEach((listener, o) -> {
listener.eventFired(event);
});
Expand All @@ -44,16 +45,16 @@ public void post(Object event) {
@Override
public void registerEventBusListener(EventBusListener listener) {
synchronized (eventListeners) {
logger.info("EventBus ({}) listenerer registered", this.hashCode());
logger.debug("EventBus ({}) listenerer registered", listener.hashCode());
eventListeners.put(listener, null);
}
}

@Override
public void unregisterEventBusListener(EventBusListener listener) {
synchronized (eventListeners) {
logger.info("EventBus ({}) listenerer un-registered",
this.hashCode());
logger.debug("EventBus ({}) listenerer un-registered",
listener.hashCode());
eventListeners.remove(listener);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.vaadin.tatu.vaadincreate.locking;

import java.io.Serializable;

import org.vaadin.tatu.vaadincreate.backend.data.User;

@SuppressWarnings("serial")
public interface LockedObjects {

public User isLocked(Class<?> type, Integer id);

public void lock(Class<?> type, Integer id, User user);

public void unlock(Class<?> type, Integer id);

public static LockedObjects get() {
return LockedObjectsImpl.getInstance();
}

public static class LockingEvent implements Serializable {
private Integer id;
private User user;
private Class<?> type;

public LockingEvent(Class<?> type, Integer id, User user) {
this.id = id;
this.type = type;
this.user = user;
}

public User getUser() {
return user;
}

public Integer getId() {
return id;
}

public Class<?> getType() {
return type;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.vaadin.tatu.vaadincreate.locking;

import java.io.Serializable;
import java.util.Optional;
import java.util.WeakHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.tatu.vaadincreate.backend.data.User;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;

@SuppressWarnings("serial")
public class LockedObjectsImpl implements LockedObjects {

private static LockedObjectsImpl INSTANCE;
private EventBus eventBus = EventBus.get();

private final WeakHashMap<LockedObject, Object> lockedObjects = new WeakHashMap<>();

public synchronized static LockedObjects getInstance() {
if (INSTANCE == null) {
INSTANCE = new LockedObjectsImpl();
}
return INSTANCE;
}

private LockedObjectsImpl() {
}

@Override
public User isLocked(Class<?> type, Integer id) {
synchronized (lockedObjects) {
var match = findObject(type, id);
if (match.isPresent()) {
return match.get().user;
}
}
return null;
}

@Override
public void lock(Class<?> type, Integer id, User user) {
if (id != null && id < 0) {
throw new IllegalArgumentException(
"Id can't be null and must be positive");
}
synchronized (lockedObjects) {
var match = findObject(type, id);
if (match.isPresent()) {
throw new IllegalStateException(
"Can't locked book already locked: " + id);
}
lockedObjects.put(new LockedObject(type, id, user), null);
eventBus.post(new LockingEvent(type, id, user));
logger.debug("Locked book {}", id);
}
}

@Override
public void unlock(Class<?> type, Integer id) {
synchronized (lockedObjects) {
var match = findObject(type, id);
if (match.isPresent()) {
var object = match.get();
lockedObjects.remove(object);
eventBus.post(new LockingEvent(type, id, object.user));
}
logger.debug("Unlocked book {}", id);
}
}

private Optional<LockedObject> findObject(Class<?> type, Integer id) {
return lockedObjects.keySet().stream()
.filter(obj -> obj.type.equals(type) && obj.id.equals(id))
.findFirst();
}

static class LockedObject implements Serializable {
Integer id;
Class<?> type;
User user;

LockedObject(Class<?> type, Integer id, User user) {
this.type = type;
this.id = id;
this.user = user;
}
}

private Logger logger = LoggerFactory.getLogger(this.getClass());
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ not-found-desc = You tried to navigate to a view "{0}" that does not exist.
count = Count
vaadin = This application is using Vaadin version "{0}"
vaadin-web = Vaadin web page
edited-by = Edited by {0}
Loading

0 comments on commit 103236d

Please sign in to comment.