<workflow-app name="import Claims as Graph" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>migrationClaimsPathStep1</name>
            <description>the base path to store hdfs file</description>
        </property>
        <property>
            <name>migrationClaimsPathStep2</name>
            <description>the temporary path to store entities before dispatching</description>
        </property>
        <property>
            <name>migrationClaimsPathStep3</name>
            <description>the graph Raw base path</description>
        </property>
        <property>
            <name>postgresURL</name>
            <description>the postgres URL to access to the database</description>
        </property>
        <property>
            <name>postgresUser</name>
            <description>the user postgres</description>
        </property>
        <property>
            <name>postgresPassword</name>
            <description>the password postgres</description>
        </property>
        <property>
            <name>mongoURL</name>
            <description>mongoDB url, example: mongodb://[username:password@]host[:port]</description>
        </property>
        <property>
            <name>mongoDb</name>
            <description>mongo database</description>
        </property>
        <property>
            <name>sparkDriverMemory</name>
            <description>memory for driver process</description>
        </property>
        <property>
            <name>sparkExecutorMemory</name>
            <description>memory for individual executor</description>
        </property>
        <property>
            <name>sparkExecutorCores</name>
            <description>number of cores used by single executor</description>
        </property>
    </parameters>

	<start to="ResetWorkingPath"/>

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

    <action name="ResetWorkingPath">
        <fs>
            <delete path='${migrationClaimsPathStep1}'/>
            <mkdir path='${migrationClaimsPathStep1}'/>
        </fs>
        <ok to="ImportDBClaims"/>
        <error to="Kill"/>
    </action>
   
    <action name="ImportDBClaims">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateDbEntitiesApplication</main-class>
            <arg>-p</arg><arg>${migrationClaimsPathStep1}/db_claims</arg>
            <arg>-pgurl</arg><arg>${postgresURL}</arg>
            <arg>-pguser</arg><arg>${postgresUser}</arg>
            <arg>-pgpasswd</arg><arg>${postgresPassword}</arg>
            <arg>-a</arg><arg>claims</arg>
        </java>
		<ok to="ImportODFClaims"/>
        <error to="Kill"/>
    </action>
    
    <action name="ImportODFClaims">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateMongoMdstoresApplication</main-class>
            <arg>-p</arg><arg>${migrationClaimsPathStep1}/odf_claims</arg>
            <arg>-mongourl</arg><arg>${mongoURL}</arg>
            <arg>-mongodb</arg><arg>${mongoDb}</arg>
            <arg>-f</arg><arg>ODF</arg>
            <arg>-l</arg><arg>store</arg>
            <arg>-i</arg><arg>claim</arg>
        </java>
        <ok to="ImportOAFClaims"/>
        <error to="Kill"/>
    </action>

    <action name="ImportOAFClaims">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateMongoMdstoresApplication</main-class>
            <arg>-p</arg><arg>${migrationClaimsPathStep1}/oaf_claims</arg>
            <arg>-mongourl</arg><arg>${mongoURL}</arg>
            <arg>-mongodb</arg><arg>${mongoDb}</arg>
            <arg>-f</arg><arg>OAF</arg>
            <arg>-l</arg><arg>store</arg>
            <arg>-i</arg><arg>claim</arg>
        </java>
        <ok to="ResetClaimEntities"/>
        <error to="Kill"/>
    </action>

	<action name="ResetClaimEntities">
        <fs>
            <delete path='${migrationClaimsPathStep2}'/>
            <mkdir path='${migrationClaimsPathStep2}'/>
        </fs>
        <ok to="GenerateClaimEntities"/>
        <error to="Kill"/>
    </action>

    <action name="GenerateClaimEntities">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>GenerateClaimEntities</name>
            <class>eu.dnetlib.dhp.migration.step2.GenerateEntitiesApplication</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>--executor-memory ${sparkExecutorMemory} --executor-cores ${sparkExecutorCores} --driver-memory=${sparkDriverMemory} --conf spark.extraListeners="com.cloudera.spark.lineage.NavigatorAppListener" --conf spark.sql.queryExecutionListeners="com.cloudera.spark.lineage.NavigatorQueryListener" --conf spark.sql.warehouse.dir="/user/hive/warehouse"</spark-opts>
            <arg>-mt</arg> <arg>yarn-cluster</arg>
            <arg>-s</arg><arg>${migrationClaimsPathStep1}/db_claims,${migrationClaimsPathStep1}/oaf_claims,${migrationClaimsPathStep1}/odf_claims</arg>
            <arg>-t</arg><arg>${migrationClaimsPathStep2}/claim_entities</arg>
            <arg>-pgurl</arg><arg>${postgresURL}</arg>
            <arg>-pguser</arg><arg>${postgresUser}</arg>
            <arg>-pgpasswd</arg><arg>${postgresPassword}</arg>
        </spark>
        <ok to="ResetClaimGraph"/>
        <error to="Kill"/>
    </action>

	<action name="ResetClaimGraph">
        <fs>
            <delete path='${migrationClaimsPathStep3}'/>
            <mkdir path='${migrationClaimsPathStep3}'/>
        </fs>
        <ok to="GenerateClaimGraph"/>
        <error to="Kill"/>
    </action>

    <action name="GenerateClaimGraph">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>GenerateClaimGraph</name>
            <class>eu.dnetlib.dhp.migration.step3.DispatchEntitiesApplication</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>--executor-memory ${sparkExecutorMemory} --executor-cores ${sparkExecutorCores} --driver-memory=${sparkDriverMemory} --conf spark.extraListeners="com.cloudera.spark.lineage.NavigatorAppListener" --conf spark.sql.queryExecutionListeners="com.cloudera.spark.lineage.NavigatorQueryListener" --conf spark.sql.warehouse.dir="/user/hive/warehouse"</spark-opts>
            <arg>-mt</arg> <arg>yarn-cluster</arg>
            <arg>-s</arg><arg>${migrationClaimsPathStep2}/claim_entities</arg>
            <arg>-g</arg><arg>${migrationClaimsPathStep3}</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>  

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