use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class EntityServiceImpl method getTreeField.
private Object getTreeField(final Object databaseEntity, final FieldDefinition fieldDefinition) {
Long parentId = getId(databaseEntity);
TreeType treeFieldType = (TreeType) fieldDefinition.getType();
InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) treeFieldType.getDataDefinition();
return new EntityTreeImpl(referencedDataDefinition, treeFieldType.getJoinFieldName(), parentId);
}
use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class EntityServiceImpl method getManyToManyField.
private Object getManyToManyField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer) {
ManyToManyType manyToManyType = (ManyToManyType) fieldDefinition.getType();
InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) manyToManyType.getDataDefinition();
if (manyToManyType.isLazyLoading()) {
return new ProxyList(fieldDefinition, getId(databaseEntity), performer);
} else {
@SuppressWarnings("unchecked") Set<Object> databaseEntities = (Set<Object>) getPrimitiveField(databaseEntity, fieldDefinition);
if (databaseEntities == null) {
return null;
}
List<Entity> genericEntities = Lists.newArrayList();
@SuppressWarnings("unchecked") final Iterable<Object> fieldValues = (Iterable<Object>) getField(databaseEntity, fieldDefinition.getName());
for (Object innerDatabaseEntity : fieldValues) {
Entity innerEntity = null;
Long id = getId(innerDatabaseEntity);
if (id == null) {
innerEntity = convertToGenericEntity(referencedDataDefinition, innerDatabaseEntity);
} else {
innerEntity = new ProxyEntity(referencedDataDefinition, id);
}
genericEntities.add(innerEntity);
}
return genericEntities;
}
}
use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class EntityServiceImpl method getBelongsToField.
private Object getBelongsToField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer) {
BelongsToType belongsToFieldType = (BelongsToType) fieldDefinition.getType();
InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) belongsToFieldType.getDataDefinition();
Object value = getField(databaseEntity, fieldDefinition.getName());
if (value == null) {
return null;
}
if (performer != null && referencedDataDefinition.equals(performer.getDataDefinition()) && performer.getId() != null && performer.getId().equals(getId(value))) {
return performer;
}
if (belongsToFieldType.isLazyLoading()) {
Long id = null;
if (value instanceof HibernateProxy) {
id = (Long) ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier();
} else {
id = getId(getField(databaseEntity, fieldDefinition.getName()));
}
if (id == null) {
return null;
}
return new ProxyEntity(referencedDataDefinition, id);
} else {
Entity currentPerformer = performer;
if (performer == null || performer.getId() == null && referencedDataDefinition.equals(performer.getDataDefinition())) {
currentPerformer = new ProxyEntity(referencedDataDefinition, getId(value));
}
return convertToGenericEntity(referencedDataDefinition, value, currentPerformer);
}
}
use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class EntityServiceImpl method convertToDatabaseEntity.
@SuppressWarnings("unchecked")
@Override
public Object convertToDatabaseEntity(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Object existingDatabaseEntity) {
Object databaseEntity = getDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
FieldDefinition fieldDefinition = fieldDefinitionEntry.getValue();
if (fieldDefinition.isPersistent() && ((InternalFieldDefinition) fieldDefinition).isEnabled()) {
Object fieldValue = genericEntity.getField(fieldDefinitionEntry.getKey());
if (fieldDefinition.getType() instanceof ManyToManyType && fieldValue instanceof Iterable) {
Set<Object> innerDatabaseEntities = Sets.newHashSet();
for (Entity innerGenericEntity : (Iterable<Entity>) fieldValue) {
innerDatabaseEntities.add(getDatabaseEntity((InternalDataDefinition) innerGenericEntity.getDataDefinition(), innerGenericEntity, null));
}
setField(databaseEntity, fieldDefinitionEntry.getValue(), innerDatabaseEntities);
} else {
setField(databaseEntity, fieldDefinitionEntry.getValue(), fieldValue);
}
}
}
if (dataDefinition.isPrioritizable() && genericEntity.getField(dataDefinition.getPriorityField().getName()) != null) {
setField(databaseEntity, dataDefinition.getPriorityField(), genericEntity.getField(dataDefinition.getPriorityField().getName()));
}
if (dataDefinition.isActivable()) {
setActive(databaseEntity, genericEntity.isActive());
}
return databaseEntity;
}
use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class EntityListImplTest method shouldLoadEntities.
@Test
public void shouldLoadEntities() throws Exception {
// given
Entity entity = mock(Entity.class);
List<Entity> entities = Collections.singletonList(entity);
BelongsToType fieldType = mock(BelongsToType.class);
InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
given(fieldType.getDataDefinition()).willReturn(dataDefinition);
FieldDefinition fieldDefinition = mock(FieldDefinition.class);
given(fieldDefinition.getName()).willReturn("field");
given(fieldDefinition.getType()).willReturn(fieldType);
given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L)).list().getEntities()).willReturn(entities);
given(dataAccessService.get(dataDefinition, 1L)).willReturn(entity);
EntityListImpl list = new EntityListImpl(dataDefinition, "hasMany", 1L);
// then
assertEquals(1, list.size());
assertEquals(entity, list.get(0));
}
Aggregations