2 Replies Latest reply on Aug 13, 2010 5:40 AM by deiviz2001

    Problem with reading arraylist in javascript for SuggestionBox

    deiviz2001

      Hello guys,

       

      I'm writing to you with my fingers crossed to see if someone may throw some light into this issue that is slowing me down.

      My intention is to search for the word typed in the input text (id="input") in a database through a bean (ontologySuggestion) and throw an ArrayList of results that contain that text and will be shown in the frontend.

       

      The ontologySuggestion bean is mapped in faces-config.xml and has an attribute inputString to store the word inputed. It also has an attribute name that stores the short name of URI instance and an attribute instance that stores the URI itself. The method that returns the object ArrayList<ontologySuggestion> resultArray has the name of getSuggestions.

         <div>
         
                  <h:outputText value="Input word to get suggestions" />
                  <h:inputText style="margin:0px;" id="input" />
                  <h:outputText value="Words suggested" />
                  <h:outputText id="objects" style="font-weight:bold" />
                 
                  <rich:suggestionbox height="200" width="150"
                      usingSuggestObjects="true"
                      onobjectchange="printObjectsSelected(#{rich:element('objects')}, #{rich:component('suggestion')});"
                      suggestionAction="#{actionsContainer.ontologyActions.getSuggestions}" var="resultArray"
                      for="input" fetchValue="#{resultArray.name}" id="suggestion">
                      <h:column>
                          <h:outputText value="#{resultArray.instance}" />
                      </h:column>
                      <h:column>
                          <h:outputText value="#{resultArray.name}" />
                      </h:column>
                  </rich:suggestionbox>
          </div>
        

       

       

           This is the javascript code (the alert shows "undefined"):

       

      <script type="text/javascript">//<![CDATA[
             function printObjectsSelected(output, sgcomponent){

                      output.innerHTML=sgcomponent.getSelectedItems().pluck('name');
                      alert("Array length: " + sgcomponent.length + "##");
                  }
              //]]></script>     

       

              This is the snippet of code used to return the resultArray :

       


          public ArrayList<SuggestedItemBean> getSuggestions(Object inputText){
         
        
           //Convert to string the text typed by the user through input text box
            String text2search = (String) inputText;
           
           
           SuggestedItemBean suggestion = new SuggestedItemBean(); //Contains the suggestion to be shown
           String queryLang="SPARQL";    //Ontology query language
           String query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX owl: <http://www.w3.org/2002/07/owl#>  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> SELECT DISTINCT ?instance ?name WHERE {  ?instance rdf:type ?class .   ?class rdf:type owl:Class . ?instance rdfs:label ?name .   FILTER regex(str(?name), \""+ text2search +"\", \"i\") } ORDER BY ?name";
             

              //Load ontology from file
              OntologyRepository repository = Factory.createOntologyRepository();
              Identifier identifier = Factory.createIdentifier("http://xxx.yyy.zzz#");
              Ontology ontology = repository.getOntology(identifier);   
             
              //ArrayList for storing the result to be sent to javascript
              ArrayList<SuggestedItemBean> resultArray = new ArrayList<SuggestedItemBean>();
             
              try{ 
                 
                  //Execute query and store results in a list
                  List<Map<String, String>> result = ontology.executeQuery(queryLang, query);
                 
                  //Loop, fill arrayList and show all map values
                  for (Map<String,String> map : result) {
                      for (String name : map.keySet()) {
                          String instance = map.get(name);
                         
                          suggestion.setSuggestedItemBean(name, instance);
                          resultArray.add(suggestion);
                         
                        }        
                  }
              }
              catch(Exception e)
              {
                  System.out.println("Excepcion en testOntologyLoader: " + e.toString());
              }

              return resultArray;

          }
         
      }

       

      Any ideas? Thanks in advance