Search in sources :

Example 26 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImpl method removeRelatedFields.

private void removeRelatedFields(List<Field> fields, List<String> modulesToRefresh) {
    for (Field field : fields) {
        Entity entity = allEntities.retrieveByClassName(field.getMetadataValue(RELATED_CLASS));
        Field relatedField = entity.getField(field.getMetadataValue(RELATED_FIELD));
        entity.removeField(relatedField.getId());
        entity.incrementVersion();
        addModuleToRefresh(entity, modulesToRefresh);
    }
}
Also used : Field(org.motechproject.mds.domain.Field) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity)

Example 27 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImpl method addDisplayedFields.

@Override
@Transactional
public void addDisplayedFields(EntityDto entityDto, Map<String, Long> positions) {
    Entity entity = allEntities.retrieveById(entityDto.getId());
    assertEntityExists(entity, entityDto.getId());
    List<Field> fields = entity.getFields();
    if (MapUtils.isEmpty(positions)) {
        for (long i = 0; i < fields.size(); ++i) {
            Field field = fields.get((int) i);
            // user fields and auto generated fields are ignored
            if (isFieldFromDde(field)) {
                field.setUIDisplayable(true);
                field.setUIDisplayPosition(i);
            }
        }
    } else {
        for (Field field : fields) {
            String fieldName = field.getName();
            boolean isUIDisplayable = positions.containsKey(fieldName);
            Long uiDisplayPosition = positions.get(fieldName);
            field.setUIDisplayable(isUIDisplayable);
            field.setUIDisplayPosition(uiDisplayPosition);
        }
    }
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with Field

use of org.motechproject.mds.domain.Field 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);
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeSetting(org.motechproject.mds.domain.TypeSetting) Lookup(org.motechproject.mds.domain.Lookup) HashSet(java.util.HashSet)

Example 29 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class EntityServiceImpl method toFieldDtos.

private List<FieldDto> toFieldDtos(Entity entity, List<Field> fields, boolean fetchComboboxOptions) {
    List<FieldDto> fieldDtos = new ArrayList<>();
    for (Field field : fields) {
        FieldDto fieldDto = field.toDto();
        if (fetchComboboxOptions && field.getType().isCombobox()) {
            List<String> values = getAllComboboxValues(entity, field);
            fieldDto.setSetting(Constants.Settings.COMBOBOX_VALUES, values);
        }
        fieldDtos.add(fieldDto);
    }
    return fieldDtos;
}
Also used : Field(org.motechproject.mds.domain.Field) ArrayList(java.util.ArrayList) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 30 with Field

use of org.motechproject.mds.domain.Field in project motech by motech.

the class MdsBundleIT method verifyColumnNameChange.

private void verifyColumnNameChange() throws ClassNotFoundException, InterruptedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    getLogger().info("Verifying column name change");
    Long entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
    List<Field> fields = entityService.getEntityDraft(entityId).getFields();
    Field fieldToUpdate = findFieldByName(fields, "someString");
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forFieldEdit(fieldToUpdate.getId(), "basic.name", "newFieldName"));
    entityService.commitChanges(entityId);
    SchemaHolder schemaHolder = entityService.getSchema();
    generator.regenerateMdsDataBundle(schemaHolder);
    FieldDto fieldToUpdateDto = DtoHelper.findByName(entityService.getEntityFields(entityId), "newFieldName");
    assertNotNull("Unable to find field named 'newFieldName'", fieldToUpdate);
    assertEquals(String.class.getName(), fieldToUpdateDto.getType().getTypeClass());
    service = (MotechDataService) ServiceRetriever.getService(bundleContext, ClassName.getInterfaceName(FOO_CLASS), true);
    Object retrieved = service.retrieveAll(QueryParams.ascOrder("someDateTime")).get(0);
    Object fieldValue = MethodUtils.invokeMethod(retrieved, "getNewFieldName", null);
    assertNotNull(fieldValue);
    entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
    fields = entityService.getEntityDraft(entityId).getFields();
    fieldToUpdate = findFieldByName(fields, "newFieldName");
    entityService.saveDraftEntityChanges(entityId, DraftBuilder.forFieldEdit(fieldToUpdate.getId(), "basic.name", "someString"));
    entityService.commitChanges(entityId);
    schemaHolder = entityService.getSchema();
    generator.regenerateMdsDataBundle(schemaHolder);
    service = (MotechDataService) ServiceRetriever.getService(bundleContext, ClassName.getInterfaceName(FOO_CLASS), true);
}
Also used : Field(org.motechproject.mds.domain.Field) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

Field (org.motechproject.mds.domain.Field)73 Entity (org.motechproject.mds.domain.Entity)33 Test (org.junit.Test)24 Lookup (org.motechproject.mds.domain.Lookup)16 MdsEntity (org.motechproject.mds.domain.MdsEntity)15 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)15 Type (org.motechproject.mds.domain.Type)14 ArrayList (java.util.ArrayList)13 Transactional (org.springframework.transaction.annotation.Transactional)13 FieldDto (org.motechproject.mds.dto.FieldDto)12 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)12 HashSet (java.util.HashSet)8 Matchers.anyString (org.mockito.Matchers.anyString)6 FieldSetting (org.motechproject.mds.domain.FieldSetting)6 TypeSetting (org.motechproject.mds.domain.TypeSetting)6 UserPreferences (org.motechproject.mds.domain.UserPreferences)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)5 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)4 LookupDto (org.motechproject.mds.dto.LookupDto)4