use of com.qcadoo.model.api.FieldDefinition 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.api.FieldDefinition in project qcadoo by qcadoo.
the class ExpressionServiceImpl method getValue.
@Override
public String getValue(final Entity entity, final List<FieldDefinition> fieldDefinitions, final Locale locale) {
String value = null;
if (fieldDefinitions.size() == 1) {
FieldDefinition field = fieldDefinitions.get(0);
value = field.getValue(entity.getField(field.getName()), locale);
} else {
List<String> values = new ArrayList<String>();
for (FieldDefinition fieldDefinition : fieldDefinitions) {
values.add(fieldDefinition.getValue(entity.getField(fieldDefinition.getName()), locale));
}
value = StringUtils.join(values, ", ");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Calculating value of fields " + fieldDefinitions + " for " + entity + " : " + value);
}
if (StringUtils.isEmpty(value) || "null".equals(value)) {
return null;
} else {
return value;
}
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class AbstractEntityWrapperTest method superInit.
@Before
public final void superInit() {
belongsToFieldDefinition = mock(FieldDefinition.class);
BelongsToType belongsToType = mock(BelongsToType.class);
when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
belongsToFieldDataDefinition = mock(DataDefinition.class);
when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
FieldDefinition stringFieldDefinition = mock(FieldDefinition.class);
when(stringFieldDefinition.isPersistent()).thenReturn(false);
dataDefinition = mock(DataDefinition.class);
FieldDefinition booleanFieldDefinition = mock(FieldDefinition.class);
Map<String, FieldDefinition> fieldsMap = Maps.newHashMap();
fieldsMap.put(BELONGS_TO_FIELD_NAME, belongsToFieldDefinition);
fieldsMap.put(STRING_FIELD_NAME, stringFieldDefinition);
fieldsMap.put(BOOLEAN_FIELD_NAME, booleanFieldDefinition);
for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
}
when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
use of com.qcadoo.model.api.FieldDefinition 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));
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class EntityListImplTest 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("hasMany")).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, "hasMany", 1L);
// then
assertEquals(searchCriteriaBuilder, list.find());
}
Aggregations