Search in sources :

Example 6 with IAutoCloseable

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;
    }
}
Also used : ITrxManager(org.adempiere.ad.trx.api.ITrxManager) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable)

Example 7 with IAutoCloseable

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;
    }
}
Also used : IAutoCloseable(org.adempiere.util.lang.IAutoCloseable)

Example 8 with IAutoCloseable

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;
    }
}
Also used : IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) SourceDocument(de.metas.letters.model.MADBoilerPlate.SourceDocument)

Example 9 with IAutoCloseable

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);
    }
}
Also used : IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) SourceDocument(de.metas.letters.model.MADBoilerPlate.SourceDocument)

Example 10 with IAutoCloseable

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();
    }
}
Also used : IRangeAwareParams(org.adempiere.util.api.IRangeAwareParams) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) ViewId(de.metas.ui.web.view.ViewId) ProcessDescriptor(de.metas.ui.web.process.descriptor.ProcessDescriptor) WebuiRelatedProcessDescriptor(de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor) ProcessInfo(de.metas.process.ProcessInfo) ProcessId(de.metas.ui.web.process.ProcessId) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) Document(de.metas.ui.web.window.model.Document)

Aggregations

IAutoCloseable (org.adempiere.util.lang.IAutoCloseable)10 SourceDocument (de.metas.letters.model.MADBoilerPlate.SourceDocument)3 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)3 ProcessInfo (de.metas.process.ProcessInfo)2 ProcessId (de.metas.ui.web.process.ProcessId)2 ProcessDescriptor (de.metas.ui.web.process.descriptor.ProcessDescriptor)2 WebuiRelatedProcessDescriptor (de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor)2 ViewId (de.metas.ui.web.view.ViewId)2 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)2 Document (de.metas.ui.web.window.model.Document)2 Strings (com.google.common.base.Strings)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Check (de.metas.printing.esb.base.util.Check)1 IADPInstanceDAO (de.metas.process.IADPInstanceDAO)1 IProcessDefaultParametersProvider (de.metas.process.IProcessDefaultParametersProvider)1 JavaProcess (de.metas.process.JavaProcess)1 ProcessDefaultParametersUpdater (de.metas.process.ProcessDefaultParametersUpdater)1 ProcessInfoBuilder (de.metas.process.ProcessInfo.ProcessInfoBuilder)1