cancel
Showing results for 
Search instead for 
Did you mean: 

How can i sort an Extended Collection via BeanShell?

Former Member
0 Kudos
148

Hallo Community,

i do create an ExtendedCollection via BeanShel like this:


        colln = bean.getExtensionCollection(collectionName);

        while (...) {

         collBean = colln.create();

         ext_field = collBean.getExtensionField(FIELD_DOC_NAME);

        ext_field.set(docName);

         ext_field = collBean.getExtensionField(FIELD_DOC_VERSION);

        ext_field.set(docVersion);

        colln.add(collBean);

        }

I add several doc names wit their versions.

After that i have to sort this collection according to DOC_NAME and DOC_VERSION

How can i do that?

thanks.

Waldemar

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

is there really no way to sort a extended collection?

Former Member
0 Kudos

Hi Waldemar,

Please check this post -

I have added sample code using java comparator to sort an extension collection, hope it helps you.

Thanks,

Gayathri

Former Member
0 Kudos

thakn yo u for your answer

unfortunatelly this solution does not work as i expected.

i have to sort an extended collection and not an array.

the result must be visible in the Master Agreement

former_member190023
Contributor
0 Kudos

Unfortunately there is no 'direct' method to sort the Extension Collection via the public iapi.

You could use Gayathri's example to sort the collection members, then remove/re-add them in the correct order.

Bogdan

Former Member
0 Kudos

Hallo Bogdan,

thank you for your answer.

i tryed to do this this way:

class CustomListComparator implements Comparator {

    public int compare(Object o1, Object o2) {

        ExtensionFieldIfc e_name_1 = o1.getExtensionField(FIELD_DOC_NAME);

        ExtensionFieldIfc e_name_2 = o2.getExtensionField(FIELD_DOC_NAME);

        name_1 = e_name_1.get();

        name_2 = e_name_2.get();

        ret = name_1.compareTo(name_2);

        if (ret == 0) {

             ExtensionFieldIfc e_ver_1 = o1.getExtensionField(FIELD_DOC__VER);

             ExtensionFieldIfc e_ver_2 = o2.getExtensionField(FIELD_DOC_VER);

             ver_1 = e_ver_1.get();

             ver_2 = e_ver_2.get();

             ret = ver_1.compareTo(ver_2);

        }

        return ret;

    }

}   

public void sortCollection(IBeanIfc bean, String collectionName) {

        ArrayList customArrayList = new ArrayList();

        colln = bean.getExtensionCollection(collectionName);

        for (iter = colln.size(); iter > 0; iter--) {

        ExtensionCollectionMemberIfc extMember = colln.get(iter-1);

          customArrayList.add(extMember);

        }

       

        for (iter = colln.size(); iter > 0; iter--) {

        ExtensionCollectionMemberIfc extMember = colln.get(iter-1);

          colln.delete(extMember);

        }

        

       Collections.sort(customArrayList, new CustomListComparator());

       iterator = customArrayList.iterator();

       for (list_member: iterator) {

            colln.add(list_member);

       }

}

But my problem is that i manipulate the extended collection and then try to sort it in the same step.

So i add or delete some members of the collection and then try to sort it.

When i do that the system tells me, that another user has already changed the collection and the changes are rejected. i have to press the "cancel" button.

former_member190023
Contributor
0 Kudos

Hi Waldemar,

Java will not let you do concurrent modifications on any instance of java.util.Collection<E>

Bogdan

Former Member
0 Kudos

Hallo Bogdan,

is there maybe a work around?

have i after i validated and saved the master agrement a posibility to sort the collection?

i tried to sort on "loaded" event but i can see the same message

Waldemar

former_member190023
Contributor
0 Kudos

You should only do such actions on VALIDATE (or toolbar scripts). Not LOADED, SAVED because these will have repercussions on system functionality. (recommend you to read carefully the Scripting and Workflow guide for SAP Sourcing, to understand how script contexts work)

Script context does not have any impact on your concurrency issue ... and there is no way to circumvent standard Java security.

As I said before, you could first sort your members in a separate List, then clear the extension collection and re-add them in the right order.

PS: I have not done this before, so it's just a recommendation as the easiest solution. There are also other ways to do this sort, but again I haven't tested any so I can't recommend.

Bogdan

Answers (0)