Skip to content

Commit

Permalink
#19 invalidate included documents
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Mar 2, 2017
1 parent 9774f90 commit 3e7acdb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -62,11 +63,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 @@ -283,10 +284,10 @@ private Set<String> buildFieldsWithCharacteristic(final Characteristic character
return getFields()
.stream()
.filter(field -> field.hasCharacteristic(characteristic))
.map(field->field.getFieldName())
.map(field -> field.getFieldName())
.collect(GuavaCollectors.toImmutableSet());
}

public IIncludedDocumentsCollection createIncludedDocumentsCollection(final Document parentDocument)
{
return includedDocumentsCollectionFactory.createIncludedDocumentsCollection(parentDocument, this);
Expand All @@ -312,11 +313,18 @@ public DocumentEntityDescriptor getIncludedEntityByDetailId(final DetailId detai
return includedEntityDescriptor;
}

public Stream<DocumentEntityDescriptor> getIncludedEntitiesByTableName(final String tableName)
{
return includedEntitiesByDetailId.values()
.stream()
.filter(entityDescriptor -> tableName.equals(entityDescriptor.getTableNameOrNull()));
}

public DocumentEntityDataBindingDescriptor getDataBinding()
{
return dataBinding;
}

public boolean isHighVolume()
{
return highVolume;
Expand Down Expand Up @@ -425,7 +433,6 @@ public static final class Builder
private Optional<String> _tableName = Optional.empty();
private Optional<Boolean> _isSOTrx = Optional.empty();


private Builder()
{
super();
Expand Down Expand Up @@ -563,7 +570,7 @@ public String getIdFieldName()
.stream()
.filter(fieldBuilder -> fieldBuilder.isKey())
.collect(Collectors.toList());
if(idFields.isEmpty())
if (idFields.isEmpty())
{
return null;
}
Expand Down Expand Up @@ -602,7 +609,7 @@ public Map<DetailId, DocumentEntityDescriptor> buildIncludedEntitiesByDetailId()
{
return ImmutableMap.copyOf(_includedEntitiesByDetailId);
}

public IIncludedDocumentsCollectionFactory getIncludedDocumentsCollectionFactory()
{
if (isHighVolume())
Expand All @@ -614,15 +621,14 @@ public IIncludedDocumentsCollectionFactory getIncludedDocumentsCollectionFactory
else
{
// TODO implement an IIncludedDocumentCollection which does not cache the documents but which allows New/Delete
// Case: e.g. Product->Price, Product->CU-TU etc
// Case: e.g. Product->Price, Product->CU-TU etc
}
}

// Fallback
return IncludedDocumentsCollection::new;
return IncludedDocumentsCollection::new;
}


public Builder setDataBinding(final DocumentEntityDataBindingDescriptorBuilder dataBindingBuilder)
{
_dataBinding = dataBindingBuilder;
Expand All @@ -641,19 +647,18 @@ private DocumentEntityDataBindingDescriptor getOrBuildDataBinding()
Preconditions.checkNotNull(_dataBinding, "dataBinding");
return _dataBinding.getOrBuild();
}

public Builder setHighVolume(final boolean highVolume)
{
this._highVolume = highVolume;
return this;
}

public boolean isHighVolume()
{
return _highVolume;
}


private DocumentFieldDependencyMap buildDependencies()
{
final DocumentFieldDependencyMap.Builder dependenciesBuilder = DocumentFieldDependencyMap.builder();
Expand All @@ -667,7 +672,7 @@ public Builder setDocumentType(final DocumentType documentType, final DocumentId
_documentTypeId = documentTypeId;
return this;
}

public Builder setDocumentType(final DocumentType documentType, final int documentTypeIdInt)
{
setDocumentType(documentType, DocumentId.of(documentTypeIdInt));
Expand Down Expand Up @@ -724,7 +729,7 @@ public String getTableNameOrNull()
{
return _tableName.orElse(null);
}

public boolean isTableName(final String expectedTableName)
{
return Objects.equals(expectedTableName, _tableName.orElse(null));
Expand Down Expand Up @@ -912,7 +917,7 @@ private ICalloutExecutor buildCalloutExecutorFactory(final Collection<DocumentFi

//
// Standard callouts provider (which will fetch the callouts from application dictionary)
if(isDefaultTableCalloutsEnabled())
if (isDefaultTableCalloutsEnabled())
{
calloutExecutorBuilder.addDefaultCalloutProvider();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public interface DocumentDescriptorFactory
{
DocumentDescriptor getDocumentDescriptor(int AD_Window_ID) throws DocumentLayoutBuildException;

default DocumentEntityDescriptor getDocumentEntityDescriptor(final int AD_Window_ID)
default DocumentEntityDescriptor getDocumentEntityDescriptor(int AD_Window_ID)
{
return getDocumentDescriptor(AD_Window_ID).getEntityDescriptor();
}

default String getTableNameOrNull(final int AD_Window_ID)
default String getTableNameOrNull(int AD_Window_ID)
{
return getDocumentEntityDescriptor(AD_Window_ID).getTableName();
}

default String getTableNameOrNull(final int AD_Window_ID, final DetailId detailId)
default String getTableNameOrNull(int AD_Window_ID, DetailId detailId)
{
final DocumentEntityDescriptor descriptor = getDocumentEntityDescriptor(AD_Window_ID);
DocumentEntityDescriptor descriptor = getDocumentEntityDescriptor(AD_Window_ID);
if (detailId == null)
{
return descriptor.getTableName();
Expand All @@ -59,23 +59,23 @@ default String getTableNameOrNull(final int AD_Window_ID, final DetailId detailI
}
}

default TableRecordReference getTableRecordReference(final DocumentPath documentPath)
default TableRecordReference getTableRecordReference(DocumentPath documentPath)
{
final DocumentEntityDescriptor rootEntityDescriptor = getDocumentEntityDescriptor(documentPath.getAD_Window_ID());
DocumentEntityDescriptor rootEntityDescriptor = getDocumentEntityDescriptor(documentPath.getAD_Window_ID());

if (documentPath.isRootDocument())
{
final String tableName = rootEntityDescriptor.getTableName();
final int recordId = documentPath.getDocumentId().toInt();
String tableName = rootEntityDescriptor.getTableName();
int recordId = documentPath.getDocumentId().toInt();
return TableRecordReference.of(tableName, recordId);
}

final DocumentEntityDescriptor includedEntityDescriptor = rootEntityDescriptor.getIncludedEntityByDetailId(documentPath.getDetailId());
final String tableName = includedEntityDescriptor.getTableName();
final int recordId = documentPath.getSingleRowId().toInt();
DocumentEntityDescriptor includedEntityDescriptor = rootEntityDescriptor.getIncludedEntityByDetailId(documentPath.getDetailId());
String tableName = includedEntityDescriptor.getTableName();
int recordId = documentPath.getSingleRowId().toInt();
return TableRecordReference.of(tableName, recordId);
}

List<DocumentPath> getDocumentPaths(String tableName, int recordIdInt);
List<DocumentPath> getDocumentPaths(String tableName, int documentIdInt, String includedTableName, int includedDocumentIdInt);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import org.adempiere.util.Check;
import org.compiere.model.I_AD_Window;
Expand All @@ -14,6 +15,7 @@

import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.DocumentType;
import de.metas.ui.web.window.descriptor.DocumentDescriptor;
import de.metas.ui.web.window.descriptor.DocumentEntityDescriptor;
import de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory;
Expand Down Expand Up @@ -87,13 +89,26 @@ private Set<DocumentEntityDescriptor> getEntityDescriptorsForTableName(final Str
}

@Override
public List<DocumentPath> getDocumentPaths(final String tableName, final int recordIdInt)
public List<DocumentPath> getDocumentPaths(final String tableName, final int documentIdInt, final String includedTableName, final int includedDocumentIdInt)
{
final DocumentId recordId = DocumentId.of(recordIdInt);
final DocumentId documentId = DocumentId.of(documentIdInt);

return getEntityDescriptorsForTableName(tableName)
.stream()
.map(entityDescriptor -> DocumentPath.rootDocumentPath(entityDescriptor.getDocumentType(), entityDescriptor.getDocumentTypeId(), recordId))
.flatMap(entityDescriptor -> {
final DocumentType documentType = entityDescriptor.getDocumentType();
final DocumentId documentTypeId = entityDescriptor.getDocumentTypeId();
if (includedTableName == null)
{
return Stream.of(DocumentPath.rootDocumentPath(documentType, documentTypeId, documentId));
}
else
{
final DocumentId rowId = DocumentId.of(includedDocumentIdInt);
return entityDescriptor.getIncludedEntitiesByTableName(includedTableName)
.map(includedEntityDescriptor -> DocumentPath.includedDocumentPath(documentType, documentTypeId, documentId, includedEntityDescriptor.getDetailId(), rowId));
}
})
.collect(ImmutableList.toImmutableList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,13 @@ public Document copy(final CopyMode copyMode)
{
return;
}

if (!isWritable())
{
throw new InvalidDocumentStateException(this, "not a writable copy");
}

if(isDeleted())
if (isDeleted())
{
throw new DocumentNotFoundException(getDocumentPath());
}
Expand Down Expand Up @@ -1643,6 +1643,12 @@ private DocumentStaleState getStale()
return _staleStatus.isStaled();
}

/* package */void markStaled()
{
_staleStatus.markStaled();
// includedDocuments.values().forEach(includedDocumentsForDetail -> includedDocumentsForDetail.markStaleAll());
}

public IAutoCloseable lockForReading()
{
// assume _lock is not null
Expand Down Expand Up @@ -1725,6 +1731,11 @@ private boolean checkStaled()
return true;
}

private void markStaled()
{
staled = true;
}

private void markNotStaled(final String version)
{
staled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.adempiere.util.Check;
import org.adempiere.util.lang.IAutoCloseable;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.util.CacheInvalidateRequest;
import org.compiere.util.CacheMgt;
import org.compiere.util.CacheMgtListener;
import org.slf4j.Logger;
Expand Down Expand Up @@ -86,12 +87,17 @@ public Document load(final DocumentKey documentKey)

});

private CacheMgtListener cacheMgtListener = new CacheMgtListener()
private final CacheMgtListener cacheMgtListener = new CacheMgtListener()
{
@Override
public void onReset(String tableName, int recordId)
public void onReset(final CacheInvalidateRequest request)
{
getDocumentPaths(tableName, recordId)
final String tableName = request.getTableName();
final int documentId = request.getRecordId();
final boolean hasChild = request.hasChild();
final String includedTableName = hasChild ? request.getChildTableName() : null;
final int includedDocumentIdInt = hasChild ? request.getChildRecordId() : -1;
getDocumentPaths(tableName, documentId, includedTableName, includedDocumentIdInt)
.forEach(documentPath -> invalidateDocumentByPath(documentPath));
}
};
Expand Down Expand Up @@ -259,7 +265,12 @@ public <R> R forRootDocumentWritable(final DocumentPath documentPathOrNew, final

private final void invalidateDocumentByPath(final DocumentPath documentPath)
{
forDocumentReadonlyIfLoaded(documentPath, document -> document.refreshFromRepository());
forDocumentReadonlyIfLoaded(documentPath, document -> {
document.markStaled();

// TODO: if it's in current scope, shall we also refresh it???
document.refreshFromRepositoryIfStaled();
});
}

/**
Expand Down Expand Up @@ -391,9 +402,9 @@ public TableRecordReference getTableRecordReference(final DocumentPath documentP
return documentDescriptorFactory.getTableRecordReference(documentPath);
}

public List<DocumentPath> getDocumentPaths(final String tableName, final int recordIdInt)
public List<DocumentPath> getDocumentPaths(final String tableName, final int recordIdInt, final String includedTableName, final int includedDocumentIdInt)
{
return documentDescriptorFactory.getDocumentPaths(tableName, recordIdInt);
return documentDescriptorFactory.getDocumentPaths(tableName, recordIdInt, includedTableName, includedDocumentIdInt);
}

public DocumentAttachments getDocumentAttachments(final DocumentPath documentPath)
Expand Down

1 comment on commit 3e7acdb

@teosarca
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task: #19
note: in commit message i forgot to add the repository prefix

Please sign in to comment.