use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.
the class ModelValidator method getFieldsValidationList.
/**
* Computes and returns a collection of ValidationComponents
* representing the field and relationship tests to be performed.
* @return a collection of ValidationComponents representing the
* field and relationship tests to be performed.
*/
private Collection getFieldsValidationList() {
ArrayList list = new ArrayList();
Model model = getModel();
String className = getClassName();
PersistenceClassElement persistenceClass = getPersistenceClass(className);
if (persistenceClass != null) {
PersistenceFieldElement[] fields = persistenceClass.getFields();
int i, count = ((fields != null) ? fields.length : 0);
Iterator iterator = getMappingClass(className).getFields().iterator();
for (i = 0; i < count; i++) {
PersistenceFieldElement field = fields[i];
list.add(createFieldExistenceComponent(field));
// only want to add the others if the field exists
if (model.hasField(className, field.getName())) {
list.add(createFieldPersistenceComponent(field));
list.add(createFieldPersistenceTypeComponent(field));
list.add(createFieldConsistencyComponent(field));
if (isLegalRelationship(field)) {
RelationshipElement rel = (RelationshipElement) field;
/* user modifiable collection class not yet supported
list.add(createCollectionClassComponent(rel));*/
list.add(createElementClassComponent(rel));
list.add(createRelatedClassMatchesComponent(rel));
}
}
}
while (iterator.hasNext()) {
MappingFieldElement field = (MappingFieldElement) iterator.next();
String fieldName = field.getName();
// only check this if it is not in the jdo model
if (persistenceClass.getField(fieldName) == null) {
list.add(createFieldExistenceComponent(field));
// only want to add the others if the field exists
if (model.hasField(className, fieldName))
list.add(createFieldConsistencyComponent(field));
}
if (!isRelationship(field))
list.add(createColumnOverlapComponent(field));
// preliminary fix for CR6239630
if (// NOI18N
Boolean.getBoolean("AllowManagedFieldsInDefaultFetchGroup")) {
// Do nothing - AllowManagedFieldsInDefaultFetchGroup:
// disabled single model validation test;
// may use checked read/write access to managed fields
} else {
list.add(createFieldDefaultFetchGroupComponent(field));
}
}
}
return list;
}
use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.
the class ModelValidator method createCollectionClassComponent.
/**
* Create a validation component which can check whether the collection
* class is valid given the relationship field type.
* @param field the relationship whose collection class is being checked
* @return the validation component
*/
protected ValidationComponent createCollectionClassComponent(final RelationshipElement field) {
return new ValidationComponent() {
public void validate() throws ModelValidationException {
String className = getClassName();
String fieldName = field.getName();
if (isCollection(className, fieldName)) {
Model model = getModel();
String collectionClass = field.getCollectionClass();
String fieldType = model.getFieldType(className, fieldName);
boolean missingCollectionClass = StringHelper.isEmpty(collectionClass);
if (!missingCollectionClass && !model.getSupportedCollectionClasses(fieldType).contains(collectionClass)) {
throw constructFieldException(fieldName, // NOI18N
"util.validation.collection_class_invalid");
}
}
}
};
}
use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.
the class ModelValidator method createInverseFieldComponent.
/**
* Create a validation component which can check whether the inverse of
* the inverse of the relationship is the relationship itself.
* @param field the relationship whose inverse relationship is being checked
* @return the validation component
*/
protected ValidationComponent createInverseFieldComponent(final RelationshipElement field) {
return new ValidationComponent() {
public void validate() throws ModelValidationException {
Model model = getModel();
RelationshipElement inverse = field.getInverseRelationship(model);
RelationshipElement inverseInverse = ((inverse != null) ? inverse.getInverseRelationship(model) : null);
if ((inverse != null) && (!field.equals(inverseInverse) || (inverseInverse == null))) {
String fieldName = field.getName();
throw new ModelValidationException(model.getField(getClassName(), fieldName), I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.inverse_field_invalid", new Object[] { fieldName, inverse.getName() }));
}
}
};
}
use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.
the class ModelValidator method matchesMethod.
// =============== Misc. private convenience methods ================
/**
* Checks whether the specified method element exists and if so whether it
* has the expected modifiers and the expected return type.
* @param method the method element to be checked
* @param expectedModifiers the modifiers the method should have
* @param optionalModifiers additional modifiers the method might have
* @param expectedReturnType the return type the method should have
* @return <code>true</code> if the method matches,
* <code>false</code> otherwise.
*/
private boolean matchesMethod(final Object method, final int expectedModifiers, final int optionalModifiers, final String expectedReturnType) {
boolean matches = false;
if (method != null) {
Model model = getModel();
int modifiers = model.getModifiers(method);
matches = (((modifiers == expectedModifiers) || (modifiers == (expectedModifiers | optionalModifiers))) && expectedReturnType.equals(model.getType(method)));
}
return matches;
}
use of com.sun.jdo.api.persistence.model.Model in project Payara by payara.
the class ModelValidator method createInverseMappingComponent.
/**
* Create a validation component which can check whether the mapping of
* the relationship and the mapping of its inverse are inverses of each
* other.
* @param field the relationship whose inverse relationship is being checked
* @return the validation component
*/
protected ValidationComponent createInverseMappingComponent(final RelationshipElement field) {
return new ValidationComponent() {
public void validate() throws ModelValidationException {
Model model = getModel();
RelationshipElement inverse = field.getInverseRelationship(model);
if ((inverse != null) && !isInverseMapping(field, inverse)) {
String fieldName = field.getName();
throw new ModelValidationException(model.getField(getClassName(), fieldName), I18NHelper.getMessage(getMessages(), // NOI18N
"util.validation.inverse_mapping_mismatch", new Object[] { fieldName, inverse.getName() }));
}
}
private boolean hasMappingRows(MappingRelationshipElement field2) {
if (field2 != null) {
ArrayList columns = field2.getColumns();
return ((columns != null) && !columns.isEmpty());
}
return false;
}
private boolean isInverseMapping(RelationshipElement jdoField1, RelationshipElement jdoField2) {
MappingRelationshipElement field1 = getMappingRelationship(jdoField1);
MappingRelationshipElement field2 = getMappingRelationship(jdoField2);
boolean field1HasMapping = hasMappingRows(field1);
boolean field2HasMapping = hasMappingRows(field2);
// if both have rows, they must be exact inverses
if (field1HasMapping && field2HasMapping) {
boolean field1IsJoin = isJoin(field1);
if (field1IsJoin == isJoin(field2)) {
ArrayList pairs1 = field1.getColumns();
ArrayList pairs2 = field2.getColumns();
return ((!field1IsJoin) ? isInverse(pairs1, pairs2) : (isInverse(pairs1, field2.getAssociatedColumns()) && isInverse(field1.getAssociatedColumns(), pairs2)));
}
return false;
}
// if neither have rows that's fine
return (field1HasMapping == field2HasMapping);
}
private boolean isInverse(ArrayList pairs1, ArrayList pairs2) {
int i, size1 = pairs1.size(), size2 = pairs2.size();
if (size1 == size2) {
for (i = 0; i < size1; i++) {
String nextPair = (String) pairs1.get(i);
String inversePair = (String) pairs2.get(i);
int semicolonIndex1 = nextPair.indexOf(';');
int semicolonIndex2 = inversePair.indexOf(';');
if (((semicolonIndex1 == -1) || (semicolonIndex2 == -1)) || (!nextPair.substring(0, semicolonIndex1).equals(inversePair.substring(semicolonIndex2 + 1)) || !nextPair.substring(semicolonIndex1 + 1).equals(inversePair.substring(0, semicolonIndex2)))) {
return false;
}
}
return true;
}
return false;
}
};
}
Aggregations