Search in sources :

Example 6 with Type

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

the class EntityServiceImpl method addRelatedField.

private void addRelatedField(Field draftField, List<String> modulesToRefresh) {
    Entity entity = allEntities.retrieveByClassName(draftField.getMetadataValue(RELATED_CLASS));
    String fieldName = draftField.getMetadataValue(RELATED_FIELD);
    String collectionType = draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE);
    String relatedClass = draftField.getEntity().getClassName();
    Set<Lookup> fieldLookups = new HashSet<>();
    Field relatedField = new Field(entity, fieldName, fieldName, false, false, false, false, false, null, null, null, fieldLookups);
    Type type = allTypes.retrieveByClassName(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass());
    relatedField.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            relatedField.addSetting(new FieldSetting(relatedField, setting));
        }
    }
    relatedField.setUIDisplayable(true);
    relatedField.setUIDisplayPosition((long) entity.getFields().size());
    FieldHelper.createMetadataForManyToManyRelationship(relatedField, relatedClass, collectionType, draftField.getName(), false);
    entity.addField(relatedField);
    entity.incrementVersion();
    addModuleToRefresh(entity, modulesToRefresh);
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) HashSet(java.util.HashSet)

Example 7 with Type

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

the class EntityServiceImpl method getSchema.

@Override
@Transactional
public SchemaHolder getSchema() {
    StopWatch stopWatch = new StopWatch();
    SchemaHolder entitiesHolder = new SchemaHolder();
    LOGGER.debug("Retrieving entities for processing");
    stopWatch.start();
    List<Entity> entities = allEntities.getActualEntities();
    stopWatch.stop();
    LOGGER.debug("{} entities retrieved in {} ms", entities.size(), stopWatch.getTime());
    StopWatchHelper.restart(stopWatch);
    for (Entity entity : entities) {
        LOGGER.debug("Preparing entity: {}", entity.getClassName());
        StopWatchHelper.restart(stopWatch);
        EntityDto entityDto = entity.toDto();
        stopWatch.stop();
        LOGGER.debug("Entity dto created in {} ms", stopWatch.getTime());
        StopWatchHelper.restart(stopWatch);
        AdvancedSettingsDto advSettingsDto = entity.advancedSettingsDto();
        stopWatch.stop();
        LOGGER.debug("Advanced settings dto created in {} ms", stopWatch.getTime());
        StopWatchHelper.restart(stopWatch);
        List<FieldDto> fieldDtos = entity.getFieldDtos();
        stopWatch.stop();
        LOGGER.debug("Field dtos created in {} ms", stopWatch.getTime());
        StopWatchHelper.restart(stopWatch);
        entitiesHolder.addEntity(entityDto, advSettingsDto, fieldDtos);
        stopWatch.stop();
        LOGGER.debug("Result stored in {} ms", stopWatch.getTime());
    }
    LOGGER.debug("Retrieving types for processing");
    List<Type> types = allTypes.retrieveAll();
    for (Type type : types) {
        TypeDto typeDto = type.toDto();
        entitiesHolder.addType(typeDto);
        entitiesHolder.addTypeValidation(typeDto, type.getTypeValidationDtos());
    }
    LOGGER.debug("Entities holder ready");
    return entitiesHolder;
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) EntityDto(org.motechproject.mds.dto.EntityDto) Type(org.motechproject.mds.domain.Type) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) TypeDto(org.motechproject.mds.dto.TypeDto) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) StopWatch(org.apache.commons.lang.time.StopWatch) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Type

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

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

use of org.motechproject.mds.domain.Type 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)

Aggregations

Type (org.motechproject.mds.domain.Type)19 Field (org.motechproject.mds.domain.Field)14 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)7 TypeSetting (org.motechproject.mds.domain.TypeSetting)7 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 HashSet (java.util.HashSet)5 TypeValidation (org.motechproject.mds.domain.TypeValidation)4 ArrayList (java.util.ArrayList)3 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)3 Lookup (org.motechproject.mds.domain.Lookup)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 FieldDto (org.motechproject.mds.dto.FieldDto)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)2 FieldValidation (org.motechproject.mds.domain.FieldValidation)2 MdsEntity (org.motechproject.mds.domain.MdsEntity)2 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)2 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)2