Search in sources :

Example 1 with IAutoCloseable

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

Example 2 with IAutoCloseable

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

Example 3 with IAutoCloseable

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

Example 4 with IAutoCloseable

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

Example 5 with IAutoCloseable

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;
    }
}
Also used : Env(org.compiere.util.Env) Autowired(org.springframework.beans.factory.annotation.Autowired) DocumentCollection(de.metas.ui.web.window.model.DocumentCollection) ProcessDefaultParametersUpdater(de.metas.process.ProcessDefaultParametersUpdater) IProcessInstancesRepository(de.metas.ui.web.process.IProcessInstancesRepository) IView(de.metas.ui.web.view.IView) IRangeAwareParams(org.adempiere.util.api.IRangeAwareParams) ProcessDescriptor(de.metas.ui.web.process.descriptor.ProcessDescriptor) TableRecordReference(org.adempiere.util.lang.impl.TableRecordReference) ImmutableSet(com.google.common.collect.ImmutableSet) ProcessInfoBuilder(de.metas.process.ProcessInfo.ProcessInfoBuilder) NonNull(lombok.NonNull) Set(java.util.Set) JavaProcess(de.metas.process.JavaProcess) UserSession(de.metas.ui.web.session.UserSession) Services(org.adempiere.util.Services) I_AD_Process(org.compiere.model.I_AD_Process) Stream(java.util.stream.Stream) ProcessId(de.metas.ui.web.process.ProcessId) IDocumentChangesCollector(de.metas.ui.web.window.model.IDocumentChangesCollector) CacheBuilder(com.google.common.cache.CacheBuilder) IProcessInstanceController(de.metas.ui.web.process.IProcessInstanceController) IViewsRepository(de.metas.ui.web.view.IViewsRepository) ProcessInfo(de.metas.process.ProcessInfo) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) CreateProcessInstanceRequest(de.metas.ui.web.process.CreateProcessInstanceRequest) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) SqlOptions(de.metas.ui.web.window.model.sql.SqlOptions) IDocumentEvaluatee(de.metas.ui.web.window.model.IDocumentEvaluatee) Function(java.util.function.Function) NullDocumentChangesCollector(de.metas.ui.web.window.model.NullDocumentChangesCollector) Strings(com.google.common.base.Strings) SqlDocumentEntityDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor) Nullable(javax.annotation.Nullable) DocumentIdsSelection(de.metas.ui.web.window.datatypes.DocumentIdsSelection) ViewRowIdsSelection(de.metas.ui.web.view.ViewRowIdsSelection) DocumentDescriptorFactory(de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory) IUserRolePermissions(org.adempiere.ad.security.IUserRolePermissions) IADPInstanceDAO(de.metas.process.IADPInstanceDAO) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) WebuiRelatedProcessDescriptor(de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) AdempiereException(org.adempiere.exceptions.AdempiereException) WebuiPreconditionsContext(de.metas.ui.web.process.WebuiPreconditionsContext) Check(de.metas.printing.esb.base.util.Check) IProcessDefaultParametersProvider(de.metas.process.IProcessDefaultParametersProvider) Cache(com.google.common.cache.Cache) Document(de.metas.ui.web.window.model.Document) ViewId(de.metas.ui.web.view.ViewId) IProcessDefaultParametersProvider(de.metas.process.IProcessDefaultParametersProvider) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) ProcessDescriptor(de.metas.ui.web.process.descriptor.ProcessDescriptor) WebuiRelatedProcessDescriptor(de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor) ProcessInfo(de.metas.process.ProcessInfo) IADPInstanceDAO(de.metas.process.IADPInstanceDAO) 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