Search in sources :

Example 91 with FieldDefinition

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

Example 92 with FieldDefinition

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));
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType)

Example 93 with FieldDefinition

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();
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) ParseException(java.text.ParseException) GridComponentFilterException(com.qcadoo.view.internal.components.grid.GridComponentFilterException) GridComponentFilterOperator(com.qcadoo.view.internal.components.grid.GridComponentFilterOperator) Date(java.util.Date)

Example 94 with FieldDefinition

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"));
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 95 with FieldDefinition

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());
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) EntityList(com.qcadoo.model.api.EntityList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

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