use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class ASIRepository method forASIDocumentWritable.
public <R> R forASIDocumentWritable(final DocumentId asiDocId, final IDocumentChangesCollector changesCollector, final Function<ASIDocument, R> processor) {
try (final IAutoCloseable readLock = getASIDocumentNoLock(asiDocId).lockForWriting()) {
final ASIDocument asiDoc = getASIDocumentNoLock(asiDocId).copy(CopyMode.CheckOutWritable, changesCollector);
final R result = processor.apply(asiDoc);
Services.get(ITrxManager.class).getCurrentTrxListenerManagerOrAutoCommit().newEventListener(TrxEventTiming.AFTER_COMMIT).registerHandlingMethod(trx -> commit(asiDoc));
return result;
}
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class HUReportProcessInstancesRepository method forProcessInstanceWritable.
@Override
public <R> R forProcessInstanceWritable(final DocumentId pinstanceId, final IDocumentChangesCollector changesCollector, final Function<IProcessInstanceController, R> processor) {
try (final IAutoCloseable readLock = getInstance(pinstanceId).lockForWriting()) {
final HUReportProcessInstance processInstance = getInstance(pinstanceId).copyReadWrite(changesCollector);
final R result = processor.apply(processInstance);
putInstance(processInstance);
return result;
}
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class DocumentCollection method forRootDocumentWritable.
public <R> R forRootDocumentWritable(final DocumentPath documentPathOrNew, final IDocumentChangesCollector changesCollector, final Function<Document, R> rootDocumentProcessor) {
final DocumentPath rootDocumentPathOrNew = documentPathOrNew.getRootDocumentPath();
final Document lockHolder;
final boolean isNewRootDocument;
final DocumentKey rootDocumentKey;
if (rootDocumentPathOrNew.isNewDocument()) {
final Document newRootDocument = createRootDocument(rootDocumentPathOrNew, changesCollector);
lockHolder = newRootDocument;
rootDocumentKey = DocumentKey.ofRootDocumentPath(newRootDocument.getDocumentPath());
isNewRootDocument = true;
} else {
rootDocumentKey = DocumentKey.ofRootDocumentPath(rootDocumentPathOrNew);
lockHolder = getOrLoadDocument(rootDocumentKey);
isNewRootDocument = false;
}
try (final IAutoCloseable writeLock = lockHolder.lockForWriting()) {
final Document rootDocument;
if (isNewRootDocument) {
rootDocument = lockHolder;
} else {
rootDocument = getOrLoadDocument(rootDocumentKey).copy(CopyMode.CheckOutWritable, changesCollector).refreshFromRepositoryIfStaled();
DocumentPermissionsHelper.assertCanEdit(rootDocument);
}
//
// Execute the actual processor
final R result = rootDocumentProcessor.apply(rootDocument);
// Commit or remove it from cache if deleted
if (rootDocument.isDeleted()) {
rootDocuments.invalidate(rootDocumentKey);
changesCollector.collectDeleted(rootDocument.getDocumentPath());
} else {
commitRootDocument(rootDocument);
}
// Return the result
return result;
}
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class DocumentCollection method forRootDocumentReadonly.
public <R> R forRootDocumentReadonly(@NonNull final DocumentPath documentPath, final Function<Document, R> rootDocumentProcessor) {
final DocumentKey rootDocumentKey = DocumentKey.ofRootDocumentPath(documentPath.getRootDocumentPath());
try (final IAutoCloseable readLock = getOrLoadDocument(rootDocumentKey).lockForReading()) {
final Document rootDocument = getOrLoadDocument(rootDocumentKey).copy(CopyMode.CheckInReadonly, NullDocumentChangesCollector.instance);
DocumentPermissionsHelper.assertCanView(rootDocument, UserSession.getCurrentPermissions());
return rootDocumentProcessor.apply(rootDocument);
}
}
use of org.adempiere.util.lang.IAutoCloseable in project metasfresh-webui-api by metasfresh.
the class ADProcessInstancesRepository method retrieveProcessInstance.
private ADProcessInstanceController retrieveProcessInstance(final DocumentId adPInstanceId) {
Check.assumeNotNull(adPInstanceId, "Parameter adPInstanceId is not null");
Check.assume(adPInstanceId.toInt() > 0, "adPInstanceId > 0");
//
// Load process info
final ProcessInfo processInfo = ProcessInfo.builder().setCtx(Env.getCtx()).setCreateTemporaryCtx().setAD_PInstance_ID(adPInstanceId.toInt()).build();
final Object processClassInstance = processInfo.newProcessClassInstanceOrNull();
try (final IAutoCloseable c = JavaProcess.temporaryChangeCurrentInstance(processClassInstance)) {
//
// Build the parameters document
final ProcessId processId = ProcessId.ofAD_Process_ID(processInfo.getAD_Process_ID());
final ProcessDescriptor processDescriptor = getProcessDescriptor(processId);
//
// Build the parameters (as document)
final DocumentEntityDescriptor parametersDescriptor = processDescriptor.getParametersDescriptor();
final Document parametersDoc = parametersDescriptor.getDataBinding().getDocumentsRepository().retrieveDocumentById(parametersDescriptor, adPInstanceId, NullDocumentChangesCollector.instance);
// TODO: handle the case when the process was already executed
// In that case we need to load the result and provide it to ProcessInstance constructor
//
// View informations
final IRangeAwareParams processInfoParams = processInfo.getParameterAsIParams();
final String viewIdStr = processInfoParams.getParameterAsString(ViewBasedProcessTemplate.PARAM_ViewId);
final ViewId viewId = Strings.isNullOrEmpty(viewIdStr) ? null : ViewId.ofViewIdString(viewIdStr);
//
return ADProcessInstanceController.builder().caption(processDescriptor.getCaption()).instanceId(adPInstanceId).parameters(parametersDoc).processClassInstance(processClassInstance).viewId(viewId).build();
}
}
Aggregations