Skip to content

Commit

Permalink
#150 DocumentViewChanges.changedDocumentIds
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Feb 19, 2017
1 parent fd971c8 commit bef5179
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.adempiere.util.GuavaCollectors;
import org.adempiere.util.Services;
import org.adempiere.util.lang.ExtendedMemorizingSupplier;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
Expand Down Expand Up @@ -222,7 +223,7 @@ public DocumentPath getReferencingDocumentPath()
{
return referencingDocumentPath;
}

public String getReferencingTableName()
{
return referencingTableName;
Expand All @@ -242,11 +243,39 @@ private void invalidateAllNoNotify()
documentViewsLoader.getAttributesProvider().invalidateAll();
}

@Override
public void notifyRecordChanged(final TableRecordReference recordRef)
{
if (!I_M_HU.Table_Name.equals(recordRef.getTableName()))
{
return;
}

final IndexedDocumentViews records = getRecordsNoLoad();
if (records == null)
{
return;
}

final DocumentId documentId = DocumentId.of(recordRef.getRecord_ID());
if (!records.contains(documentId))
{
return;
}

invalidateAll();
}

private IndexedDocumentViews getRecords()
{
return _recordsSupplier.get();
}

private IndexedDocumentViews getRecordsNoLoad()
{
return _recordsSupplier.peek();
}

private IndexedDocumentViews retrieveRecords()
{
final List<HUDocumentView> recordsList = documentViewsLoader.retrieveDocumentViews();
Expand Down Expand Up @@ -277,6 +306,11 @@ public HUDocumentView getById(final DocumentId documentId)
return record;
}

public boolean contains(final DocumentId documentId)
{
return allRecordsById.containsKey(documentId);
}

public Stream<HUDocumentView> streamByIds(final Collection<DocumentId> documentIds)
{
if (documentIds == null || documentIds.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.adempiere.util.Check;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand All @@ -27,11 +29,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand Down Expand Up @@ -83,4 +85,13 @@ private final void onViewRemoved(final RemovalNotification<Object, Object> notif
final IDocumentViewSelection view = (IDocumentViewSelection)notification.getValue();
view.close();
}

@Override
public void notifyRecordChanged(final TableRecordReference recordRef)
{
Check.assumeNotNull(recordRef, "Parameter recordRef is not null");

views.asMap().values().stream()
.forEach(view -> view.notifyRecordChanged(recordRef));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ default IDocumentViewSelection assertWindowIdMatches(final int expectedWindowId)
<T> List<T> retrieveModelsByIds(Collection<DocumentId> documentIds, Class<T> modelClass);

Stream<? extends IDocumentView> streamByIds(Collection<DocumentId> documentIds);

/**
* Notify the view that given record has changed.
*/
void notifyRecordChanged(TableRecordReference recordRef);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.adempiere.util.lang.impl.TableRecordReference;

import de.metas.ui.web.view.json.JSONCreateDocumentViewRequest;

/*
Expand All @@ -17,11 +19,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand All @@ -43,4 +45,9 @@ default <T extends IDocumentViewSelection> T getView(final String viewId, final

List<IDocumentViewSelection> getViews();

/**
* Notify all views that given record was changed.
*/
void notifyRecordChanged(TableRecordReference recordRef);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.adempiere.model.PlainContextAware;
import org.adempiere.util.Check;
import org.adempiere.util.Services;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.util.CCache;
import org.compiere.util.DB;
import org.compiere.util.Evaluatee;
Expand All @@ -39,6 +40,7 @@
import de.metas.ui.web.view.descriptor.SqlDocumentViewBinding;
import de.metas.ui.web.view.descriptor.SqlDocumentViewBinding.SqlDocumentViewFieldValueLoader;
import de.metas.ui.web.view.descriptor.SqlDocumentViewBinding.ViewFieldsBinding;
import de.metas.ui.web.view.event.DocumentViewChangesCollector;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.DocumentType;
Expand Down Expand Up @@ -66,11 +68,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand Down Expand Up @@ -499,6 +501,19 @@ public <T> List<T> retrieveModelsByIds(final Collection<DocumentId> documentIds,
.list(modelClass);
}

@Override
public void notifyRecordChanged(final TableRecordReference recordRef)
{
if (!Objects.equals(getTableName(), recordRef.getTableName()))
{
return;
}

final DocumentId documentId = DocumentId.of(recordRef.getRecord_ID());
DocumentViewChangesCollector.getCurrentOrAutoflush()
.collectDocumentChanged(this, documentId);
}

//
//
// Builder
Expand Down Expand Up @@ -700,6 +715,5 @@ private DocumentReferencesService getDocumentReferencesService()
Check.assumeNotNull(documentReferencesService, "Parameter documentReferencesService is not null");
return documentReferencesService;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package de.metas.ui.web.view.event;

import java.util.HashSet;
import java.util.Set;

import org.adempiere.util.Check;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;

import de.metas.ui.web.window.datatypes.DocumentId;

/*
* #%L
Expand All @@ -15,11 +23,11 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand All @@ -30,6 +38,7 @@ public class DocumentViewChanges
private final int adWindowId;

private boolean fullyChanged;
private Set<DocumentId> changedDocumentIds = null;

/* package */ DocumentViewChanges(final String viewId, final int adWindowId)
{
Expand All @@ -44,6 +53,15 @@ public void collectFrom(final DocumentViewChanges changes)
{
fullyChanged = true;
}

if (changes.changedDocumentIds != null && !changes.changedDocumentIds.isEmpty())
{
if (changedDocumentIds == null)
{
changedDocumentIds = new HashSet<>();
}
changedDocumentIds.addAll(changes.changedDocumentIds);
}
}

@Override
Expand All @@ -54,6 +72,7 @@ public String toString()
.add("viewId", viewId)
.add("AD_Window_ID", adWindowId)
.add("fullyChanged", fullyChanged ? Boolean.TRUE : null)
.add("changedDocumentIds", changedDocumentIds)
.toString();
}

Expand All @@ -77,8 +96,24 @@ public boolean isFullyChanged()
return fullyChanged;
}

public Boolean getFullyChanged()
public void addChangedDocumentId(final DocumentId documentId)
{
Check.assumeNotNull(documentId, "Parameter documentId is not null");

if (changedDocumentIds == null)
{
changedDocumentIds = new HashSet<>();
}
changedDocumentIds.add(documentId);
}

public boolean hasChangedDocumentIds()
{
return changedDocumentIds != null && !changedDocumentIds.isEmpty();
}

public Set<DocumentId> getChangedDocumentIds()
{
return fullyChanged ? Boolean.TRUE : null;
return changedDocumentIds == null ? ImmutableSet.of() : ImmutableSet.copyOf(changedDocumentIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.metas.logging.LogManager;
import de.metas.ui.web.view.IDocumentViewSelection;
import de.metas.ui.web.websocket.WebSocketConfig;
import de.metas.ui.web.window.datatypes.DocumentId;
import groovy.transform.Immutable;

/*
Expand Down Expand Up @@ -110,13 +111,12 @@ public static final DocumentViewChangesCollector getCurrentOrAutoflush()
@Autowired
@Lazy
private SimpMessagingTemplate websocketMessagingTemplate;

private final boolean autoflush;

private final AtomicBoolean closed = new AtomicBoolean(false);
private final Map<DocumentViewChangesKey, DocumentViewChanges> viewChangesMap = new LinkedHashMap<>();


private DocumentViewChangesCollector()
{
this(false); // autoflush=false
Expand Down Expand Up @@ -170,15 +170,22 @@ private DocumentViewChanges viewChanges(final DocumentViewChangesKey key)
public void collectFullyChanged(final IDocumentViewSelection view)
{
viewChanges(view).setFullyChanged();


autoflushIfEnabled();
}

public void collectDocumentChanged(final IDocumentViewSelection view, final DocumentId documentId)
{
viewChanges(view).addChangedDocumentId(documentId);

autoflushIfEnabled();
}

private void collectFromChanges(final DocumentViewChanges changes)
{
final DocumentViewChangesKey key = new DocumentViewChangesKey(changes.getViewId(), changes.getAD_Window_ID());
viewChanges(key).collectFrom(changes);

autoflushIfEnabled();
}

Expand All @@ -196,11 +203,11 @@ private DocumentViewChangesCollector getParentOrNull()
private void flush()
{
final List<DocumentViewChanges> changesList = getAndClean();
if(changesList.isEmpty())
if (changesList.isEmpty())
{
return;
}

//
// Try flushing to parent collector if any
final DocumentViewChangesCollector parentCollector = getParentOrNull();
Expand All @@ -220,24 +227,24 @@ private void flush()
.forEach(this::sendToWebsocket);
}
}

private void autoflushIfEnabled()
{
if(!autoflush)
if (!autoflush)
{
return;
}

flush();
}

private List<DocumentViewChanges> getAndClean()
{
if(viewChangesMap.isEmpty())
if (viewChangesMap.isEmpty())
{
return ImmutableList.of();
}

final List<DocumentViewChanges> changesList = ImmutableList.copyOf(viewChangesMap.values());
viewChangesMap.clear();
return changesList;
Expand Down
Loading

0 comments on commit bef5179

Please sign in to comment.