<workflow-app name="Graph Stats" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>stats_db_name</name>
            <description>the target stats database name</description>
        </property>
        <property>
            <name>openaire_db_name</name>
            <description>the original graph database name</description>
        </property>
        <property>
            <name>external_stats_db_name</name>
            <value>stats_ext</value>
            <description>the external stats that should be added since they are not included in the graph database</description>
        </property>
        <property>
            <name>stats_db_shadow_name</name>
            <description>the name of the shadow schema</description>
        </property>
        <property>
            <name>monitor_db_name</name>
            <description>the target monitor db name</description>
        </property>
        <property>
            <name>monitor_db_shadow_name</name>
            <description>the name of the shadow monitor db</description>
        </property>
        <property>
            <name>stats_tool_api_url</name>
            <description>The url of the API of the stats tool. Is used to trigger the cache update.</description>
        </property>
        <property>
            <name>hive_metastore_uris</name>
            <description>hive server metastore URIs</description>
        </property>
        <property>
            <name>hive_jdbc_url</name>
            <description>hive server jdbc url</description>
        </property>
        <property>
            <name>hive_timeout</name>
            <description>the time period, in seconds, after which Hive fails a transaction if a Hive client has not sent a hearbeat. The default value is 300 seconds.</description>
        </property>
        <property>
            <name>context_api_url</name>
            <description>the base url of the context api (https://services.openaire.eu/openaire)</description>
        </property>
    </parameters>

    <global>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>hive.metastore.uris</name>
                <value>${hive_metastore_uris}</value>
            </property>
            <property>
            	<name>hive.txn.timeout</name>
            	<value>${hive_timeout}</value>
            </property>
        </configuration>
    </global>

    <start to="Step1"/>

    <kill name="Kill">
        <message>Action failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message>
    </kill>

    <action name="Step1">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step1.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step2"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step2">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step2.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step3"/>
        <error to="Kill"/>
    </action>
        
    <action name="Step3">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step3.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step4"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step4">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step4.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step5"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step5">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step5.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step6"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step6">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step6.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step7"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step7">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step7.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step8"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step8">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step8.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step9"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step9">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step9.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step10"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step10">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step10.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
			<param>external_stats_db_name=${external_stats_db_name}</param>
        </hive2>
        <ok to="Step11"/>
        <error to="Kill"/>
    </action>    

    <action name="Step11">
    	<hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step11.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
			<param>external_stats_db_name=${external_stats_db_name}</param>
        </hive2>
        <ok to="Step12"/>
        <error to="Kill"/>
    </action>  
    
    <action name="Step12">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step12.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step13"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step13">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step13.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step14"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step14">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step14.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step15"/>
        <error to="Kill"/>
    </action>
    
    <action name="Step15">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step15.sql</script>
			<param>stats_db_name=${stats_db_name}</param>
			<param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step16"/>
        <error to="Kill"/>
    </action>

    <action name="Step16">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step16.sql</script>
            <param>stats_db_name=${stats_db_name}</param>
            <param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step16_5"/>
        <error to="Kill"/>
    </action>

    <action name="Step16_5">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step16_5.sql</script>
            <param>stats_db_name=${stats_db_name}</param>
            <param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step16_6"/>
        <error to="Kill"/>
    </action>

    <action name="Step16_6">
        <hive2 xmlns="uri:oozie:hive2-action:0.1">
            <jdbc-url>${hive_jdbc_url}</jdbc-url>
            <script>scripts/step16_6.sql</script>
            <param>stats_db_name=${stats_db_name}</param>
            <param>openaire_db_name=${openaire_db_name}</param>
        </hive2>
        <ok to="Step17"/>
        <error to="Kill"/>
    </action>

    <action name="Step17">
        <shell xmlns="uri:oozie:shell-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <exec>contexts.sh</exec>
            <argument>${context_api_url}</argument>
            <argument>${stats_db_name}</argument>
            <file>contexts.sh</file>
        </shell>
        <ok to="Step19"/>
        <error to="Kill"/>
    </action>
        
    <action name="Step19">
        <shell xmlns="uri:oozie:shell-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <exec>finalizedb.sh</exec>
            <argument>${stats_db_name}</argument>
            <argument>${stats_db_shadow_name}</argument>
            <file>finalizedb.sh</file>
        </shell>
        <ok to="step20-createMonitorDB"/>
        <error to="Kill"/>
    </action>

    <action name="step20-createMonitorDB">
        <shell xmlns="uri:oozie:shell-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <exec>monitor.sh</exec>
            <argument>${stats_db_name}</argument>
            <argument>${monitor_db_name}</argument>
            <argument>${monitor_db_shadow_name}</argument>
            <argument>${wf:appPath()}/scripts/step20-createMonitorDB.sql</argument>
            <file>monitor.sh</file>
        </shell>
        <ok to="Step21"/>
        <error to="Kill"/>
    </action>

    <action name="Step21">
        <shell xmlns="uri:oozie:shell-action:0.1">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <exec>updateCache.sh</exec>
            <argument>${stats_tool_api_url}</argument>
            <file>updateCache.sh</file>
        </shell>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

    <end name="End"/>
</workflow-app>
