Search in sources :

Example 26 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class NSArraySerializer method tryUnmarshall.

// TODO: try unMarshall and unMarshall share 90% code. Put in into an
// intermediate function.
// TODO: Also cache the result somehow so that an unmarshall
// following a tryUnmarshall doesn't do the same work twice!
public ObjectMatch tryUnmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    String java_class;
    try {
        java_class = jso.getString("javaClass");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }
    Class klass;
    try {
        klass = Class.forName(java_class);
    } catch (ClassNotFoundException cnfe) {
        throw new UnmarshallException("Could not find class named: " + java_class);
    }
    if (!NSArray.class.isAssignableFrom(klass)) {
        throw new UnmarshallException("not an NSArray");
    }
    JSONArray jsonNSArray;
    try {
        jsonNSArray = jso.getJSONArray("nsarray");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read nsarray: " + e.getMessage(), e);
    }
    if (jsonNSArray == null) {
        throw new UnmarshallException("nsarray missing");
    }
    int i = 0;
    ObjectMatch m = new ObjectMatch(-1);
    state.setSerialized(o, m);
    try {
        for (; i < jsonNSArray.length(); i++) {
            m.setMismatch(ser.tryUnmarshall(state, null, jsonNSArray.get(i)).max(m).getMismatch());
        }
    } catch (UnmarshallException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    } catch (JSONException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    }
    return m;
}
Also used : JSONObject(org.json.JSONObject) NSArray(com.webobjects.foundation.NSArray) ObjectMatch(org.jabsorb.serializer.ObjectMatch) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 27 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class NSArraySerializer method unmarshall.

public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
    JSONObject jso = (JSONObject) o;
    String java_class;
    try {
        java_class = jso.getString("javaClass");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }
    NSMutableArray al = new NSMutableArray();
    boolean immutableClone = true;
    Class klass;
    try {
        klass = Class.forName(java_class);
    } catch (ClassNotFoundException cnfe) {
        throw new UnmarshallException("Could not find class named: " + java_class);
    }
    if (NSMutableArray.class.isAssignableFrom(klass)) {
        immutableClone = false;
    } else if (!NSArray.class.isAssignableFrom(klass)) {
        throw new UnmarshallException("not an NSArray");
    }
    JSONArray jsonNSArray;
    try {
        jsonNSArray = jso.getJSONArray("nsarray");
    } catch (JSONException e) {
        throw new UnmarshallException("Could not read nsarray: " + e.getMessage(), e);
    }
    if (jsonNSArray == null) {
        throw new UnmarshallException("nsarray missing");
    }
    int i = 0;
    try {
        for (; i < jsonNSArray.length(); i++) {
            Object obj = ser.unmarshall(state, null, jsonNSArray.get(i));
            if (obj != null) {
                al.addObject(obj);
            } else {
                al.addObject(NSKeyValueCoding.NullValue);
            }
        }
        NSArray finalArray = al;
        if (immutableClone) {
            finalArray = al.immutableClone();
        }
        state.setSerialized(o, finalArray);
        return finalArray;
    } catch (UnmarshallException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    } catch (JSONException e) {
        throw new UnmarshallException("element " + i + " " + e.getMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 28 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class AjaxSelectionList method list.

public NSArray list() {
    NSArray list = (NSArray) valueForBinding("list");
    if (_list == null || !_list.equals(list)) {
        _list = list;
        if (!ERXComponentUtilities.booleanValueForBinding(this, "mandatory", true)) {
            NSMutableArray optionList = _list.mutableClone();
            optionList.insertObjectAtIndex(NSKeyValueCoding.NullValue, 0);
            _list = optionList;
        }
    }
    return _list;
}
Also used : NSArray(com.webobjects.foundation.NSArray) NSMutableArray(com.webobjects.foundation.NSMutableArray)

Example 29 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class AjaxSelectionList method takeValuesFromRequest.

@Override
public void takeValuesFromRequest(WORequest request, WOContext context) {
    super.takeValuesFromRequest(request, context);
    if (context.wasFormSubmitted()) {
        if (_value == null) {
            setSelection(null);
        } else {
            int index = Integer.parseInt(_value);
            NSArray list = list();
            Object selection = null;
            if (index >= 0 && index < list.count()) {
                selection = list.objectAtIndex(index);
            }
            if (selection instanceof NSKeyValueCoding.Null || selection == null) {
                selection = null;
            }
            setSelection(selection);
        }
    }
}
Also used : NSArray(com.webobjects.foundation.NSArray) NSKeyValueCoding(com.webobjects.foundation.NSKeyValueCoding)

Example 30 with NSArray

use of com.webobjects.foundation.NSArray in project wonder-slim by undur.

the class AjaxSortableList method handleRequest.

@SuppressWarnings("unchecked")
@Override
public WOActionResults handleRequest(WORequest request, WOContext context) {
    if (!canGetValueForBinding("list")) {
        throw new IllegalArgumentException("You must specify a readable 'list'.");
    }
    if (!canGetValueForBinding("listItemIDKeyPath")) {
        throw new IllegalArgumentException("You must specify 'listItemIDKeyPath' if you specify 'list'.");
    }
    String listItemIDKeyPath = (String) valueForBinding("listItemIDKeyPath");
    Object listItemIDArrayObj = request.formValues().objectForKey(_sortOrderKeyName + "[]");
    NSArray<String> listItemIDArray;
    if (listItemIDArrayObj instanceof NSArray) {
        listItemIDArray = (NSArray<String>) listItemIDArrayObj;
    } else if (listItemIDArrayObj instanceof String) {
        String listItemIDStr = (String) listItemIDArrayObj;
        listItemIDArray = new NSArray<>(listItemIDStr);
    } else {
        throw new IllegalArgumentException("Unknown list item ID array " + listItemIDArrayObj);
    }
    NSArray<Object> list = (NSArray<Object>) valueForBinding("list");
    boolean mutableList = (list instanceof NSMutableArray);
    NSMutableArray<Object> reorderedList;
    if (mutableList) {
        reorderedList = (NSMutableArray<Object>) list;
    } else {
        reorderedList = new NSMutableArray<>();
    }
    int startIndex = 0;
    // If we're starting at an index > 0, add the initial objects
    if (canGetValueForBinding("startIndex")) {
        Number startIndexNumber = (Number) valueForBinding("startIndex");
        startIndex = startIndexNumber.intValue();
        if (!mutableList) {
            for (int i = 0; i < startIndex; i++) {
                reorderedList.addObject(list.objectAtIndex(i));
            }
        }
    }
    // Add the reordered objects
    int listItemIDCount = listItemIDArray.count();
    for (int listItemIDIndex = 0; listItemIDIndex < listItemIDCount; listItemIDIndex++) {
        String itemID = (String) listItemIDArray.objectAtIndex(listItemIDIndex);
        NSRange itemPageRange;
        if (mutableList) {
            itemPageRange = new NSRange(startIndex + listItemIDIndex, listItemIDCount - listItemIDIndex);
        } else {
            itemPageRange = new NSRange(startIndex, listItemIDCount);
        }
        NSArray<Object> itemPageArray = list.subarrayWithRange(itemPageRange);
        try {
            // FIXME: This is disabled because we no longer have EOControl (and thus not EOQualifier) // Hugi 2021-11-13
            throw new RuntimeException("Fisabled forJavaEOControl reasons");
        } catch (Exception e) {
            e.printStackTrace();
        }
        // EOQualifier itemIDQualifier = new EOKeyValueQualifier(listItemIDKeyPath, EOQualifier.QualifierOperatorEqual, itemID);
        // NSArray<Object> matchingItems = EOQualifier.filteredArrayWithQualifier(itemPageArray, itemIDQualifier);
        NSArray<Object> matchingItems = null;
        if (matchingItems.count() == 0) {
            throw new NoSuchElementException("There was no item that matched the ID '" + itemID + "' in " + list + ".");
        } else if (matchingItems.count() > 1) {
            throw new IllegalStateException("There was more than one item that matched the ID '" + itemID + "' in " + list + ".");
        }
        Object replacingItem = matchingItems.objectAtIndex(0);
        if (mutableList) {
            int replacedItemIndex = itemPageRange.location();
            Object replacedItem = reorderedList.objectAtIndex(replacedItemIndex);
            if (replacedItem != replacingItem) {
                int replacingItemIndex = replacedItemIndex + itemPageArray.indexOfObject(replacingItem);
                reorderedList.replaceObjectAtIndex(replacingItem, replacedItemIndex);
                reorderedList.replaceObjectAtIndex(replacedItem, replacingItemIndex);
            }
        } else {
            reorderedList.addObject(replacingItem);
        }
    }
    // If we're just looking at a page, add all the objects AFTER the page
    if (!mutableList) {
        int listCount = list.count();
        for (int i = startIndex + reorderedList.count(); i < listCount; i++) {
            reorderedList.addObject(list.objectAtIndex(i));
        }
        setValueForBinding(reorderedList, "list");
    }
    if (canGetValueForBinding("action")) {
        WOActionResults results = (WOActionResults) valueForBinding("action");
        if (results != null) {
            System.out.println("AjaxDroppable.handleRequest: Not quite sure what to do with non-null results yet ...");
        }
    }
    return null;
}
Also used : NSRange(com.webobjects.foundation.NSRange) WOActionResults(com.webobjects.appserver.WOActionResults) NSArray(com.webobjects.foundation.NSArray) NoSuchElementException(java.util.NoSuchElementException) NSMutableArray(com.webobjects.foundation.NSMutableArray) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NSArray (com.webobjects.foundation.NSArray)53 Enumeration (java.util.Enumeration)17 NSMutableArray (com.webobjects.foundation.NSMutableArray)13 NSBundle (com.webobjects.foundation.NSBundle)5 NSMutableDictionary (com.webobjects.foundation.NSMutableDictionary)5 File (java.io.File)4 List (java.util.List)4 WOAssociation (com.webobjects.appserver.WOAssociation)3 WOComponent (com.webobjects.appserver.WOComponent)3 NSDictionary (com.webobjects.foundation.NSDictionary)3 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 WOElement (com.webobjects.appserver.WOElement)2 WOResponse (com.webobjects.appserver.WOResponse)2 WOConstantValueAssociation (com.webobjects.appserver._private.WOConstantValueAssociation)2 EOEvent (com.webobjects.eocontrol.EOEvent)2 EOSortOrdering (com.webobjects.eocontrol.EOSortOrdering)2 NSForwardException (com.webobjects.foundation.NSForwardException)2 NSKeyValueCoding (com.webobjects.foundation.NSKeyValueCoding)2