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);
}
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;
}
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;
}
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()));
}
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;
}
}
Aggregations