}
}
protected void configureRelationships() throws IdentityException {
if (relationshipClass == null) {
throw new IdentityException("Error initializing JpaIdentityStore - relationshipClass not set.");
}
List<Property<Object>> props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new TypedPropertyCriteria(identityClass))
.addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_FROM))
.getResultList();
if (props.size() == 1) {
modelProperties.put(PROPERTY_RELATIONSHIP_FROM, props.get(0));
} else if (props.size() > 1) {
throw new IdentityException(
"Ambiguous relationshipFrom property in relationship class " +
relationshipClass.getName());
} else {
Property<Object> p = findNamedProperty(relationshipClass, "relationshipFrom",
"fromIdentityObject", "fromIdentity");
if (p != null) {
modelProperties.put(PROPERTY_RELATIONSHIP_FROM, p);
} else {
// Last resort - search for a property with a type of identityClass
// and a "from" in its name
props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new TypedPropertyCriteria(identityClass))
.getResultList();
for (Property<Object> prop : props) {
if (prop.getName().contains("from")) {
modelProperties.put(PROPERTY_RELATIONSHIP_FROM, prop);
break;
}
}
}
}
props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new TypedPropertyCriteria(identityClass))
.addCriteria(new PropertyTypeCriteria(PropertyType.RELATIONSHIP_TO))
.getResultList();
if (props.size() == 1) {
modelProperties.put(PROPERTY_RELATIONSHIP_TO, props.get(0));
} else if (props.size() > 1) {
throw new IdentityException(
"Ambiguous relationshipTo property in relationship class " +
relationshipClass.getName());
} else {
Property<Object> p = findNamedProperty(relationshipClass, "relationshipTo",
"toIdentityObject", "toIdentity");
if (p != null) {
modelProperties.put(PROPERTY_RELATIONSHIP_TO, p);
} else {
// Last resort - search for a property with a type of identityClass
// and a "to" in its name
props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new TypedPropertyCriteria(identityClass))
.getResultList();
for (Property<Object> prop : props) {
if (prop.getName().contains("to")) {
modelProperties.put(PROPERTY_RELATIONSHIP_TO, prop);
break;
}
}
}
}
props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new PropertyTypeCriteria(PropertyType.TYPE))
.getResultList();
if (props.size() == 1) {
modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, props.get(0));
} else if (props.size() > 1) {
throw new IdentityException(
"Ambiguous relationshipType property in relationship class " +
relationshipClass.getName());
} else {
Property<Object> p = findNamedProperty(relationshipClass,
"identityRelationshipType", "relationshipType", "type");
if (p != null) {
modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, p);
} else {
props = PropertyQueries.createQuery(relationshipClass)
.getResultList();
for (Property<Object> prop : props) {
if (prop.getName().contains("type")) {
modelProperties.put(PROPERTY_RELATIONSHIP_TYPE, prop);
break;
}
}
}
}
props = PropertyQueries.createQuery(relationshipClass)
.addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
.addCriteria(new TypedPropertyCriteria(String.class))
.getResultList();
if (props.size() == 1) {
modelProperties.put(PROPERTY_RELATIONSHIP_NAME, props.get(0));
} else if (props.size() > 1) {
throw new IdentityException(
"Ambiguous relationship name property in relationship class " +
relationshipClass.getName());
} else {
Property<Object> p = findNamedProperty(relationshipClass, "relationshipName", "name");
if (p != null) {
modelProperties.put(PROPERTY_RELATIONSHIP_NAME, p);
}
}
if (modelProperties.containsKey(PROPERTY_RELATIONSHIP_NAME)) {
namedRelationshipsSupported = true;
}
if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_FROM)) {
throw new IdentityException(
"Error initializing JpaIdentityStore - no valid relationship from property found.");
}
if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TO)) {
throw new IdentityException(
"Error initializing JpaIdentityStore - no valid relationship to property found.");
}
if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE)) {
throw new IdentityException(
"Error initializing JpaIdentityStore - no valid relationship type property found.");
}
if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_NAME)) {
throw new IdentityException(
"Error initializing JpaIdentityStore - no valid relationship name property found.");
}
Class<?> typeClass = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE).getJavaClass();
if (!String.class.equals(typeClass)) {
props = PropertyQueries.createQuery(typeClass)
.addCriteria(new PropertyTypeCriteria(PropertyType.NAME))
.addCriteria(new TypedPropertyCriteria(String.class))
.getResultList();
if (props.size() == 1) {
modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, props.get(0));
} else if (props.size() > 1) {
throw new IdentityException(
"Ambiguous relationship type name property in class " +
typeClass.getName());
} else {
Property<Object> p = findNamedProperty(typeClass, "relationshipTypeName",
"typeName", "name");
if (p != null) {
modelProperties.put(PROPERTY_RELATIONSHIP_TYPE_NAME, p);
}
}
if (!modelProperties.containsKey(PROPERTY_RELATIONSHIP_TYPE_NAME)) {
throw new IdentityException(
"Error initializing JpaIdentityStore - no valid relationship type name property found");
}
}
}