Search in sources :

Example 1 with TreeType

use of com.qcadoo.model.api.types.TreeType 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 2 with TreeType

use of com.qcadoo.model.api.types.TreeType in project qcadoo by qcadoo.

the class EntityServiceImpl method getTreeField.

private Object getTreeField(final Object databaseEntity, final FieldDefinition fieldDefinition) {
    Long parentId = getId(databaseEntity);
    TreeType treeFieldType = (TreeType) fieldDefinition.getType();
    InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) treeFieldType.getDataDefinition();
    return new EntityTreeImpl(referencedDataDefinition, treeFieldType.getJoinFieldName(), parentId);
}
Also used : TreeType(com.qcadoo.model.api.types.TreeType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 3 with TreeType

use of com.qcadoo.model.api.types.TreeType 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)

Aggregations

TreeType (com.qcadoo.model.api.types.TreeType)3 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)3 Entity (com.qcadoo.model.api.Entity)2 FieldDefinition (com.qcadoo.model.api.FieldDefinition)2 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)2 ArrayList (java.util.ArrayList)2 EntityList (com.qcadoo.model.api.EntityList)1 EntityOpResult (com.qcadoo.model.api.EntityOpResult)1 HasManyType (com.qcadoo.model.api.types.HasManyType)1 List (java.util.List)1