use of com.qcadoo.model.internal.api.InternalFieldDefinition in project qcadoo by qcadoo.
the class ValidationServiceImpl method parseFields.
private void parseFields(final InternalDataDefinition dataDefinition, final Entity genericEntity) {
for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
final InternalFieldDefinition fieldDefinition = (InternalFieldDefinition) fieldDefinitionEntry.getValue();
final FieldType fieldType = fieldDefinition.getType();
final Object fieldValue = genericEntity.getField(fieldDefinitionEntry.getKey());
Object parsedValue = null;
if (fieldType instanceof BelongsToType) {
parsedValue = parseBelongsToField(fieldDefinition, trimAndNullIfEmpty(fieldValue), genericEntity);
} else {
parsedValue = fieldValue;
}
genericEntity.setField(fieldDefinitionEntry.getKey(), parsedValue);
}
}
use of com.qcadoo.model.internal.api.InternalFieldDefinition 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.InternalFieldDefinition 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.internal.api.InternalFieldDefinition in project qcadoo by qcadoo.
the class ValidationServiceImpl method parseAndValidateEntity.
private void parseAndValidateEntity(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Entity existingGenericEntity) {
for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
final String fieldName = fieldDefinitionEntry.getKey();
final Object newValue = genericEntity.getField(fieldName);
final Object oldValue = getOldFieldValue(existingGenericEntity, fieldName);
final InternalFieldDefinition fieldDefinition = (InternalFieldDefinition) fieldDefinitionEntry.getValue();
final Object validatedFieldValue = parseAndValidateField(fieldDefinition, oldValue, newValue, genericEntity);
genericEntity.setField(fieldName, validatedFieldValue);
}
if (genericEntity.isValid()) {
dataDefinition.callValidators(genericEntity);
}
}
use of com.qcadoo.model.internal.api.InternalFieldDefinition in project qcadoo by qcadoo.
the class ValidationServiceImplTest method shouldCallFieldCustomValidatorWithNonNullOldValueOnCopy.
@Test
public final void shouldCallFieldCustomValidatorWithNonNullOldValueOnCopy() {
// given
final Long savedEntityId = null;
final String someFieldName = "someField";
final String someFieldOldValue = "someFieldValue";
final String someFieldNewValue = "someFieldValueAfterUpdate";
InternalFieldDefinition someFieldDefinition = mockFieldDefinition(someFieldName, new StringType());
Map<String, FieldDefinition> fieldsMap = Maps.newHashMap();
fieldsMap.put(someFieldName, someFieldDefinition);
stubFieldDefinitions(dataDefinition, fieldsMap);
when(genericEntity.getId()).thenReturn(savedEntityId);
when(genericEntity.getField(someFieldName)).thenReturn(someFieldNewValue);
when(genericEntity.isFieldValid(someFieldName)).thenReturn(true);
when(existingGenericEntity.getField(someFieldName)).thenReturn(someFieldOldValue);
// when
validationService.validateGenericEntity(dataDefinition, genericEntity, existingGenericEntity);
// then
verify(someFieldDefinition, times(1)).callValidators(genericEntity, someFieldOldValue, someFieldNewValue);
}
Aggregations