use of com.orientechnologies.orient.core.metadata.schema.OProperty in project wicket-orientdb by OrienteerBAP.
the class TestPrototypers method testOIndexPrototyper.
@Test
public void testOIndexPrototyper() throws Exception {
OClass newClass = wicket.getTester().getSchema().createClass("NewClass");
OProperty property = newClass.createProperty("name", OType.STRING);
OIndex<?> newIndex = OIndexPrototyper.newPrototype("NewClass", Arrays.asList("name"));
assertTrue(property.getAllIndexes().size() == 0);
PropertyResolver.setValue("type", newIndex, "notunique", null);
assertNotNull(newIndex.getDefinition());
assertTrue(newIndex.getDefinition().getFields().contains("name"));
assertTrue(newIndex instanceof IPrototype);
OIndex<?> realizedNewIndex = ((IPrototype<OIndex<?>>) newIndex).realizePrototype();
assertEquals(1, property.getAllIndexes().size());
assertEquals(1, newClass.getIndexes().size());
property = newClass.createProperty("description", OType.STRING);
newIndex = OIndexPrototyper.newPrototype("NewClass", Arrays.asList("description"));
PropertyResolver.setValue("type", newIndex, "notunique", null);
assertEquals(0, property.getAllIndexes().size());
PropertyResolver.setValue("algorithm", newIndex, ODefaultIndexFactory.SBTREE_ALGORITHM, null);
ODocument metadata = new ODocument();
metadata.field("test", "test123", OType.STRING);
PropertyResolver.setValue("metadata", newIndex, metadata, null);
realizedNewIndex = ((IPrototype<OIndex<?>>) newIndex).realizePrototype();
assertEquals(1, property.getAllIndexes().size());
assertEquals(2, newClass.getIndexes().size());
assertEquals("test123", realizedNewIndex.getMetadata().field("test"));
wicket.getTester().getSchema().dropClass(newClass.getName());
}
use of com.orientechnologies.orient.core.metadata.schema.OProperty in project wicket-orientdb by OrienteerBAP.
the class TestPrototypers method testOPropertyPrototyper.
@Test
public void testOPropertyPrototyper() throws Exception {
OClass newClass = wicket.getTester().getSchema().createClass("NewClass");
OProperty toCompare = newClass.createProperty("toCompare", OType.STRING);
try {
OProperty newProperty = OPropertyPrototyper.newPrototype("NewClass");
assertNull(newClass.getProperty("newProperty"));
newProperty.setName("newProperty");
assertEquals("newProperty", newProperty.getName());
assertNull(newClass.getProperty("newProperty"));
assertEquals("NewClass.newProperty", newProperty.getFullName());
newProperty.setType(OType.STRING);
assertEquals(OType.STRING, newProperty.getType());
newProperty.setCustom("myCustom", "myCustomValue");
assertEquals("myCustomValue", newProperty.getCustom("myCustom"));
assertTrue(newProperty.compareTo(toCompare) < 0);
// Realization
assertTrue(newProperty instanceof IPrototype);
OProperty realizedNewProperty = ((IPrototype<OProperty>) newProperty).realizePrototype();
assertEquals(newClass.getProperty("newProperty"), realizedNewProperty);
assertEquals("myCustomValue", realizedNewProperty.getCustom("myCustom"));
} finally {
// Drop
wicket.getTester().getSchema().dropClass(newClass.getName());
}
}
use of com.orientechnologies.orient.core.metadata.schema.OProperty in project wicket-orientdb by OrienteerBAP.
the class OPropertyPrototyper method handleCustom.
@Override
protected Object handleCustom(Object proxy, Method method, Object[] args) {
String methodName = method.getName();
if ("compareTo".equals(methodName)) {
OProperty otherProperty = (OProperty) args[0];
String thisName = (String) values.get(NAME);
return thisName != null ? thisName.compareTo(otherProperty.getName()) : 1;
} else {
return super.handleCustom(proxy, method, args);
}
}
use of com.orientechnologies.orient.core.metadata.schema.OProperty in project wicket-orientdb by OrienteerBAP.
the class OPropertyValueValidator method validate.
@Override
public void validate(IValidatable<T> validatable) {
T fieldValue = validatable.getValue();
OProperty p = getProperty();
if (fieldValue == null) {
if (p.isNotNull()) {
validatable.error(newValidationError("required"));
} else if (p.isMandatory() && Strings.isEmpty(p.getDefaultValue())) {
ODocument doc = getDocument();
// If doc is not defined: lets assume that mandatory fields must be not null
if (doc == null || !doc.containsField(p.getName())) {
validatable.error(newValidationError("mandatory"));
}
}
} else {
OType type = p.getType();
switch(type) {
case LINK:
validateLink(validatable, p, fieldValue);
break;
case LINKLIST:
if (!(fieldValue instanceof List))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null)
for (Object item : ((List<?>) fieldValue)) validateLink(validatable, p, item);
break;
case LINKSET:
if (!(fieldValue instanceof Collection))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null)
for (Object item : ((Collection<?>) fieldValue)) validateLink(validatable, p, item);
break;
case LINKMAP:
if (!(fieldValue instanceof Map))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null)
for (Entry<?, ?> entry : ((Map<?, ?>) fieldValue).entrySet()) validateLink(validatable, p, entry.getValue());
break;
case EMBEDDED:
validateEmbedded(validatable, p, fieldValue);
break;
case EMBEDDEDLIST:
if (!(fieldValue instanceof List))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null) {
for (Object item : ((List<?>) fieldValue)) validateEmbedded(validatable, p, item);
} else if (p.getLinkedType() != null) {
for (Object item : ((List<?>) fieldValue)) validateType(validatable, p, item);
}
break;
case EMBEDDEDSET:
if (!(fieldValue instanceof Set))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null) {
for (Object item : ((Set<?>) fieldValue)) validateEmbedded(validatable, p, item);
} else if (p.getLinkedType() != null) {
for (Object item : ((Set<?>) fieldValue)) validateType(validatable, p, item);
}
break;
case EMBEDDEDMAP:
if (!(fieldValue instanceof Map))
validatable.error(newValidationError("wrongtype"));
else if (p.getLinkedClass() != null) {
for (Entry<?, ?> entry : ((Map<?, ?>) fieldValue).entrySet()) validateEmbedded(validatable, p, entry.getValue());
} else if (p.getLinkedType() != null) {
for (Entry<?, ?> entry : ((Map<?, ?>) fieldValue).entrySet()) validateType(validatable, p, entry.getValue());
}
break;
default:
break;
}
if (p.getMin() != null) {
// MIN
final String min = p.getMin();
if (p.getType().equals(OType.STRING) && (fieldValue != null && ((String) fieldValue).length() < Integer.parseInt(min)))
validatable.error(newValidationError("minviolationString", "min", min));
else if (p.getType().equals(OType.BINARY) && (fieldValue != null && ((byte[]) fieldValue).length < Integer.parseInt(min)))
validatable.error(newValidationError("minviolationBin", "min", min));
else if (p.getType().equals(OType.INTEGER) && (fieldValue != null && type.asInt(fieldValue) < Integer.parseInt(min)))
validatable.error(newValidationError("minviolation", "min", min));
else if (p.getType().equals(OType.LONG) && (fieldValue != null && type.asLong(fieldValue) < Long.parseLong(min)))
validatable.error(newValidationError("minviolation", "min", min));
else if (p.getType().equals(OType.FLOAT) && (fieldValue != null && type.asFloat(fieldValue) < Float.parseFloat(min)))
validatable.error(newValidationError("minviolation", "min", min));
else if (p.getType().equals(OType.DOUBLE) && (fieldValue != null && type.asDouble(fieldValue) < Double.parseDouble(min)))
validatable.error(newValidationError("minviolation", "min", min));
else if (p.getType().equals(OType.DATE)) {
try {
if (fieldValue != null && ((Date) fieldValue).before(getDatabase().getStorage().getConfiguration().getDateFormatInstance().parse(min)))
validatable.error(newValidationError("minviolationDate", "min", min));
} catch (ParseException e) {
/*NOP*/
}
} else if (p.getType().equals(OType.DATETIME)) {
try {
if (fieldValue != null && ((Date) fieldValue).before(getDatabase().getStorage().getConfiguration().getDateTimeFormatInstance().parse(min)))
validatable.error(newValidationError("minviolationDate", "min", min));
} catch (ParseException e) {
/*NOP*/
}
} else if ((p.getType().equals(OType.EMBEDDEDLIST) || p.getType().equals(OType.EMBEDDEDSET) || p.getType().equals(OType.LINKLIST) || p.getType().equals(OType.LINKSET)) && (fieldValue != null && ((Collection<?>) fieldValue).size() < Integer.parseInt(min)))
validatable.error(newValidationError("minviolationCollection", "min", min));
}
if (p.getMax() != null) {
// MAX
final String max = p.getMax();
if (p.getType().equals(OType.STRING) && (fieldValue != null && ((String) fieldValue).length() > Integer.parseInt(max)))
validatable.error(newValidationError("maxviolationString", "max", max));
else if (p.getType().equals(OType.BINARY) && (fieldValue != null && ((byte[]) fieldValue).length > Integer.parseInt(max)))
validatable.error(newValidationError("maxviolationBin", "max", max));
else if (p.getType().equals(OType.INTEGER) && (fieldValue != null && type.asInt(fieldValue) > Integer.parseInt(max)))
validatable.error(newValidationError("maxviolation", "max", max));
else if (p.getType().equals(OType.LONG) && (fieldValue != null && type.asLong(fieldValue) > Long.parseLong(max)))
validatable.error(newValidationError("maxviolation", "max", max));
else if (p.getType().equals(OType.FLOAT) && (fieldValue != null && type.asFloat(fieldValue) > Float.parseFloat(max)))
validatable.error(newValidationError("maxviolation", "max", max));
else if (p.getType().equals(OType.DOUBLE) && (fieldValue != null && type.asDouble(fieldValue) > Double.parseDouble(max)))
validatable.error(newValidationError("maxviolation", "max", max));
else if (p.getType().equals(OType.DATE)) {
try {
if (fieldValue != null && ((Date) fieldValue).before(getDatabase().getStorage().getConfiguration().getDateFormatInstance().parse(max)))
validatable.error(newValidationError("maxviolationDate", "max", max));
} catch (ParseException e) {
/*NOP*/
}
} else if (p.getType().equals(OType.DATETIME)) {
try {
if (fieldValue != null && ((Date) fieldValue).before(getDatabase().getStorage().getConfiguration().getDateTimeFormatInstance().parse(max)))
validatable.error(newValidationError("maxviolationDate", "max", max));
} catch (ParseException e) {
/*NOP*/
}
} else if ((p.getType().equals(OType.EMBEDDEDLIST) || p.getType().equals(OType.EMBEDDEDSET) || p.getType().equals(OType.LINKLIST) || p.getType().equals(OType.LINKSET)) && (fieldValue != null && ((Collection<?>) fieldValue).size() > Integer.parseInt(max)))
validatable.error(newValidationError("maxviolationCollection", "max", max));
}
if (p.getRegexp() != null && fieldValue != null) {
String stringFieldValue = fieldValue.toString();
if (!stringFieldValue.matches(p.getRegexp())) {
validatable.error(newValidationError("regexpMismatch", "regexp", p.getRegexp()));
}
}
}
}
use of com.orientechnologies.orient.core.metadata.schema.OProperty in project wicket-orientdb by OrienteerBAP.
the class DynamicPropertyValueModel method load.
@SuppressWarnings("unchecked")
@Override
protected T load() {
ODocument doc = docModel.getObject();
OProperty prop = propertyModel != null ? propertyModel.getObject() : null;
if (doc == null)
return null;
if (prop == null)
return (T) doc;
if (valueType == null) {
return (T) doc.field(prop.getName());
} else {
Object ret = doc.field(prop.getName(), valueType);
if (ORecord.class.isAssignableFrom(valueType) && ret instanceof ORID) {
ret = ((ORID) ret).getRecord();
}
return (T) ret;
}
}
Aggregations