Search in sources :

Example 6 with FieldMetadata

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();
}
Also used : FieldMetadata(org.motechproject.mds.domain.FieldMetadata)

Example 7 with FieldMetadata

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();
}
Also used : FieldMetadata(org.motechproject.mds.domain.FieldMetadata)

Example 8 with FieldMetadata

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");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) EntityDto(org.motechproject.mds.dto.EntityDto) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) FieldMetadata(org.motechproject.mds.domain.FieldMetadata) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) DraftData(org.motechproject.mds.dto.DraftData) EntityDraft(org.motechproject.mds.domain.EntityDraft) HashSet(java.util.HashSet) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 9 with FieldMetadata

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);
}
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 10 with FieldMetadata

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

FieldMetadata (org.motechproject.mds.domain.FieldMetadata)10 Field (org.motechproject.mds.domain.Field)4 Type (org.motechproject.mds.domain.Type)3 FieldSetting (org.motechproject.mds.domain.FieldSetting)2 TypeSetting (org.motechproject.mds.domain.TypeSetting)2 HashSet (java.util.HashSet)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1 EntityDraft (org.motechproject.mds.domain.EntityDraft)1 FieldValidation (org.motechproject.mds.domain.FieldValidation)1 Lookup (org.motechproject.mds.domain.Lookup)1 TypeValidation (org.motechproject.mds.domain.TypeValidation)1 DraftData (org.motechproject.mds.dto.DraftData)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 MetadataDto (org.motechproject.mds.dto.MetadataDto)1 SettingDto (org.motechproject.mds.dto.SettingDto)1