Search in sources :

Example 1 with FieldHookDefinition

use of com.qcadoo.model.internal.api.FieldHookDefinition in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField.

@Test
public void shouldCopyEntityWithUniqueField() throws Exception {
    // given
    FieldHookDefinition fieldHook = new UniqueValidator();
    // fieldHook.initialize(dataDefinition, fieldDefinitionName);
    fieldDefinitionName.withValidator(fieldHook);
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName("Mr T");
    simpleDatabaseObject.setAge(66);
    given(criteria.setProjection(Projections.rowCount()).uniqueResult()).willReturn(0);
    given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
    // when
    List<Entity> entities = dataDefinition.copy(new Long[] { 13L });
    // then
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    Assert.assertEquals(66, entities.get(0).getField("age"));
    Assert.assertEquals("Mr T(1)", entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 2 with FieldHookDefinition

use of com.qcadoo.model.internal.api.FieldHookDefinition in project qcadoo by qcadoo.

the class DecimalType method getMissingValidators.

@Override
public Collection<FieldHookDefinition> getMissingValidators(final Iterable<FieldHookDefinition> validators) {
    FieldHookDefinition defaultScaleValidator = new ScaleValidator(null, null, NumberService.DEFAULT_MAX_FRACTION_DIGITS_IN_DECIMAL);
    FieldHookDefinition defaultUnscaledValueValidator = new UnscaledValueValidator(null, null, NumberService.DEFAULT_MAX_INTEGER_DIGITS_IN_DECIMAL);
    Set<FieldHookDefinition> missingValidators = Sets.<FieldHookDefinition>newHashSet(defaultScaleValidator, defaultUnscaledValueValidator);
    for (FieldHookDefinition validator : validators) {
        if (validator instanceof UnscaledValueValidator && ((UnscaledValueValidator) validator).hasUppuerBoundDefined()) {
            missingValidators.remove(defaultUnscaledValueValidator);
        }
        if (validator instanceof ScaleValidator && ((ScaleValidator) validator).hasUppuerBoundDefined()) {
            missingValidators.remove(defaultScaleValidator);
        }
    }
    return missingValidators;
}
Also used : FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UnscaledValueValidator(com.qcadoo.model.internal.validators.UnscaledValueValidator) ScaleValidator(com.qcadoo.model.internal.validators.ScaleValidator)

Example 3 with FieldHookDefinition

use of com.qcadoo.model.internal.api.FieldHookDefinition in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueFieldWhenNull.

@Test
public void shouldCopyEntityWithUniqueFieldWhenNull() throws Exception {
    // given
    FieldHookDefinition fieldHook = new UniqueValidator();
    // fieldHook.initialize(dataDefinition, fieldDefinitionName);
    fieldDefinitionName.withValidator(fieldHook);
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName(null);
    simpleDatabaseObject.setAge(66);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(0);
    given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
    // when
    List<Entity> entities = dataDefinition.copy(new Long[] { 13L });
    // then
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    Assert.assertEquals(66, entities.get(0).getField("age"));
    Assert.assertEquals(null, entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Example 4 with FieldHookDefinition

use of com.qcadoo.model.internal.api.FieldHookDefinition in project qcadoo by qcadoo.

the class DataAccessServiceCopyTest method shouldCopyEntityWithUniqueField2.

@Test
public void shouldCopyEntityWithUniqueField2() throws Exception {
    // given
    FieldHookDefinition fieldHook = new UniqueValidator();
    // fieldHook.initialize(dataDefinition, fieldDefinitionName);
    fieldDefinitionName.withValidator(fieldHook);
    SampleSimpleDatabaseObject simpleDatabaseObject = new SampleSimpleDatabaseObject();
    simpleDatabaseObject.setId(13L);
    simpleDatabaseObject.setName("Mr T(1)");
    simpleDatabaseObject.setAge(66);
    given(hibernateService.getTotalNumberOfEntities(Mockito.any(Criteria.class))).willReturn(1, 0);
    given(session.get(any(Class.class), Matchers.anyInt())).willReturn(simpleDatabaseObject);
    // when
    List<Entity> entities = dataDefinition.copy(new Long[] { 13L });
    // then
    verify(session, times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
    assertEquals(1, entities.size());
    assertTrue(entities.get(0).isValid());
    Assert.assertEquals(66, entities.get(0).getField("age"));
    Assert.assertEquals("Mr T(3)", entities.get(0).getField(NAME));
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldHookDefinition(com.qcadoo.model.internal.api.FieldHookDefinition) UniqueValidator(com.qcadoo.model.internal.validators.UniqueValidator) Criteria(org.hibernate.Criteria) SampleSimpleDatabaseObject(com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject) Test(org.junit.Test)

Aggregations

FieldHookDefinition (com.qcadoo.model.internal.api.FieldHookDefinition)4 Entity (com.qcadoo.model.api.Entity)3 SampleSimpleDatabaseObject (com.qcadoo.model.beans.sample.SampleSimpleDatabaseObject)3 UniqueValidator (com.qcadoo.model.internal.validators.UniqueValidator)3 Test (org.junit.Test)3 Criteria (org.hibernate.Criteria)2 ScaleValidator (com.qcadoo.model.internal.validators.ScaleValidator)1 UnscaledValueValidator (com.qcadoo.model.internal.validators.UnscaledValueValidator)1