title: Conditionally invoking JavaScript from a bean
body: <p>How do I conditionally invoke JavaScript from a bean in JSF 2?</p>
  
  <p>I have a class that uses PrimeFaces which uploads user files to a particular folder, but if the user attempts to upload a file with the same name as one that is already present, I want JavaScript in this case to open up a box on the screen asking the user if he wants to overwrite the old file, or cancel the operation.  If no file with that name is present, then JavaScript is not called.</p>
  
  <p>I know how to test that a folder has a particular file in it, it is just that I need to know how to invoke JavaScript conditionally.</p>
  
  <p>I would most appreciate any advice on this.</p>
  
  <hr>
  
  <p>I have looked at variopus resources online, but still cannot get the application to work correctly.  basically, this is what I have done, in an included xhtml page I have the following code for the file upload:</p>
  
  <pre><code>&lt;p:fileUpload id="fileUpload" fileUploadListener="#{filters.upload}"
     allowTypes="#{filters.uploadTypes}" invalidFileMessage="#{filters.uploadBadType}"
     sizeLimit="#{filters.uploadSize}" invalidSizeMessag="#{filters.uploadBadSize}"
     update="fileUpload fileTable uploadMessage" description="Select Text File"
     disabled="#{filters.disableFileUploadButton}"/&gt;
  
  &lt;!--- Then further in the same file is this: --&gt;
  
  &lt;p:remoteCommand name="remoteCommandOverwrite" actionListender="#{filters.execOverwrite}"/&gt;
  </code></pre>
  
  <p>The parent xhtml page that includes the above I have the foolowing JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    function popupConfirm() {
      var overwrite = confirm('Warning: This will overwrite the existing file - Do you confirm this?');
      if (overwrite) remoteCommandOverwrite([{name: overwrite, value: true}]);
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>In my bean I have the following code in three methods:</p>
  
  <pre><code>    public void upload(FileUploadEvent event) {
      FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
      FacesContext.getCurrentInstance().addMessage(null, msg);
      overwrite = false;
      // Do what you want with the file        
      try {
          copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
      public void copyFile(String fileName, InputStream in) {
  
       // Initialization etc.
  
                      File file = new File(uploadFull + fileName);
          if (file.exists()) {
              RequestContext.getCurrentInstance().execute("popupConfirm()");
  
  // Then test to see if overwrite is true or false, and act accordingly
  
  }
  
  // Then I am supposed to get the value of overwrite here:
  public void execOverwrite() {
      System.out.println("### execOverwrite() ###");
      FacesContext context = FacesContext.getCurrentInstance();
      Map&lt;String, String&gt; map = context.getExternalContext().getRequestParameterMap();
      String soverwrite = (String) map.get("overwrite");
      if (soverwrite.equals("true")) {
          overwrite = true;
          System.out.println("overwrite: true");
      }
  }
  </code></pre>
  
  <p>What I am trying to do is first to invoke conditionally the JavaScript function popupConfirm().  On clicking the "Upload" button that is invoked if the codition is true, which is what I want.  This is then supposed to call </p>
  
  <p>That works and brings up the confirm() box, but the  is never called, so the method execOverwrite() in my bean is also never called, and I cannot pick up the return value and pass it to the code inside the method copyFile().  What is going wrong?</p>
  
  <hr>
  
  <p>I put this problem on the back burner for about a week, and have just got back to it.  I got it to work, and can pass a value back to the bean, but somehow I need to resume execution from the place where JavaScript is called.</p>
  
  <p>To sumarize, my JavaScript contains the following code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    function popupConfirm() {
      var overwrite = confirm('Warning: This will overwrite the existing file - Do you confirm this?');
      if (overwrite) remoteCommandOverwrite([{name: 'overwrite', value: 'true'}]);
    }
   &lt;/script&gt;
  </code></pre>
  
  <p>And in the xhtml code I have:</p>
  
  <pre><code>&lt;p:fileUpload id="fileUpload" fileUploadListener="#{filters.upload}" ...../&gt;
  
  &lt;!-- Other code --&gt;
  
  &lt;p:remoteCommand name="remoteCommandOverwrite" actionListener="#{filters.execOverwrite}"/&gt;
  </code></pre>
  
  <p>Then on clicking the file upload button after clicking the choose file button, the code in the JavaScript, as listed above, is executed:</p>
  
  <pre><code>RequestContext.getCurrentInstance().execute("popupConfirm()");
  </code></pre>
  
  <p>Then on clicking "OK" in the dialog box, this method in the same bean is called:</p>
  
  <pre><code>public void execOverwrite() {
      FacesContext context = FacesContext.getCurrentInstance();
      Map&lt;String, String&gt; map = context.getExternalContext().getRequestParameterMap();
      String soverwrite = map.get("overwrite");
      if (soverwrite.equals("true")) {
          overwrite = true;       }
      }
  }
  </code></pre>
  
  <p>where the flag "overwrite" will eventually be tested to see if it is true.</p>
  
  <p>Using various print statements I check that this works.  However, the code does not resume executing after encountering the statement: RequestContext.getCurrentInstance().execute("popupConfirm()"); regardless of whether I enter "OK" or "Cancel" in the dialog, which is what i want it to do.  It looks as if a callback of some type is required, and would most appreciate some ideas.</p>