<workflow-app name="Create Raw Graph Step 1: import Entities from aggregator to HDFS" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>reuseContent</name>
            <value>false</value>
            <description>should import content from the aggregator or reuse a previous version</description>
        </property>
        <property>
            <name>workingPath</name>
            <description>the working dir base path</description>
        </property>
        <property>
            <name>targetXMLPath</name>
            <description>the graph Raw base path</description>
        </property>
        <property>
            <name>targetEntityPath</name>
            <description>the graph Raw base path</description>
        </property>
        <property>
            <name>format</name>
            <description>the postgres URL to access to the database</description>
        </property>
        <property>
            <name>layout</name>
            <description>the user postgres</description>
        </property>
        <property>
            <name>interpretation</name>
            <description>the password postgres</description>
        </property>
        <property>
            <name>dbhost</name>
            <description>mongoDB url, example: mongodb://[username:password@]host[:port]</description>
        </property>
        <property>
            <name>dbName</name>
            <description>mongo database</description>
        </property>
        <property>
            <name>entity</name>
            <description>the entity type</description>
        </property>
    </parameters>

    <start to="ReuseContent"/>

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


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

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


    <action name="ImportEntitiesFromMongo">
        <java>
            <job-tracker>${jobTracker}</job-tracker>
            <name-node>${nameNode}</name-node>
            <main-class>eu.dnetlib.dhp.sx.graph.ImportDataFromMongo</main-class>
            <arg>-t</arg><arg>${targetXMLPath}</arg>
            <arg>-n</arg><arg>${nameNode}</arg>
            <arg>-h</arg><arg>${dbhost}</arg>
            <arg>-p</arg><arg>27017</arg>
            <arg>-dn</arg><arg>${dbName}</arg>
            <arg>-f</arg><arg>${format}</arg>
            <arg>-l</arg><arg>${layout}</arg>
            <arg>-i</arg><arg>${interpretation}</arg>
        </java>
        <ok to="ResetTargetPath"/>
        <error to="Kill"/>
    </action>

    <action name="ResetTargetPath">
        <fs>
            <delete path='${targetEntityPath}'/>
        </fs>
        <ok to="ConvertXML2Entity"/>
        <error to="Kill"/>
    </action>

    <action name="ConvertXML2Entity">
        <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>Import ${entity} and related entities</name>
            <class>eu.dnetlib.dhp.sx.graph.SparkScholexplorerGraphImporter</class>
            <jar>dhp-graph-mapper-${projectVersion}.jar</jar>
            <spark-opts>--executor-memory ${sparkExecutorMemory} --driver-memory=${sparkDriverMemory} ${sparkExtraOPT}</spark-opts>
            <arg>-mt</arg> <arg>yarn-cluster</arg>
            <arg>--sourcePath</arg><arg>${targetXMLPath}</arg>
            <arg>--targetPath</arg><arg>${targetEntityPath}</arg>
            <arg>--entity</arg><arg>${entity}</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>
    <end name="End"/>
</workflow-app>