<workflow-app name="CollectionWorkflow" xmlns="uri:oozie:workflow:0.5">
    <parameters>
        <property>
            <name>apiDescription</name>
            <description>A json encoding of the API Description class</description>
        </property>
        <property>
            <name>dataSourceInfo</name>
            <description>A json encoding of the Datasource Info</description>
        </property>
        <property>
            <name>identifierPath</name>
            <description>An xpath to retrieve the metadata identifier for the generation of DNet Identifier </description>
        </property>
        <property>
            <name>metadataEncoding</name>
            <description> The type of the metadata XML/JSON</description>
        </property>
        <property>
            <name>timestamp</name>
            <description>The timestamp of the collection date</description>
        </property>
        <property>
            <name>workflowId</name>
            <description>The identifier of the workflow</description>
        </property>
        <property>
            <name>mdStoreID</name>
            <description>The identifier of the mdStore</description>
        </property>
        <property>
            <name>mdStoreManagerURI</name>
            <description>The URI of the MDStore Manager</description>
        </property>

        <property>
            <name>dnetMessageManagerURL</name>
            <description>The URI of the Dnet Message Manager</description>
        </property>
        <property>
            <name>collectionMode</name>
            <description>Should be REFRESH or INCREMENTAL</description>
        </property>

        <property>
            <name>collection_java_xmx</name>
            <value>-Xmx200m</value>
            <description>Used to configure the heap size for the map JVM process. Should be 80% of mapreduce.map.memory.mb.</description>
        </property>


    </parameters>

    <global>
        <job-tracker>${jobTracker}</job-tracker>
        <name-node>${nameNode}</name-node>
    </global>

    <start to="collection_mode"/>

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

    <decision name="collection_mode">
        <switch>
            <case to="StartTransaction">${wf:conf('collectionMode') eq 'REFRESH'}</case>
            <case to="BeginRead">${wf:conf('collectionMode') eq 'INCREMENTAL'}</case>
            <default to="StartTransaction"/>
        </switch>
    </decision>

    <action name="BeginRead">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>READ_LOCK</arg>
            <arg>--mdStoreID</arg><arg>${mdStoreID}</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
            <capture-output/>
        </java>
        <ok to="StartTransaction"/>
        <error to="Kill"/>
    </action>

    <action name="StartTransaction">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>NEW_VERSION</arg>
            <arg>--mdStoreID</arg><arg>${mdStoreID}</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
            <capture-output/>
        </java>
        <ok to="CollectionWorker"/>
        <error to="Kill"/>
    </action>

    <action name="CollectionWorker">
        <java>
            <main-class>eu.dnetlib.dhp.collection.CollectorWorkerApplication</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--apidescriptor</arg><arg>${apiDescription}</arg>
            <arg>--namenode</arg><arg>${nameNode}</arg>
            <arg>--workflowId</arg><arg>${workflowId}</arg>
            <arg>--dnetMessageManagerURL</arg><arg>${dnetMessageManagerURL}</arg>
            <arg>--mdStoreVersion</arg><arg>${wf:actionData('StartTransaction')['mdStoreVersion']}</arg>
            <arg>--maxNumberOfRetry</arg><arg>${maxNumberOfRetry}</arg>
            <arg>--requestDelay</arg><arg>${requestDelay}</arg>
            <arg>--retryDelay</arg><arg>${retryDelay}</arg>
            <arg>--connectTimeOut</arg><arg>${connectTimeOut}</arg>
            <arg>--readTimeOut</arg><arg>${readTimeOut}</arg>
        </java>
        <ok to="GenerateNativeStoreSparkJob"/>
        <error to="FailCollection"/>
    </action>

    <action name="GenerateNativeStoreSparkJob">
        <spark xmlns="uri:oozie:spark-action:0.2">
            <master>yarn</master>
            <mode>cluster</mode>
            <name>Generate Native MetadataStore</name>
            <class>eu.dnetlib.dhp.collection.GenerateNativeStoreSparkJob</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>--encoding</arg><arg>${metadataEncoding}</arg>
            <arg>--dateOfCollection</arg><arg>${timestamp}</arg>
            <arg>--provenance</arg><arg>${dataSourceInfo}</arg>
            <arg>--xpath</arg><arg>${identifierPath}</arg>
            <arg>--mdStoreVersion</arg><arg>${wf:actionData('StartTransaction')['mdStoreVersion']}</arg>
            <arg>--readMdStoreVersion</arg><arg>${wf:actionData('BeginRead')['mdStoreReadLockVersion']}</arg>
        </spark>
        <ok to="collection_mode_end"/>
        <error to="FailCollection"/>
    </action>

    <decision name="collection_mode_end">
        <switch>
            <case to="CommitVersion">${wf:conf('collectionMode') eq 'REFRESH'}</case>
            <case to="EndRead">${wf:conf('collectionMode') eq 'INCREMENTAL'}</case>
            <default to="CommitVersion"/>
        </switch>
    </decision>

    <action name="EndRead">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>READ_UNLOCK</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
            <arg>--readMDStoreId</arg><arg>${wf:actionData('BeginRead')['mdStoreReadLockVersion']}</arg>
        </java>
        <ok to="CommitVersion"/>
        <error to="Kill"/>
    </action>

    <action name="CommitVersion">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>COMMIT</arg>
            <arg>--namenode</arg><arg>${nameNode}</arg>
            <arg>--mdStoreVersion</arg><arg>${wf:actionData('StartTransaction')['mdStoreVersion']}</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
        </java>
        <ok to="End"/>
        <error to="Kill"/>
    </action>

    <decision name="FailCollection">
        <switch>
            <case to="RollBack">${wf:conf('collectionMode') eq 'REFRESH'}</case>
            <case to="EndReadRollBack">${wf:conf('collectionMode') eq 'INCREMENTAL'}</case>
            <default to="RollBack"/>
        </switch>
    </decision>

    <action name="EndReadRollBack">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>READ_UNLOCK</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
            <arg>--readMDStoreId</arg><arg>${wf:actionData('BeginRead')['mdStoreReadLockVersion']}</arg>
        </java>
        <ok to="RollBack"/>
        <error to="Kill"/>
    </action>

    <action name="RollBack">
        <java>
            <main-class>eu.dnetlib.dhp.aggregation.mdstore.MDStoreActionNode</main-class>
            <java-opts>${collection_java_xmx}</java-opts>
            <arg>--action</arg><arg>ROLLBACK</arg>
            <arg>--mdStoreVersion</arg><arg>${wf:actionData('StartTransaction')['mdStoreVersion']}</arg>
            <arg>--mdStoreManagerURI</arg><arg>${mdStoreManagerURI}</arg>
        </java>
        <ok to="Kill"/>
        <error to="Kill"/>
    </action>

    <end name="End"/>

</workflow-app>