use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class WebRestApiContextProvider method switchContext.
@Override
public IAutoCloseable switchContext(@NonNull final Properties ctx) {
// then it's better to do nothing because this could end in a StackOverflowException.
if (ctx == ctxProxy) {
logger.trace("Not switching context because the given temporary context it's actually our context proxy: {}", ctx);
return NullAutoCloseable.instance;
}
final Properties previousTempCtx = temporaryCtxHolder.get();
temporaryCtxHolder.set(ctx);
logger.trace("Switched to temporary context. \n New temporary context: {} \n Previous temporary context: {}", ctx, previousTempCtx);
return new IAutoCloseable() {
private boolean closed = false;
@Override
public void close() {
if (closed) {
return;
}
if (previousTempCtx != null) {
temporaryCtxHolder.set(previousTempCtx);
} else {
temporaryCtxHolder.remove();
}
closed = true;
logger.trace("Switched back from temporary context");
}
};
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class WindowQuickInputRestController method forQuickInputWritable.
private final <R> R forQuickInputWritable(final QuickInputPath quickInputPath, final IDocumentChangesCollector changesCollector, final Function<QuickInput, R> quickInputProcessor) {
return documentsCollection.forRootDocumentWritable(quickInputPath.getRootDocumentPath(), changesCollector, rootDocument -> {
try (final IAutoCloseable c = getQuickInputNoLock(quickInputPath).lockForWriting()) {
final QuickInput quickInput = getQuickInputNoLock(quickInputPath).copy(CopyMode.CheckOutWritable, changesCollector).bindRootDocument(rootDocument).assertTargetWritable();
final R result = quickInputProcessor.apply(quickInput);
commit(quickInput);
return result;
}
});
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class DocumentCollection method invalidateIncludedDocuments.
private void invalidateIncludedDocuments(final DocumentPath documentPath) {
Check.assume(!documentPath.isRootDocument(), "included document path: {}", documentPath);
//
// Get the root document if exists
final DocumentPath rootDocumentPath = documentPath.getRootDocumentPath();
final DocumentKey documentKey = DocumentKey.ofRootDocumentPath(rootDocumentPath);
final Document document = rootDocuments.getIfPresent(documentKey);
// Invalidate
if (document != null) {
try (final IAutoCloseable lock = document.lockForWriting()) {
document.getIncludedDocumentsCollection(documentPath.getDetailId()).markStale(documentPath.getSingleRowId());
}
}
//
// Notify frontend, even if the root document does not exist (or it was not cached).
websocketPublisher.staleByDocumentPath(documentPath);
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class ADProcessInstancesRepository method forProcessInstanceWritable.
@Override
public <R> R forProcessInstanceWritable(final DocumentId pinstanceId, final IDocumentChangesCollector changesCollector, final Function<IProcessInstanceController, R> processor) {
try (final IAutoCloseable writeLock = getOrLoad(pinstanceId).lockForWriting()) {
final ADProcessInstanceController processInstance = getOrLoad(pinstanceId).copyReadWrite(changesCollector).bindContextSingleDocumentIfPossible(documentsCollection);
// Make sure the process was not already executed.
// If it was executed we are not allowed to change it.
processInstance.assertNotExecuted();
try (final IAutoCloseable c = processInstance.activate()) {
// Call the given processor to apply changes to this process instance.
final R result = processor.apply(processInstance);
// Actually put it back
// throwEx=false
processInstance.saveIfValidAndHasChanges(false);
processInstances.put(pinstanceId, processInstance.copyReadonly());
return result;
}
}
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class ADProcessInstancesRepository method createNewProcessInstance0.
/**
* @param request
* @param shadowParentDocumentEvaluatee optional shadowParentDocumentEvaluatee which will be
* @return
*/
private IProcessInstanceController createNewProcessInstance0(@NonNull final CreateProcessInstanceRequest request, @Nullable final IDocumentEvaluatee evalCtx) {
//
// Save process info together with it's parameters and get the the newly created AD_PInstance_ID
final ProcessInfo processInfo = createProcessInfo(request);
Services.get(IADPInstanceDAO.class).saveProcessInfo(processInfo);
final DocumentId adPInstanceId = DocumentId.of(processInfo.getAD_PInstance_ID());
final Object processClassInstance = processInfo.newProcessClassInstanceOrNull();
try (final IAutoCloseable c = JavaProcess.temporaryChangeCurrentInstance(processClassInstance)) {
//
// Build the parameters document
final ProcessDescriptor processDescriptor = getProcessDescriptor(request.getProcessId());
final DocumentEntityDescriptor parametersDescriptor = processDescriptor.getParametersDescriptor();
final Document parametersDoc = ADProcessParametersRepository.instance.createNewParametersDocument(parametersDescriptor, adPInstanceId, evalCtx);
final int windowNo = parametersDoc.getWindowNo();
// Set parameters's default values
ProcessDefaultParametersUpdater.newInstance().addDefaultParametersProvider(processClassInstance instanceof IProcessDefaultParametersProvider ? (IProcessDefaultParametersProvider) processClassInstance : null).onDefaultValue((parameter, value) -> parametersDoc.processValueChange(parameter.getColumnName(), value, () -> "default parameter value")).updateDefaultValue(parametersDoc.getFieldViews(), field -> DocumentFieldAsProcessDefaultParameter.of(windowNo, field));
//
// Create (webui) process instance and add it to our internal cache.
final ADProcessInstanceController pinstance = ADProcessInstanceController.builder().caption(processDescriptor.getCaption()).instanceId(adPInstanceId).parameters(parametersDoc).processClassInstance(processClassInstance).contextSingleDocumentPath(request.getSingleDocumentPath()).viewId(request.getViewRowIdsSelection() != null ? request.getViewRowIdsSelection().getViewId() : null).build();
processInstances.put(adPInstanceId, pinstance.copyReadonly());
return pinstance;
}
}
Aggregations