use of org.motechproject.mds.domain.TypeSetting 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.*");
}
use of org.motechproject.mds.domain.TypeSetting 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);
}
use of org.motechproject.mds.domain.TypeSetting 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);
}
use of org.motechproject.mds.domain.TypeSetting 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);
}
use of org.motechproject.mds.domain.TypeSetting 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");
}
Aggregations