Skip to content

Commit

Permalink
Default values prepopulation. Issue orientechnologies#4535
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Jul 11, 2015
1 parent 48f7c7e commit 9f26a5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class ODocument extends ORecordAbstract implements Iterable<Entry<String,
private String _className;
private OImmutableClass _immutableClazz;
private int _immutableSchemaVersion = 1;
private boolean _default_values_prepopulated = false;

/**
* Internal constructor used on unmarshalling.
Expand Down Expand Up @@ -175,7 +176,6 @@ public ODocument(final String iClassName) {
* OClass instance
*/
public ODocument(final OClass iClass) {
setup();

if (iClass == null)
_className = null;
Expand All @@ -184,6 +184,7 @@ public ODocument(final OClass iClass) {

_immutableClazz = null;
_immutableSchemaVersion = -1;
setup();
}

/**
Expand Down Expand Up @@ -221,6 +222,21 @@ public ODocument(final String iFieldName, final Object iFieldValue, final Object
this(iFields);
field(iFieldName, iFieldValue);
}

public ODocument populateDefaultValues() {
final OImmutableClass immutableSchemaClass = getImmutableSchemaClass();
if (immutableSchemaClass != null) {
for (OProperty p : immutableSchemaClass.properties()) {
String defValue = p.getDefaultValue();
if (defValue != null && defValue.length() > 0 && !containsField(p.getName())) {
Object curFieldValue = OSQLHelper.parseDefaultValue(this, defValue);
Object fieldValue = ODocumentHelper.convertField(this, p.getName(), p.getType().getDefaultJavaType(), curFieldValue);
rawField(p.getName(), fieldValue, p.getType());
}
}
}
return this;
}

protected static void validateField(ODocument iRecord, OImmutableProperty p) throws OValidationException {
final Object fieldValue;
Expand All @@ -241,17 +257,10 @@ protected static void validateField(ODocument iRecord, OImmutableProperty p) thr
}

} else {
String defValue = p.getDefaultValue();
if (defValue != null && defValue.length() > 0) {
Object curFieldValue = OSQLHelper.parseDefaultValue(iRecord, defValue);
fieldValue = ODocumentHelper.convertField(iRecord, p.getName(), p.getType().getDefaultJavaType(), curFieldValue);
iRecord.rawField(p.getName(), fieldValue, p.getType());
} else {
if (p.isMandatory()) {
throw new OValidationException("The field '" + p.getFullName() + "' is mandatory, but not found on record: " + iRecord);
}
fieldValue = null;
}
}

final OType type = p.getType();
Expand Down Expand Up @@ -2341,6 +2350,10 @@ protected void setup() {
if (_recordFormat == null)
// GET THE DEFAULT ONE
_recordFormat = ODatabaseDocumentTx.getDefaultSerializer();
if(!_default_values_prepopulated) {
populateDefaultValues();
_default_values_prepopulated=true;
}
}

protected String checkFieldName(final String iFieldName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.orientechnologies.orient.test.database.auto;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.*;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -102,4 +102,30 @@ public void testDefaultValueConvertion() {
database.drop();
}
}

@Test
public void testPrepopulation() throws Exception {
final ODatabaseDocumentTx database = new ODatabaseDocumentTx("memory:defaultValues");
try {

database.create();

// create example schema
OSchema schema = database.getMetadata().getSchema();
OClass classA = schema.createClass("ClassA");

classA.createProperty("name", OType.STRING).setDefaultValue("default name");
classA.createProperty("date", OType.DATETIME).setDefaultValue("sysdate()");
classA.createProperty("active", OType.BOOLEAN).setDefaultValue("true");

ODocument doc = new ODocument(classA);
assertEquals("default name", doc.field("name"));
assertNotNull(doc.field("date"));
assertEquals(true, doc.field("active"));


} finally {
database.drop();
}
}
}

2 comments on commit 9f26a5a

@lvca
Copy link

@lvca lvca commented on 9f26a5a Jul 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to the populateDefaultValues() should be done also on setClass() and setClassname() right?

@PhantomYdn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was that once document created we can do with the object what ever we want, but fields values remains the same. Other wise there are lots of additional questions like: what to do with the a prepopulated field if super class was changed? Clean it or keep it? What to do if new class has the same property by with some default value? And so on.

Please sign in to comment.