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());
}
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());
}
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);
}
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);
}
Aggregations