iRequest.data.commandInfo = "Studio edit document";
if (rid == null)
throw new IllegalArgumentException("Record ID not found in request");
ODocument doc = new ODocument(db, className, new ORecordId(rid));
doc = (ODocument) doc.load();
// BIND ALL CHANGED FIELDS
Object oldValue;
Object newValue;
for (Entry<String, String> f : fields.entrySet()) {
oldValue = doc.field(f.getKey());
newValue = f.getValue();
if (oldValue != null) {
if (oldValue instanceof ORecord<?>) {
ORecord<?> rec = (ORecord<?>) oldValue;
String parsedRid = f.getValue();
if (parsedRid != null && parsedRid.charAt(0) == '#')
parsedRid = parsedRid.substring(1);
if (!rec.getIdentity().toString().equals(parsedRid)) {
// CHANGED RID
rec.reset();
((ORecordId) rec.getIdentity()).fromString(parsedRid);
// RELOAD TO ASSURE IT EXISTS
rec.load();
}
newValue = oldValue;
} else if (oldValue instanceof Collection<?>) {
newValue = new ArrayList<ODocument>();
if (f.getValue() != null) {
String[] items = f.getValue().split(",");
for (String s : items) {
((List<ODocument>) newValue).add(new ODocument(db, new ORecordId(s)));
}
}
}
}
doc.field(f.getKey(), newValue);
}
doc.save();
sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, "Record " + rid
+ " updated successfully.");
} else if ("add".equals(operation)) {
iRequest.data.commandInfo = "Studio create document";
final ODocument doc = new ODocument(db, className);
// BIND ALL CHANGED FIELDS
for (Entry<String, String> f : fields.entrySet())
doc.field(f.getKey(), f.getValue());
doc.save();
sendTextContent(iRequest, 201, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, "Record " + doc.getIdentity()
+ " updated successfully.");
} else if ("del".equals(operation)) {
iRequest.data.commandInfo = "Studio delete document";
if (rid == null)
throw new IllegalArgumentException("Record ID not found in request");
final ODocument doc = new ODocument(db, new ORecordId(rid));
doc.load();
doc.delete();
sendTextContent(iRequest, OHttpUtils.STATUS_OK_CODE, "OK", null, OHttpUtils.CONTENT_TEXT_PLAIN, "Record " + rid
+ " deleted successfully.");