Search in sources :

Example 26 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition 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 27 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityServiceImpl method getHasManyField.

private Object getHasManyField(final Object databaseEntity, final FieldDefinition fieldDefinition) {
    Long parentId = getId(databaseEntity);
    HasManyType hasManyFieldType = (HasManyType) fieldDefinition.getType();
    InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) hasManyFieldType.getDataDefinition();
    return new EntityListImpl(referencedDataDefinition, hasManyFieldType.getJoinFieldName(), parentId);
}
Also used : HasManyType(com.qcadoo.model.api.types.HasManyType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 28 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class ModelXmlToDefinitionConverterTest method init.

@BeforeClass
public static void init() throws Exception {
    applicationContext = mock(ApplicationContext.class);
    dataAccessService = mock(DataAccessService.class);
    translationService = mock(TranslationService.class);
    dataDefinitionService = new DataDefinitionServiceImpl();
    modelXmlToDefinitionConverter = new ModelXmlToDefinitionConverterImpl();
    setField(modelXmlToDefinitionConverter, "dataDefinitionService", dataDefinitionService);
    setField(modelXmlToDefinitionConverter, "dataAccessService", dataAccessService);
    setField(modelXmlToDefinitionConverter, "applicationContext", applicationContext);
    setField(modelXmlToDefinitionConverter, "translationService", translationService);
    given(applicationContext.getBean(CustomHook.class)).willReturn(new CustomHook());
    modelXmlToClassConverter = new ModelXmlToClassConverterImpl();
    ((ModelXmlToClassConverterImpl) modelXmlToClassConverter).setBeanClassLoader(ClassLoader.getSystemClassLoader());
    dataDefinitions = performConvert(Utils.FULL_FIRST_ENTITY_XML_RESOURCE, Utils.FULL_SECOND_ENTITY_XML_RESOURCE, Utils.FULL_THIRD_ENTITY_XML_RESOURCE, Utils.OTHER_FIRST_ENTITY_XML_RESOURCE, Utils.OTHER_SECOND_ENTITY_XML_RESOURCE);
    for (DataDefinition dd : dataDefinitions.toArray(new DataDefinition[dataDefinitions.size()])) {
        if (dd.getName().equals("firstEntity") && dd.getPluginIdentifier().equals("full")) {
            dataDefinition = (InternalDataDefinition) dd;
        }
    }
}
Also used : CustomHook(com.qcadoo.model.CustomHook) ApplicationContext(org.springframework.context.ApplicationContext) TranslationService(com.qcadoo.localization.api.TranslationService) DataAccessService(com.qcadoo.model.internal.api.DataAccessService) DataDefinitionServiceImpl(com.qcadoo.model.internal.DataDefinitionServiceImpl) ModelXmlToClassConverterImpl(com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterImpl) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) BeforeClass(org.junit.BeforeClass)

Aggregations

InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)28 Entity (com.qcadoo.model.api.Entity)16 FieldDefinition (com.qcadoo.model.api.FieldDefinition)12 BelongsToType (com.qcadoo.model.api.types.BelongsToType)10 Test (org.junit.Test)8 Monitorable (com.qcadoo.model.api.aop.Monitorable)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ArrayList (java.util.ArrayList)6 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)5 EntityList (com.qcadoo.model.api.EntityList)3 ManyToManyType (com.qcadoo.model.api.types.ManyToManyType)3 TreeType (com.qcadoo.model.api.types.TreeType)3 EntityOpResult (com.qcadoo.model.api.EntityOpResult)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 HasManyType (com.qcadoo.model.api.types.HasManyType)2 TranslationService (com.qcadoo.localization.api.TranslationService)1 CustomHook (com.qcadoo.model.CustomHook)1 CopyException (com.qcadoo.model.api.CopyException)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 Cascadeable (com.qcadoo.model.api.types.Cascadeable)1