use of com.qcadoo.model.api.FieldDefinition 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;
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class DataAccessServiceImpl method copyManyToManyField.
private void copyManyToManyField(final Entity sourceEntity, final Entity targetEntity, final DataDefinition dataDefinition, final String fieldName) {
FieldDefinition fieldDefinition = dataDefinition.getField(fieldName);
if (!isFieldCopyable(ManyToManyType.class, fieldDefinition, dataDefinition)) {
return;
}
targetEntity.setField(fieldName, sourceEntity.getField(fieldName));
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class GridComponentFilterSQLUtils method addFilters.
public static String addFilters(final Map<String, String> filters, final Map<String, GridComponentColumn> columns, String table, final DataDefinition dataDefinition) throws GridComponentFilterException {
StringBuilder filterQuery = new StringBuilder(" 1=1 ");
for (Entry<String, String> filter : filters.entrySet()) {
String field = getFieldNameByColumnName(columns, filter.getKey());
if (field != null) {
try {
FieldDefinition fieldDefinition = getFieldDefinition(dataDefinition, field);
Entry<GridComponentFilterOperator, String> filterValue = parseFilterValue(filter.getValue());
if ("".equals(filterValue.getValue()) && !ISNULL.equals(filterValue.getKey())) {
continue;
}
if (fieldDefinition != null && String.class.isAssignableFrom(fieldDefinition.getType().getType())) {
addStringFilter(table, filterQuery, filterValue, field);
} else if (fieldDefinition != null && Boolean.class.isAssignableFrom(fieldDefinition.getType().getType())) {
addSimpleFilter(table, filterQuery, filterValue, field, "1".equals(filterValue.getValue()));
} else if (fieldDefinition != null && Date.class.isAssignableFrom(fieldDefinition.getType().getType())) {
addDateFilter(table, filterQuery, filterValue, field);
} else if (fieldDefinition != null && BigDecimal.class.isAssignableFrom(fieldDefinition.getType().getType())) {
addDecimalFilter(table, filterQuery, filterValue, field);
} else if (fieldDefinition != null && Integer.class.isAssignableFrom(fieldDefinition.getType().getType())) {
addIntegerFilter(table, filterQuery, filterValue, field);
} else {
addSimpleFilter(table, filterQuery, filterValue, field, filterValue.getValue());
}
} catch (ParseException pe) {
throw new GridComponentFilterException(filter.getValue());
}
}
}
return filterQuery.toString();
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class EntityTreeImplTest method shouldLoadEntities.
@Test
public void shouldLoadEntities() throws Exception {
// given
Entity entity = mock(Entity.class);
List<Entity> entities = Collections.singletonList(entity);
BelongsToType fieldType = mock(BelongsToType.class);
InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
given(fieldType.getDataDefinition()).willReturn(dataDefinition);
FieldDefinition fieldDefinition = mock(FieldDefinition.class);
given(fieldDefinition.getType()).willReturn(fieldType);
given(fieldDefinition.getName()).willReturn("field");
given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
// then
assertEquals(1, tree.size());
assertEquals(entity, tree.get(0));
assertEquals(entity, getField(tree.getRoot(), "entity"));
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class EntityTreeImplTest method shouldReturnCriteriaBuilder.
@Test
public void shouldReturnCriteriaBuilder() throws Exception {
// given
BelongsToType fieldType = mock(BelongsToType.class);
InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
given(fieldType.getDataDefinition()).willReturn(dataDefinition);
FieldDefinition fieldDefinition = mock(FieldDefinition.class);
given(fieldDefinition.getType()).willReturn(fieldType);
given(fieldDefinition.getName()).willReturn("field");
given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
SearchCriteriaBuilder searchCriteriaBuilder = mock(SearchCriteriaBuilder.class);
given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L))).willReturn(searchCriteriaBuilder);
EntityList list = new EntityListImpl(dataDefinition, "tree", 1L);
// then
assertEquals(searchCriteriaBuilder, list.find());
}
Aggregations