Skip to content

Commit

Permalink
NSData-PK support, NPE fix on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Robert committed May 21, 2012
1 parent acaaa85 commit 9ebe14b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
40 changes: 20 additions & 20 deletions Frameworks/EOF/ERRest/Sources/er/rest/ERXEORestDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.webobjects.eocontrol.EOClassDescription;
import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.eocontrol.EOEnterpriseObject;
import com.webobjects.eocontrol.EOGlobalID;
import com.webobjects.eocontrol.EOTemporaryGlobalID;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSData;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation._NSUtilities;

Expand Down Expand Up @@ -61,30 +64,26 @@ public Object primaryKeyForObject(Object obj, ERXRestContext context) {
else {
EOEnterpriseObject eo = (EOEnterpriseObject) obj;
EOEditingContext editingContext = eo.editingContext();
editingContext.lock();
try {
NSDictionary pkDict = EOUtilities.primaryKeyForObject(editingContext, eo);
if (pkDict == null) {
// EOGlobalID gid = editingContext.globalIDForObject(eo);
// if (gid instanceof EOTemporaryGlobalID) {
// pkValue = _NSStringUtilities.byteArrayToHexString(((EOTemporaryGlobalID)gid)._rawBytes());
// }
// else {
// pkValue = null;
// }
pkValue = null;
if (editingContext != null) {
editingContext.lock();
try {
pkValue = ERXEOControlUtilities.primaryKeyObjectForObject(eo);
if (pkValue == null) {
EOGlobalID gid = editingContext.globalIDForObject(eo);
if (gid instanceof EOTemporaryGlobalID) {
pkValue = new NSData(((EOTemporaryGlobalID) gid)._rawBytes());
}
}
}
else if (pkDict.count() == 1) {
pkValue = pkDict.allValues().lastObject();
}
else {
pkValue = pkDict;
finally {
editingContext.unlock();
}
}
finally {
editingContext.unlock();
else {
pkValue = ERXEOControlUtilities.primaryKeyObjectForObject(eo);
}
}

return pkValue;
}

Expand All @@ -99,7 +98,8 @@ public Object objectOfEntityWithID(EOClassDescription entity, Object id, ERXRest
editingContext.lock();
Object obj;
try {
return ERXEOControlUtilities.objectWithPrimaryKeyValue(editingContext, eoEntity.name(), pkValue, null, false);
EOGlobalID gid = ERXEOControlUtilities.globalIDForString(editingContext, entity.entityName(), strPKValue);
return editingContext.faultForGlobalID(gid, editingContext);
}
finally {
editingContext.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ else if (obj instanceof Map) {
else {
clazz = obj.getClass();
}
classDescription = ERXRestClassDescriptionFactory.classDescriptionForClass(clazz, true);
classDescription = null;
if (classDescription == null && !forceNonEntity) {
classDescription = ERXRestClassDescriptionFactory.classDescriptionForClass(clazz, true);
}
if (classDescription == null && !forceNonEntity) {
classDescription = ERXRestClassDescriptionFactory.classDescriptionForEntityName(clazz.getSimpleName());
}
Expand Down
6 changes: 6 additions & 0 deletions Frameworks/EOF/ERRest/Sources/er/rest/ERXRestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ else if (value instanceof Date) {
Date date = (Date) value;
formattedValue = ERXRestUtils.dateFormat(false, context).format(value);
}
else if (value instanceof NSData && ((NSData)value).length() == 24) {
formattedValue = NSPropertyListSerialization.stringFromPropertyList(value);
}
else {
formattedValue = value.toString();
}
Expand Down Expand Up @@ -226,6 +229,9 @@ else if (valueType != null && Float.class.isAssignableFrom(valueType)) {
else if (valueType != null && Double.class.isAssignableFrom(valueType)) {
parsedValue = ERXValueUtilities.DoubleValueWithDefault(value, null);
}
else if (valueType != null && NSData.class.isAssignableFrom(valueType)) {
parsedValue = ERXValueUtilities.dataValueWithDefault(value, null);
}
else if (valueType != null && NSTimestamp.class.isAssignableFrom(valueType)) {
if (value instanceof NSTimestamp) {
parsedValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ public Object idForEO(EOEntity entity, EOEnterpriseObject eo) {

protected String idForNode(ERXRestRequestNode attributeNode) {
Object idObj = attributeNode.id();
if(idObj instanceof NSData) {
idObj = NSPropertyListSerialization.stringFromPropertyList(idObj);
}
String id = String.valueOf(idObj);
if (idObj != null && id.length() == 0) {
id = null;
Expand Down
18 changes: 18 additions & 0 deletions Frameworks/EOF/ERRest/Sources/er/rest/format/_ERXJSONConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.JsonValueProcessorMatcher;

import com.webobjects.foundation.NSData;
import com.webobjects.foundation.NSTimestamp;

import er.rest.ERXRestContext;
Expand Down Expand Up @@ -41,10 +42,27 @@ public Object processObjectValue(String s, Object obj, JsonConfig jsonconfig) {
}
}

public static class NSDataProcessor implements JsonValueProcessor {
private ERXRestContext _context;

public NSDataProcessor(ERXRestContext context) {
_context = context;
}

public Object processArrayValue(Object obj, JsonConfig jsonconfig) {
return ERXRestUtils.coerceValueToString(obj, _context);
}

public Object processObjectValue(String s, Object obj, JsonConfig jsonconfig) {
return ERXRestUtils.coerceValueToString(obj, _context);
}
}

public static JsonConfig createDefaultConfig(ERXRestContext context) {
JsonConfig config = new JsonConfig();
config.registerJsonValueProcessor(NSTimestamp.class, new NSTimestampProcessor(context));
config.registerJsonValueProcessor(Date.class, new NSTimestampProcessor(context));
config.registerJsonValueProcessor(NSData.class, new NSDataProcessor(context));
config.setJsonValueProcessorMatcher(new ERXRestValueProcessorMatcher());
return config;
}
Expand Down

0 comments on commit 9ebe14b

Please sign in to comment.