// This is called upon the first creation of the XForms engine
  private void initialize() {

    // Scope the containing document for the XForms API
    XFormsAPI.withContainingDocumentJava(
        this,
        new Runnable() {
          public void run() {
            // Create XForms controls and models
            createControlsAndModels();

            // Group all xforms-model-construct-done and xforms-ready events within a single
            // outermost action handler in
            // order to optimize events
            // Perform deferred updates only for xforms-ready
            startOutermostActionHandler();
            {
              // Initialize models
              initializeModels();

              // After initialization, some async submissions might be running
              processCompletedAsynchronousSubmissions(true, true);
            }
            // End deferred behavior
            endOutermostActionHandler();
          }
        });
  }
  private void restoreDynamicState(final DynamicState dynamicState) {

    this.uuid = dynamicState.uuid();
    this.sequence = dynamicState.sequence();

    indentedLogger()
        .logDebug(
            "initialization",
            "restoring dynamic state for UUID",
            "UUID",
            this.uuid,
            "sequence",
            Long.toString(this.sequence));

    // Restore request information
    restoreRequestInformation(dynamicState);
    restorePathMatchers(dynamicState);
    restoreTemplate(dynamicState);

    // Restore other encoded objects
    this.pendingUploads =
        new HashSet<String>(
            dynamicState.decodePendingUploadsJava()); // make copy as must be mutable
    this.lastAjaxResponse = dynamicState.decodeLastAjaxResponseJava();

    // Scope the containing document for the XForms API
    XFormsAPI.withContainingDocumentJava(
        this,
        new Runnable() {
          public void run() {
            Controls.withDynamicStateToRestoreJava(
                dynamicState.decodeInstancesControls(),
                new Runnable() {
                  public void run() {
                    // Restore models state
                    // Create XForms controls and models
                    createControlsAndModels();

                    // Restore top-level models state, including instances
                    restoreModelsState();

                    // Restore controls state
                    // Store serialized control state for retrieval later
                    xformsControls.createControlTree();

                    // Once the control tree is rebuilt, restore focus if needed
                    if (dynamicState.decodeFocusedControlJava() != null)
                      xformsControls.setFocusedControl(
                          xformsControls
                              .getCurrentControlTree()
                              .getControl(dynamicState.decodeFocusedControlJava()));
                  }
                });
          }
        });
  }