Marin
Marin

Reputation: 1020

invoke confirmDialog in bean

I use Primefaces 5.1 with JSF 2.2.6.

i need identify if one file already exists in folder when user fileupload (primefaces).

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 need alert and cancel the handler.

Any idea?

the code in xhtml is:

    <h:form enctype="multipart/form-data" id="addFileDialog">

        <p:fileUpload fileUploadListener="#{attachmentsComponent.handleFileUpload}" 
                      mode="advanced" multiple="true" dragDropSupport="true" 
                      label="#{text['tasksbacking.addFile.choose']}" uploadLabel="#{text['tasksbacking.addFile.upload']}"  cancelLabel="#{text['tasksbacking.addFile.cancel']}"
                      allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc|docx|txt|xml)$/" invalidFileMessage="#{text['tasksbacking.addFile.invalidFile']}"
                      fileLimit="4" fileLimitMessage="#{text['tasksbacking.addFile.invalidCount']}"               
                      oncomplete="PF('addfile').hide();"/>  
    </h:form>

the code in bean is:

public void handleFileUpload(FileUploadEvent event) {

    // vars
    String originalName = event.getFile().getFileName();
    if (log.isDebugEnabled()) {
        log.debug("create file: " + originalName);
    }
    User loggedInUser = getSessionUser();

    // define parent folder
    Folder folder = null;

    // versionamento
    FileDetail fileDTSVersion = null;

    // verifica se ficheiro já existe na folder
    if (this.selectedNodeAttach != null) {

        if (this.selectedNodeAttach.getData() instanceof Attachment) {
            folder = (Folder) (this.selectedNodeAttach.getParent().getData());
        } else {
            folder = (Folder) (this.selectedNodeAttach.getData());
        }
        for (TreeNode tree : this.selectedNodeAttach.getChildren()) {
            if (tree.getType().equals("file")) {
                Attachment fileChild = this.attachmentManager.initializeAttachment((Attachment) (tree.getData()));

                if (fileChild.getName().equals(originalName)) {
                    if (fileChild.getContentObject() instanceof File) {
                        //file already exists --- need confirm action ???????????
                    }
                }
            }
        }
    }

    // guarda ficheiro localmente 
    try {
        if (log.isDebugEnabled()) {
            log.debug("saving file in local..." + internalName);
        }
        copyFileToDisk(internalName, event.getFile().getInputstream());
    } catch (IOException ex) {
        log.error(null, ex);
    }
}

Upvotes: 1

Views: 1298

Answers (1)

VulfCompressor
VulfCompressor

Reputation: 1410

You can achieve that by first defining a <p:confirmDialog/> in your XHTML and showing it with your Managed Bean by sending a JS through the Request Context, like so:

XHTML:

<p:confirmDialog widgetVar="dialog" appendToBody="true" header="Erro!"/>

Controller:

RequestContext.getCurrentInstance().execute("PF('dialog').show();")

By the way, there is already another question just like yours. Remember to search before posting a new question. Calling Primefaces dialog box from Managed Bean function

Upvotes: 2

Related Questions