.. -*- coding: utf-8 -*-

Définition de workflow
======================
On peut mettre une condition rql ou/et un groupe auquel doit appartenir l'utilisateur.

Si on met à la fois un(ou plusieurs) groupe et une condition RQL, il faut que les deux soient respectés.

Si on met plusieurs groupes, il faut que l'utilisateur soit dans un des groupes.

Pour la condition RQL sur une transition, on peut y mettre les substitutions suivantes :

* `%(eid)s`, eid de l'objet
* `%(ueid)s`, eid de l'utilisateur qui fait la requête
* `%(seid)s`, eid de l'état courant de l'objet

Dans le script de création d'un workflow, penser à mettre `_()` autour des noms d'états et de transitions
pour que ceux si soient pris en compte par les scripts de gestion des catalogues i18n.

General
-------

A workflow can be defined in a `LAX` application thanks to the system 
entities ``State`` and ``Transition``. Those are defined within all 
LAX application and can be set-up through the main administrator interface.

Once your schema is defined, you can start creating the set of states and
the required transitions for your applications entities.

You first need to define the states and then the transitions between those
to complete your workflow.

A ``State`` defines the status of an entity. While creating a new state, 
you will be first given the option to select the entity type the state
can be applied to. By choosing ``Apply``, a new section will be displayed
in the editing screen to enable you to add relation to the state you are
creating.

A ``Transition`` is also based on an entity type it can be applied to.
By choosing ``Apply``, a new section will be displayed in the editing 
screen to enable you to add relation to the transition you are
creating.

At the transition level you will also define the group of user which can
aplly this transition to an object.


Example of a simple workflow
----------------------------

Please see the tutorial to view and example of a simple workflow.


[Create a simple workflow for BlogDemo, to have a moderator approve new blog 
entry to be published. This implies, specify a dedicated group of blog
moderator as well as hide the view of a blog entry to the user until
it reaches the state published]

Set-up a workflow
-----------------

Before starting, make sure you refresh your mind by reading [link to
definition_workflow chapter].

We want to create a workflow to control the quality of the BlogEntry 
submitted on your application. When a BlogEntry is created by a user
its state should be `submitted`. To be visible to all, it needs to
be in the state `published`. To move from `submitted` to `published`
we need a transition that we can name `approve_blogentry`.

We do not want every user to be allowed to change the state of a 
BlogEntry. We need to define a group of user, `moderators`, and 
this group will have appropriate permissions to approve BlogEntry
to be published and visible to all.

There are two ways to create a workflow, form the user interface,
and also by defining it in ``migration/postcreate.py``. This script
is executed each time a new ``./bin/laxctl db-init`` is done. 
If you create the states and transitions through the user interface
this means that next time you will need to initialize the database
you will have to re-create all the entities. 
We strongly recommand you create the workflow in ``migration\postcreate.py``
and we will now show you how.
The user interface would only be a reference for you to view the states 
and transitions but is not the appropriate interface to define your
application workflow.

Update the schema
~~~~~~~~~~~~~~~~~
To enable a BlogEntry to have a State, we have to define a relation
``in_state`` in the schema of BlogEntry. Please do as follows, add
the line ``in_state (...)``::

  class BlogEntry(EntityType):
      title = String(maxsize=100, required=True)
      publish_date = Date(default='TODAY')
      text_format = String(meta=True, internationalizable=True, maxsize=50,
                           default='text/rest', constraints=[format_constraint])
      text = String(fulltextindexed=True)
      category = String(vocabulary=('important','business'))
      entry_of = SubjectRelation('Blog', cardinality='?*')
      in_state = SubjectRelation('State', cardinality='1*')

As you updated the schema, you will have re-execute ``./bin/laxctl db-init``
to initialize the database and migrate your existing entities.
[WRITE ABOUT MIGRATION]

Create states, transitions and group permissions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

At the time the ``postcreate.py`` script is executed, several methods
can be used. They are all defined in the ``class ServerMigrationHelper``.
We will only discuss the method we use to create a wrokflow here.

To define our workflow for BlogDemo, please add the following lines
to ``migration/postcreate.py``::
  
  _ = unicode

  moderators      = add_entity('CWGroup', name=u"moderators")

  submitted = add_state(_('submitted'), 'BlogEntry', initial=True)
  published = add_state(_('published'), 'BlogEntry')

  add_transition(_('approve_blogentry'), 'BlogEntry', (submitted,), published, ('moderators', 'managers'),)

  checkpoint()

``add_entity`` is used here to define the new group of users that we
need to define the transitions, `moderators`.
If this group required by the transition is not defined before the
transition is created, it will not create the relation `transition 
require the group moderator`.

``add_state`` expects as the first argument the name of the state you are
willing to create, then the entity type on which the state can be applied, 
and an optionnal argument to set if the state is the initial state
of the entity type or not.

``add_transition`` expects as the first argument the name of the 
transition, then the entity type on which we can apply the transition,
then the list of possible initial states from which the transition
can be applied, the target state of the transition, and the permissions
(e.g. list of the groups of users who can apply the transition).

.. image:: images/lax-book.03-transitions-view.fr.png

You can now notice that in the actions box of a BlogEntry, the state
is now listed as well as the possible transitions from this state
defined by the workflow. This transition, as defined in the workflow,
will only being displayed for the users belonging to the group
moderators of managers.


