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;
}
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);
}
}
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;
}
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);
}
}
}
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;
}
Aggregations