Search in sources :

Example 1 with IteratorSessionResource

use of com.hp.oo.sdk.content.plugin.IteratorSessionResource in project cs-actions by CloudSlang.

the class IteratorProcessor method init.

public void init(String list, String delim, GlobalSessionObject<Map<String, Object>> session) throws IteratorProcessorException {
    if (session.get() == null) {
        session.setResource(new IteratorSessionResource(new HashMap<String, Object>()));
    }
    Map<String, Object> sessionMap = session.get();
    if (list != null && delim != null) {
        if (delim.length() == 0) {
            throw new IteratorProcessorException("delimiter has null or 0 length");
        }
        // Get array list value
        try {
            splitList = (String[]) sessionMap.get(TEXT_ARRAYLIST);
        } catch (Exception e) {
            splitList = new String[0];
        }
        // Get String list value
        String stringList;
        try {
            stringList = (String) sessionMap.get(TEXT_STRINGLIST);
        } catch (Exception e) {
            stringList = "";
        }
        // Get index value
        try {
            index = (Integer) sessionMap.get(TEXT_INDEX);
        } catch (Exception e) {
            index = 0;
        }
        if (splitList == null) {
            splitList = new String[0];
        }
        if (stringList == null) {
            stringList = "";
        }
        if (index == 0 && initialized(session)) {
            if (list.indexOf(stringList) != 0) {
                stringList = "";
                splitList = new String[0];
            }
            sessionMap.put(TEXT_ARRAYLIST, splitList);
            sessionMap.put(TEXT_INDEX, 0);
        }
        if (!initialized(session) || !stringList.equals(list)) {
            String lastIn = list;
            int index = list.indexOf(stringList);
            if (index == 0) {
                if (list.length() == 0) {
                    throw new IteratorProcessorException("list has null or 0 length");
                }
                splitList = ListProcessor.toArray(list, delim);
            } else {
                if (initialized(session) && list.startsWith(delim)) {
                    list = list.substring(1, list.length());
                }
                String[] listarray = ListProcessor.toArray(list, delim);
                splitList = combine(this.splitList, listarray);
            }
            if (list == "" && splitList.length == 0) {
                throw new IteratorProcessorException("list has null or zero length");
            }
            // ok, now push data into context
            if (!initialized(session)) {
                sessionMap.put(TEXT_INDEX, 0);
            }
            sessionMap.put(TEXT_ARRAYLIST, splitList);
            sessionMap.put(TEXT_STRINGLIST, lastIn);
        }
    }
    // pull data from context
    splitList = (String[]) sessionMap.get(TEXT_ARRAYLIST);
    index = (Integer) sessionMap.get(TEXT_INDEX);
}
Also used : HashMap(java.util.HashMap) GlobalSessionObject(com.hp.oo.sdk.content.plugin.GlobalSessionObject) IteratorSessionResource(com.hp.oo.sdk.content.plugin.IteratorSessionResource) IteratorProcessorException(io.cloudslang.content.exceptions.IteratorProcessorException) IteratorProcessorException(io.cloudslang.content.exceptions.IteratorProcessorException)

Aggregations

GlobalSessionObject (com.hp.oo.sdk.content.plugin.GlobalSessionObject)1 IteratorSessionResource (com.hp.oo.sdk.content.plugin.IteratorSessionResource)1 IteratorProcessorException (io.cloudslang.content.exceptions.IteratorProcessorException)1 HashMap (java.util.HashMap)1