use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class DataAccessServiceImpl method move.
@Transactional
@Monitorable
private void move(final InternalDataDefinition dataDefinition, final Long entityId, final int position, final int offset) {
InternalDataDefinition dataDefinitionToMove = getDataDefinitionByMasterModel(dataDefinition);
checkNotNull(dataDefinitionToMove, L_DATA_DEFINITION_MUST_BE_GIVEN);
checkState(dataDefinitionToMove.isPrioritizable(), "Entity must be prioritizable");
checkState(dataDefinitionToMove.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
checkNotNull(entityId, "EntityId must be given");
Object databaseEntity = getDatabaseEntity(dataDefinitionToMove, entityId);
if (databaseEntity == null) {
logEntityInfo(dataDefinitionToMove, entityId, "hasn't been prioritized, because it doesn't exist");
return;
}
priorityService.move(dataDefinitionToMove, databaseEntity, position, offset);
logEntityInfo(dataDefinitionToMove, entityId, "has been prioritized");
}
use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.
the class DataAccessServiceImpl method activate.
@Override
@Transactional
@Monitorable
public List<Entity> activate(final InternalDataDefinition dataDefinition, final Long... entityIds) {
if (!dataDefinition.isActivable()) {
return Collections.emptyList();
}
InternalDataDefinition dataDefinitionToActivate = getDataDefinitionByMasterModel(dataDefinition);
List<Entity> activatedEntities = new ArrayList<Entity>();
for (Long entityId : entityIds) {
Entity entity = get(dataDefinitionToActivate, entityId);
if (entity == null) {
throw new IllegalStateException("Cannot activate " + entityId);
}
if (!entity.isActive()) {
entity.setActive(true);
entity = save(dataDefinitionToActivate, entity);
if (!entity.isValid()) {
throw new IllegalStateException("Cannot activate " + entity);
}
LOG.debug(entity + " has been activated");
activatedEntities.add(entity);
}
}
return activatedEntities;
}
use of com.qcadoo.model.internal.api.InternalDataDefinition 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.internal.api.InternalDataDefinition 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.internal.api.InternalDataDefinition 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