Skip to content

Commit

Permalink
after process execution, refresh required documents
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Oct 4, 2017
1 parent d384a32 commit 4d535d6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.LookupValuesList;
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import de.metas.ui.web.window.model.DocumentCollection;
import de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier;
import de.metas.ui.web.window.model.IDocumentFieldView;

Expand Down Expand Up @@ -48,7 +49,7 @@ public interface IProcessInstanceController
{
DocumentId getInstanceId();

ProcessInstanceResult startProcess(IViewsRepository viewsRepo);
ProcessInstanceResult startProcess(IViewsRepository viewsRepo, DocumentCollection documentsCollection);

/**
* @return execution result or throws exception if the process was not already executed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import de.metas.ui.web.window.datatypes.json.JSONLookupValuesList;
import de.metas.ui.web.window.datatypes.json.JSONOptions;
import de.metas.ui.web.window.model.DocumentCollection;
import de.metas.ui.web.window.model.IDocumentChangesCollector;
import de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier;
import de.metas.ui.web.window.model.NullDocumentChangesCollector;
Expand Down Expand Up @@ -86,6 +87,8 @@ public class ProcessRestController

@Autowired
private IViewsRepository viewsRepo;
@Autowired
private DocumentCollection documentsCollection;

private final ConcurrentHashMap<String, IProcessInstancesRepository> pinstancesRepositoriesByHandlerType = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -249,7 +252,7 @@ public JSONProcessInstanceResult startProcess(
.execute(() -> {
final IDocumentChangesCollector changesCollector = NullDocumentChangesCollector.instance;
return instancesRepository.forProcessInstanceWritable(pinstanceId, changesCollector, processInstance -> {
final ProcessInstanceResult result = processInstance.startProcess(viewsRepo);
final ProcessInstanceResult result = processInstance.startProcess(viewsRepo, documentsCollection);
return JSONProcessInstanceResult.of(result);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.function.Function;
import java.util.function.Supplier;

import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.IQueryFilter;
Expand All @@ -27,6 +28,7 @@
import org.slf4j.Logger;

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

import de.metas.adempiere.report.jasper.OutputType;
Expand Down Expand Up @@ -181,6 +183,11 @@ public DocumentId getInstanceId()
{
return instanceId;
}

public ViewId getViewId()
{
return viewId;
}

private Document getParametersDocument()
{
Expand Down Expand Up @@ -264,7 +271,7 @@ private boolean isExecuted()
}

@Override
public ProcessInstanceResult startProcess(final IViewsRepository viewsRepo)
public ProcessInstanceResult startProcess(final IViewsRepository viewsRepo, final DocumentCollection documentsCollection)
{
assertNotExecuted();

Expand All @@ -277,26 +284,25 @@ public ProcessInstanceResult startProcess(final IViewsRepository viewsRepo)
}

//
executionResult = executeADProcess(getInstanceId(), getDescriptor(), viewsRepo);
executionResult = executeADProcess(viewsRepo, documentsCollection);
if (executionResult.isSuccess())
{
executed = false;
}
return executionResult;
}

private static final ProcessInstanceResult executeADProcess(final DocumentId adPInstanceId, final ProcessDescriptor processDescriptor, final IViewsRepository viewsRepo)
private final ProcessInstanceResult executeADProcess(final IViewsRepository viewsRepo, final DocumentCollection documentsCollection)
{
//
// Create the process info and execute the process synchronously
final Properties ctx = Env.getCtx(); // We assume the right context was already used when the process was loaded
final String adLanguage = Env.getAD_Language(ctx);
final String name = processDescriptor.getCaption().translate(adLanguage);
final ProcessExecutor processExecutor = ProcessInfo.builder()
.setCtx(ctx)
.setCreateTemporaryCtx()
.setAD_PInstance_ID(adPInstanceId.toInt())
.setTitle(name)
.setAD_PInstance_ID(getInstanceId().toInt())
.setTitle(getDescriptor().getCaption().translate(adLanguage))
.setPrintPreview(true)
.setJRDesiredOutputType(OutputType.PDF)
//
Expand All @@ -305,6 +311,8 @@ private static final ProcessInstanceResult executeADProcess(final DocumentId adP
.onErrorThrowException() // throw exception directly... this will allow the original exception (including exception params) to be sent back to frontend
.executeSync();
final ProcessExecutionResult processExecutionResult = processExecutor.getResult();

invalidateDocumentsAndViews(processExecutionResult, viewsRepo, documentsCollection);

//
// Build and return the execution result
Expand All @@ -319,7 +327,7 @@ private static final ProcessInstanceResult executeADProcess(final DocumentId adP
final ProcessInstanceResult.Builder resultBuilder = ProcessInstanceResult.builder(DocumentId.of(processExecutionResult.getAD_PInstance_ID()))
.setSummary(summary)
.setError(processExecutionResult.isError());

//
// Create result post process actions
{
Expand Down Expand Up @@ -383,7 +391,48 @@ else if (recordsToOpen != null && recordsToOpen.getTarget() == OpenTarget.Single
final ProcessInstanceResult result = resultBuilder.build();
return result;
}
}

private void invalidateDocumentsAndViews(final ProcessExecutionResult processExecutionResult,
final IViewsRepository viewsRepo,
final DocumentCollection documentsCollection)
{
final Supplier<IView> viewSupplier = Suppliers.memoize(()->{
final ViewId viewId = getViewId();
if(viewId == null)
{
return null;
}

final IView view = viewsRepo.getViewIfExists(viewId);
if(view == null)
{
logger.warn("No view found for {}. View invalidation will be skipped for {}", viewId, processExecutionResult);
}
return view;
});

//
// Refresh all
boolean viewInvalidateAllCalled = false;
if(processExecutionResult.isRefreshAllAfterExecution() && viewSupplier.get() != null)
{
viewSupplier.get().invalidateAll();
viewInvalidateAllCalled = true;
}

//
// Refresh required document
final TableRecordReference recordToRefresh = processExecutionResult.getRecordToRefreshAfterExecution();
if (recordToRefresh != null)
{
documentsCollection.invalidateDocumentByRecordId(recordToRefresh.getTableName(), recordToRefresh.getRecord_ID());

if(!viewInvalidateAllCalled && viewSupplier.get() != null)
{
viewSupplier.get().notifyRecordsChanged(ImmutableSet.of(recordToRefresh));
}
}
}

private static final File saveReportToDiskIfAny(final ProcessExecutionResult processExecutionResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import de.metas.ui.web.window.descriptor.DocumentEntityDescriptor;
import de.metas.ui.web.window.model.Document;
import de.metas.ui.web.window.model.DocumentCollection;
import de.metas.ui.web.window.model.DocumentValidStatus;
import de.metas.ui.web.window.model.IDocumentChangesCollector;
import de.metas.ui.web.window.model.IDocumentChangesCollector.ReasonSupplier;
Expand Down Expand Up @@ -159,7 +160,7 @@ public ProcessInstanceResult getExecutionResult()
}

@Override
public ProcessInstanceResult startProcess(final IViewsRepository viewsRepo)
public ProcessInstanceResult startProcess(final IViewsRepository viewsRepo, final DocumentCollection documentsCollection_NOTUSED)
{
assertNotExecuted();

Expand Down

0 comments on commit 4d535d6

Please sign in to comment.