use of org.motechproject.mds.domain.FieldSetting 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");
}
use of org.motechproject.mds.domain.FieldSetting in project motech by motech.
the class FieldReader method readSettings.
private void readSettings(Field field) throws IOException {
objectReader.expect("settings");
jsonReader.beginArray();
while (jsonReader.hasNext()) {
FieldSetting setting = new FieldSetting();
setting.setField(field);
jsonReader.beginObject();
setting.setDetails(getTypeSetting(field.getType(), objectReader.readString("key")));
setting.setValue(objectReader.readString("value"));
jsonReader.endObject();
field.addSetting(setting);
}
jsonReader.endArray();
}
use of org.motechproject.mds.domain.FieldSetting 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