<commit_before><!--
  Copyright (C) 2012 Orbeon, Inc.

  This program is free software; you can redistribute it and/or modify it under the terms of the
  GNU Lesser General Public License as published by the Free Software Foundation; either version
  2.1 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Lesser General Public License for more details.

  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
  -->
<xf:model id="fr-persistence-model"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xf="http://www.w3.org/2002/xforms"
        xmlns:exf="http://www.exforms.org/exf/1-0"
        xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
        xmlns:xi="http://www.w3.org/2001/XInclude"
        xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
        xmlns:saxon="http://saxon.sf.net/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
        xmlns:frf="java:org.orbeon.oxf.fr.FormRunner"
        xmlns:secure="java:org.orbeon.oxf.util.SecureUtils"
        xmlns:process="java:org.orbeon.oxf.fr.SimpleProcess">

    <xf:var name="parameters" value="xxf:instance('fr-parameters-instance')" as="element()"/>
    <xf:var name="app" value="$parameters/app" as="xs:string"/>
    <xf:var name="form" value="$parameters/form" as="xs:string"/>
    <xf:var name="document" value="$parameters/document"/>
    <xf:var name="mode" value="$parameters/mode"/>

    <xf:var name="persistence-instance" value="instance('fr-persistence-instance')"/>
    <xf:var name="autosave" value="$persistence-instance/autosave"/>
    <xf:var name="autosave-enabled" value="xxf:property('oxf.fr.support-autosave') and exists(xxf:get-remote-user())"/>

    <!-- 1. Model initialization when we have a specific app and form -->
    <xf:action event="xforms-ready" if="not($mode = ('summary', 'home')) and $app != '*' and $form != '*'">

        <!-- Initialize parameters to pass to "new" service -->
        <xf:var name="pass-request-parameters" value="xxf:property(string-join(('oxf.fr.detail.new.service.passing-request-parameters', $app, $form), '.'))"/>
        <xf:action iterate="xxf:split($pass-request-parameters)">
            <xf:var name="parameter-name" value="."/>
            <xf:var name="parameter-value" value="xxf:get-request-parameter($parameter-name)"/>
            <xf:insert context="instance('fr-service-parameters')" origin="xxf:element($parameter-name, $parameter-value)"/>
        </xf:action>

        <!-- Handle loading of initial data for new/test/edit/view/pdf/email modes only -->
        <xf:action if="$mode = ('new', 'test', 'edit', 'view', 'pdf', 'email')">

            <xf:var name="is-new-id" value="normalize-space($document) = ''"/>
            <xf:var name="is-xml-submit" value="exists(xxf:get-request-attribute('fr-form-data'))"/>
            <xf:var name="is-submit" value="$is-xml-submit or exists(xxf:get-request-parameter('fr-form-data'))"/>

            <!-- Create random document id if none is provided. This is the default for 'new', but a missing id is also
                 allowed for 'edit', 'view', etc., when data is posted to the page. -->
            <xf:setvalue if="$is-new-id" ref="$document" value="secure:randomHexId()"/>

            <!-- Case of 'new' mode where we load the initial data via a service -->
            <xf:var name="new-from-service" value="$mode = ('new', 'test') and xxf:property(string-join(('oxf.fr.detail.new.service.enable', $app, $form), '.'))"/>
            <xf:action if="$new-from-service">
                <xf:message level="xxf:log-debug">Loading new data from service</xf:message>
                <xf:send submission="fr-get-instance-from-service"/>
            </xf:action>

            <!-- Keep initial data for clear button. We can argue if we would like instead to keep data submitted to<commit_msg>Autosave only for the edit mode - don't prompt user whether to load draft in, say, view mode<commit_after><!--
  Copyright (C) 2012 Orbeon, Inc.

  This program is free software; you can redistribute it and/or modify it under the terms of the
  GNU Lesser General Public License as published by the Free Software Foundation; either version
  2.1 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Lesser General Public License for more details.

  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
  -->
<xf:model id="fr-persistence-model"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xf="http://www.w3.org/2002/xforms"
        xmlns:exf="http://www.exforms.org/exf/1-0"
        xmlns:xxf="http://orbeon.org/oxf/xml/xforms"
        xmlns:xi="http://www.w3.org/2001/XInclude"
        xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
        xmlns:saxon="http://saxon.sf.net/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
        xmlns:frf="java:org.orbeon.oxf.fr.FormRunner"
        xmlns:secure="java:org.orbeon.oxf.util.SecureUtils"
        xmlns:process="java:org.orbeon.oxf.fr.SimpleProcess">

    <xf:var name="parameters" value="xxf:instance('fr-parameters-instance')" as="element()"/>
    <xf:var name="app" value="$parameters/app" as="xs:string"/>
    <xf:var name="form" value="$parameters/form" as="xs:string"/>
    <xf:var name="document" value="$parameters/document"/>
    <xf:var name="mode" value="$parameters/mode"/>

    <xf:var name="persistence-instance" value="instance('fr-persistence-instance')"/>
    <xf:var name="autosave" value="$persistence-instance/autosave"/>
    <xf:var name="autosave-enabled" value="xxf:property('oxf.fr.support-autosave') and exists(xxf:get-remote-user()) and $mode = 'edit'"/>

    <!-- 1. Model initialization when we have a specific app and form -->
    <xf:action event="xforms-ready" if="not($mode = ('summary', 'home')) and $app != '*' and $form != '*'">

        <!-- Initialize parameters to pass to "new" service -->
        <xf:var name="pass-request-parameters" value="xxf:property(string-join(('oxf.fr.detail.new.service.passing-request-parameters', $app, $form), '.'))"/>
        <xf:action iterate="xxf:split($pass-request-parameters)">
            <xf:var name="parameter-name" value="."/>
            <xf:var name="parameter-value" value="xxf:get-request-parameter($parameter-name)"/>
            <xf:insert context="instance('fr-service-parameters')" origin="xxf:element($parameter-name, $parameter-value)"/>
        </xf:action>

        <!-- Handle loading of initial data for new/test/edit/view/pdf/email modes only -->
        <xf:action if="$mode = ('new', 'test', 'edit', 'view', 'pdf', 'email')">

            <xf:var name="is-new-id" value="normalize-space($document) = ''"/>
            <xf:var name="is-xml-submit" value="exists(xxf:get-request-attribute('fr-form-data'))"/>
            <xf:var name="is-submit" value="$is-xml-submit or exists(xxf:get-request-parameter('fr-form-data'))"/>

            <!-- Create random document id if none is provided. This is the default for 'new', but a missing id is also
                 allowed for 'edit', 'view', etc., when data is posted to the page. -->
            <xf:setvalue if="$is-new-id" ref="$document" value="secure:randomHexId()"/>

            <!-- Case of 'new' mode where we load the initial data via a service -->
            <xf:var name="new-from-service" value="$mode = ('new', 'test') and xxf:property(string-join(('oxf.fr.detail.new.service.enable', $app, $form), '.'))"/>
            <xf:action if="$new-from-service">
                <xf:message level="xxf:log-debug">Loading new data from service</xf:message>
                <xf:send submission="fr-get-instance-from-service"/>
            </xf:action>

            <!-- Keep initial data for clear button. We can argue if we would like instead to keep data submitted to
{'fraction_non_alphanumeric': 0.1700844390832328, 'fraction_numerical': 0.006996381182147165, 'mean_word_length': 3.2803304078471864, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, 'xml': 34}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555036', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2537, 'n_words': 620}