====== Systems - DB: Users' contracts synchronization ======
===== What do you need before you start =====
  * You need install **CzechIdM 7.7.0**  (and higher).
  * You need be logged in as **admin**.
  * You need enable **ACC**  module.
  * You need to add users **john.roe **and **john.doe **if the already doesent exist.
  * You must have database **jdbcTest**  with table **contracts**:
CREATE DATABASE "jdbcTest"
  WITH OWNER = idmadmin
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'cs_CZ.UTF-8'
       LC_CTYPE = 'cs_CZ.UTF-8'
       CONNECTION LIMIT = -1;
CREATE TABLE public.contracts
(
  id character varying(255),
  name character varying(255),
  state character varying(255),
  disbled character varying(255),
  description character varying(255),
  valid_from date,
  valid_to date,
  team_leader character varying(255),
  leader character varying(255),
  main character varying(255),
  changed character varying(255),
  organization_name character varying(255),
  organization_code character varying(255),
  owner character varying(255)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.contracts
  OWNER TO idmadmin;
  * And add two records:
INSERT INTO public.contracts(
            id, name, state, disbled, description, valid_from, valid_to,
            team_leader, leader, main, changed, organization_name, organization_code,
            owner)
    VALUES (
'1','Contract one','30','false','visualize dot-com ROI','2016-05-04','2016-06-21','john.doe','john.doe','true','2016-06-25','Skinder','68016-376','john.roe');
INSERT INTO public.contracts(
            id, name, state, disbled, description, valid_from, valid_to,
            team_leader, leader, main, changed, organization_name, organization_code,
            owner)
    VALUES (
'2','Contract two','20','true','revolutionize viral experiences','2017-04-06',null,null,null,'true','2016-06-15','Einti','60429-911','john.roe');
===== Create and edit DB system =====
  * We go into **Systems**  agenda and add a new system. Click on green button **Add**.
{{  .:sync_02.png  }}
  * Fill **System name**  and at bottom of page there is green button **Save and continue**, so click on it.
{{  .:contracts_03.png  }}Go to **Configuration**  tab and fill in information about your database. Ours are as such:
^ Host                 ^ jdbc:postgresql://localhost:5432/jdbcTest  ^
| Port                 | 5432                                       |
| User                 | idmadmin                                   |
| User Password        | ********                                   |
| Database             | jdbcTest                                   |
| Table                | contracts                                  |
| Key Column           | id                                         |
| JDBC Driver          | org.postgresql.Driver                      |
| JDBC Connection URL  | jdbc:postgresql://localhost:5432/jdbcTest  |
{{  .:contracts_04.png  }}
  * Then go to tab **Scheme**  and generate scheme.
{{  .:contracts_05.png  }}
  * Then go to **Mapping**  and click on **Add**  button.
{{  .:contracts_06.png  }}
  * Now fill in information from the picture below and click on **Save and continue**.
{{  .:contracts_07.png  }}
  * At the bottom of page, there are mapped attributes. We are going to add 7 attributes:
^ Attribute in schema ^ Name       ^ Identifier ^ Entity attr. ^ Entity field     ^ IdM key    ^ Transform from system ^
| leader              | leader     | false      | true         | Guarantess       | guarantees | does not have         |
| name                | name       | false      | true         | Name of position | position   | does not have         |
| __NAME__            | __NAME__   | true       | false        |   |            | does not have         |
| owner               | owner      | false      | true         | Owner            | identity   | does not have         |
| state               | state      | false      | true         | State            | state      | has                   |
| valid_from          | valid_from | false      | true         | Valid from       | validFrom  | has                   |
| valid_to            | valid_to   | false      | true         | Valid till       | validTill  | has                   |
  * In **Transformation from system**  field of **state**  attribute there is green button **Insert script**  select **Compiles identity-relation state**  and click **Select**
{{  .:contracts_07_02.png  }}
  * Paste to **Transformation from system**  field of **valid\_from**  and **valid\_to**  attributes:
import org.joda.time.LocalDate;
if(attributeValue){
    return new LocalDate(attributeValue);
}
  * Two examples how to fill attributes:
{{  .:contracts_08.png  }}{{  .:contracts_09.png  }}
  * Now we have 7 mapped attributes:
{{  .:contracts_10.png  }}
  * Go to tab **Synchronization**  and **Add**  new synchronization.
{{  .:contracts_11.png  }}
  * Fill the form as in the picture below and click on **Save and continue**, then click on green **arrow**  and option **Save and run sync**  will show up, so click on it:
{{  .:contracts_12.png  }}
  * Confirm start synchronization:
{{  .:sync_13.png  }}
  * Wait a few seconds, so synchronization can finish its job. And then go to **Users**  section (in the left menu). And find user **john.roe**.
  * In tab positions there is now 3 contracts. The **Default**  contract has been here since creating this user (for testing purposes) and the other two are synchronized from the database.
{{  .:contracts_14.png  }}If you have it in a state like in the upper picture you successfully complete this tutorial.
Thank you for following this tutorial, I hope it was useful.