Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Polish

Table of Contents
outlinetrue

Zmienne kontekstowe umożliwiają tworzenie komponentów zbudowanych w sposób funkcyjny. Twórca komponentu może zdefiniować wiele dowolnych zmiennych kontekstowych, które będą mogły być następnie wykorzystane przez użytkownika w funkcjach zdefiniowanych jako parametry tego komponentu.

Info

Zmienne kontekstowe mogą być wykorzystane tylko jako argumenty funkcji stanowiącej parametr komponentu.

 

Przykładem takiego komponentu może być walidator, który pozwala na walidację danych w tabelce dynamicznej. Udostępniana jest wtedy zmienna kontekstowa $currentRow przechowująca indeks aktualnego wiersza w tabelce. Jedynym parametrem będzie funkcja, która zwróci wartość logiczną mówiącą czy dany wiersz jest poprawny czy nie.

Image Modified

Tak skonfigurowana akcja będzie oznaczała, że funkcja not(empty(item($currentRow, $Nazwa Dostawcy))) zostanie wywołana dla każdego wiersza ze zmieniającym się parametrem $currentRow:

  • not(empty(item(0$Nazwa Dostawcy)))
  • not(empty(item(1$Nazwa Dostawcy)))
  • not(empty(item(2$Nazwa Dostawcy)))

Pozwala to na tworzenie bardziej uniwersalnych komponentów. Nie musimy też tworzyć wielu przeciążeń funkcji np. dla parametrów tablicowych

-

wystarczy wykorzystać komponent, który umożliwi iterację po wierszach takich tablic.

Definiowanie zmiennych kontekstowych

Definiowanie zmiennych kontekstowych (

Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariable
) odbywa się na etapie tworzenia definicji komponentu. Poniżej przykład dodania jednej zmiennej kontekstowej dla walidatora:

Code Block
languagejava
@Define
public void definition( ValidatorDefinitionBuilder builder )
{
    builder
        .id( "contextVariableExample" )
        .name( "Context Variable Example" )
        .category( Categories.TEST )
        .contextVariable()
            .id( "currentRow" )
            .name( "currentRow" )
            .description( "Current table row index" )
            .type( Types.INTEGER )
            .create()
        .parameter()
            .id( "fn" )
            .name( "Function" )
            .type( Types.FUNCTION )
            .create();
}
Info

Zmienne kontekstowe będą dostępne z poziomu edytora procesów do

...

wyboru tylko jako parametry funkcji. Dlatego zawsze przy wykorzystaniu tej funkcjonalności należy też zdefiniować parametr typu FUNCTION.

Ustalenie wartości zmiennych kontekstowych

Wartość zmiennych kontekstowych ustawia się przy użyciu obiektu 

Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariables

 który

⁣, który może być wstrzyknięty jako parametr metody komponentu. Wystarczy dodać parametr typu 

Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariables
 do metody komponentu (np. validate dla walidatora) i wykorzystać metodę set(id, value):

Code Block
languagejava
public void validate( @Param FunctionCall fn, ContextVariables contextVariables, ValidationErrors errors )
{
    int length = ...; // liczba wierszy

...

 

...

 

...

  

...

for ( int i = 0; i < length; i++ )
    {
        contextVariables.set( "currentRow", i );
        if ( !fn.<Boolean> call() )
        {
            errors.add( "Validation errror" );
        }
    }
}

 

Zmienne kontekstowe w akcjach formularza

Ze względu na inne środowisko uruchamiania akcji formularza, sposób ustawiania tych zmiennych jest trochę inny. Wartość kontekstowych zmiennych ustawiamy przy użyciu metody setContextVariable klasy 

Jsdoc
propertyjsdoc.plusworkflow
classNamePW.form.action.Action
:

Code Block
languagejs
 PW.FormActions.create('context-var-example', {
	defaultActions: {
		variableSet: function(vs, added, updated, removed) {
			this.invoke(vs);
		}
	},
	invoke: function(vs) {
		var fn = this.get('fn');
		var target = this.get('target');
 
		PW.each(vs.getRecords(), function(record, index) {
			this.setContextVariable("currentRow", index);


			var newValue = fn.call();
			target.setItemValue(newValue, index);
		}, this);
	}
});

 

 

English

Table of Contents
outlinetrue

Context variables allow the creation of components built in a functional way. The creator of a component can define many arbitrary context variables, which can then be used by the user in functions defined as parameters of that component.

Info

Context variables can only be used as arguments of a function that is a component parameter.

 

An example of such a component could be a validator, which allows you to validate data in a dynamic table. A context variable $currentRow is then provided, storing the index of the current row in the table. The only parameter would be a function that would return a logical value saying whether the row is valid or not

Image Added

The action configured this way will mean that the not(empty(item($currentRow, $Nazwa Dostawcy))) function will be called for each row with a changing $currentRow parameter:

  • not(empty(item(0$Nazwa Dostawcy)))
  • not(empty(item(1$Nazwa Dostawcy)))
  • not(empty(item(2$Nazwa Dostawcy)))

This allows us to create more versatile components. Also, we don't need to create multiple function overloads, for example, for array parameters - it's enough to use a component that allows iteration over rows of such arrays.

Defining context variables

Defining context variables (

Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariable
) is done at the stage of creating the component definition. Below is an example of adding one context variable for a validator:

Code Block
languagejava
@Define
public void definition( ValidatorDefinitionBuilder builder )
{
    builder
        .id( "contextVariableExample" )
        .name( "Context Variable Example" )
        .category( Categories.TEST )
        .contextVariable()
            .id( "currentRow" )
            .name( "currentRow" )
            .description( "Current table row index" )
            .type( Types.INTEGER )
            .create()
        .parameter()
            .id( "fn" )
            .name( "Function" )
            .type( Types.FUNCTION )
            .create();
}
Info

Context variables will be available from the process editor for selection only as function parameters. Therefore, always when using this functionality, you should also define a parameter of type FUNCTION.

Determine the value of context variables

The value of context variables is set using an object 

Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariables
 that can be injected as a parameter of a component method. Just add a type
Javadoc
propertyjavadoc.plusworkflow
classNamecom.suncode.pwfl.workflow.component.ContextVariables

parameter to the component method (e.g. validate for validator) 

and use the set(id, value) method:

Code Block
languagejava
public void validate( @Param FunctionCall fn, ContextVariables contextVariables, ValidationErrors errors )
{
    int length = ...; // liczba wierszy
    for ( int i = 0; i < length; i++ )
    {
        contextVariables.set( "currentRow", i );
        if ( !fn.<Boolean> call() )
        {
            errors.add( "Validation errror" );
        }
    }
}

Context variables in form actions

Due to the different environment of running form actions, the way of setting these variables is a little different. We set the value of context variables using the setContextVariable

Jsdoc
propertyjsdoc.plusworkflow
classNamePW.form.action.Action
class:

Code Block
languagejs
 PW.FormActions.create('context-var-example', {
	defaultActions: {
		variableSet: function(vs, added, updated, removed) {
			this.invoke(vs);
		}
	},
	invoke: function(vs) {
		var fn = this.get('fn');
		var target = this.get('target');
 
		PW.each(vs.getRecords(), function(record, index) {
			this.setContextVariable("currentRow", index);


			var newValue = fn.call();
			target.setItemValue(newValue, index);
		}, this);
	}
});