use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class EntityTreeImplTest 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.getType()).willReturn(fieldType);
given(fieldDefinition.getName()).willReturn("field");
given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
// then
assertEquals(1, tree.size());
assertEquals(entity, tree.get(0));
assertEquals(entity, getField(tree.getRoot(), "entity"));
}
use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class EntityTreeImplTest method shouldReturnCriteriaBuilder.
@Test
public void shouldReturnCriteriaBuilder() throws Exception {
// given
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.getType()).willReturn(fieldType);
given(fieldDefinition.getName()).willReturn("field");
given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
SearchCriteriaBuilder searchCriteriaBuilder = mock(SearchCriteriaBuilder.class);
given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L))).willReturn(searchCriteriaBuilder);
EntityList list = new EntityListImpl(dataDefinition, "tree", 1L);
// then
assertEquals(searchCriteriaBuilder, list.find());
}
use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class EntityServiceImpl method convertToGenericEntity.
public Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity, final Entity performer) {
Entity genericEntity = null;
if (databaseEntity instanceof Object[]) {
genericEntity = dataDefinition.create();
Object[] databaseArray = (Object[]) databaseEntity;
List<String> fields = new ArrayList<String>(dataDefinition.getFields().keySet());
for (int i = 0; i < fields.size(); i++) {
if (dataDefinition.getField(fields.get(i)).getType() instanceof BelongsToType) {
InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) ((BelongsToType) dataDefinition.getField(fields.get(i)).getType()).getDataDefinition();
genericEntity.setField(fields.get(i), convertToGenericEntity(referencedDataDefinition, databaseArray[i]));
} else {
genericEntity.setField(fields.get(i), databaseArray[i]);
}
}
} else if (databaseEntity.getClass().getName().startsWith("com.qcadoo.model.beans")) {
genericEntity = dataDefinition.create(getId(databaseEntity));
if (dataDefinition.isActivable()) {
genericEntity.setActive(getActive(databaseEntity));
}
for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
if (fieldDefinitionEntry.getValue().isPersistent() && ((InternalFieldDefinition) fieldDefinitionEntry.getValue()).isEnabled()) {
Entity currentPerformer = performer;
if (currentPerformer == null) {
currentPerformer = genericEntity;
}
genericEntity.setField(fieldDefinitionEntry.getKey(), getField(databaseEntity, fieldDefinitionEntry.getValue(), currentPerformer));
}
}
if (dataDefinition.isPrioritizable()) {
genericEntity.setField(dataDefinition.getPriorityField().getName(), getField(databaseEntity, dataDefinition.getPriorityField()));
}
for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
if (fieldDefinitionEntry.getValue().getExpression() != null && ((InternalFieldDefinition) fieldDefinitionEntry.getValue()).isEnabled()) {
genericEntity.setField(fieldDefinitionEntry.getKey(), expressionService.getValue(genericEntity, fieldDefinitionEntry.getValue().getExpression(), Locale.ENGLISH));
}
}
dataDefinition.callViewHook(genericEntity);
} else {
genericEntity = new DefaultEntity(dataDefinition);
genericEntity.setField(dataDefinition.getFields().keySet().iterator().next(), databaseEntity);
}
return genericEntity;
}
use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class ExpressionServiceImpl method getValuesForEntity.
private Map<String, Object> getValuesForEntity(final Entity entity, final Locale locale, final int level) {
if (entity == null) {
return null;
}
Map<String, Object> values = new HashMap<>();
values.put("id", entity.getId());
if (level == 0) {
values.putAll(entity.getFields());
return values;
}
for (Map.Entry<String, Object> entry : entity.getFields().entrySet()) {
if (entry.getValue() instanceof Collection) {
values.put(entry.getKey(), entry.getValue());
} else {
FieldType type = entity.getDataDefinition().getField(entry.getKey()).getType();
if (type instanceof BelongsToType) {
Entity belongsToEntity = getBelongsToEntity(entry.getValue(), (BelongsToType) type);
values.put(entry.getKey(), getValuesForEntity(belongsToEntity, locale, level - 1));
} else {
String value = null;
if (entry.getValue() != null) {
value = type.toString(entry.getValue(), locale);
}
values.put(entry.getKey(), value);
}
}
}
return values;
}
use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.
the class ExpressionUtilTest method shouldGenerateValueOfTheBelongsToColumn.
@Test
public void shouldGenerateValueOfTheBelongsToColumn() throws Exception {
// given
DataDefinition dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
Entity product = new DefaultEntity(dataDefinition, 1L);
product.setField("name", "P1");
BelongsToType belongsToType = mock(BelongsToType.class);
given(dataDefinition.getField(eq("name")).getType().toString(eq("P1"), eq(Locale.ENGLISH))).willReturn("P1");
given(dataDefinition.getField(eq("product")).getType()).willReturn(belongsToType);
given(dataDefinition.getField(eq("product")).getType()).willReturn(belongsToType);
given(belongsToType.getDataDefinition()).willReturn(dataDefinition);
Entity entity = new DefaultEntity(dataDefinition, 1L);
entity.setField("product", product);
// when
String value = expressionService.getValue(entity, "#product['name']", Locale.ENGLISH);
// then
assertEquals("P1", value);
}
Aggregations