use of org.motechproject.mds.domain.FieldMetadata in project motech by motech.
the class FieldReader method readMetadata.
private void readMetadata(Field field) throws IOException {
objectReader.expect("metadata");
jsonReader.beginArray();
while (jsonReader.hasNext()) {
FieldMetadata metadata = new FieldMetadata();
metadata.setField(field);
jsonReader.beginObject();
metadata.setKey(objectReader.readString("key"));
metadata.setValue(objectReader.readString("value"));
jsonReader.endObject();
field.addMetadata(metadata);
}
jsonReader.endArray();
}
use of org.motechproject.mds.domain.FieldMetadata in project motech by motech.
the class FieldWriter method writeMetadata.
private void writeMetadata(List<FieldMetadata> metadata) throws IOException {
jsonWriter.name("metadata");
jsonWriter.beginArray();
for (FieldMetadata metadataEntry : metadata) {
jsonWriter.beginObject();
jsonWriter.name("key").value(metadataEntry.getKey());
jsonWriter.name("value").value(metadataEntry.getValue());
jsonWriter.endObject();
}
jsonWriter.endArray();
}
use of org.motechproject.mds.domain.FieldMetadata 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.FieldMetadata 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.FieldMetadata 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