<workflow-app name="create RAW Organizations" xmlns="uri:oozie:workflow:0.5">

    <parameters>
        <property>
            <name>graphOutputPath</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>contentPath</name>
            <description>path location to store (or reuse) content from the aggregator</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>postgresOpenOrgsURL</name>
            <description>the postgres URL to access to the OpenOrgs database</description>
        </property>
        <property>
            <name>postgresOpenOrgsUser</name>
            <description>the user of OpenOrgs database</description>
        </property>
        <property>
            <name>postgresOpenOrgsPassword</name>
            <description>the password of OpenOrgs database</description>
        </property>
		<property>
            <name>dbSchema</name>
            <value>beta</value>
            <description>the database schema according to the D-Net infrastructure (beta or production)</description>
        </property>
        <property>
            <name>openOrgsAction</name>
            <value>openorgs_dedup</value>
            <description>The action to be executed during the import for OpenOrgs</description>
        </property>
        <property>
            <name>isLookupUrl</name>
            <description>the address of the lookUp service</description>
        </property>
        <property>
            <name>nsPrefixBlacklist</name>
            <value>foo</value>
            <description>a blacklist of nsprefixes (comma separeted)</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>
        <property>
            <name>oozieActionShareLibForSpark2</name>
            <description>oozie action sharelib for spark 2.*</description>
        </property>
        <property>
            <name>spark2ExtraListeners</name>
            <value>com.cloudera.spark.lineage.NavigatorAppListener</value>
            <description>spark 2.* extra listeners classname</description>
        </property>
        <property>
            <name>spark2SqlQueryExecutionListeners</name>
            <value>com.cloudera.spark.lineage.NavigatorQueryListener</value>
            <description>spark 2.* sql query execution listeners classname</description>
        </property>
        <property>
            <name>spark2YarnHistoryServerAddress</name>
            <description>spark 2.* yarn history server address</description>
        </property>
        <property>
            <name>spark2EventLogDir</name>
            <description>spark 2.* event log dir location</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>
            <property>
                <name>oozie.action.sharelib.for.spark</name>
                <value>${oozieActionShareLibForSpark2}</value>
            </property>
        </configuration>
    </global>

    <start to="reuse_aggregator_content"/>

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

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

    <fork name="ImportDB">
        <path start="ImportDB_openaire_organizations"/>
        <path start="ImportDB_openorgs"/>
    </fork>

    <action name="ImportDB_openaire_organizations">
        <java>
            <prepare>
                <delete path="${contentPath}/db_openaire_organizations"/>
            </prepare>
            <main-class>eu.dnetlib.dhp.oa.graph.raw.MigrateDbEntitiesApplication</main-class>
            <arg>--hdfsPath</arg><arg>${contentPath}/db_openaire_organizations</arg>
            <arg>--postgresUrl</arg><arg>${postgresURL}</arg>
            <arg>--postgresUser</arg><arg>${postgresUser}</arg>
            <arg>--postgresPassword</arg><arg>${postgresPassword}</arg>
            <arg>--isLookupUrl</arg><arg>${isLookupUrl}</arg>
            <arg>--action</arg><arg>openaire_organizations</arg>
            <arg>--dbschema</arg><arg>${dbSchema}</arg>
            <arg>--nsPrefixBlacklist</arg><arg>${nsPrefixBlacklist}</arg>
        </java>
        <ok to="wait_import"/>
        <error to="Kill"/>
    </action>

    <action name="ImportDB_openorgs">
        <java>
            <prepare>
                <delete path="${contentPath}/db_openorgs"/>
            </prepare>
            <main-class>eu.dnetlib.dhp.oa.graph.raw.MigrateDbEntitiesApplication</main-class>
            <arg>--hdfsPath</arg><arg>${contentPath}/db_openorgs</arg>
            <arg>--postgresUrl</arg><arg>${postgresOpenOrgsURL}</arg>
            <arg>--postgresUser</arg><arg>${postgresOpenOrgsUser}</arg>
            <arg>--postgresPassword</arg><arg>${postgresOpenOrgsPassword}</arg>
            <arg>--isLookupUrl</arg><arg>${isLookupUrl}</arg>
            <arg>--action</arg><arg>${openOrgsAction}</arg>
            <arg>--dbschema</arg><arg>${dbSchema}</arg>
            <arg>--nsPrefixBlacklist</arg><arg>${nsPrefixBlacklist}</arg>
        </java>
        <ok to="wait_import"/>
        <error to="Kill"/>
    </action>

    <join name="wait_import" to="GenerateEntities"/>

    <action name="GenerateEntities">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn</master>
            <mode>cluster</mode>
            <name>GenerateEntities</name>
            <class>eu.dnetlib.dhp.oa.graph.raw.GenerateEntitiesApplication</class>
            <jar>dhp-graph-mapper-${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>--sourcePaths</arg><arg>${contentPath}/db_openaire_organizations,${contentPath}/db_openorgs</arg>
            <arg>--targetPath</arg><arg>${workingDir}/entities</arg>
            <arg>--isLookupUrl</arg><arg>${isLookupUrl}</arg>
        </spark>
        <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.oa.graph.raw.DispatchEntitiesApplication</class>
            <jar>dhp-graph-mapper-${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}
                --conf spark.sql.shuffle.partitions=7680
            </spark-opts>
            <arg>--sourcePath</arg><arg>${workingDir}/entities</arg>
            <arg>--graphRawPath</arg><arg>${graphOutputPath}</arg>
        </spark>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

    <end name="End"/>

</workflow-app>