Search in sources :

Example 1 with NSRange

use of com.webobjects.foundation.NSRange 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)

Example 2 with NSRange

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

the class ERXValueUtilities method dataValueWithDefault.

/**
 * Basic utility method for reading <code>NSData</code> values which also
 * works with serialized NSData. The default value is used if the object is
 * null.
 *
 * @param obj
 *            object to be evaluated
 * @param def
 *            default value if object is null
 * @return NSData evaluation of the given object
 */
public static NSData dataValueWithDefault(Object obj, NSData def) {
    NSData value = def;
    if (!ERXValueUtilities.isNull(obj)) {
        if (obj instanceof NSData) {
            value = (NSData) obj;
        } else if (obj instanceof byte[]) {
            byte[] byteValue = (byte[]) obj;
            value = new NSData(byteValue, new NSRange(0, byteValue.length), true);
        } else if (obj instanceof String) {
            String strValue = ((String) obj).trim();
            if (strValue.length() > 0) {
                // MS:
                Object objValue = NSPropertyListSerialization.propertyListFromString(strValue);
                // Encoding?
                if (objValue == null || !(objValue instanceof NSData)) {
                    throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
                }
                value = (NSData) objValue;
                if (value instanceof NSMutableData) {
                    // AK: we need NSData if we want to use it for a PK, but
                    // we get NSMutableData
                    value = new NSData(value);
                }
            }
        } else {
            throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
        }
    }
    return value;
}
Also used : NSRange(com.webobjects.foundation.NSRange) NSData(com.webobjects.foundation.NSData) NSMutableData(com.webobjects.foundation.NSMutableData)

Aggregations

NSRange (com.webobjects.foundation.NSRange)2 WOActionResults (com.webobjects.appserver.WOActionResults)1 NSArray (com.webobjects.foundation.NSArray)1 NSData (com.webobjects.foundation.NSData)1 NSMutableArray (com.webobjects.foundation.NSMutableArray)1 NSMutableData (com.webobjects.foundation.NSMutableData)1 NoSuchElementException (java.util.NoSuchElementException)1