Skip to Content
avatar image
Former Member

Custom code and collections question

Hi All

I am developing some application which gets data from R/3 system and should represent it as a tree. The data received in two tables the first contains data the second describes the hierarchical structure of the data.

In order to implement the task I nee to write custom code: mostly accessing and sorting context data.

All this raised a number of points/problems I want to share with you:

1. It is possible to bind collection to node element but impossible to get a reference to collection inside a node. Why is this? It would be very useful to get a direct access to collection or at least to have a tools like iterators to work with.

2. I want to sort one of the node element so I need to implement the Java Comparator class. What is a right place to do it?

3. I can add method the model classes which represents elements of collection, but is there a way to add a method related to collection (like getters or finders by some criteria in the collection of elements)?

4. Is there a way to write custom code and classes and then use it in webdynpro app ( not throw the model)?

5. If the answer to the previous question is yes then how to write code (or even classes) which are depend on generated WD code? (Like a Comparator class).

I think that those questions are general enough and I am not only one who have such problems.

Thanks in advance,

Best Regards,

Victor.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • May 21, 2004 at 01:18 PM

    Hallo Victor,

    you find my answers to your qestions below ...

    >1. It is possible to bind collection to node element but impossible to get a reference to collection inside a node. Why is this? It would be very useful to get a direct access to collection or at least to have a tools like iterators to work with.

    You are right, the IWDNode interface does not provide an accessor to the node element collection of type java.util.Collection. So you have to iterate over all existing node elements via a for-loop.

    >2. I want to sort one of the node element so I need to implement the Java Comparator class. What is a right place to do it?

    The

    IWDNode

    interface provides a method sortElements:

    public void  sortElements(java.util.Comparator comparator)

    Sorts the elements according to the Comparator. The Comparator gets IWDNodeElements for this node. Since Collections.sort(List) is used, this sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

    The comparator has to be implemented by your own, e.g. as a separate class in folder src/packages/ ... /MyComparator.java or as an inner class in your Web Dynpro controller. Assume you have declared a context value node MyData in the context of controller MyView. For sorting the contained node elements based on value attribute MyAttribute you would call

    wdContext.nodeMyData().sortElements(new MyDataComparator(sessionLocale);

    The session locale can be retrieved via

    WDResourceHandler.getCurrentSessionLocale()

    The MyDataComparator class would look like this:

    package com.mycompany.myapp.mycomp.util;
    import java.util.*;
    import java.text.*;
    import com.mycompany.myapp.mycomp.wdp.IPrivateMyView;
    /**
    * MyComparator is used by the generic sorting-mechanism provided by IWDNode. 
    */
    public class MyComparator implements Comparator {
    private Collator collator; 
    /**
    * Constructor for MyComparator. Need local-information for correct sorting.
    */
    public MyComparator(Locale locale) {
    super();
    this.collator = Collator.getInstance(locale);
    }
    /**
    * @see java.util.Comparator#compare(Object, Object)
    */
    public int compare(Object o1, Object o2) {
    IPrivateMyView.IMyDataElement e1 = (IPrivateMyView.IMyDataElement ) o1;
    IPrivateMyView.IMyDataElement e2 = (IPrivateMyView.IMyDataElement ) o2;
    return collator.compare(e1.getMyAttribute(), e2.getMyAttribute()); 
    }
    }
    

    Be aware of the fact that a Collator is used for local-specific sorting and that you have to cast the objects o1 and o2 to the generated type of your own node element (here IPrivateMyView.IMyDataElement for a View Controller named MyView).

    >3. I can add method the model classes which represents elements of collection, but is there a way to add a method related to collection (like getters or finders by some criteria in the collection of elements)?

    You have to implement this on your own, e.g. by exposing such a context functionality in additional controller methods. The context you operate on belongs to this Web Dynpro controller.

    >4. Is there a way to write custom code and classes and then use it in webdynpro app ( not throw the model)?

    Yes, you just add these classes to packages inside the folder src/packages. This folder normally only contains all metadata-files but it can also be used for storing your own helper classes. These classes will be automatically deployed within your Web Dynpro Package ear-File (Web Dynpro Deployable Object).

    >5. If the answer to the previous question is yes then how to write code (or even classes) which are depend on generated WD code? (Like a Comparator class).

    See my comments above. The first approach is to implement these classes as inner classes inside a Web Dynpro controller class. Alternatively you can add separate java classes under src/packes an then add the required import-lines.

    Add comment
    10|10000 characters needed characters exceeded