Search in sources :

Example 46 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceContextIT method shouldUpdateRestOptions.

@Test
public void shouldUpdateRestOptions() throws IOException {
    // given
    EntityDto entityDto = new EntityDto();
    entityDto.setName("RestTestEnt");
    entityDto = entityService.createEntity(entityDto);
    FieldDto boolField = FieldTestHelper.fieldDto("boolField", Boolean.class);
    FieldDto dtField = FieldTestHelper.fieldDto("dtField", DateTime.class);
    FieldDto strField = FieldTestHelper.fieldDto("strField", String.class);
    entityService.addFields(entityDto, asList(boolField, dtField, strField));
    List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("boolField", "dtField", "strField");
    LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);
    List<LookupFieldDto> strLookupFieldDtos = lookupFieldsFromNames("strField");
    LookupDto strLookup = new LookupDto("strLookup", false, false, strLookupFieldDtos, true);
    entityService.addLookups(entityDto.getId(), asList(lookup, strLookup));
    RestOptionsDto restOptionsDto = new RestOptionsDto(true, false, true, false, false);
    restOptionsDto.addField("boolField");
    restOptionsDto.addField("strField");
    restOptionsDto.addLookup("strLookup");
    // when
    entityService.updateRestOptions(entityDto.getId(), restOptionsDto);
    // then
    AdvancedSettingsDto advancedSettingsDto = entityService.getAdvancedSettings(entityDto.getId(), true);
    RestOptionsDto fromDb = advancedSettingsDto.getRestOptions();
    assertTrue(fromDb.isCreate());
    assertFalse(fromDb.isRead());
    assertTrue(fromDb.isUpdate());
    assertFalse(fromDb.isDelete());
    assertEquals(asList("strLookup"), fromDb.getLookupNames());
    assertEquals(asList("boolField", "strField"), fromDb.getFieldNames());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 47 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceContextIT method testRelatedFieldGenerationForManyToManyRelationship.

@Test
public void testRelatedFieldGenerationForManyToManyRelationship() {
    EntityDto entityDto1 = new EntityDto();
    entityDto1.setName("RelationTestEnt1");
    entityDto1 = entityService.createEntity(entityDto1);
    EntityDto entityDto2 = new EntityDto();
    entityDto2.setName("RelationTestEnt2");
    entityDto2 = entityService.createEntity(entityDto2);
    EntityDto entityDto3 = new EntityDto();
    entityDto3.setName("RelationTestEnt3");
    entityDto3 = entityService.createEntity(entityDto3);
    EntityDraft entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    Set<Lookup> fieldLookups = new HashSet<>();
    Field field = new Field(entityDraft1, "newField", "Display Name", fieldLookups);
    Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            field.addSetting(new FieldSetting(field, setting));
        }
    }
    FieldMetadata metadata = new FieldMetadata(field, RELATED_CLASS);
    metadata.setValue(entityDto2.getClassName());
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, RELATIONSHIP_COLLECTION_TYPE);
    metadata.setValue("java.util.Set");
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, RELATED_FIELD);
    metadata.setValue("relatedField");
    field.addMetadata(metadata);
    metadata = new FieldMetadata(field, OWNING_SIDE);
    metadata.setValue("true");
    entityDraft1.addField(field);
    allEntityDrafts.update(entityDraft1);
    entityService.commitChanges(entityDto1.getId());
    FieldDto relatedField = getField(entityDto2.getId(), "relatedField");
    // Changing related class
    entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    DraftData draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.0.value", entityDto3.getClassName());
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    entityService.commitChanges(entityDto1.getId());
    // We changed related entity, so the old related entity field must be removed
    relatedField = getField(entityDto2.getId(), "relatedField");
    assertNull(relatedField);
    relatedField = getField(entityDto3.getId(), "relatedField");
    assertRelatedField(entityDto1, relatedField, "java.util.Set");
    entityDraft1 = entityService.getEntityDraft(entityDto1.getId());
    draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.1.value", "java.util.List");
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    draftData = DraftBuilder.forFieldEdit(entityDraft1.getField("newField").getId(), "metadata.2.value", "newNameForRelatedField");
    entityService.saveDraftEntityChanges(entityDto1.getId(), draftData);
    entityService.commitChanges(entityDto1.getId());
    relatedField = getField(entityDto3.getId(), "newNameForRelatedField");
    assertRelatedField(entityDto1, relatedField, "java.util.List");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) DraftData(org.motechproject.mds.dto.DraftData) EntityDraft(org.motechproject.mds.domain.EntityDraft) HashSet(java.util.HashSet) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 48 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceContextIT method shouldNotAddFieldsWithTheSameNameToAnEntity.

@Test
public void shouldNotAddFieldsWithTheSameNameToAnEntity() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("SameFieldTest");
    entityDto = entityService.createEntity(entityDto);
    FieldDto field1 = new FieldDto();
    field1.setBasic(new FieldBasicDto("disp", "name"));
    field1.setType(typeService.findType(Boolean.class));
    FieldDto field2 = new FieldDto();
    field2.setBasic(new FieldBasicDto("dispName2", "name"));
    field2.setType(typeService.findType(Integer.class));
    entityService.addFields(entityDto, asList(field1, field2));
    List<FieldDto> fieldsFromDb = entityService.getFields(entityDto.getId());
    assertNotNull(fieldsFromDb);
    assertEquals(7, fieldsFromDb.size());
    assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "dispName2"), extract(fieldsFromDb, on(FieldDto.class).getBasic().getDisplayName()));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 49 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityServiceContextIT method testDraftWorkflow.

@Test
public void testDraftWorkflow() throws IOException {
    EntityDto entityDto = new EntityDto();
    entityDto.setName("DraftTest");
    entityDto = entityService.createEntity(entityDto);
    final Long entityId = entityDto.getId();
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    List<FieldDto> fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate", "f1name"), extract(fields, on(FieldDto.class).getBasic().getName()));
    // check if abandoning works
    entityService.abandonChanges(entityId);
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("id", "creator", "owner", "modifiedBy", "creationDate", "modificationDate"), extract(fields, on(FieldDto.class).getBasic().getName()));
    // check add-edit-commit
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forNewField("disp", "f1name", Long.class.getName()));
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(7, fields.size());
    FieldDto field = selectFirst(fields, having(on(FieldDto.class).getBasic().getName(), equalTo("f1name")));
    entityService.saveDraftEntityChanges(entityDto.getId(), DraftBuilder.forFieldEdit(field.getId(), "basic.displayName", "newDisp"));
    fields = entityService.getFields(entityId);
    assertNotNull(fields);
    assertEquals(asList("Id", "Created By", "Owner", "Modified By", "Creation Date", "Modification Date", "newDisp"), extract(fields, on(FieldDto.class).getBasic().getDisplayName()));
    entityService.commitChanges(entityId);
    // check if changes were persisted in db
    Entity entityFromDb = getEntities().get(0);
    assertEquals(7, entityFromDb.getFields().size());
    Field fieldFromDb = entityFromDb.getField("f1name");
    assertNotNull(fieldFromDb);
    assertEquals("newDisp", fieldFromDb.getDisplayName());
    // no drafts in db
    assertTrue(entityService.listWorkInProgress().isEmpty());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 50 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class DisplayHelperTest method shouldReturnNullForRegularFields.

@Test
public void shouldReturnNullForRegularFields() {
    FieldDto field = FieldTestHelper.fieldDto("strField", String.class);
    assertNull(DisplayHelper.getDisplayValueForField(field, "value"));
    field = FieldTestHelper.fieldDto("intField", Integer.class);
    assertNull(DisplayHelper.getDisplayValueForField(field, 14));
    field = FieldTestHelper.fieldDto("dtField", DateTime.class);
    assertNull(DisplayHelper.getDisplayValueForField(field, DateUtil.now()));
}
Also used : DateTime(org.joda.time.DateTime) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7