//construct entity view from already loaded entity
private View getView(Entity entity) {
return getView(entity, new HashSet<>());
}
private View getView(Entity entity, Set<String> visitedProperties) {
View view = new View(entity.getClass());
for (MetaProperty property : metadata.getClassNN(entity.getClass()).getProperties()) {
if (!PersistenceHelper.isLoaded(entity, property.getName()) || !metadata.getTools().isPersistent(property)) {
continue;
}
String propertyAddress = entity.getClass().getName() + "." + property.getName();
String reversePropertyAddress = null;
if (property.getInverse() != null) {
reversePropertyAddress = property.getInverse().getDomain().getJavaClass().getName() + "." + property.getInverse().getName();
}
if (visitedProperties.contains(propertyAddress) || visitedProperties.contains(reversePropertyAddress)) {//prevent loop specify it as local
if (property.getRange().isClass()) {
view.addProperty(property.getName(), viewRepository.getView(property.getRange().asClass(), View.LOCAL));
continue;
}
}
visitedProperties.add(propertyAddress);
if (property.getRange().isClass()) {
Object value = entity.getValue(property.getName());
if (value instanceof Collection) {
for (Object item : (Collection) value) {
if (item != null) {
value = item;
break;
}
}
}
if (value == null || value instanceof Collection) {
view.addProperty(property.getName(), viewRepository.getView(property.getRange().asClass(), View.LOCAL));
} else {
view.addProperty(property.getName(), getView((Entity) value, visitedProperties));
}
} else {
view.addProperty(property.getName());
}
}
return view;
}