Search in sources :

Example 1 with HasManyType

use of com.qcadoo.model.api.types.HasManyType in project qcadoo by qcadoo.

the class InitializationTest method shouldGetDataDefinitionFromHasManyTypeScopeFieldDefinition.

@Test
public void shouldGetDataDefinitionFromHasManyTypeScopeFieldDefinition() throws Exception {
    // given
    HasManyType fieldType = mock(HasManyType.class);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    DataDefinition dataDefinition = mock(DataDefinition.class);
    given(dataDefinition.getField("field")).willReturn(fieldDefinition);
    DataDefinition belongsToDefinition = mock(DataDefinition.class);
    given(fieldType.getDataDefinition()).willReturn(belongsToDefinition);
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    AbstractContainerPattern parent = new WindowComponentPattern(getComponentDefinition("parent", viewDefinition));
    setField(parent, "dataDefinition", dataDefinition);
    setField(parent, "initialized", true);
    AbstractComponentPattern pattern = new TextInputComponentPattern(getComponentDefinition("test", null, "field", parent, viewDefinition));
    // when
    pattern.initialize();
    // then
    Assert.assertEquals(belongsToDefinition, getField(pattern, "dataDefinition"));
    Assert.assertEquals(fieldDefinition, getField(pattern, "scopeFieldDefinition"));
}
Also used : HasManyType(com.qcadoo.model.api.types.HasManyType) WindowComponentPattern(com.qcadoo.view.internal.components.window.WindowComponentPattern) InternalViewDefinition(com.qcadoo.view.internal.api.InternalViewDefinition) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) TextInputComponentPattern(com.qcadoo.view.internal.components.TextInputComponentPattern) Test(org.junit.Test)

Example 2 with HasManyType

use of com.qcadoo.model.api.types.HasManyType in project qcadoo by qcadoo.

the class DataAccessServiceImpl method copyHasManyField.

private void copyHasManyField(final Entity sourceEntity, final Entity targetEntity, final DataDefinition dataDefinition, final String fieldName) {
    FieldDefinition fieldDefinition = dataDefinition.getField(fieldName);
    if (!isFieldCopyable(HasManyType.class, fieldDefinition, dataDefinition)) {
        return;
    }
    HasManyType hasManyType = ((HasManyType) fieldDefinition.getType());
    List<Entity> entities = new ArrayList<Entity>();
    for (Entity childEntity : sourceEntity.getHasManyField(fieldName)) {
        childEntity.setField(hasManyType.getJoinFieldName(), null);
        Entity savedChildEntity = copy((InternalDataDefinition) hasManyType.getDataDefinition(), childEntity);
        if (savedChildEntity != null) {
            entities.add(savedChildEntity);
        }
    }
    targetEntity.setField(fieldName, entities);
}
Also used : Entity(com.qcadoo.model.api.Entity) HasManyType(com.qcadoo.model.api.types.HasManyType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ArrayList(java.util.ArrayList)

Example 3 with HasManyType

use of com.qcadoo.model.api.types.HasManyType in project qcadoo by qcadoo.

the class ViewDefinitionParserImplTest method init.

@Before
public void init() throws Exception {
    applicationContext = mock(ApplicationContext.class);
    dataDefinitionService = mock(DataDefinitionService.class);
    translationService = mock(TranslationService.class);
    RibbonParserService ribbonService = new RibbonParserService();
    contextualHelpService = mock(ContextualHelpService.class);
    securityRolesService = mock(SecurityRolesService.class);
    viewDefinitionService = new ViewDefinitionServiceImpl();
    hookFactory = new HookFactory();
    setField(hookFactory, "applicationContext", applicationContext);
    viewDefinitionParser = new ViewDefinitionParserImpl();
    setField(viewDefinitionParser, "dataDefinitionService", dataDefinitionService);
    setField(viewDefinitionParser, "viewDefinitionService", viewDefinitionService);
    setField(viewDefinitionParser, "hookFactory", hookFactory);
    setField(viewDefinitionParser, "translationService", translationService);
    setField(viewDefinitionParser, "applicationContext", applicationContext);
    setField(viewDefinitionParser, "contextualHelpService", contextualHelpService);
    setField(viewDefinitionParser, "viewComponentsResolver", viewComponentsResolver);
    setField(viewDefinitionParser, "ribbonService", ribbonService);
    setField(viewDefinitionParser, "securityRolesService", securityRolesService);
    userRoleMock = mock(SecurityRole.class);
    given(securityRolesService.getRoleByIdentifier("ROLE_USER")).willReturn(userRoleMock);
    adminRoleMock = mock(SecurityRole.class);
    given(securityRolesService.getRoleByIdentifier("ROLE_ADMIN")).willReturn(adminRoleMock);
    xml1 = "view/test1.xml";
    xml2 = "view/test2.xml";
    given(applicationContext.getBean(CustomViewService.class)).willReturn(new CustomViewService());
    dataDefinitionA = mock(DataDefinition.class);
    dataDefinitionB = mock(DataDefinition.class);
    FieldDefinition nameA = mock(FieldDefinition.class, "nameA");
    FieldDefinition nameB = mock(FieldDefinition.class, "nameB");
    FieldDefinition hasManyB = mock(FieldDefinition.class, "hasManyB");
    FieldDefinition belongToA = mock(FieldDefinition.class, "belongsToA");
    HasManyType hasManyBType = mock(HasManyType.class);
    BelongsToType belongToAType = mock(BelongsToType.class);
    given(nameA.getDataDefinition()).willReturn(dataDefinitionA);
    given(nameA.getType()).willReturn(new StringType());
    given(nameB.getType()).willReturn(new StringType());
    given(nameB.getDataDefinition()).willReturn(dataDefinitionA);
    given(hasManyB.getType()).willReturn(hasManyBType);
    given(hasManyB.getDataDefinition()).willReturn(dataDefinitionB);
    given(belongToA.getType()).willReturn(belongToAType);
    given(belongToA.getDataDefinition()).willReturn(dataDefinitionB);
    given(hasManyBType.getDataDefinition()).willReturn(dataDefinitionB);
    given(belongToAType.getDataDefinition()).willReturn(dataDefinitionA);
    given(dataDefinitionA.getField("beansB")).willReturn(hasManyB);
    given(dataDefinitionA.getField("name")).willReturn(nameA);
    given(dataDefinitionB.getField("activeA")).willReturn(nameA);
    given(dataDefinitionB.getField("activeB")).willReturn(nameA);
    given(dataDefinitionB.getField("activeC")).willReturn(nameA);
    given(dataDefinitionB.getField("beanA")).willReturn(belongToA);
    given(dataDefinitionB.getField("beanM")).willReturn(belongToA);
    given(dataDefinitionB.getField("beanB")).willReturn(belongToA);
    given(dataDefinitionB.getField("name")).willReturn(nameB);
    given(dataDefinitionA.getName()).willReturn("beanA");
    given(dataDefinitionB.getName()).willReturn("beanB");
    given(dataDefinitionA.getFields()).willReturn(ImmutableMap.of("name", nameA, "beansB", hasManyB));
    given(dataDefinitionB.getFields()).willReturn(ImmutableMap.of("name", nameB, "beanA", belongToA));
    given(dataDefinitionService.get("sample", "beanA")).willReturn(dataDefinitionA);
    given(dataDefinitionService.get("sample", "beanB")).willReturn(dataDefinitionB);
}
Also used : SecurityRole(com.qcadoo.security.api.SecurityRole) CustomViewService(com.qcadoo.view.beans.sample.CustomViewService) StringType(com.qcadoo.model.internal.types.StringType) ViewDefinitionServiceImpl(com.qcadoo.view.internal.ViewDefinitionServiceImpl) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) DataDefinition(com.qcadoo.model.api.DataDefinition) RibbonParserService(com.qcadoo.view.internal.ribbon.RibbonParserService) ApplicationContext(org.springframework.context.ApplicationContext) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) TranslationService(com.qcadoo.localization.api.TranslationService) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) Before(org.junit.Before)

Example 4 with HasManyType

use of com.qcadoo.model.api.types.HasManyType in project qcadoo by qcadoo.

the class DefaultEntityTest method init.

@Before
public final void init() {
    belongsToFieldDefinition = mock(FieldDefinition.class);
    final BelongsToType belongsToType = mock(BelongsToType.class);
    when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
    belongsToFieldDataDefinition = mock(DataDefinition.class);
    when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
    decimalFieldDefinition = mock(FieldDefinition.class);
    final DecimalType decimalType = new DecimalType();
    when(decimalFieldDefinition.getType()).thenReturn(decimalType);
    integerFieldDefinition = mock(FieldDefinition.class);
    final IntegerType integerType = new IntegerType();
    when(integerFieldDefinition.getType()).thenReturn(integerType);
    manyToManyFieldDefinition = mock(FieldDefinition.class);
    final ManyToManyType manyToManyType = mock(ManyToManyType.class);
    when(manyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    manyToManyDataDefinition = mock(DataDefinition.class);
    when(manyToManyFieldDefinition.getDataDefinition()).thenReturn(manyToManyDataDefinition);
    secondManyToManyFieldDefinition = mock(FieldDefinition.class);
    when(secondManyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    secondManyToManyDataDefinition = mock(DataDefinition.class);
    when(secondManyToManyFieldDefinition.getDataDefinition()).thenReturn(secondManyToManyDataDefinition);
    hasManyFieldDefinition = mock(FieldDefinition.class);
    final HasManyType hasManyType = mock(HasManyType.class);
    when(hasManyType.getJoinFieldName()).thenReturn(BELONGS_TO_FIELD_NAME);
    when(hasManyFieldDefinition.getType()).thenReturn(hasManyType);
    hasManyDataDefinition = mock(DataDefinition.class);
    when(hasManyFieldDefinition.getDataDefinition()).thenReturn(hasManyDataDefinition);
    stringFieldDefinition = mock(FieldDefinition.class);
    when(stringFieldDefinition.isPersistent()).thenReturn(false);
    dataDefinition = mock(DataDefinition.class);
    booleanFieldDefinition = mock(FieldDefinition.class);
    defaultEntity = new DefaultEntity(dataDefinition);
    final 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);
    fieldsMap.put(DECIMAL_FIELD_NAME, decimalFieldDefinition);
    fieldsMap.put(INTEGER_FIELD_NAME, integerFieldDefinition);
    fieldsMap.put(MANY_TO_MANY_FIELD_NAME, manyToManyFieldDefinition);
    fieldsMap.put(SECOND_MANY_TO_MANY_FIELD_NAME, secondManyToManyFieldDefinition);
    fieldsMap.put(HAS_MANY_FIELD_NAME, hasManyFieldDefinition);
    for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
        when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
    }
    when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) DecimalType(com.qcadoo.model.internal.types.DecimalType) DataDefinition(com.qcadoo.model.api.DataDefinition) Map(java.util.Map) Before(org.junit.Before)

Example 5 with HasManyType

use of com.qcadoo.model.api.types.HasManyType in project qcadoo by qcadoo.

the class DataAccessServiceImpl method performSave.

@SuppressWarnings("unchecked")
private Entity performSave(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Set<Entity> alreadySavedEntities, final Set<Entity> newlySavedEntities, boolean fast) {
    checkNotNull(dataDefinition, L_DATA_DEFINITION_MUST_BE_GIVEN);
    checkState(dataDefinition.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    checkNotNull(genericEntity, "Entity must be given");
    if (alreadySavedEntities.contains(genericEntity)) {
        return genericEntity;
    }
    Entity genericEntityToSave = genericEntity.copy();
    Object existingDatabaseEntity = getExistingDatabaseEntity(dataDefinition, genericEntity);
    Entity existingGenericEntity = null;
    if (existingDatabaseEntity != null) {
        existingGenericEntity = entityService.convertToGenericEntity(dataDefinition, existingDatabaseEntity);
    }
    if (!fast) {
        validationService.validateGenericEntity(dataDefinition, genericEntity, existingGenericEntity);
    }
    if (!genericEntity.isValid()) {
        copyValidationErrors(dataDefinition, genericEntityToSave, genericEntity);
        if (existingGenericEntity != null) {
            copyMissingFields(genericEntityToSave, existingGenericEntity);
        }
        logValidationErrors(genericEntityToSave);
        return genericEntityToSave;
    }
    Object databaseEntity = entityService.convertToDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
    if (genericEntity.getId() == null) {
        priorityService.prioritizeEntity(dataDefinition, databaseEntity);
    }
    saveDatabaseEntity(dataDefinition, databaseEntity);
    if (dataDefinition.isVersionable()) {
        hibernateService.getCurrentSession().flush();
    }
    Entity savedEntity = entityService.convertToGenericEntity(dataDefinition, databaseEntity);
    copyGlobalMessages(dataDefinition, savedEntity, genericEntity);
    for (Entry<String, FieldDefinition> fieldEntry : dataDefinition.getFields().entrySet()) {
        if (fieldEntry.getValue().getType() instanceof HasManyType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            HasManyType hasManyType = (HasManyType) fieldEntry.getValue().getType();
            if (entities == null || entities instanceof EntityListImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            List<Entity> savedEntities = saveHasManyEntities(alreadySavedEntities, newlySavedEntities, hasManyType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) hasManyType.getDataDefinition());
            EntityList dbEntities = savedEntity.getHasManyField(fieldEntry.getKey());
            EntityOpResult results = removeOrphans(hasManyType, findOrphans(savedEntities, dbEntities));
            if (!results.isSuccessfull()) {
                // #TODO MAKU
                copyValidationErrors(dataDefinition, savedEntity, results.getMessagesHolder());
                savedEntity.setField(fieldEntry.getKey(), existingGenericEntity.getField(fieldEntry.getKey()));
                return savedEntity;
            }
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        } else if (fieldEntry.getValue().getType() instanceof TreeType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            if (entities == null || entities instanceof EntityTreeImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            TreeType treeType = (TreeType) fieldEntry.getValue().getType();
            List<Entity> savedEntities = saveTreeEntities(alreadySavedEntities, newlySavedEntities, treeType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) treeType.getDataDefinition(), null);
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(savedEntity + " has been saved");
    }
    alreadySavedEntities.add(savedEntity);
    if (genericEntity.getId() == null && savedEntity.getId() != null) {
        newlySavedEntities.add(savedEntity);
    }
    return savedEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) TreeType(com.qcadoo.model.api.types.TreeType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) EntityList(com.qcadoo.model.api.EntityList) EntityOpResult(com.qcadoo.model.api.EntityOpResult) HasManyType(com.qcadoo.model.api.types.HasManyType) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Aggregations

HasManyType (com.qcadoo.model.api.types.HasManyType)11 FieldDefinition (com.qcadoo.model.api.FieldDefinition)10 DataDefinition (com.qcadoo.model.api.DataDefinition)8 BelongsToType (com.qcadoo.model.api.types.BelongsToType)5 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)5 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)5 Test (org.junit.Test)5 Entity (com.qcadoo.model.api.Entity)3 GridComponentPattern (com.qcadoo.view.internal.components.grid.GridComponentPattern)3 Before (org.junit.Before)3 TranslationService (com.qcadoo.localization.api.TranslationService)2 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)2 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)2 SecurityRolesService (com.qcadoo.security.api.SecurityRolesService)2 ComponentDefinition (com.qcadoo.view.internal.ComponentDefinition)2 ComponentOption (com.qcadoo.view.internal.ComponentOption)2 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)2 AbstractComponentPattern (com.qcadoo.view.internal.patterns.AbstractComponentPattern)2 AbstractPatternTest (com.qcadoo.view.internal.patterns.AbstractPatternTest)2 ArrayList (java.util.ArrayList)2