Search in sources :

Example 1 with InternalFieldDefinition

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);
    }
}
Also used : InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition)

Example 2 with InternalFieldDefinition

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 3 with InternalFieldDefinition

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;
}
Also used : Entity(com.qcadoo.model.api.Entity) Entry(java.util.Map.Entry) BelongsToType(com.qcadoo.model.api.types.BelongsToType) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 4 with InternalFieldDefinition

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);
    }
}
Also used : InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition)

Example 5 with InternalFieldDefinition

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);
}
Also used : InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) StringType(com.qcadoo.model.internal.types.StringType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) Test(org.junit.Test)

Aggregations

InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)8 FieldDefinition (com.qcadoo.model.api.FieldDefinition)6 Entity (com.qcadoo.model.api.Entity)3 StringType (com.qcadoo.model.internal.types.StringType)3 Test (org.junit.Test)3 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)2 BelongsToType (com.qcadoo.model.api.types.BelongsToType)1 ManyToManyType (com.qcadoo.model.api.types.ManyToManyType)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1