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