0

I am trying to load contents of file in the center layout. On selection event of tree node. But I am not able to display it in the center layout. When i refresh the page then also it is not displaying the contents of file.

This is my xhtml page.I am going wrong somewhere?

<h:body>
        <h:form id="form">
<h:commandLink id="back" action="Welcome?faces-redirect=true" type="link"/>
            <p:layout fullPage="true">                
                <p:layoutUnit position="west" size="200" header=
                              "document List" resizable="true" closable="false" collapsible="false">
                    <h:outputText value="Searched document's " />
                    <p:tree value="#{searchContent.root}" var="node" dynamic="true" selectionMode="single" 
                            selection="#{searchContent.singleSelectedNode}">
                        <p:treeNode >
                            <h:outputText value="#{node}" />
                        </p:treeNode>
                        <p:ajax event="select" listener="#{searchContent.onNodeSelect}"/>
                    </p:tree>
                </p:layoutUnit>
                <p:layoutUnit position="east" size="200" header=
                              "extra" resizable="true" closable="false" collapsible="false">
                    <h:outputText value="Uploaded by Author's Name" />
                </p:layoutUnit>
                <p:layoutUnit position="center">
<!--                    <h:panelGroup id ="test">-->
                    <h:outputText value="#{searchContent.fileData}" />
<!--                    </h:panelGroup>-->
                </p:layoutUnit>
            </p:layout>
        </h:form>
    </h:body>

and here is my managed bean of this page.on select event I am getting the name of the node.And I am reading the file contents line by line. I am getting the contents properly but not able to display in layout

public void onNodeSelect(NodeSelectEvent event) throws FileNotFoundException, IOException {
        String path = null;
        FileInputStream fis = null;
        COSDocument cosdocument = null;
        Document doc = new Document();
        TreeNode treeNode = event.getTreeNode();
        String namecheck = treeNode.toString();
        System.out.println("\t" + namecheck);
        String name = null, fullname = null, filePath = null;
        System.out.println("treeNode contains:" + treeNode);
        System.out.println("Node Data ::" + treeNode + " :: Selected");
        int _icnt = 0;
        while (_icnt < treeList.size()) {
            name = treeList.get(_icnt);
            System.out.println("Name :" + name);
            fullname = field.get(_icnt);
            System.out.println("Full Name:" + fullname);
            //String nodeTokens[] = name.split(Pattern.quote("."));
            if (namecheck.equalsIgnoreCase(name)) {
                System.out.println("*******************************");
                break;
            }
            System.out.println(MessageFormat.format("Icnt is:{0}", _icnt++));
        }
        System.out.println("After while loop");
        /*
         Database Call to get the filePath based on fileName 
         from f.getName() .
         */
        String result = UserDAO.getFileName(fullname);
        System.out.println("Result" + result);
        if (!(result.equals(""))) {
            File f = new File(result);
            if (f.isFile()) {
                if (f.getName().endsWith(".docx")) {
                    try {
                        fileData = null;
                        path = f.getAbsolutePath();
                        fis = new FileInputStream(f.getAbsolutePath());
                        XWPFDocument document = new XWPFDocument(fis);
                        List<XWPFParagraph> paragraphs = document.getParagraphs();
                        for(XWPFParagraph paras : paragraphs){
                            fileData = paras.getText();
                            System.out.println("File data of docx file is:"+fileData);
                        }
                        System.out.println("Path is:" + path);
                    } catch (Exception e) {
                        System.err.println("Exception in reading docx file is:");
                        e.printStackTrace();
                    } finally {
                        fis.close();
                    }
                } else if (f.getName().endsWith(".doc")) {
                    try{
                        fileData = null;
                    path = f.getAbsolutePath();
                    System.out.println("Path is:" + path);
                    fis = new FileInputStream(f.getAbsolutePath());
                    HWPFDocument hwpfDoc = new HWPFDocument(fis);
                        WordExtractor wrdExtract = new WordExtractor(hwpfDoc);
                        String[] paragraphText = wrdExtract.getParagraphText();
                        for (String paragraph : paragraphText) {
                            fileData = paragraph;
                            System.out.println("File Data of doc file is:" + fileData);
                        }                    
                }
                    catch(Exception ex){
                        System.err.println("Exception in reading doc file is:");ex.printStackTrace();
                    }
                    finally{
                        fis.close();
                    }
                }   else {
                    if (f.getName().endsWith(".pdf")) {
                        try{
                            fileData = null;
                        path = f.getAbsolutePath();
                        System.out.println("Path is:" + path);
                        PDFParser parser = new PDFParser(new FileInputStream(f.getAbsolutePath()));
                        parser.parse();
                         cosdocument = parser.getDocument();
                        PDFTextStripper stripper = new PDFTextStripper();
                        fileData = stripper.getText(new PDDocument(cosdocument));
                            System.out.println("File Data of pdf is:"+fileData);                        
                        }catch(Exception ex){
                            System.err.println("Exxception in reading pdf is:");ex.printStackTrace();
                        }
                        finally{
                            cosdocument.close();
                        }
                    } else {
                        if (f.getName().endsWith(".txt")) {
                            fileData = null;
                            path = f.getAbsolutePath();
                            System.out.println("Path is:" + path);
                            BufferedReader br = new BufferedReader(new FileReader(f));
                            String line = null;
                            while ((line = br.readLine()) != null) {
                                fileData = line;
                                System.out.println("fileData:" + fileData);
                            }
                            System.out.println("fileData:" + fileData);
                        }
                    }
                }
            } 
        }
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
deep
  • 9
  • 1

1 Answers1

0

You don't update content you want to be refreshed.

<p:ajax event="select" listener="#{searchContent.onNodeSelect}" update="form:fileData"/>

Add id to your h:outputText

<p:layoutUnit position="center">
    <h:outputText id="fileData" value="#{searchContent.fileData}" />
</p:layoutUnit>

Related:

Community
  • 1
  • 1
Geinmachi
  • 1,251
  • 1
  • 8
  • 20
  • There are several duplicates of this question (maybe not directly the same cause (which is even a more basic 'issue') . Searching for them is the better thing to do instead of answering (although the former will not give you reputation points, it is much more appreciated) http://stackoverflow.com/questions/18913547/how-to-update-a-layoutunit-in-primefaces – Kukeltje Oct 09 '15 at 10:22
  • BalusC was here and he didn't mark it as duplicate. I edited my answer for linking related threads. – Geinmachi Oct 09 '15 at 10:29
  • BalusC does not have to do everything. He edits many questions that he does not answer while he could or mark duplicate while he could. He leaves work for others. So him not marking it as a duplicate does not mean it isn't a duplicate. But if you think otherwise... be my guest... He is no God (although he comes close according to some) – Kukeltje Oct 09 '15 at 10:34