<workflow-app name="import regular entities as Graph (all steps)" xmlns="uri:oozie:workflow:0.5">

    <parameters>
        <property>
            <name>workingPath</name>
            <value>/tmp/dhp_migration</value>
            <description>the base path to store temporary intermediate data</description>
        </property>
        <property>
            <name>graphBasePath</name>
            <description>the target path to store raw graph</description>
        </property>
        <property>
            <name>reuseContent</name>
            <value>false</value>
            <description>should import content from the aggregator or reuse a previous version</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>

    <global>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
        <configuration>
            <property>
                <name>mapreduce.job.queuename</name>
                <value>${queueName}</value>
            </property>
            <property>
                <name>oozie.launcher.mapred.job.queue.name</name>
                <value>${oozieLauncherQueueName}</value>
            </property>
        </configuration>
    </global>

	<start to="ReuseContent"/>

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

    <decision name="ReuseContent">
        <switch>
            <case to="ResetWorkingPath">${wf:conf('reuseContent') eq false}</case>
            <case to="ResetAllEntitiesPath">${wf:conf('reuseContent') eq true}</case>
            <default to="ResetWorkingPath"/>
        </switch>
    </decision>

    <action name="ResetWorkingPath">
        <fs>
            <delete path="${workingPath}"/>
            <mkdir path="${workingPath}"/>
        </fs>
        <ok to="ImportDB"/>
        <error to="Kill"/>
    </action>

    <action name="ImportDB">
        <java>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateDbEntitiesApplication</main-class>
            <arg>-p</arg><arg>${workingPath}/db_records</arg>
            <arg>-pgurl</arg><arg>${postgresURL}</arg>
            <arg>-pguser</arg><arg>${postgresUser}</arg>
            <arg>-pgpasswd</arg><arg>${postgresPassword}</arg>
        </java>
        <ok to="ImportODF"/>
        <error to="Kill"/>
    </action>
    
    <action name="ImportODF">
        <java>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateMongoMdstoresApplication</main-class>
            <arg>-p</arg><arg>${workingPath}/odf_records</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>cleaned</arg>
        </java>
        <ok to="ImportOAF"/>
        <error to="Kill"/>
    </action>

    <action name="ImportOAF">
        <java>
            <main-class>eu.dnetlib.dhp.migration.step1.MigrateMongoMdstoresApplication</main-class>
            <arg>-p</arg><arg>${workingPath}/oaf_records</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>cleaned</arg>
        </java>
        <ok to="ResetAllEntitiesPath"/>
        <error to="Kill"/>
    </action>

    <action name="ResetAllEntitiesPath">
        <fs>
            <delete path="${workingPath}/all_entities"/>
        </fs>
        <ok to="GenerateEntities"/>
        <error to="Kill"/>
    </action>

    <action name="GenerateEntities">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn</master>
            <mode>cluster</mode>
            <name>GenerateEntities</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>${workingPath}/db_records,${workingPath}/oaf_records,${workingPath}/odf_records</arg>
            <arg>-t</arg><arg>${workingPath}/all_entities</arg>
            <arg>-pgurl</arg><arg>${postgresURL}</arg>
            <arg>-pguser</arg><arg>${postgresUser}</arg>
            <arg>-pgpasswd</arg><arg>${postgresPassword}</arg>
        </spark>
        <ok to="ResetGraphPath"/>
        <error to="Kill"/>
    </action>

    <action name="ResetGraphPath">
        <fs>
            <delete path="${graphBasePath}/graph_raw"/>
            <mkdir path="${graphBasePath}/graph_raw"/>
        </fs>
        <ok to="GenerateGraph"/>
        <error to="Kill"/>
    </action>

    <action name="GenerateGraph">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn</master>
            <mode>cluster</mode>
            <name>GenerateGraph</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>${workingPath}/all_entities</arg>
            <arg>-g</arg><arg>${graphBasePath}/graph_raw</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>  

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