Skip to content

Commit

Permalink
process params: use referenced document to evaluate the default values
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed May 17, 2017
1 parent d549592 commit 2e87aea
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 34 deletions.
39 changes: 27 additions & 12 deletions src/main/java/de/metas/ui/web/process/ProcessRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import de.metas.ui.web.process.json.JSONProcessInstanceResult;
import de.metas.ui.web.process.json.JSONProcessLayout;
import de.metas.ui.web.session.UserSession;
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.window.controller.Execution;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.json.JSONDocument;
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import de.metas.ui.web.window.datatypes.json.JSONLookupValuesList;
Expand Down Expand Up @@ -124,7 +127,7 @@ private Collection<IProcessInstancesRepository> getAllRepositories()
@RequestMapping(value = "/{processId}/layout", method = RequestMethod.GET)
public JSONProcessLayout getLayout(
@PathVariable("processId") final String adProcessIdStr //
)
)
{
userSession.assertLoggedIn();

Expand All @@ -139,16 +142,28 @@ public JSONProcessInstance createInstanceFromRequest(@PathVariable("processId")
{
userSession.assertLoggedIn();

final ViewId viewId = jsonRequest.getViewId();
final DocumentIdsSelection viewDocumentIds = jsonRequest.getViewDocumentIds();

// Get the effective singleDocumentPath, i.e.
// * if provided, use it
// * if not provided and we have a single selected row in the view, use that row
DocumentPath singleDocumentPath = jsonRequest.getSingleDocumentPath();
if (singleDocumentPath == null && viewDocumentIds.isSingleDocumentId())
{
singleDocumentPath = DocumentPath.rootDocumentPath(viewId.getWindowId(), viewDocumentIds.getSingleDocumentId());
}

final CreateProcessInstanceRequest request = CreateProcessInstanceRequest.builder()
.processId(ProcessId.fromJson(processIdStr))
.singleDocumentPath(jsonRequest.getSingleDocumentPath())
.viewId(jsonRequest.getViewId())
.viewDocumentIds(jsonRequest.getViewDocumentIds())
.singleDocumentPath(singleDocumentPath)
.viewId(viewId)
.viewDocumentIds(viewDocumentIds)
.build();
// Validate request's AD_Process_ID
// Validate request's AD_Process_ID
// (we are not using it, but just for consistency)
request.assertProcessIdEquals(jsonRequest.getProcessId());

final IProcessInstancesRepository instancesRepository = getRepository(request.getProcessId());

return Execution.callInNewExecution("pinstance.create", () -> {
Expand All @@ -161,7 +176,7 @@ public JSONProcessInstance createInstanceFromRequest(@PathVariable("processId")
public JSONProcessInstance getInstance(
@PathVariable("processId") final String processIdStr //
, @PathVariable("pinstanceId") final String pinstanceIdStr //
)
)
{
userSession.assertLoggedIn();

Expand All @@ -178,7 +193,7 @@ public List<JSONDocument> processParametersChangeEvents(
@PathVariable("processId") final String processIdStr //
, @PathVariable("pinstanceId") final String pinstanceIdStr //
, @RequestBody final List<JSONDocumentChangedEvent> events //
)
)
{
userSession.assertLoggedIn();

Expand All @@ -200,7 +215,7 @@ public List<JSONDocument> processParametersChangeEvents(
public JSONProcessInstanceResult startProcess(
@PathVariable("processId") final String processIdStr //
, @PathVariable("pinstanceId") final String pinstanceIdStr //
)
)
{
userSession.assertLoggedIn();

Expand All @@ -224,7 +239,7 @@ public ResponseEntity<byte[]> getReport(
@PathVariable("processId") final String processIdStr //
, @PathVariable("pinstanceId") final String pinstanceIdStr //
, @PathVariable("filename") final String filename //
)
)
{
userSession.assertLoggedIn();

Expand Down Expand Up @@ -255,7 +270,7 @@ public JSONLookupValuesList getParameterTypeahead(
, @PathVariable("pinstanceId") final String pinstanceIdStr //
, @PathVariable("parameterName") final String parameterName //
, @RequestParam(name = "query", required = true) final String query //
)
)
{
userSession.assertLoggedIn();

Expand All @@ -273,7 +288,7 @@ public JSONLookupValuesList getParameterDropdown(
@PathVariable("processId") final String processIdStr //
, @PathVariable("pinstanceId") final String pinstanceIdStr //
, @PathVariable("parameterName") final String parameterName //
)
)
{
userSession.assertLoggedIn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.function.Function;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.adempiere.ad.security.IUserRolePermissions;
import org.adempiere.util.Services;
import org.adempiere.util.api.IRangeAwareParams;
Expand Down Expand Up @@ -45,6 +47,8 @@
import de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor;
import de.metas.ui.web.window.model.Document;
import de.metas.ui.web.window.model.Document.CopyMode;
import de.metas.ui.web.window.model.DocumentCollection;
import de.metas.ui.web.window.model.IDocumentEvaluatee;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -85,6 +89,8 @@ public class ADProcessInstancesRepository implements IProcessInstancesRepository
private DocumentDescriptorFactory documentDescriptorFactory;
@Autowired
private IViewsRepository viewsRepo;
@Autowired
private DocumentCollection documentsCollection;
//
private final ADProcessDescriptorsFactory processDescriptorFactory = new ADProcessDescriptorsFactory();

Expand Down Expand Up @@ -129,6 +135,27 @@ public Stream<WebuiRelatedProcessDescriptor> streamDocumentRelatedProcesses(fina

@Override
public IProcessInstanceController createNewProcessInstance(final CreateProcessInstanceRequest request)
{
if(request.getSingleDocumentPath() != null)
{
// In case we have a single document path, we shall fetch it as use it as evaluation context.
// This will make sure that the parameter's default values will be correctly computed
return documentsCollection.forDocumentReadonly(request.getSingleDocumentPath(), document -> createNewProcessInstance0(request, document.asEvaluatee()));
}
else
{
final IDocumentEvaluatee shadowParentDocumentEvaluatee = null; // N/A
return createNewProcessInstance0(request, shadowParentDocumentEvaluatee);
}
}

/**
*
* @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
Expand All @@ -143,7 +170,7 @@ public IProcessInstanceController createNewProcessInstance(final CreateProcessIn
// Build the parameters document
final ProcessDescriptor processDescriptor = getProcessDescriptor(request.getProcessId());
final DocumentEntityDescriptor parametersDescriptor = processDescriptor.getParametersDescriptor();
final Document parametersDoc = ADProcessParametersRepository.instance.createNewParametersDocument(parametersDescriptor, adPInstanceId);
final Document parametersDoc = ADProcessParametersRepository.instance.createNewParametersDocument(parametersDescriptor, adPInstanceId, evalCtx);
final int windowNo = parametersDoc.getWindowNo();

// Set parameters's default values
Expand All @@ -166,7 +193,7 @@ public IProcessInstanceController createNewProcessInstance(final CreateProcessIn
return pinstance;
}
}

private ProcessInfo createProcessInfo(@NonNull final CreateProcessInstanceRequest request)
{
final String tableName;
Expand All @@ -187,8 +214,8 @@ private ProcessInfo createProcessInfo(@NonNull final CreateProcessInstanceReques

if (viewDocumentIds.isSingleDocumentId())
{
final DocumentId singleDocumentId = viewDocumentIds.getSingleDocumentId();
recordId = singleDocumentId.toIntOr(-1);
final DocumentId viewSingleDocumentId = viewDocumentIds.getSingleDocumentId();
recordId = viewSingleDocumentId.toIntOr(-1);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.metas.ui.web.window.model.Document.DocumentValuesSupplier;
import de.metas.ui.web.window.model.DocumentQuery;
import de.metas.ui.web.window.model.DocumentsRepository;
import de.metas.ui.web.window.model.IDocumentEvaluatee;
import de.metas.ui.web.window.model.IDocumentFieldView;
import de.metas.ui.web.window.model.lookup.LookupValueByIdSupplier;

Expand Down Expand Up @@ -126,11 +127,10 @@ public Document createNewDocument(final DocumentEntityDescriptor parametersDescr
throw new UnsupportedOperationException();
}

Document createNewParametersDocument(final DocumentEntityDescriptor parametersDescriptor, final DocumentId adPInstanceId)
Document createNewParametersDocument(final DocumentEntityDescriptor parametersDescriptor, final DocumentId adPInstanceId, final IDocumentEvaluatee evalCtx)
{
//
// Build the parameters (as document)
return Document.builder(parametersDescriptor)
.setShadowParentDocumentEvaluatee(evalCtx)
.initializeAsNewDocument(adPInstanceId, VERSION_DEFAULT);
}

Expand Down
42 changes: 27 additions & 15 deletions src/main/java/de/metas/ui/web/window/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.function.IntSupplier;
import java.util.function.Supplier;

import javax.annotation.Nullable;

import org.adempiere.ad.callout.api.ICalloutExecutor;
import org.adempiere.ad.callout.api.ICalloutRecord;
import org.adempiere.ad.expression.api.IExpression;
Expand Down Expand Up @@ -531,7 +533,6 @@ private static final class InitialFieldValueSupplier implements DocumentValuesSu
{
// Parameters
private final DocumentValuesSupplier parentSupplier;
private final Properties ctx;
private final DocumentType documentType;
private final DocumentId documentTypeId;
private final IDocumentEvaluatee _evaluatee;
Expand All @@ -542,8 +543,6 @@ private InitialFieldValueSupplier(final Document document, final DocumentValuesS
super();
this.parentSupplier = parentSupplier;

ctx = document.getCtx();

final DocumentEntityDescriptor entityDescriptor = document.getEntityDescriptor();
documentType = entityDescriptor.getDocumentType();
documentTypeId = entityDescriptor.getDocumentTypeId();
Expand Down Expand Up @@ -643,6 +642,7 @@ public Object getValue(final DocumentFieldDescriptor fieldDescriptor)
// Window User Preferences (only if it's not a virtual field)
if (documentType == DocumentType.Window && !fieldDescriptor.isVirtualField())
{
final Properties ctx = Env.getCtx();
final int adWindowId = documentTypeId.toInt();
final String fieldName = fieldDescriptor.getFieldName();

Expand Down Expand Up @@ -934,7 +934,7 @@ public DocumentId getDocumentId()
public Object getDocumentIdAsJson()
{
final DocumentId documentId = getDocumentId();
if(documentId.isInt())
if (documentId.isInt())
{
return documentId.toInt();
}
Expand Down Expand Up @@ -1243,10 +1243,10 @@ private void updateFieldFlag(
, final String triggeringFieldName //
, final DependencyType triggeringDependencyType //
, final IDocumentChangesCollector documentChangesCollector //
)
)
{
final ReasonSupplier reason = () -> "TriggeringField=" + triggeringFieldName + ", DependencyType=" + triggeringDependencyType;

if (DependencyType.ReadonlyLogic == triggeringDependencyType)
{
final LogicExpressionResult valueOld = documentField.getReadonly();
Expand Down Expand Up @@ -1277,20 +1277,20 @@ else if (DependencyType.LookupValues == triggeringDependencyType)
documentChangesCollector.collectLookupValuesStaled(documentField, reason);
}
}
else if(DependencyType.FieldValue == triggeringDependencyType)
else if (DependencyType.FieldValue == triggeringDependencyType)
{
final IDocumentFieldValueProvider valueProvider = documentField.getDescriptor().getVirtualFieldValueProvider().orElse(null);
if(valueProvider != null)
if (valueProvider != null)
{
try
{
final Object valueOld = documentField.getValue();
final Object valueNew = valueProvider.calculateValue(this);

documentField.setInitialValue(valueNew);
documentField.setValue(valueNew);
documentField.updateValid(); // make sure is still valid

documentChangesCollector.collectValueIfChanged(documentField, valueOld, reason);
}
catch (Exception ex)
Expand Down Expand Up @@ -1523,11 +1523,11 @@ public DocumentValidStatus checkAndGetValidStatus()
for (final IDocumentField documentField : getFields())
{
// skip virtual fields, those does not matter
if(documentField.isVirtualField())
if (documentField.isVirtualField())
{
continue;
}

final DocumentValidStatus validState = documentField.getValidStatus();
if (!validState.isValid())
{
Expand Down Expand Up @@ -1770,19 +1770,19 @@ public IAutoCloseable lockForWriting()
logger.debug("Released write lock for {}: {}", this, writeLock);
};
}

public int getAD_Client_ID()
{
final IDocumentField field = getFieldOrNull(WindowConstants.FIELDNAME_AD_Client_ID);
return field != null ? field.getValueAsInt(-1) : -1;
}

public int getAD_Org_ID()
{
final IDocumentField field = getFieldOrNull(WindowConstants.FIELDNAME_AD_Org_ID);
return field != null ? field.getValueAsInt(-1) : -1;
}

private final class DocumentStaleState
{
private boolean staled;
Expand Down Expand Up @@ -1860,6 +1860,7 @@ public static final class Builder
private DocumentPath _documentPath;

private Integer _windowNo;
private IDocumentEvaluatee shadowParentDocumentEvaluatee;
private static final AtomicInteger nextWindowNo = new AtomicInteger(1);

private Builder(final DocumentEntityDescriptor entityDescriptor)
Expand All @@ -1873,6 +1874,11 @@ public Document build()
{
final Document document = new Document(this);

if (shadowParentDocumentEvaluatee != null)
{
document.setShadowParentDocumentEvaluatee(shadowParentDocumentEvaluatee);
}

final ITabCallout documentCallout = entityDescriptor.createAndInitializeDocumentCallout(document.asCalloutRecord());
document.documentCallout = ExceptionHandledTabCallout.wrapIfNeeded(documentCallout);

Expand Down Expand Up @@ -1925,6 +1931,12 @@ public Builder setParentDocument(final Document parentDocument)
return this;
}

public Builder setShadowParentDocumentEvaluatee(@Nullable final IDocumentEvaluatee shadowParentDocumentEvaluatee)
{
this.shadowParentDocumentEvaluatee = shadowParentDocumentEvaluatee;
return this;
}

public Document initializeAsNewDocument(final DocumentValuesSupplier documentValuesSupplier)
{
Preconditions.checkNotNull(documentValuesSupplier, "documentValuesSupplier");
Expand Down

0 comments on commit 2e87aea

Please sign in to comment.