1 2 Previous Next 23 Replies Latest reply on May 27, 2009 9:08 PM by meghiddo Go to original post
      • 15. Re: Need help with data source to populate nodes of rich:tre
        nbelaevski

         

        "Meghiddo" wrote:
        No I dont, that is exactly what I need but I am no tsure how to write it. That is exactly what I am needign though

        Good, that's what I wanted to know. Just to clarify: tree component needs data model to show the tree. As there are no standard interfaces for Tree abstract data type in JDK, there are several options that rich:tree component is able to work with:

        1. Usage of TreeNode class from RF API
        2. Swing TreeNode
        3. Collections + adaptors

        You can do the following:

        1. Use http://www.jboss.org/file-access/default/members/jbossrichfaces/freezone/docs/apidoc/org/richfaces/component/xml/XmlTreeDataBuilder.html. It will create one TreeNode per each XML node
        2. Use commons-digester to fill in TreeNodeImpl and link them
        3a) Use http://xmlbeans.apache.org/ to unmasrhal XML into graph of JAVA objects and build tree upon them using adaptors
        3b) The same as 3a), but using JAXB
        3c) The same as 3a) and 3b) but using another XML-Java framework

        • 16. Re: Need help with data source to populate nodes of rich:tre

           

          "ilya_shaikovsky" wrote:
          we just showing basic creation of Tree Data structure.. Sure the datasources could be different for every application :) And this is out of scope for the RF. We just providing samples of basic model creation and adaptors usage which you could adopt to your data source.


          I know, and I appreciate the patience the forum has shown with me. My problem is in the adopting to my data source though, I just dont have enough experience to figure this out quickly, so all I can do is look at tutorials and ask for help on the forums.

          nbelevski - thank you for all the options, I will spend time looking into them and hopefully I will be able to solve this problem soon.

          Thanks

          • 17. Re: Need help with data source to populate nodes of rich:tre

            Just to update, I have been trying to figure out how to correctly use XMLTreeDataBuilder and commons-digester.

            I am not having an easy time with it but I dont plan to give up trying

            • 18. Re: Need help with data source to populate nodes of rich:tre

              Im not sure what to pass for inputsource:

              public static org.richfaces.model.TreeNode build(org.xml.sax.InputSource inputsource) throws org.xml.sax.SAXException, java.io.IOException {


              inputsource = books.xml;

              return null;
              }

              • 19. Re: Need help with data source to populate nodes of rich:tre

                I think I know what should be used as "inputsource", please tell me if I am correct -

                if I have code like this to deserialize my xml data:

                Serializer serializer = new Persister();
                File source = new File("example.xml");
                
                Example example = serializer.read(Example.class, source);


                Then my inputsource will be either source or example.xml (not sure which one) right?

                • 20. Re: Need help with data source to populate nodes of rich:tre
                  nbelaevski
                  • 21. Re: Need help with data source to populate nodes of rich:tre

                    Hmm I think I can use that. I already have this:

                    package testContainer;
                    
                    import deviceContainer.DeviceXMLAO;
                    
                    import java.io.IOException;
                    import java.io.InputStream;
                    
                    import org.apache.commons.httpclient.HttpClient;
                    import org.apache.commons.httpclient.HttpStatus;
                    import org.apache.commons.httpclient.methods.GetMethod;
                    
                    public class Temp {
                    
                     static String devicesUrl = "http://webc-apps.ni.com/measure/1.01/devices";
                    
                    
                     public byte[] getResponse(byte[] x) throws IOException {
                     // Create an instance of HttpClient.
                     HttpClient client = new HttpClient();
                    
                     // Create a method instance.
                     GetMethod method = new GetMethod(devicesUrl);
                    
                     //responseBody = null;
                    
                     byte[] responseBody = method.getResponseBody();
                    
                     try {
                     // Execute the method.
                     int statusCode = client.executeMethod(method);
                     System.out.println( method.getStatusCode());
                    
                     if (statusCode != HttpStatus.SC_OK) {
                     System.out.println("Method failed: " + method.getStatusLine());
                     }
                    
                     // Read the response body.
                     //byte[] responseBody = method.getResponseBody();
                     System.out.println(method.getResponseBodyAsString());
                     } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     }
                     return responseBody;
                     }
                    
                    }


                    So I can use responseBody from this code, is that correct?

                    • 22. Re: Need help with data source to populate nodes of rich:tre
                      nbelaevski

                      Yes, you can.

                      • 23. Re: Need help with data source to populate nodes of rich:tre

                        I am very close now.

                        I just need to figure out how to bind the items to TreeNode. I have my DeviceCollection class:

                        @Root
                        public class DeviceCollection {
                        
                        
                         @ElementList(inline = true)
                         private Collection<Device> devices;
                        
                        
                        
                         //get devices
                         public Collection<Device> getDevices() {
                         return devices;
                         }
                        
                         //set devices
                         public void setDevices(Collection<Device> devices) {
                         this.devices = devices;
                         }
                        
                        }


                        and my device class:

                        @Root
                        public class Device {
                         static String devicesUrl = "http://webc-apps.ni.com/measure/1.01/devices";
                        
                         LinkedList device = new LinkedList();
                        
                         @Element(required=false)
                         private String deviceId;
                        
                         @Element(required=false)
                         private String name;
                        
                         @ElementMap(entry = "property", key = "name", attribute = true, inline = true, required=true, data=false, empty=true)
                         private Map<String, String> properties;
                        
                         public String getDeviceId() {
                         return deviceId;
                         }
                        
                         public void setDeviceId(String deviceId) {
                         this.deviceId = deviceId;
                         }
                        
                         public String getName() {
                         return name;
                         }
                        
                         public void setName(String name) {
                         this.name = name;
                         }
                        
                         public Map<String, String> getProperties() {
                         return properties;
                         }
                        
                         public void setProperties(Map<String, String> properties) {
                         this.properties = properties;
                         }
                        
                        }


                        Then I have a class to read dxml data from a web service:

                        public DeviceCollection readXML(InputStream xml) throws Exception {
                         Serializer serializer = new Persister();
                        
                         DeviceCollection deviceCollection = null;
                         deviceCollection = serializer.read(DeviceCollection.class, xml);
                         xml.close();
                         return deviceCollection;
                         }


                        and I have this Temp class where I am trying to get the data to be set to TreeNode data; :

                        static String devicesUrl = "http://webc-apps.ni.com/measure/1.01/devices";
                        
                         public static void updateNode() {
                        
                         // Create an instance of HttpClient.
                         HttpClient client = new HttpClient();
                        
                         // Create a method instance.
                         GetMethod method = new GetMethod(devicesUrl);
                        
                         XMLTreeDataBuilderTest node = new XMLTreeDataBuilderTest();
                         DeviceCollectionXMLAO access = new DeviceCollectionXMLAO();
                         DeviceCollection deviceCollection = null;
                        
                         try {
                         deviceCollection = access.readXML(method.getResponseBodyAsStream());
                         } catch (IOException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         } catch (Exception e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                         }
                        
                        
                         deviceCollection.getDevices();
                        
                         node.setData((TreeNode) deviceCollection.getDevices());
                         //node.setData(deviceCollection.get(0));
                         //node.setData((TreeNode) deviceCollection.getDevices());
                        
                         for (Device device : deviceCollection.getDevices()) {
                        
                         device.getDeviceId();
                         }


                        Now I mean its obvious something is wrong in this temp class. I thought I would be able to do something like deviceCollection.setData(deviceCollection.get(0));
                        and it would bind the value i that first element to data, but this ill not work for me.

                        Can anyone help me see what is wrong here?

                        1 2 Previous Next