<workflow-app name="Transformation_Workflow" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>mdstoreInputPath</name>
            <description>the path of the input MDStore</description>
        </property>

        <property>
            <name>mdstoreOutputPath</name>
            <description>the path of the cleaned mdstore</description>
        </property>
        <property>
            <name>nativeInputPath</name>
            <description>the path of the input MDStore</description>
        </property>


    </parameters>

    <start to="resume_from"/>


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

    <decision name="resume_from">
        <switch>
            <case to="TransformJob">${wf:conf('resumeFrom') eq 'TransformJob'}</case>
            <case to="ExportDataset">${wf:conf('resumeFrom') eq 'ExportDataset'}</case>
            <default to="ImportDatacite"/>
        </switch>
    </decision>

    <action name="ImportDatacite">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>ImportDatacite</name>
            <class>eu.dnetlib.dhp.actionmanager.datacite.ImportDatacite</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>-t</arg><arg>${nativeInputPath}</arg>
            <arg>-d</arg><arg>${mdstoreInputPath}</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>--master</arg><arg>yarn-cluster</arg>
        </spark>
        <ok to="TransformJob"/>
        <error to="Kill"/>
    </action>


    <action name="TransformJob">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>TransformJob</name>
            <class>eu.dnetlib.dhp.actionmanager.datacite.GenerateDataciteDatasetSpark</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.sql.shuffle.partitions=3840
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--sourcePath</arg><arg>${mdstoreInputPath}</arg>
            <arg>--targetPath</arg><arg>${mdstoreOutputPath}</arg>
            <arg>--isLookupUrl</arg><arg>${isLookupUrl}</arg>
            <arg>-tr</arg><arg>${isLookupUrl}</arg>
            <arg>--master</arg><arg>yarn-cluster</arg>
        </spark>
        <ok to="DeletePathIfExists"/>
        <error to="Kill"/>
    </action>

    <action name="DeletePathIfExists">
        <fs>
            <delete path='${mdstoreOutputPath}_raw_AS'/>
        </fs>
        <ok to="ExportDataset"/>
        <error to="Kill"/>
    </action>


    <action name="ExportDataset">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn-cluster</master>
            <mode>cluster</mode>
            <name>ExportDataset</name>
            <class>eu.dnetlib.dhp.actionmanager.datacite.ExportActionSetJobNode</class>
            <jar>dhp-aggregation-${projectVersion}.jar</jar>
            <spark-opts>
                --executor-memory=${sparkExecutorMemory}
                --executor-cores=${sparkExecutorCores}
                --driver-memory=${sparkDriverMemory}
                --conf spark.sql.shuffle.partitions=3840
                --conf spark.extraListeners=${spark2ExtraListeners}
                --conf spark.sql.queryExecutionListeners=${spark2SqlQueryExecutionListeners}
                --conf spark.yarn.historyServer.address=${spark2YarnHistoryServerAddress}
                --conf spark.eventLog.dir=${nameNode}${spark2EventLogDir}
            </spark-opts>
            <arg>--sourcePath</arg><arg>${mdstoreOutputPath}</arg>
            <arg>--targetPath</arg><arg>${mdstoreOutputPath}_raw_AS</arg>
            <arg>--master</arg><arg>yarn-cluster</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

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