Skip to content

Commit

Permalink
board: invalidate newCardsView(s) when a card is added/removed
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Jul 22, 2017
1 parent b4e780e commit 843150d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/de/metas/ui/web/board/BoardRestController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package de.metas.ui.web.board;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;

import org.adempiere.exceptions.AdempiereException;
Expand Down Expand Up @@ -39,6 +42,7 @@
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.view.ViewResult;
import de.metas.ui.web.view.descriptor.ViewLayout;
import de.metas.ui.web.view.event.ViewChangesCollector;
import de.metas.ui.web.view.json.JSONFilterViewRequest;
import de.metas.ui.web.view.json.JSONViewDataType;
import de.metas.ui.web.view.json.JSONViewResult;
Expand Down Expand Up @@ -88,11 +92,39 @@ public class BoardRestController
@Autowired
private IViewsRepository viewsRepo;

private final ConcurrentHashMap<Integer, Set<IView>> boardId2newCardsViewId = new ConcurrentHashMap<>();

private JSONOptions newJSONOptions()
{
return JSONOptions.builder(userSession).build();
}

private void addActiveNewCardsView(final int boardId, final IView view)
{
final Set<IView> boardActiveViews = boardId2newCardsViewId.computeIfAbsent(boardId, id -> Collections.newSetFromMap(new WeakHashMap<>()));
synchronized (boardActiveViews)
{
boardActiveViews.add(view);
}
}

private void invalidateAllNewCardsViews(final int boardId)
{
final Set<IView> boardActiveViews = boardId2newCardsViewId.get(boardId);
if (boardActiveViews == null)
{
return;
}

synchronized (boardActiveViews)
{
// NOTE: because we are actually not deleting from view but just filtering out the board cardIds,
// we don't have to actually invalidate the view but just fire an "refresh request" to frontend
boardActiveViews.forEach(view -> ViewChangesCollector.getCurrentOrAutoflush()
.collectFullyChanged(view));
}
}

@GetMapping("/{boardId}")
public JSONBoard getBoard(@PathVariable("boardId") final int boardId)
{
Expand Down Expand Up @@ -130,6 +162,7 @@ public JSONBoardCard addCard(@PathVariable("boardId") final int boardId, @Reques

final int position = request.getPosition() != null ? request.getPosition() : Integer.MAX_VALUE;
final BoardCard card = boardsRepo.addCardForDocumentId(boardId, request.getLaneId(), request.getDocumentId(), position);
invalidateAllNewCardsViews(boardId);
return JSONBoardCard.of(card, userSession.getAD_Language());
}

Expand Down Expand Up @@ -164,6 +197,7 @@ public void removeCard(@PathVariable("boardId") final int boardId, @PathVariable
userSession.assertLoggedIn();

boardsRepo.removeCard(boardId, cardId);
invalidateAllNewCardsViews(boardId);
}

@PatchMapping("/{boardId}/card/{cardId}")
Expand Down Expand Up @@ -221,6 +255,8 @@ public JSONViewResult createNewCardsView(@PathVariable("boardId") final int boar
.setStickyFilters(boardDescriptor.getDocumentFilters())
.build();
final IView view = viewsRepo.createView(request);
addActiveNewCardsView(boardId, view);

return toJSONCardsViewResult(boardId, view, userSession.getAD_Language());
}

Expand Down

0 comments on commit 843150d

Please sign in to comment.