use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.
the class StateChangeViewClientValidationUtilTest method mockDataDefinition.
private DataDefinition mockDataDefinition(final Iterable<FieldDefinition> fieldDefinitions) {
final DataDefinition dataDefinition = mock(DataDefinition.class);
final Map<String, FieldDefinition> fields = Maps.newHashMap();
for (final FieldDefinition fieldDefinition : fieldDefinitions) {
fields.put(fieldDefinition.getName(), fieldDefinition);
}
given(dataDefinition.getFields()).willReturn(Collections.unmodifiableMap(fields));
given(dataDefinition.getField(Mockito.anyString())).willAnswer(new Answer<FieldDefinition>() {
@Override
public FieldDefinition answer(final InvocationOnMock invocation) throws Throwable {
return fields.get(invocation.getArguments()[0]);
}
});
return dataDefinition;
}
use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.
the class ProductValidatorsTest method shouldCheckEanUniquenessJustReturnTrueIfValueWasNotChanged.
public final void shouldCheckEanUniquenessJustReturnTrueIfValueWasNotChanged() {
// given
// for example when updating product without modifying ean
String oldVal = "123456";
String newVal = oldVal;
// when
boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
// then
assertTrue(isValid);
verify(entity, never()).addError(any(FieldDefinition.class), anyString());
}
use of com.qcadoo.model.api.FieldDefinition in project mes by qcadoo.
the class ProductValidatorsTest method shouldCheckEanUniquenessJustReturnTrueIfNewEanIsEmpty.
@Test
public final void shouldCheckEanUniquenessJustReturnTrueIfNewEanIsEmpty() {
// given
String oldVal = "123456";
String newVal = "";
// when
boolean isValid = productValidators.checkEanUniqueness(dataDefinition, fieldDefinition, entity, oldVal, newVal);
// then
assertTrue(isValid);
verify(entity, never()).addError(any(FieldDefinition.class), anyString());
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class DefaultEntityTest method init.
@Before
public final void init() {
belongsToFieldDefinition = mock(FieldDefinition.class);
final BelongsToType belongsToType = mock(BelongsToType.class);
when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
belongsToFieldDataDefinition = mock(DataDefinition.class);
when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
decimalFieldDefinition = mock(FieldDefinition.class);
final DecimalType decimalType = new DecimalType();
when(decimalFieldDefinition.getType()).thenReturn(decimalType);
integerFieldDefinition = mock(FieldDefinition.class);
final IntegerType integerType = new IntegerType();
when(integerFieldDefinition.getType()).thenReturn(integerType);
manyToManyFieldDefinition = mock(FieldDefinition.class);
final ManyToManyType manyToManyType = mock(ManyToManyType.class);
when(manyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
manyToManyDataDefinition = mock(DataDefinition.class);
when(manyToManyFieldDefinition.getDataDefinition()).thenReturn(manyToManyDataDefinition);
secondManyToManyFieldDefinition = mock(FieldDefinition.class);
when(secondManyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
secondManyToManyDataDefinition = mock(DataDefinition.class);
when(secondManyToManyFieldDefinition.getDataDefinition()).thenReturn(secondManyToManyDataDefinition);
hasManyFieldDefinition = mock(FieldDefinition.class);
final HasManyType hasManyType = mock(HasManyType.class);
when(hasManyType.getJoinFieldName()).thenReturn(BELONGS_TO_FIELD_NAME);
when(hasManyFieldDefinition.getType()).thenReturn(hasManyType);
hasManyDataDefinition = mock(DataDefinition.class);
when(hasManyFieldDefinition.getDataDefinition()).thenReturn(hasManyDataDefinition);
stringFieldDefinition = mock(FieldDefinition.class);
when(stringFieldDefinition.isPersistent()).thenReturn(false);
dataDefinition = mock(DataDefinition.class);
booleanFieldDefinition = mock(FieldDefinition.class);
defaultEntity = new DefaultEntity(dataDefinition);
final Map<String, FieldDefinition> fieldsMap = Maps.newHashMap();
fieldsMap.put(BELONGS_TO_FIELD_NAME, belongsToFieldDefinition);
fieldsMap.put(STRING_FIELD_NAME, stringFieldDefinition);
fieldsMap.put(BOOLEAN_FIELD_NAME, booleanFieldDefinition);
fieldsMap.put(DECIMAL_FIELD_NAME, decimalFieldDefinition);
fieldsMap.put(INTEGER_FIELD_NAME, integerFieldDefinition);
fieldsMap.put(MANY_TO_MANY_FIELD_NAME, manyToManyFieldDefinition);
fieldsMap.put(SECOND_MANY_TO_MANY_FIELD_NAME, secondManyToManyFieldDefinition);
fieldsMap.put(HAS_MANY_FIELD_NAME, hasManyFieldDefinition);
for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
}
when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.
the class DataAccessServiceImpl method deleteEntity.
private EntityOpResult deleteEntity(final InternalDataDefinition dataDefinition, final Long entityId, final boolean testOnly, final Set<EntitySignature> traversedEntities) {
Object databaseEntity = getDatabaseEntity(dataDefinition, entityId);
if (databaseEntity == null) {
logEntityDebug(dataDefinition, entityId, "has been deleted earlier, for example onDelete hook");
return new EntityOpResult(true, new EntityMessagesHolderImpl());
}
Entity entity = get(dataDefinition, entityId);
if (!dataDefinition.callDeleteHook(entity)) {
logDeletionErrors(entity);
entity.addGlobalError("qcadooView.message.deleteFailedMessage");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return EntityOpResult.failure(entity);
}
priorityService.deprioritizeEntity(dataDefinition, databaseEntity);
Map<String, FieldDefinition> fields = dataDefinition.getFields();
for (FieldDefinition fieldDefinition : fields.values()) {
if (fieldDefinition.getType() instanceof CollectionFieldType) {
CollectionFieldType collectionFieldType = (CollectionFieldType) fieldDefinition.getType();
@SuppressWarnings("unchecked") Collection<Entity> children = (Collection<Entity>) entity.getField(fieldDefinition.getName());
EntityOpResult cascadeDeletionRes = performCascadeStrategy(entity, collectionFieldType, children, traversedEntities);
if (!cascadeDeletionRes.isSuccessfull()) {
return cascadeDeletionRes;
}
}
}
if (testOnly) {
logEntityInfo(dataDefinition, entityId, "may be cascade deleted");
} else {
try {
databaseEntity = getDatabaseEntity(dataDefinition, entityId);
if (databaseEntity != null) {
hibernateService.getCurrentSession().delete(databaseEntity);
hibernateService.getCurrentSession().flush();
}
} catch (ConstraintViolationException e) {
throw new IllegalStateException(getConstraintViolationMessage(entity), e);
}
logEntityDebug(dataDefinition, entityId, "has been deleted");
}
return new EntityOpResult(true, entity);
}
Aggregations