Submit button for xp:fileUpload needs full update?

52 Views Asked by At

On my XPage I have an xp:fileUpload component and a xp:button to submit the file. however it seems I need to set the Full Update option otherwise the files are not send to the server? As a result in Chrome I remain focus on place the form but in Edge the focus is set at the top of the form.

Is it possible to Just set a partial refresh?

Here is what the code for the button looks like:

<xp:button id="btnSaveFile" styleClass="btnSaveFile">
    <xp:this.attrs>
        <xp:attr name="data-placement" value="bottom" />
        <xp:attr name="data-toggle" value="tooltip" />
    </xp:this.attrs>
    <xp:this.title><![CDATA[#{javascript:xptI18NBean.getValue("matter.gen_Attachment_Help")}]]></xp:this.title>
    <xp:this.value><![CDATA[#{javascript:xptI18NBean.getValue("matter.attachment_upload")}]]></xp:this.value>
    <i class="fa fa-upload" aria-hidden="true" />
    &#160;
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete" disableValidators="true"
        onError="console.log('onerror')" onStart="console.log('onstart')">
        <xp:this.action><![CDATA[#{javascript:attachmentBean.save(compositeData.ref,compositeData.key, compositeData.copyFields);
attachmentBean.loadList(ref, key);
viewScope.put("attachments" + ref + key, attachmentBean.getAttachments());}]]></xp:this.action>
        
    </xp:eventHandler>
</xp:button>

And this is the java code that I call:

public void save(String parentId, String type, String fields, IUploadedFile iUploadedFile) {
    utils.printToConsole(this.getClass().getSimpleName().toString() + " - save()");
    try {
        if (null != iUploadedFile) {
            Database db = sess.getDatabase(server, filePath + fileName);
            if (db.isOpen()){
                String fieldName = null;
                //get the uploaded file
                //IUploadedFile iUploadedFile = uploadedFile.getUploadedFile();

                //get the original filename
                String tempClientFile = iUploadedFile.getClientFileName();
                utils.printToConsole("# iUploadedFile - getClientFileName: " + iUploadedFile.getClientFileName());
                utils.printToConsole("# iUploadedFile - getServerFileName:  " + iUploadedFile.getServerFileName());
                utils.printToConsole("# iUploadedFile - getContentType:  " + iUploadedFile.getContentType());
                utils.printToConsole("# iUploadedFile - getContentLength:  " + iUploadedFile.getContentLength());

                //get the server file (with a cryptic filename)
                File tempFile = iUploadedFile.getServerFile();                  
                utils.printToConsole("# tempFile exist: " + tempFile.exists()); 
                if(!tempFile.exists()){
                    XspOpenLogUtil.logErrorEx(null, "tempFile " + tempFile + " does not exist for user " + sess.getUserName(), Level.SEVERE, null);
                }
                utils.printToConsole("# tempFile canread: " + tempFile.canRead());
                if(!tempFile.canRead()){
                    XspOpenLogUtil.logErrorEx(null, "tempFile " + tempFile + " not readable for user " + sess.getUserName(), Level.SEVERE, null);
                }
                utils.printToConsole("# tempFile canwrite: " + tempFile.canWrite());
                if(!tempFile.canWrite()){
                    XspOpenLogUtil.logErrorEx(null, "tempFile " + tempFile + " not writable for user " + sess.getUserName(), Level.SEVERE, null);
                }
                utils.printToConsole("# tempFile ishidden: " + tempFile.isHidden());
                if(tempFile.isHidden()){
                    XspOpenLogUtil.logErrorEx(null, "tempFile " + tempFile + " is hidden for user " + sess.getUserName(), Level.SEVERE, null);
                }
                
                File correctedFile = new File( tempFile.getParentFile().getAbsolutePath() + File.separator + tempClientFile );
                utils.printToConsole("# correctedFile getName: " + correctedFile.getName());
                utils.printToConsole("# correctedFile getPath: " + correctedFile.getPath());
                utils.printToConsole("# correctedFile getAbsolutePath: " + correctedFile.getAbsolutePath());
                utils.printToConsole("# correctedFile getCanonicalPath: " + correctedFile.getCanonicalPath());

                boolean success = false;                    
                try{
                    success = tempFile.renameTo(correctedFile);
                } catch (Exception e){
                    XspOpenLogUtil.logErrorEx(e, "Could not rename temp file " + tempFile + " to " + correctedFile + " for user " + sess.getUserName(), Level.SEVERE, null);
                }                   
                
                if (success) {
                    //do whatever you want here with correctedFile
                    String borreSummary = null;
                    Document parentDoc = db.getDocumentByUNID(parentId);
                    if (null != parentDoc){
                        if(type.trim().equalsIgnoreCase(parentId.trim())) {
                            String newParentID = parentDoc.getItemValueString("parentId");                              
                            parentDoc = db.getDocumentByUNID(newParentID);
                        }
                        
                        fieldName = "fldKundSammandrag";
                        if (parentDoc.hasItem(fieldName)){
                            borreSummary = parentDoc.getItemValueString(fieldName);
                        }
                    }                   

                    Document doc = db.createDocument();
                    doc.appendItemValue("Form","fa_Attachment");
                    doc.appendItemValue("parentId", parentId);
                    doc.appendItemValue("type", type);

                    Name origAuthor = sess.createName(sess.getEffectiveUserName());

                    //get the groups the user belongs to
                    Vector<Name> groupNameList = sess.getUserGroupNameList();
                    Vector<String> groups = new Vector<String>();
                    for (int i = 0; i < groupNameList.size(); i++) {
                        Name groupName = groupNameList.elementAt(i);
                        groups.add(groupName.getAbbreviated()); 
                    }

                    Vector<String> vecAuthors = new Vector<String>(4);
                    vecAuthors.add("[Admin]");
                    vecAuthors.add("[SuperAdmin]");
                    vecAuthors.add("[SuperDuper]");
                    if(groups.contains("KKOM_OpAdmin")){
                        vecAuthors.add("KKOM_OpAdmin"); 
                    }
                    if(groups.contains("KKOM_KYC Center")){
                        vecAuthors.add("KKOM_KYC Center");  
                    }
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("fldKontorChefFName")) {
                            if(!parentDoc.getItemValueString("fldKontorChefFName").trim().equalsIgnoreCase("")){
                                vecAuthors.add(parentDoc.getItemValueString("fldKontorChefFName").trim());
                            }
                        }
                        if (parentDoc.hasItem("fldKundsvarig")) {
                            if(!parentDoc.getItemValueString("fldKundsvarig").trim().equalsIgnoreCase("")){
                                vecAuthors.add(parentDoc.getItemValueString("fldKundsvarig").trim());
                            }
                        }                                   
                    }
                    vecAuthors.add(origAuthor.getCanonical());

                    TreeSet<String> uniqueAuthors = new TreeSet<String>(vecAuthors);
                    vecAuthors = new Vector<String>(uniqueAuthors);

                    Item authorRole = doc.replaceItemValue("Authors", vecAuthors);
                    authorRole.setAuthors(true);
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Officers")){
                            Item item = parentDoc.getFirstItem("Officers");
                            doc.copyItem(item, null);
                        }
                    }
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Reciters")){
                            Item item = parentDoc.getFirstItem("Reciters");
                            doc.copyItem(item, null);
                        }
                    }
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Readers")){
                            Item item = parentDoc.getFirstItem("Readers");
                            doc.copyItem(item, null);
                        }
                    }
                    
                    if(null != borreSummary){
                        if (parentDoc.hasItem("Support")){
                            Item item = parentDoc.getFirstItem("Support");
                            doc.copyItem(item, null);
                        }       
                    }

                    RichTextItem rtFiles = doc.createRichTextItem("files");               
                    rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null);
                    try {
                        JsonJavaObject objJson = (JsonJavaObject) JsonParser.fromJson(JsonJavaFactory.instanceEx, fields);
                        if (null != objJson){
                            ArrayList<JsonJavaObject> listFields = (ArrayList<JsonJavaObject>) objJson.get("fields");       
                            if(null != listFields){
                                
                                for(int i=0; i<listFields.size(); i++){
                                    JsonJavaObject fieldItem = listFields.get(i);
                                    
                                    String itemRef = (String) fieldItem.get("name");
                                    if (parentDoc.hasItem(itemRef)){
                                        Item item = parentDoc.getFirstItem(itemRef);
                                        doc.copyItem(item, null);
                                        Item docItem = doc.getFirstItem(itemRef);
                                        String fieldType = (String) fieldItem.get("type");
                                        if (fieldType.equals("Names")){
                                            docItem.setNames(true);
                                        }
                                        if (fieldType.equals("Authors")){
                                            docItem.setAuthors(true);
                                        }
                                        if (fieldType.equals("Readers")){
                                            docItem.setReaders(true);
                                        }
                                        if (fieldType.equals("Text")){
                                            docItem.setSummary(true);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (JsonException e1) {
                        XspOpenLogUtil.logErrorEx(e1, JSFUtil.getXSPContext().getUrl().toString(), Level.SEVERE, null);
                    }
                    
                    doc.save();
                    utils.printToConsole("# attachment doc saved");
                    
                    //new!
                    if(utils.validValueInPropertyFile(propDataSources,"vw_attach_unid_type")){
                        View vw = db.getView(propDataSources.getProperty("vw_attach_unid_type"));
                        if (null != vw){    
                            vw.refresh();   
                        }
                    }
                    
                    //set a viewscope with doc UNID of latest uploaded file for whatever purpose
                    ExtLibUtil.getViewScope().put("attachmentUnid", doc.getUniversalID());                      

                    //if we're done: rename it back to the original filename, so it gets cleaned up by the server
                    utils.printToConsole("# correctedFile exists: " + correctedFile.exists());
                    if(!correctedFile.exists()){
                        XspOpenLogUtil.logErrorEx(null, "correctedFile " + correctedFile + " does not exist for user " + sess.getUserName(), Level.SEVERE, null);
                    }
                    utils.printToConsole("# correctedFile canread: " + correctedFile.canRead());
                    if(!correctedFile.canRead()){
                        XspOpenLogUtil.logErrorEx(null, "correctedFile " + correctedFile + " is not readable for user " + sess.getUserName(), Level.SEVERE, null);
                    }
                    utils.printToConsole("# correctedFile canwrite: " + correctedFile.canWrite());
                    if(!correctedFile.canWrite()){
                        XspOpenLogUtil.logErrorEx(null, "correctedFile " + correctedFile + " is not writablee for user " + sess.getUserName(), Level.SEVERE, null);
                    }
                    if(correctedFile.isHidden()){
                        XspOpenLogUtil.logErrorEx(null, "correctedFile " + correctedFile + " is hidden for user " + sess.getUserName(), Level.SEVERE, null);
                    }
                    utils.printToConsole("# correctedFile ishidden: " + correctedFile.isHidden());
                    try{
                       correctedFile.renameTo(tempFile);
                    } catch (Exception e){
                        String msg = null;
                        if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
                            msg = propStrings.getProperty("attachment_growl_file_upload_error");
                        }
                        MultiGrowlMessages.createGrowlMessage(formatPropertyValueToUTF(msg), "warning");                            
                        XspOpenLogUtil.logErrorEx(e, "Could not rename correctedFile file " + correctedFile + " to " + correctedFile + " for user " + sess.getUserName(), Level.SEVERE, null);
                    }

                    rtFiles.recycle();
                    
                    String msg = null;
                    if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file")){
                        msg = propStrings.getProperty("attachment_growl_file");
                    }                       
                    MultiGrowlMessages.createGrowlMessage(formatPropertyValueToUTF(msg) + iUploadedFile.getClientFileName(), "success");
                    
                    if(null != tempFile && tempFile.canWrite()){
                        tempFile.delete();
                    }
                    if(null != correctedFile && correctedFile.canWrite()){
                        correctedFile.delete();
                    }
                }
                else{
                    String msg = null;
                    if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
                        msg = propStrings.getProperty("attachment_growl_file_upload_error");
                    }                       
                    MultiGrowlMessages.createGrowlMessage(msg, "warning");                      
                    XspOpenLogUtil.logErrorEx(null, "Could not rename file " + correctedFile + " to " + tempFile + " for user " + sess.getUserName(), Level.SEVERE, null);
                }
            }        
        }else{
            String msg = null;
            if(utils.validValueInPropertyFile(propStrings,"attachment_growl_no_file")){
                msg = propStrings.getProperty("attachment_growl_no_file");
            }               
            MultiGrowlMessages.createGrowlMessage(formatPropertyValueToUTF(msg), "error");
        }
    } catch (Exception e) {
        String msg = null;
        if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
            msg = propStrings.getProperty("attachment_growl_file_upload_error");
        }           
        MultiGrowlMessages.createGrowlMessage(formatPropertyValueToUTF(msg), "warning");    
        XspOpenLogUtil.logErrorEx(e, JSFUtil.getXSPContext().getUrl().toString(), Level.SEVERE, null);
    } 
}

I tried the other options (Partial Update, No Update, No Submission, set partial execution) so I could set an action on the eventhandler for oncomplete:

<xp:this.onComplete><![CDATA[console.log("completed remove");
XSP.partialRefreshGet("#{id:pnlDocuments}");
var el = XSP.getElementById('#{id:pnlFiles}');
var ef = dojo.byId(el);
ef.focus();]]></xp:this.onComplete>

Note: this code resides on a custom control which I reuse a lot on the form. It works perfectly well in chrome but (mostly) not in edge/internet explorer.

0

There are 0 best solutions below