Search in sources :

Example 41 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class InitializationTest method shouldTakeDataDefinitionFromScopeComponent.

@Test
public void shouldTakeDataDefinitionFromScopeComponent() throws Exception {
    // given
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    DataDefinition dataDefinition = mock(DataDefinition.class);
    given(dataDefinition.getField("field")).willReturn(fieldDefinition);
    InternalViewDefinition viewDefinition = mock(InternalViewDefinition.class);
    AbstractComponentPattern sourceComponent = new TextInputComponentPattern(getComponentDefinition("component", viewDefinition));
    setField(sourceComponent, "dataDefinition", dataDefinition);
    setField(sourceComponent, "initialized", true);
    given(viewDefinition.getComponentByReference("component")).willReturn(sourceComponent);
    AbstractComponentPattern pattern = new TextInputComponentPattern(getComponentDefinition("test", null, "#{component}.field", null, viewDefinition));
    // when
    pattern.initialize();
    // then
    Assert.assertEquals(dataDefinition, getField(pattern, "dataDefinition"));
    Assert.assertEquals(fieldDefinition, getField(pattern, "scopeFieldDefinition"));
}
Also used : 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 42 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition 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 43 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class DataAccessServiceImpl method copyTreeField.

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

Example 44 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition 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 45 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class DataAccessServiceImpl method copy.

public Entity copy(final InternalDataDefinition dataDefinition, final Entity sourceEntity) {
    InternalDataDefinition dataDefinitionToCopy = getDataDefinitionByMasterModel(dataDefinition);
    Entity targetEntity = dataDefinitionToCopy.create();
    for (Entry<String, FieldDefinition> fieldEntry : dataDefinitionToCopy.getFields().entrySet()) {
        FieldDefinition fieldDefinition = fieldEntry.getValue();
        String fieldName = fieldEntry.getKey();
        boolean copy = fieldDefinition.getType().isCopyable();
        if (copy) {
            targetEntity.setField(fieldName, getCopyValueOfSimpleField(sourceEntity, dataDefinitionToCopy, fieldName));
        }
    }
    if (!dataDefinitionToCopy.callCopyHook(targetEntity)) {
        return null;
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyHasManyField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyTreeField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyManyToManyField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    return targetEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Aggregations

FieldDefinition (com.qcadoo.model.api.FieldDefinition)142 Test (org.junit.Test)92 DataDefinition (com.qcadoo.model.api.DataDefinition)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 Entity (com.qcadoo.model.api.Entity)32 BelongsToType (com.qcadoo.model.api.types.BelongsToType)19 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)15 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)13 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)12 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)11 HasManyType (com.qcadoo.model.api.types.HasManyType)9 StringType (com.qcadoo.model.internal.types.StringType)9 JSONObject (org.json.JSONObject)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)6 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)6 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)6 Before (org.junit.Before)6 EntityList (com.qcadoo.model.api.EntityList)5