Search in sources :

Example 1 with Type

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

the class TrashServiceTest method shouldMoveObjectToTrash.

@Test
public void shouldMoveObjectToTrash() throws Exception {
    doReturn(Record__Trash.class).when(classLoader).loadClass(Record__Trash.class.getName());
    Entity entity = mock(Entity.class);
    Field field = mock(Field.class);
    Type type = mock(Type.class);
    doReturn(17L).when(entity).getEntityVersion();
    doReturn(field).when(entity).getField("id");
    doReturn(field).when(entity).getField("value");
    doReturn(type).when(field).getType();
    doReturn(false).when(type).isRelationship();
    doReturn(entity).when(allEntities).retrieveByClassName(anyString());
    Record instance = new Record();
    trashService.moveToTrash(instance, 1L);
    verify(manager).makePersistent(trashCaptor.capture());
    Record__Trash trash = trashCaptor.getValue();
    assertEquals(instance.getValue(), trash.getValue());
}
Also used : Record__Trash(org.motechproject.mds.testutil.records.history.Record__Trash) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) Record(org.motechproject.mds.testutil.records.Record) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Type

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

the class FieldTestHelper method fieldWithComboboxSettings.

public static Field fieldWithComboboxSettings(Entity entity, String name, String displayName, Class<?> typeClass, boolean allowsMultipleSelections, boolean allowsUserSupplied, List<String> items) {
    Type type = new Type();
    type.setTypeClass(typeClass);
    type.setDisplayName(Constants.DisplayNames.COMBOBOX);
    Field field = new Field();
    field.setName(name);
    field.setDisplayName(displayName);
    field.setType(type);
    field.setEntity(entity);
    setAllowUserSupplied(field, allowsUserSupplied);
    setAllowMultipleSelections(field, allowsMultipleSelections);
    setComboboxValues(field, items);
    return field;
}
Also used : Field(org.motechproject.mds.domain.Field) LookupFieldType(org.motechproject.mds.dto.LookupFieldType) Type(org.motechproject.mds.domain.Type)

Example 3 with Type

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

the class PropertyBuilderTest method shouldGenerateAppropriatePropertyType.

@Test
public void shouldGenerateAppropriatePropertyType() throws Exception {
    Set<String> set = new HashSet<>();
    Range<Integer> range = new Range<>(0, 1);
    Type type = new Type("mds.field.combobox", "", List.class);
    Entity entity = new Entity("org.motechproject.mds.Sample");
    Field field = new Field(entity, "roles", "roles", type, true, false, false);
    // should not create collection property for fields without list field
    assertProperty(PropertyBuilder.create("roles", 1L, Long.class.getName()), EqualProperty.class, "roles", 1L);
    field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS), "true"));
    // should create collection property for enum list
    assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
    field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_USER_SUPPLIED), "true"));
    // should create collection property for string list
    assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
    assertProperty(PropertyBuilder.create("set", set, String.class), SetProperty.class, "set", set);
    assertProperty(PropertyBuilder.create("range", range, Integer.class), RangeProperty.class, "range", range);
    assertProperty(PropertyBuilder.create("equal", 1L, Long.class), EqualProperty.class, "equal", 1L);
    assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches()"), MatchesProperty.class, "text", ".*someString.*");
    assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches((?i))"), MatchesCaseInsensitiveProperty.class, "text", "(?i).*someString.*");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeSetting(org.motechproject.mds.domain.TypeSetting) Range(org.motechproject.commons.api.Range) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Type

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

the class PropertyBuilderTest method shouldCreatePropertyByFieldAndValue.

@Test
public void shouldCreatePropertyByFieldAndValue() throws Exception {
    Type type = new Type(String.class);
    Field field = new Field(null, "ala", "ala", type, true, false, false);
    assertProperty(PropertyBuilder.create(field, "cat"), "ala", "cat");
}
Also used : Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) Test(org.junit.Test)

Example 5 with Type

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

the class EntityServiceImpl method createFieldForDraft.

private void createFieldForDraft(EntityDraft draft, DraftData draftData) {
    String typeClass = draftData.getValue(DraftData.TYPE_CLASS).toString();
    String displayName = draftData.getValue(DraftData.DISPLAY_NAME).toString();
    String name = draftData.getValue(DraftData.NAME).toString();
    Type type = ("textArea".equalsIgnoreCase(typeClass)) ? allTypes.retrieveByClassName("java.lang.String") : allTypes.retrieveByClassName(typeClass);
    if (type == null) {
        throw new NoSuchTypeException(typeClass);
    }
    Set<Lookup> fieldLookups = new HashSet<>();
    Field field = new Field(draft, name, displayName, fieldLookups);
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            field.addSetting(new FieldSetting(field, setting));
        }
    }
    if (type.hasValidation()) {
        for (TypeValidation validation : type.getValidations()) {
            field.addValidation(new FieldValidation(field, validation));
        }
    }
    if (TypeDto.BLOB.getTypeClass().equals(typeClass)) {
        field.setUIDisplayable(false);
    } else {
        field.setUIDisplayable(true);
        field.setUIDisplayPosition((long) draft.getFields().size());
    }
    if ("textArea".equalsIgnoreCase(typeClass)) {
        setSettingForTextArea(field);
    }
    FieldHelper.addMetadataForRelationship(typeClass, field);
    FieldHelper.addOrUpdateMetadataForCombobox(field);
    draft.addField(field);
    allEntityDrafts.update(draft);
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) NoSuchTypeException(org.motechproject.mds.exception.type.NoSuchTypeException) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) FieldValidation(org.motechproject.mds.domain.FieldValidation) HashSet(java.util.HashSet)

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