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