Search in sources :

Example 16 with Document

use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.

the class WindowRestController method duplicate.

@PostMapping("/{windowId}/{documentId}/duplicate")
public JSONDocument duplicate(@PathVariable("windowId") final String windowIdStr, @PathVariable("documentId") final String documentIdStr, @RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced) {
    userSession.assertLoggedIn();
    final DocumentPath fromDocumentPath = DocumentPath.rootDocumentPath(WindowId.fromJson(windowIdStr), DocumentId.of(documentIdStr));
    final Document documentCopy = documentCollection.duplicateDocument(fromDocumentPath);
    final JSONDocumentOptions jsonOpts = newJSONDocumentOptions().showAdvancedFields(advanced).build();
    return JSONDocument.ofDocument(documentCopy, jsonOpts);
}
Also used : DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) JSONDocumentPath(de.metas.ui.web.window.datatypes.json.JSONDocumentPath) JSONDocumentOptions(de.metas.ui.web.window.datatypes.json.JSONDocumentOptions) JSONDocument(de.metas.ui.web.window.datatypes.json.JSONDocument) Document(de.metas.ui.web.window.model.Document) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 17 with Document

use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.

the class JSONDocument method ofEventOrNull.

private static JSONDocument ofEventOrNull(final DocumentChanges documentChangedEvents, final JSONDocumentOptions options) {
    if (documentChangedEvents.isEmpty()) {
        return null;
    }
    final DocumentPath documentPath = documentChangedEvents.getDocumentPath();
    final JSONDocument jsonDocument = new JSONDocument(documentPath);
    // If the document was deleted, we just need to export that flag. All the other changes are not relevant.
    if (documentChangedEvents.isDeleted()) {
        jsonDocument.setDeleted();
        return jsonDocument;
    }
    // 
    // Fields
    {
        final List<JSONDocumentField> jsonFields = new ArrayList<>();
        documentChangedEvents.getFieldChangesList().stream().filter(options.documentFieldChangeFilter()).forEach((field) -> {
            // Add the pseudo-field "ID" first
            if (field.isKey()) {
                jsonFields.add(0, JSONDocumentField.idField(field.getValueAsJsonObject(options.getJsonOpts())));
            }
            // Append the other fields
            final JSONDocumentField jsonField = JSONDocumentField.ofDocumentFieldChangedEvent(field, options.getJsonOpts());
            // apply permissions
            options.getDocumentPermissions().apply(documentPath, jsonField);
            jsonFields.add(jsonField);
        });
        jsonDocument.setFields(jsonFields);
    }
    // 
    // Valid status
    final DocumentValidStatus documentValidStatus = documentChangedEvents.getDocumentValidStatus();
    if (documentValidStatus != null) {
        jsonDocument.setValidStatus(documentValidStatus);
    }
    // 
    // Save status
    final DocumentSaveStatus documentSaveStatus = documentChangedEvents.getDocumentSaveStatus();
    if (documentSaveStatus != null) {
        jsonDocument.setSaveStatus(documentSaveStatus);
    }
    // 
    // Included tabs info
    documentChangedEvents.getIncludedDetailInfos().stream().map(JSONDocument::createIncludedTabInfo).peek(jsonIncludedTabInfo -> options.getDocumentPermissions().apply(documentPath, jsonIncludedTabInfo)).forEach(jsonDocument::addIncludedTabInfo);
    return jsonDocument;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IIncludedDocumentsCollection(de.metas.ui.web.window.model.IIncludedDocumentsCollection) DocumentChanges(de.metas.ui.web.window.model.DocumentChanges) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ToString(lombok.ToString) DocumentStandardAction(de.metas.ui.web.window.model.DocumentStandardAction) ImmutableSet(com.google.common.collect.ImmutableSet) WindowConstants(de.metas.ui.web.window.WindowConstants) NonNull(lombok.NonNull) Collection(java.util.Collection) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) Set(java.util.Set) WindowId(de.metas.ui.web.window.datatypes.WindowId) Collectors(java.util.stream.Collectors) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) List(java.util.List) AdempiereException(org.adempiere.exceptions.AdempiereException) IDocumentChangesCollector(de.metas.ui.web.window.model.IDocumentChangesCollector) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) WebSocketConfig(de.metas.ui.web.websocket.WebSocketConfig) Document(de.metas.ui.web.window.model.Document) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 18 with Document

use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.

the class JSONDocument method ofDocument.

public static JSONDocument ofDocument(@NonNull final Document document, @NonNull final JSONDocumentOptions options) {
    final JSONDocument jsonDocument = new JSONDocument(document.getDocumentPath());
    // fieldsBy
    // Fields
    {
        final List<JSONDocumentField> jsonFields = new ArrayList<>();
        // Add pseudo "ID" field first
        jsonFields.add(0, JSONDocumentField.idField(document.getDocumentIdAsJson()));
        // Append the other fields
        document.getFieldViews().stream().filter(options.documentFieldFilter()).map(field -> JSONDocumentField.ofDocumentField(field, options.getJsonOpts())).peek(// apply permissions
        jsonField -> options.getDocumentPermissions().apply(document, jsonField)).forEach(jsonFields::add);
        jsonDocument.setFields(jsonFields);
    }
    // 
    // Valid Status
    final DocumentValidStatus documentValidStatus = document.getValidStatus();
    if (documentValidStatus != null) {
        jsonDocument.setValidStatus(documentValidStatus);
    }
    // 
    // Save Status
    final DocumentSaveStatus documentSaveStatus = document.getSaveStatus();
    if (documentSaveStatus != null) {
        jsonDocument.setSaveStatus(documentSaveStatus);
    }
    // Included tabs info
    if (!options.isDoNotFetchIncludedTabs()) {
        document.getIncludedDocumentsCollections().stream().map(JSONDocument::createIncludedTabInfo).peek(jsonIncludedTabInfo -> options.getDocumentPermissions().apply(document, jsonIncludedTabInfo)).forEach(jsonDocument::addIncludedTabInfo);
    }
    // 
    // Available standard actions
    jsonDocument.setStandardActions(document.getStandardActions());
    // Set debugging info
    if (WindowConstants.isProtocolDebugging()) {
        jsonDocument.putDebugProperty("tablename", document.getEntityDescriptor().getTableNameOrNull());
        jsonDocument.putDebugProperty(JSONOptions.DEBUG_ATTRNAME, options.toString());
        jsonDocument.putDebugProperty("fields-count", jsonDocument.getFieldsCount());
    }
    return jsonDocument;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IIncludedDocumentsCollection(de.metas.ui.web.window.model.IIncludedDocumentsCollection) DocumentChanges(de.metas.ui.web.window.model.DocumentChanges) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ToString(lombok.ToString) DocumentStandardAction(de.metas.ui.web.window.model.DocumentStandardAction) ImmutableSet(com.google.common.collect.ImmutableSet) WindowConstants(de.metas.ui.web.window.WindowConstants) NonNull(lombok.NonNull) Collection(java.util.Collection) LogicExpressionResult(org.adempiere.ad.expression.api.LogicExpressionResult) Set(java.util.Set) WindowId(de.metas.ui.web.window.datatypes.WindowId) Collectors(java.util.stream.Collectors) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) List(java.util.List) AdempiereException(org.adempiere.exceptions.AdempiereException) IDocumentChangesCollector(de.metas.ui.web.window.model.IDocumentChangesCollector) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) WebSocketConfig(de.metas.ui.web.websocket.WebSocketConfig) Document(de.metas.ui.web.window.model.Document) DocumentSaveStatus(de.metas.ui.web.window.model.DocumentSaveStatus) DocumentValidStatus(de.metas.ui.web.window.model.DocumentValidStatus) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 19 with Document

use of de.metas.ui.web.window.model.Document in project metasfresh-webui-api by metasfresh.

the class SqlDocumentQueryBuilder method buildSqlWhereClause.

private IPair<IStringExpression, List<Object>> buildSqlWhereClause() {
    final SqlParamsCollector sqlParams = SqlParamsCollector.newInstance();
    final CompositeStringExpression.Builder sqlWhereClauseBuilder = IStringExpression.composer();
    // 
    // Entity's WHERE clause
    {
        final IStringExpression entityWhereClauseExpression = entityBinding.getSqlWhereClause();
        if (!entityWhereClauseExpression.isNullExpression()) {
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* entity where clause */ (").append(entityWhereClauseExpression).append(")");
        }
    }
    // 
    // Key column
    // FIXME: handle AD_Reference/AD_Ref_List(s). In that case the recordId will be AD_Ref_List.Value,
    // so the SQL where clause which is currently build is AD_Ref_List_ID=<the AD_Ref_List.Value>.
    // The build SQL where clause shall be something like AD_Reference_ID=<the reference, i think we shall fetch it somehow from Lookup> AND Value=<the value, which currently is the recordId>
    final Set<DocumentId> recordIds = getRecordIds();
    if (!recordIds.isEmpty()) {
        final List<SqlDocumentFieldDataBindingDescriptor> keyFields = entityBinding.getKeyFields();
        if (keyFields.isEmpty()) {
            throw new AdempiereException("Failed building where clause because there is no Key Column defined in " + entityBinding);
        }
        // Single primary key
        if (keyFields.size() == 1) {
            final String singleKeyColumnName = keyFields.get(0).getColumnName();
            final ImmutableSet<Integer> recordIdsIntSet = recordIds.stream().map(DocumentId::toInt).collect(ImmutableSet.toImmutableSet());
            sqlWhereClauseBuilder.appendIfNotEmpty("\n /* key */ AND ");
            sqlWhereClauseBuilder.append(DB.buildSqlList(singleKeyColumnName, recordIdsIntSet, sqlParams.toLiveList()));
        } else // Composed primary key
        {
            final boolean parenthesesRequired = !sqlWhereClauseBuilder.isEmpty();
            if (parenthesesRequired) {
                sqlWhereClauseBuilder.append(" AND ( ");
            }
            boolean firstRecord = true;
            final boolean appendParentheses = recordIds.size() > 1;
            for (final DocumentId recordId : recordIds) {
                if (!firstRecord) {
                    sqlWhereClauseBuilder.append("\n OR ");
                }
                if (appendParentheses) {
                    sqlWhereClauseBuilder.append("(");
                }
                final Map<String, Object> keyColumnName2value = extractComposedKey(recordId, keyFields);
                boolean firstKeyPart = true;
                for (final Map.Entry<String, Object> keyPart : keyColumnName2value.entrySet()) {
                    if (!firstKeyPart) {
                        sqlWhereClauseBuilder.append(" AND ");
                    }
                    final String keyColumnName = keyPart.getKey();
                    final Object value = keyPart.getValue();
                    sqlWhereClauseBuilder.append(" ").append(keyColumnName).append("=").append(sqlParams.placeholder(value));
                    firstKeyPart = false;
                }
                if (appendParentheses) {
                    sqlWhereClauseBuilder.append(")");
                }
                firstRecord = false;
            }
            if (parenthesesRequired) {
                sqlWhereClauseBuilder.append(")");
            }
        }
    }
    // 
    // Parent link where clause (if any)
    final Document parentDocument = getParentDocument();
    if (parentDocument != null) {
        final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
        final String linkColumnName = entityBinding.getLinkColumnName();
        if (parentLinkColumnName != null && linkColumnName != null) {
            final IDocumentFieldView parentLinkField = parentDocument.getFieldView(parentLinkColumnName);
            final Object parentLinkValue = parentLinkField.getValue();
            final DocumentFieldWidgetType parentLinkWidgetType = parentLinkField.getWidgetType();
            final Class<?> targetClass = entityBinding.getFieldByFieldName(linkColumnName).getSqlValueClass();
            final Object sqlParentLinkValue = SqlDocumentsRepository.convertValueToPO(parentLinkValue, parentLinkColumnName, parentLinkWidgetType, targetClass);
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* parent link */ ").append(linkColumnName).append("=").append(sqlParams.placeholder(sqlParentLinkValue));
        }
    }
    // 
    // Document filters
    {
        final SqlDocumentFilterConverterContext context = SqlDocumentFilterConverterContext.EMPTY;
        final String sqlFilters = SqlDocumentFilterConverters.createEntityBindingEffectiveConverter(entityBinding).getSql(sqlParams, getDocumentFilters(), SqlOptions.usingTableAlias(entityBinding.getTableAlias()), context);
        if (!Check.isEmpty(sqlFilters, true)) {
            sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
            sqlWhereClauseBuilder.append(" /* filters */ (\n").append(sqlFilters).append(")\n");
        }
    }
    // Build the final SQL where clause
    return ImmutablePair.of(sqlWhereClauseBuilder.build(), Collections.unmodifiableList(sqlParams.toList()));
}
Also used : CompositeStringExpression(org.adempiere.ad.expression.api.impl.CompositeStringExpression) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) SqlDocumentFilterConverterContext(de.metas.ui.web.document.filter.sql.SqlDocumentFilterConverterContext) Document(de.metas.ui.web.window.model.Document) IDocumentFieldView(de.metas.ui.web.window.model.IDocumentFieldView) SqlDocumentFieldDataBindingDescriptor(de.metas.ui.web.window.descriptor.sql.SqlDocumentFieldDataBindingDescriptor) DocumentFieldWidgetType(de.metas.ui.web.window.descriptor.DocumentFieldWidgetType) AdempiereException(org.adempiere.exceptions.AdempiereException) IStringExpression(org.adempiere.ad.expression.api.IStringExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SqlParamsCollector(de.metas.ui.web.document.filter.sql.SqlParamsCollector)

Example 20 with Document

use of de.metas.ui.web.window.model.Document 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.getPinstanceId());
    final Object processClassInstance = processInfo.newProcessClassInstanceOrNull();
    if (processClassInstance == null) {
        throw new AdempiereException("No process instance created for " + processInfo);
    }
    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).startProcessDirectly(processDescriptor.isStartProcessDirectly()).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) PInstanceId(de.metas.process.PInstanceId) 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) 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) IUserRolePermissions(de.metas.security.IUserRolePermissions) 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) AdWindowId(org.adempiere.ad.element.api.AdWindowId) ViewRowIdsSelection(de.metas.ui.web.view.ViewRowIdsSelection) DocumentDescriptorFactory(de.metas.ui.web.window.descriptor.factory.DocumentDescriptorFactory) IADPInstanceDAO(de.metas.process.IADPInstanceDAO) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) WebuiRelatedProcessDescriptor(de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor) Services(de.metas.util.Services) 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) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Cache(com.google.common.cache.Cache) Document(de.metas.ui.web.window.model.Document) ViewId(de.metas.ui.web.view.ViewId) DocumentId(de.metas.ui.web.window.datatypes.DocumentId) ProcessInfo(de.metas.process.ProcessInfo) IADPInstanceDAO(de.metas.process.IADPInstanceDAO) Document(de.metas.ui.web.window.model.Document) IProcessDefaultParametersProvider(de.metas.process.IProcessDefaultParametersProvider) AdempiereException(org.adempiere.exceptions.AdempiereException) IAutoCloseable(org.adempiere.util.lang.IAutoCloseable) ProcessDescriptor(de.metas.ui.web.process.descriptor.ProcessDescriptor) WebuiRelatedProcessDescriptor(de.metas.ui.web.process.descriptor.WebuiRelatedProcessDescriptor) DocumentEntityDescriptor(de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)

Aggregations

Document (de.metas.ui.web.window.model.Document)36 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)16 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)12 IDocumentChangesCollector (de.metas.ui.web.window.model.IDocumentChangesCollector)10 List (java.util.List)9 Set (java.util.Set)9 AdempiereException (org.adempiere.exceptions.AdempiereException)9 ImmutableList (com.google.common.collect.ImmutableList)8 DocumentEntityDescriptor (de.metas.ui.web.window.descriptor.DocumentEntityDescriptor)8 ImmutableSet (com.google.common.collect.ImmutableSet)7 EntityNotFoundException (de.metas.ui.web.exceptions.EntityNotFoundException)6 WindowConstants (de.metas.ui.web.window.WindowConstants)6 Services (de.metas.util.Services)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 JSONDocument (de.metas.ui.web.window.datatypes.json.JSONDocument)5 DocumentSaveStatus (de.metas.ui.web.window.model.DocumentSaveStatus)5 Collectors (java.util.stream.Collectors)5 NonNull (lombok.NonNull)5 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)4