Search in sources :

Example 16 with Type

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

the class PropertyBuilder method create.

public static Property create(Field field, Object value) {
    String name = field.getName();
    Type type = field.getType();
    if (type.isCombobox()) {
        ComboboxHolder holder = new ComboboxHolder(field);
        if (holder.isCollection()) {
            if (holder.getTypeClassName().equals(List.class.getName())) {
                return new CollectionProperty(name, value, new ArrayList(), holder.getUnderlyingType());
            } else if (holder.getTypeClassName().equals(Set.class.getName())) {
                return new CollectionProperty(name, value, new HashSet(), holder.getUnderlyingType());
            }
        }
    }
    return create(name, value, field.getType().getTypeClassName());
}
Also used : Type(org.motechproject.mds.domain.Type) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 17 with Type

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

the class HistoryServiceTest method shouldCreateNewRecord.

@Test
public void shouldCreateNewRecord() throws Exception {
    Entity entity = mock(Entity.class);
    Field idField = mock(Field.class);
    doReturn("id").when(idField).getName();
    Type idType = mock(Type.class);
    doReturn(idType).when(idField).getType();
    doReturn(Long.class.getName()).when(idType).getTypeClassName();
    doReturn(Long.class).when(idType).getTypeClass();
    Field valueField = mock(Field.class);
    doReturn("value").when(valueField).getName();
    Type valueType = mock(Type.class);
    doReturn(valueType).when(valueField).getType();
    doReturn(String.class.getName()).when(valueType).getTypeClassName();
    doReturn(String.class).when(valueType).getTypeClass();
    Field dateField = mock(Field.class);
    doReturn("date").when(dateField).getName();
    Type dateType = mock(Type.class);
    doReturn(dateType).when(dateField).getType();
    doReturn(Date.class.getName()).when(dateType).getTypeClassName();
    doReturn(Date.class).when(dateType).getTypeClass();
    doReturn(17L).when(entity).getEntityVersion();
    doReturn(true).when(entity).isRecordHistory();
    doReturn(Arrays.asList(idField, valueField, dateField)).when(entity).getFields();
    doReturn(null).when(query).execute(anyLong());
    final String serviceName = ClassName.getServiceName(Record.class.getName());
    doReturn(true).when(applicationContext).containsBean(serviceName);
    doReturn(dataService).when(applicationContext).getBean(serviceName);
    doReturn(4L).when(dataService).getSchemaVersion();
    Record instance = new Record();
    historyService.record(instance);
    verify(manager).makePersistent(recordHistoryCaptor.capture());
    Record__History history = recordHistoryCaptor.getValue();
    assertEquals(instance.getId(), history.getRecord__HistoryCurrentVersion());
    assertEquals(Long.valueOf(4), history.getRecord__HistorySchemaVersion());
    assertEquals(instance.getValue(), history.getValue());
    assertEquals(instance.getDate(), history.getDate());
}
Also used : Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) Matchers.anyLong(org.mockito.Matchers.anyLong) Record(org.motechproject.mds.testutil.records.Record) Matchers.anyString(org.mockito.Matchers.anyString) Record__History(org.motechproject.mds.testutil.records.history.Record__History) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with Type

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

the class EntityHelper method addDefaultFields.

/**
 * Adds default fields to entity
 * @param entity entity to add fields to
 * @param allTypes types repository
 */
public static void addDefaultFields(Entity entity, AllTypes allTypes) {
    Type longType = allTypes.retrieveByClassName(Long.class.getName());
    Type stringType = allTypes.retrieveByClassName(String.class.getName());
    Type dateTimeType = allTypes.retrieveByClassName(DateTime.class.getName());
    Field idField = new Field(entity, Constants.Util.ID_FIELD_NAME, Constants.Util.ID_DISPLAY_FIELD_NAME, longType, true, true, true);
    idField.addMetadata(new FieldMetadata(idField, AUTO_GENERATED, TRUE));
    Field creatorField = new Field(entity, Constants.Util.CREATOR_FIELD_NAME, Constants.Util.CREATOR_DISPLAY_FIELD_NAME, stringType, true, false, true);
    creatorField.addMetadata(new FieldMetadata(creatorField, AUTO_GENERATED, TRUE));
    Field ownerField = new Field(entity, Constants.Util.OWNER_FIELD_NAME, Constants.Util.OWNER_DISPLAY_FIELD_NAME, stringType, false, false, true);
    ownerField.addMetadata(new FieldMetadata(ownerField, AUTO_GENERATED_EDITABLE, TRUE));
    Field modifiedByField = new Field(entity, Constants.Util.MODIFIED_BY_FIELD_NAME, Constants.Util.MODIFIED_BY_DISPLAY_FIELD_NAME, stringType, true, false, true);
    modifiedByField.addMetadata(new FieldMetadata(modifiedByField, AUTO_GENERATED, TRUE));
    Field modificationDateField = new Field(entity, Constants.Util.MODIFICATION_DATE_FIELD_NAME, Constants.Util.MODIFICATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false, true);
    modificationDateField.addMetadata(new FieldMetadata(modificationDateField, AUTO_GENERATED, TRUE));
    Field creationDateField = new Field(entity, Constants.Util.CREATION_DATE_FIELD_NAME, Constants.Util.CREATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false, true);
    creationDateField.addMetadata(new FieldMetadata(creationDateField, AUTO_GENERATED, TRUE));
    entity.addField(idField);
    entity.addField(creatorField);
    entity.addField(ownerField);
    entity.addField(modifiedByField);
    entity.addField(creationDateField);
    entity.addField(modificationDateField);
}
Also used : Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) DateTime(org.joda.time.DateTime)

Example 19 with Type

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

the class EntityServiceImpl method addField.

private void addField(Entity entity, FieldDto fieldDto) {
    FieldBasicDto basic = fieldDto.getBasic();
    String typeClass = fieldDto.getType().getTypeClass();
    Type type = allTypes.retrieveByClassName(typeClass);
    Field field = new Field(entity, basic.getName(), basic.getDisplayName(), basic.isRequired(), basic.isUnique(), fieldDto.isReadOnly(), fieldDto.isNonEditable(), fieldDto.isNonDisplayable(), (String) basic.getDefaultValue(), basic.getTooltip(), basic.getPlaceholder(), null);
    field.setType(type);
    if (type.hasSettings()) {
        for (TypeSetting setting : type.getSettings()) {
            SettingDto settingDto = fieldDto.getSetting(setting.getName());
            FieldSetting fieldSetting = new FieldSetting(field, setting);
            if (null != settingDto) {
                fieldSetting.setValue(settingDto.getValueAsString());
            }
            field.addSetting(fieldSetting);
        }
    }
    if (type.hasValidation()) {
        for (TypeValidation validation : type.getValidations()) {
            FieldValidation fieldValidation = new FieldValidation(field, validation);
            FieldValidationDto validationDto = fieldDto.getValidation();
            if (null != validationDto) {
                ValidationCriterionDto criterion = validationDto.getCriterion(validation.getDisplayName());
                if (null != criterion) {
                    fieldValidation.setValue(criterion.valueAsString());
                    fieldValidation.setEnabled(criterion.isEnabled());
                }
            }
            field.addValidation(fieldValidation);
        }
    }
    for (MetadataDto metadata : fieldDto.getMetadata()) {
        field.addMetadata(new FieldMetadata(metadata));
    }
    entity.addField(field);
}
Also used : FieldMetadata(org.motechproject.mds.domain.FieldMetadata) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldSetting(org.motechproject.mds.domain.FieldSetting) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeValidation(org.motechproject.mds.domain.TypeValidation) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) TypeSetting(org.motechproject.mds.domain.TypeSetting) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) SettingDto(org.motechproject.mds.dto.SettingDto) FieldValidation(org.motechproject.mds.domain.FieldValidation)

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