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"));
}
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"));
}
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);
}
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);
}
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;
}
Aggregations