Search in sources :

Example 31 with Field

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

the class EnumBuilderImplTest method testBuild.

@Test
public void testBuild() throws Exception {
    String[] expectedValues = { "A", "B", "C", "D", "E" };
    TypeSetting typeSetting = new TypeSetting();
    typeSetting.setName(Constants.Settings.COMBOBOX_VALUES);
    FieldSetting fieldSetting = new FieldSetting(null, typeSetting, StringUtils.join(expectedValues, "\n"));
    Type type = new Type("mds.field.combobox", null, null);
    Field field = new Field(null, "swag", "swag", false, false, false, false, false, null, null, null, null);
    field.setType(type);
    field.addSetting(fieldSetting);
    Entity entity = new Entity(CLASS_NAME);
    entity.addField(field);
    ComboboxHolder holder = new ComboboxHolder(entity, field);
    ClassData data = new EnumBuilderImpl().build(holder);
    MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
    Class<?> enumClass = MDSClassLoader.getInstance().loadClass(data.getClassName());
    assertTrue("The class definition should be enum", enumClass.isEnum());
    Object[] enumConstants = enumClass.getEnumConstants();
    String[] actualValues = new String[enumConstants.length];
    for (int i = 0; i < enumConstants.length; i++) {
        actualValues[i] = enumConstants[i].toString();
    }
    Arrays.sort(expectedValues, String.CASE_INSENSITIVE_ORDER);
    Arrays.sort(actualValues, String.CASE_INSENSITIVE_ORDER);
    assertArrayEquals(expectedValues, actualValues);
}
Also used : Entity(org.motechproject.mds.domain.Entity) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) ClassData(org.motechproject.mds.domain.ClassData) TypeSetting(org.motechproject.mds.domain.TypeSetting) Test(org.junit.Test)

Example 32 with Field

use of org.motechproject.mds.domain.Field 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 33 with Field

use of org.motechproject.mds.domain.Field 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 34 with Field

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

the class UserPreferencesServiceImpl method unselectField.

@Override
@Transactional
public void unselectField(Long id, String username, String fieldName) {
    Entity entity = getEntity(id);
    UserPreferences userPreferences = allUserPreferences.retrieveByClassNameAndUsername(entity.getClassName(), username);
    userPreferences = checkPreferences(userPreferences, entity, username);
    Field field = entity.getField(fieldName);
    assertField(field, entity.getClassName(), fieldName);
    userPreferences.unselectField(field);
    allUserPreferences.update(userPreferences);
}
Also used : UserPreferences(org.motechproject.mds.domain.UserPreferences) AllUserPreferences(org.motechproject.mds.repository.internal.AllUserPreferences) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Transactional(org.springframework.transaction.annotation.Transactional)

Example 35 with Field

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

the class ComboboxValueRepositoryContextIT method shouldRetrieveComboboxSingleSelectValuesFromDb.

@Test
public void shouldRetrieveComboboxSingleSelectValuesFromDb() {
    Entity entity = getAllEntities().retrieveByClassName(ClassName.getEntityClassName(ENTITY_NAME));
    Field cbField = entity.getField(CB_FIELD_SINGLE_NAME);
    List<String> values = cbValueRepository.getComboboxValuesForStringField(entity.toDto(), cbField.toDto());
    assertNotNull(values);
    assertEquals(asList("five", "four", "one", "two"), values);
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) 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