========
Workflow
========

In the Jam.py framework, two tasks work at the same time: the Application builder 
and the Project. 
Each of them represents a tree of objects - there is the Application builder task 
tree and the Project task tree. Therefore, before considering the Jam.py workflow, 
you need to familiarize yourself with the concept of the 
:doc:`task tree <task_tree>`.

The the Jam.py workflow is the following:

* When server.py is run it creates WSGI application that, in its own turn, 
  creates the Application builder task tree.
* The Project task tree is created on the server by Application builder after the 
  server receives first request from the Project client. To do so, the 
  Application builder uses metadata stored in admin.sqlite database in the root folder
  of the project. After creating a task tree the server application triggers the
  :doc:`on_created </refs/server/task/on_created>`
  event, that can be used to initialize the server task tree.
* When an application on the client (Application builder or Project) is first run in 
  the browser (after ``builder.html`` or ``index.html`` have been loaded) the 
  empty task object is built that sends to the server a request to initialize 
  itself.
* If the project 
  :doc:`safe mode </admin/project/parameters>`
  parameter is set, the framework checks if a user is logged, before executing 
  any request. If not, then the 
  application on the client creates a login form, and after the user inputs its 
  login and password, the client task sends the server a request to login.
* After successfull login or if the project
  :doc:`safe mode </admin/project/parameters>`
  parameter is not set, the server sends the client information about the 
  requested task. The task on the client builds its tree, based on this 
  information, assigns event handlers to its objects and executes 
  :doc:`on_page_loaded </refs/client/task/on_page_loaded>`
  event handler. 
* In this event handler a developer should attache JQuery event handler 
  functions to HTML elements of the DOM, defined in the ``index.html`` file. 
  In these functions a developer can use methods of items of the 
  :doc:`task tree <task_tree>`
  to perform some specific tasks. 
  These methods, when executed, trigger different events in which other methods 
  could be called and so on. See
  :doc:`Client side programming <interface/index>`.
* Items of the task tree, that have corresponding database tables, have methods 
  to read and write data in the server database. See 
  :doc:`Data programming <data/index>`.
* The report items generate the reports on the server, based on the LibrOffice
  teplates. See
  :doc:`Programming reports <reports/index>`.
* All the items, whose methods generate a request to the server, do it the 
  following way: they call the method of the task that sends to the server the 
  :doc:`ID </refs/client/abstr_item/at_id>`
  of the task, the
  :doc:`ID </refs/client/abstr_item/at_id>`
  the item, the type of the request and its parameters.
  The server on receiving the request, based on passed IDs, finds the task 
  (it can be Project task or Application builder task) and the item on the server, 
  executes the corresponing method with passed parameters and returns the result 
  of the execution to the client. These server methods could trigger their own 
  events that can override the default behavior. See
  :doc:`Server side programming <server/index>`

Video
=====

`Form events`_ and `Client-server interactions`_ video tutorials illustrate 
the workflow of Jam.py project.
 
.. _`Form events`: https://youtu.be/DY463lcv0R4

.. _`Client-server interactions`: https://youtu.be/nLOhdA2FX0I

  
