private void checkReferencedOrganizations(Object entity, Object[] currentState,
Object[] previousState, String[] propertyNames) {
if (!(entity instanceof OrganizationEnabled)) {
return;
}
final Organization o1 = ((OrganizationEnabled) entity).getOrganization();
final OBContext obContext = OBContext.getOBContext();
final BaseOBObject bob = (BaseOBObject) entity;
boolean isNew = bob.getId() == null || bob.isNewOBObject();
// check if the organization of the current object has changed, if so
// then
// check all references
if (!isNew) {
for (int i = 0; i < currentState.length; i++) {
if (propertyNames[i].equals(PROPERTY_ORGANIZATION)) {
if (currentState[i] != previousState[i]) {
isNew = true;
break;
}
}
}
}
for (int i = 0; i < currentState.length; i++) {
// TODO maybe use equals
if ((isNew || currentState[i] != previousState[i])
&& !(currentState[i] instanceof Organization)
&& (currentState[i] instanceof BaseOBObject || currentState[i] instanceof HibernateProxy)
&& currentState[i] instanceof OrganizationEnabled) {
// get the organization from the current state
final OrganizationEnabled oe = (OrganizationEnabled) currentState[i];
final Organization o2 = oe.getOrganization();
if (!obContext.getOrganizationStructureProvider(o1.getClient().getId()).isInNaturalTree(o1,
o2)) {
throw new OBSecurityException("Entity " + bob.getIdentifier() + " ("
+ bob.getEntityName() + ") with organization " + o1.getIdentifier()
+ " references an entity " + ((BaseOBObject) currentState[i]).getIdentifier()
+ " through its property " + propertyNames[i] + " but this referenced entity"
+ " belongs to an organization " + o2.getIdentifier()
+ " which is not part of the natural tree of " + o1.getIdentifier());
}
}
}
}