4

In WEBMETHODS, can an element be dropped from a DocumentList while Looping through the DocumentList? If Yes then how? If No, then how could we set the value of a DocumentList variable to null.

thanks
nohsib

MrJames
  • 676
  • 2
  • 8
  • 20
Nohsib
  • 3,614
  • 14
  • 51
  • 63

3 Answers3

4

Tundra includes a service for dropping an item from a document list (com.wm.data.IData[]): tundra.list.document:drop($list[], $index).

  • $list is the document list (com.wm.data.IData[]) you want to drop the item from
  • $index is the zero-based array index for the item to be dropped

The relevant Java code is as follows:

public static final void drop(IData pipeline) throws ServiceException {
  IDataCursor cursor = pipeline.getCursor();

  try {
    Object[] list = IDataUtil.getObjectArray(cursor, "$list");
    String index = IDataUtil.getString(cursor, "$index");

    if (index != null) IDataUtil.put(cursor, "$list", drop(list, index));
  } finally {
    cursor.destroy();
  }
}

// returns a new array which contains all the elements from the given arrays
public static <T> T[] concatenate(T[] array, T[] items) {
  if (array == null) return items;
  if (items == null) return array;

  java.util.List<T> list = new java.util.ArrayList<T>(array.length + items.length);

  java.util.Collections.addAll(list, array);
  java.util.Collections.addAll(list, items);

  return list.toArray(java.util.Arrays.copyOf(array, 0));
}

// removes the element at the given index from the given list
public static <T> T[] drop(T[] array, String index) {
  return drop(array, Integer.parseInt(index));
}

// removes the element at the given index from the given list
public static <T> T[] drop(T[] array, int index) {
  if (array != null) {
    // support reverse/tail indexing
    if (index < 0) index += array.length;
    if (index < 0 || array.length <= index) throw new ArrayIndexOutOfBoundsException(index);

    T[] head = slice(array, 0, index);
    T[] tail = slice(array, index + 1, array.length - index);

    array = concatenate(head, tail);      
  }
  return array;
}

// returns a new array which is a subset of elements from the given array
public static <T> T[] slice(T[] array, int index, int length) {
  if (array == null || array.length == 0) return array;
  // support reverse/tail length
  if (length < 0) length = array.length + length;
  // support reverse/tail indexing
  if (index < 0) index += array.length;
  // don't slice past the end of the array
  if ((length += index) > array.length) length = array.length;

  return java.util.Arrays.copyOfRange(array, index, length);
}

However, I agree with MrJames: the best and simplest approach is to create a new document list and only append the items you want to the new list using pub.list:appendToDocumentList (or tundra.list.document:append, if you're using Tundra) inside your loop step.

Community
  • 1
  • 1
Lachlan Dowding
  • 4,356
  • 1
  • 15
  • 18
3

You can use document's remove document built in service for this.

Here's the very simple example, hope this reflect the usage of the function.

  1. Loop through those documents
  2. Branch to collect indexes of unwanted documents in the loop in a mapping step
  3. Outside of the loop, use the pub.document:deleteDocuments to remove those list of documents from original document.

Hope this helps.

Koziro Kun
  • 31
  • 2
  • 1
    BE AWARE the index to remove = $iteration - 1 ! index is zero-based and $iteration starts with 1 – Niek Mar 31 '15 at 11:55
1

I've some tests and it's possible to set a particular Document item to null (using service pub.list:setListItem) but the document list will remain with the same size.

Other way, is looping the document list and append to a new document list the ones your interested in (pub.list:appendToDocumentList).

The other question about how to set a variable to null, you can use the Drop on the pipeline

PS: using webMethods 7.1.2

MrJames
  • 676
  • 2
  • 8
  • 20