Skip to content

Commit

Permalink
fixed issue with default value on mandatory, readonly fields from htt…
Browse files Browse the repository at this point in the history
…p/json API, issue #7211
  • Loading branch information
tglman committed Mar 27, 2017
1 parent 2c5431f commit fdf5cc6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public ORecord fromString(String iSource, ORecord iRecord, final String[] iField
public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, final String iOptions, boolean needReload) {
iSource = unwrapSource(iSource);

String className = null;
boolean noMap = false;
if (iOptions != null) {
final String[] format = iOptions.split(",");
Expand Down Expand Up @@ -216,8 +217,9 @@ public ORecord fromString(String iSource, ORecord iRecord, final String[] iField
iRecord = localRecord;
}
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS) && iRecord instanceof ODocument) {
ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord),
"null".equals(fieldValueAsString) ? null : fieldValueAsString);
className = "null".equals(fieldValueAsString) ? null : fieldValueAsString;
ODocumentInternal.fillClassNameIfNeeded(((ODocument) iRecord),className);

}
}

Expand All @@ -235,7 +237,9 @@ public ORecord fromString(String iSource, ORecord iRecord, final String[] iField
ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION))
ORecordInternal.setVersion(iRecord, Integer.parseInt(fieldValue));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS)) {
continue;
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
continue;
} else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
continue;
Expand Down Expand Up @@ -284,8 +288,8 @@ else if (iRecord instanceof OBlob) {
continue;
}
} else if (v instanceof ODocument && type != null && type.isLink()) {
String className = ((ODocument) v).getClassName();
if (className != null && className.length() > 0)
String className1 = ((ODocument) v).getClassName();
if (className1 != null && className1.length() > 0)
((ODocument) v).save();
}

Expand All @@ -307,6 +311,10 @@ else if (iRecord instanceof OBlob) {
}

}
if (className != null) {
//Trigger the default value
((ODocument) iRecord).setClassName(className);
}
} catch (Exception e) {
if (iRecord.getIdentity().isValid())
throw OException.wrapException(
Expand All @@ -315,6 +323,7 @@ else if (iRecord instanceof OBlob) {
throw OException.wrapException(new OSerializationException("Error on unmarshalling JSON content for record: " + iSource),
e);
}

}

return iRecord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public void testDefaultValueDate() {
OProperty some = classA.createProperty("id", OType.STRING);
some.setDefaultValue("uuid()");

System.out.println(prop.getDefaultValue());

ODocument doc = new ODocument(classA);
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
Expand All @@ -82,4 +80,94 @@ public void testDefaultValueDate() {

}

@Test
public void testDefaultValueFromJson() {
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

OProperty prop = classA.createProperty("date", OType.DATE);
prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));

ODocument doc = new ODocument().fromJSON("{'@class':'ClassA','other':'other'}");
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
assertTrue(saved.field("date") instanceof Date);
assertNotNull(saved.field("other"));
}

@Test
public void testDefaultValueProvidedFromJson() {
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

OProperty prop = classA.createProperty("date", OType.DATETIME);
prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));

String value1 = ODateHelper.getDateTimeFormatInstance().format(new Date());
ODocument doc = new ODocument().fromJSON("{'@class':'ClassA','date':'" + value1 + "','other':'other'}");
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
assertEquals(ODateHelper.getDateTimeFormatInstance().format(saved.field("date")), value1);
assertNotNull(saved.field("other"));
}

@Test
public void testDefaultValueMandatoryReadonlyFromJson() {
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

OProperty prop = classA.createProperty("date", OType.DATE);
prop.setMandatory(true);
prop.setReadonly(true);
prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));

ODocument doc = new ODocument().fromJSON("{'@class':'ClassA','other':'other'}");
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
assertTrue(saved.field("date") instanceof Date);
assertNotNull(saved.field("other"));
}

@Test
public void testDefaultValueProvidedMandatoryReadonlyFromJson() {
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

OProperty prop = classA.createProperty("date", OType.DATETIME);
prop.setMandatory(true);
prop.setReadonly(true);
prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));

String value1 = ODateHelper.getDateTimeFormatInstance().format(new Date());
ODocument doc = new ODocument().fromJSON("{'@class':'ClassA','date':'" + value1 + "','other':'other'}");
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
assertEquals(ODateHelper.getDateTimeFormatInstance().format(saved.field("date")), value1);
assertNotNull(saved.field("other"));
}

@Test
public void testDefaultValueUpdateMandatoryReadonlyFromJson() {
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

OProperty prop = classA.createProperty("date", OType.DATETIME);
prop.setMandatory(true);
prop.setReadonly(true);
prop.setDefaultValue(ODateHelper.getDateTimeFormatInstance().format(new Date()));

ODocument doc = new ODocument().fromJSON("{'@class':'ClassA','other':'other'}");
ODocument saved = database.save(doc);
assertNotNull(saved.field("date"));
assertTrue(saved.field("date") instanceof Date);
assertNotNull(saved.field("other"));
String val = ODateHelper.getDateTimeFormatInstance().format(doc.field("date"));
ODocument doc1 = new ODocument().fromJSON("{'@class':'ClassA','date':'" + val + "','other':'other1'}");
saved.merge(doc1, true, true);
saved = database.save(saved);
assertNotNull(saved.field("date"));
assertEquals(ODateHelper.getDateTimeFormatInstance().format(saved.field("date")), val);
assertEquals(saved.field("other"), "other1");
}

}

0 comments on commit fdf5cc6

Please sign in to comment.