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
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
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 droppedThe 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.
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.
Hope this helps.
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