{
assert !isLazy();
if (m_nState == NEW || m_nState == DIRTY)
{
PersistenceMapping persistenceMapping = getPersistenceMapping();
PersistenceAdapter adapter = (persistenceMapping == null) ? null :
(PersistenceAdapter)persistenceMapping.getDataSource()
.getComponent().getInstance(m_context);
ValidationException e = null;
for (int i = 0, nCount = m_metaclass.getInstanceAttributeCount(); i != nCount; ++i)
{
Attribute attribute = m_metaclass.getInstanceAttribute(i);
Object value = getValueDirect(i);
if (value instanceof Undefined)
{
if ((m_nState == NEW || value == Invalid.VALUE) &&
(attribute.isPersistent() ||
(attribute.isRequired() ||
attribute.getValidationFunction() != null ||
m_nState == NEW && attribute.getInitializer() != Undefined.VALUE ||
attribute.isConstrained() && attribute.getEnumeration() != attribute.getType()) && bFull))
{
value = getValue(i);
}
else
{
continue;
}
}
if (m_nState == DIRTY && !isDirty(i))
{
continue;
}
Type type = attribute.getType();
if (attribute.isRequired())
{
boolean bMissing = (value == null);
if (!bMissing)
{
if (type.isPrimitive())
{
bMissing = (type == Primitive.STRING && value.equals(""));
}
else
{
bMissing = (attribute.isCollection() && ((InstanceList)value).isEmpty());
}
}
if (bMissing)
{
e = createValidationException(e);
ValidationException x = new ValidationException("err.validation.requiredAttribute",
new Object[]{new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});
x.setClassName(m_metaclass.getName());
x.setOIDHolder(this);
e.addException(attribute.getName(), x);
}
}
if (attribute.getMaxLength() > 0)
{
int nLength = 0;
if (value instanceof String)
{
nLength = ((String)value).length();
}
else if (value instanceof Binary)
{
nLength = ((Binary)value).getData().length;
}
if (nLength > attribute.getMaxLength())
{
e = createValidationException(e);
ValidationException x = new ValidationException("err.validation.maxDataLength",
new Object[]{Primitive.createInteger(attribute.getMaxLength()),
new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});
x.setClassName(m_metaclass.getName());
x.setOIDHolder(this);
e.addException(attribute.getName(), x);
}
}
if (bFull)
{
if (attribute.isConstrained() &&
attribute.getEnumeration() != type &&
value != null && e == null)
{
if (((InstanceList)attribute.getEnumeration().invoke("read",
new Object[]{null, (type.isPrimitive()) ?
Pair.attribute(Metaclass.ENUMERATION_VALUE).eq(value) :
new Pair(Symbol.AT).eq(value), null, Primitive.createInteger(-1),
Primitive.ZERO_INTEGER, Boolean.FALSE})).isEmpty())
{
e = createValidationException(e);
ValidationException x = new ValidationException("err.validation.enumerationValue",
new Object[]{new StringId(attribute.getEnumeration().getCaption()),
new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});
x.setClassName(m_metaclass.getName());
x.setOIDHolder(this);
e.addException(attribute.getName(), x);
}
}
if (attribute.getValidationFunction() != null && e == null)
{
Object result = m_context.getMachine().invoke(attribute.getValidationFunction(), this, value, null);
if (result != null)
{
ValidationException x = null;
if (result instanceof Boolean)
{
if (!((Boolean)result).booleanValue())
{
x = new ValidationException("err.validation.valueRange",
new Object[]{new StringId(attribute.getCaption()), new StringId(m_metaclass.getCaption())});
}
}
else if (result instanceof String)
{
x = new ValidationException((String)result);
}
else if (result instanceof Pair)
{
x = new ValidationException((String)((Pair)result).getHead(),
Pair.toArray(((Pair)result).getNext()));
}
if (x != null)
{
x.setClassName(m_metaclass.getName());
x.setOIDHolder(this);
e = createValidationException(e);
e.addException(attribute.getName(), x);
}
}
}
}
if (type.isPrimitive() && attribute.isPersistent() && adapter != null)
{
try
{
adapter.validate(persistenceMapping.getAttributeMapping(attribute), value);
}
catch (ValidationException x)
{
x.setClassName(m_metaclass.getName());
x.setOIDHolder(this);