Search in sources :

Example 36 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityValidator method validateEntityComboboxesValues.

private void validateEntityComboboxesValues(EntityDraft draft) {
    Entity parent = draft.getParentEntity();
    List<Field> oldComboboxFields = draft.getParentEntity().getStringComboboxFields();
    List<Field> comboboxFields = draft.getStringComboboxFields();
    for (Field field : substract(oldComboboxFields, comboboxFields)) {
        String tableName;
        String fieldName;
        if (!field.isMultiSelectCombobox()) {
            fieldName = field.getName();
            tableName = ClassTableName.getTableName(parent);
        } else {
            fieldName = "ELEMENT";
            tableName = ClassTableName.getTableName(parent) + "_" + field.getName().toUpperCase();
        }
        List<?> values = executeSQLQuery(prepareSelectDistincQuery(tableName, fieldName));
        validateUserSuppliedValuesUsageForField(values, draft.getField(field.getName()));
    }
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field)

Example 37 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class AllEntityDrafts method setProperties.

public void setProperties(EntityDraft draft, Entity entity, String username) {
    draft.setParentEntity(entity);
    draft.setParentVersion(entity.getEntityVersion());
    draft.setDraftOwnerUsername(username);
    draft.setLastModificationDate(DateUtil.nowUTC());
    draft.setName(entity.getName());
    draft.setClassName(entity.getClassName());
    draft.setNamespace(entity.getNamespace());
    draft.setModule(entity.getModule());
    draft.setSecurityMode(entity.getSecurityMode());
    draft.setSecurityMembers(new HashSet<>(entity.getSecurityMembers()));
    draft.setReadOnlySecurityMode(entity.getReadOnlySecurityMode());
    draft.setReadOnlySecurityMembers(new HashSet<>(entity.getReadOnlySecurityMembers()));
    draft.getFields().clear();
    for (Field field : entity.getFields()) {
        draft.addField(field.copy());
    }
    draft.getLookups().clear();
    for (Lookup lookup : entity.getLookups()) {
        Lookup newLookup = lookup.copy(draft.getFields());
        draft.addLookup(newLookup);
    }
    if (entity.getRestOptions() != null) {
        RestOptions restOptions = entity.getRestOptions().copy();
        restOptions.setEntity(draft);
        draft.setRestOptions(restOptions);
    }
    if (entity.getTracking() != null) {
        Tracking tracking = entity.getTracking().copy();
        tracking.setEntity(draft);
        draft.setTracking(tracking);
    }
}
Also used : Field(org.motechproject.mds.domain.Field) Tracking(org.motechproject.mds.domain.Tracking) Lookup(org.motechproject.mds.domain.Lookup) RestOptions(org.motechproject.mds.domain.RestOptions)

Example 38 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImplTest method shouldAddNewField.

@Test
public void shouldAddNewField() {
    // given
    long entityId = 1L;
    EntityDto dto = new EntityDto(entityId, CLASS_NAME);
    TypeDto type = TypeDto.INTEGER;
    Type integerType = new Type(Integer.class);
    FieldBasicDto basic = new FieldBasicDto();
    basic.setDisplayName("pi");
    basic.setName("pi");
    basic.setRequired(true);
    basic.setDefaultValue("3.14");
    basic.setTooltip("Sets the value of the PI number");
    basic.setPlaceholder("3.14");
    FieldDto fieldDto = new FieldDto();
    fieldDto.setEntityId(dto.getId());
    fieldDto.setType(type);
    fieldDto.setBasic(basic);
    // when
    doReturn(entity).when(allEntities).retrieveById(dto.getId());
    doReturn(entityId).when(entity).getId();
    doReturn(integerType).when(allTypes).retrieveByClassName(type.getTypeClass());
    entityService.addFields(dto, asList(fieldDto));
    // then
    verify(allEntities).retrieveById(dto.getId());
    verify(entity).addField(fieldCaptor.capture());
    Field field = fieldCaptor.getValue();
    assertEquals(basic.getName(), field.getName());
    assertEquals(basic.getDisplayName(), field.getDisplayName());
    assertEquals(basic.isRequired(), field.isRequired());
    assertEquals(basic.getDefaultValue(), field.getDefaultValue());
    assertEquals(basic.getTooltip(), field.getTooltip());
    assertEquals(basic.getPlaceholder(), field.getPlaceholder());
    assertEquals(fieldDto.getType().getTypeClass(), field.getType().getTypeClass().getName());
    assertEquals(fieldDto.getEntityId(), field.getEntity().getId());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 39 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImplTest method shouldSetUIFilterableOnlyToCorrectFields.

@Test
public void shouldSetUIFilterableOnlyToCorrectFields() {
    // given
    Field filterableField = mock(Field.class);
    Field nonFilterableField = mock(Field.class);
    // #1 when
    doReturn("filterableField").when(filterableField).getName();
    doReturn("nonFilterableField").when(nonFilterableField).getName();
    doReturn(1L).when(entityDto).getId();
    doReturn(entity).when(allEntities).retrieveById(1L);
    doReturn(asList(filterableField, nonFilterableField)).when(entity).getFields();
    entityService.addFilterableFields(entityDto, asList("filterableField"));
    // #1 then
    verify(allEntities).retrieveById(1L);
    verify(entity).getFields();
    verify(filterableField).setUIFilterable(true);
    verify(nonFilterableField).setUIFilterable(false);
    // #2 when
    entityService.addFilterableFields(entityDto, asList("nonFilterableField"));
    // #2 then
    verify(allEntities, times(2)).retrieveById(1L);
    verify(entity, times(2)).getFields();
    verify(filterableField).setUIFilterable(false);
    verify(nonFilterableField).setUIFilterable(true);
    // #3 when
    entityService.addFilterableFields(entityDto, asList("filterableField", "nonFilterableField"));
    // #3 then
    verify(allEntities, times(3)).retrieveById(1L);
    verify(entity, times(3)).getFields();
    verify(filterableField, times(2)).setUIFilterable(true);
    verify(nonFilterableField, times(2)).setUIFilterable(true);
    // #4 when
    entityService.addFilterableFields(entityDto, new ArrayList<String>());
    // #4 then
    verify(allEntities, times(4)).retrieveById(1L);
    verify(entity, times(4)).getFields();
    verify(filterableField, times(2)).setUIFilterable(false);
    verify(nonFilterableField, times(2)).setUIFilterable(false);
}
Also used : Field(org.motechproject.mds.domain.Field) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 40 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImplTest method shouldUpdateExistingField.

@Test
public void shouldUpdateExistingField() {
    // given
    long entityId = 1L;
    EntityDto dto = new EntityDto(entityId, CLASS_NAME);
    TypeDto type = TypeDto.INTEGER;
    Field field = mock(Field.class);
    FieldBasicDto basic = new FieldBasicDto();
    basic.setDisplayName("pi");
    basic.setName("pi");
    basic.setRequired(true);
    basic.setDefaultValue("3.14");
    basic.setTooltip("Sets the value of the PI number");
    basic.setPlaceholder("3.14");
    FieldDto fieldDto = new FieldDto();
    fieldDto.setEntityId(dto.getId());
    fieldDto.setType(type);
    fieldDto.setBasic(basic);
    // when
    doReturn(entity).when(allEntities).retrieveById(dto.getId());
    doReturn(entityId).when(entity).getId();
    doReturn(field).when(entity).getField(basic.getName());
    entityService.addFields(dto, asList(fieldDto));
    // then
    verify(allEntities).retrieveById(dto.getId());
    verify(entity).getField(basic.getName());
    verify(entity, never()).addField(any(Field.class));
    verify(field).update(fieldDto);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) TypeDto(org.motechproject.mds.dto.TypeDto) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

Field (org.motechproject.mds.domain.Field)73 Entity (org.motechproject.mds.domain.Entity)33 Test (org.junit.Test)24 Lookup (org.motechproject.mds.domain.Lookup)16 MdsEntity (org.motechproject.mds.domain.MdsEntity)15 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)15 Type (org.motechproject.mds.domain.Type)14 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)13 FieldDto (org.motechproject.mds.dto.FieldDto)12 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)12 HashSet (java.util.HashSet)8 Matchers.anyString (org.mockito.Matchers.anyString)6 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 TypeSetting (org.motechproject.mds.domain.TypeSetting)6 UserPreferences (org.motechproject.mds.domain.UserPreferences)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)5 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)4 LookupDto (org.motechproject.mds.dto.LookupDto)4