Search in sources :

Example 16 with Entity

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

the class EntityServiceImpl method getEntityFieldsByClassName.

private List<FieldDto> getEntityFieldsByClassName(String className, boolean forUI) {
    Entity entity = allEntities.retrieveByClassName(className);
    assertEntityExists(entity, className);
    List<Field> fields = new ArrayList<>(entity.getFields());
    Collections.sort(fields, new UIDisplayFieldComparator());
    return toFieldDtos(entity, fields, forUI);
}
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) ArrayList(java.util.ArrayList) UIDisplayFieldComparator(org.motechproject.mds.domain.UIDisplayFieldComparator)

Example 17 with Entity

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

the class EntityServiceImpl method updateRelatedField.

private void updateRelatedField(Field oldField, Field draftField, List<String> modulesToRefresh) {
    Entity relatedEntity = allEntities.retrieveByClassName(oldField.getMetadataValue(RELATED_CLASS));
    Field relatedField = relatedEntity.getField(oldField.getMetadataValue(RELATED_FIELD));
    boolean fieldChanged = false;
    boolean relatedEntityChanged = false;
    if (!StringUtils.equals(draftField.getMetadataValue(RELATED_CLASS), oldField.getMetadataValue(RELATED_CLASS))) {
        addRelatedField(draftField, modulesToRefresh);
        relatedEntity.removeField(relatedField.getId());
        relatedEntityChanged = true;
    }
    if (!relatedEntityChanged && !StringUtils.equals(draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE), oldField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE))) {
        relatedField.setMetadataValue(RELATIONSHIP_COLLECTION_TYPE, draftField.getMetadataValue(RELATIONSHIP_COLLECTION_TYPE));
        fieldChanged = true;
    }
    if (!relatedEntityChanged && !StringUtils.equals(draftField.getMetadataValue(RELATED_FIELD), oldField.getMetadataValue(RELATED_FIELD))) {
        relatedField.setName(draftField.getMetadataValue(RELATED_FIELD));
        fieldChanged = true;
    }
    if (!relatedEntityChanged && !oldField.getName().equals(draftField.getName())) {
        relatedField.setMetadataValue(RELATED_FIELD, draftField.getName());
        fieldChanged = true;
    }
    if (fieldChanged || relatedEntityChanged) {
        relatedEntity.incrementVersion();
    }
    addModuleToRefresh(relatedEntity, modulesToRefresh);
}
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)

Example 18 with Entity

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

the class EntityServiceImpl method incrementVersion.

@Override
@Transactional
public void incrementVersion(Long entityId) {
    Entity entity = allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    entity.incrementVersion();
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with Entity

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

the class EntityServiceImpl method getFields.

private List<FieldDto> getFields(Long entityId, boolean forDraft, boolean forUi) {
    Entity entity = (forDraft) ? getEntityDraft(entityId) : allEntities.retrieveById(entityId);
    assertEntityExists(entity, entityId);
    // the returned collection is unmodifiable
    List<Field> fields = new ArrayList<>(entity.getFields());
    // for data browser purposes, we sort the fields by their ui display order
    if (!forDraft) {
        Collections.sort(fields, new UIDisplayFieldComparator());
    }
    // if it's for the UI, then we add combobox options
    List<FieldDto> fieldDtos = toFieldDtos(entity, fields, forUi);
    return addNonPersistentFieldsData(fieldDtos, entity);
}
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) ArrayList(java.util.ArrayList) UIDisplayFieldComparator(org.motechproject.mds.domain.UIDisplayFieldComparator) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 20 with Entity

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

the class EntityServiceImpl method findFieldByName.

@Override
@Transactional
public FieldDto findFieldByName(Long entityId, String name) {
    Entity entity = getEntityDraft(entityId);
    Field field = entity.getField(name);
    if (field == null) {
        throw new FieldNotFoundException(entity.getClassName(), name);
    }
    return field.toDto();
}
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) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Entity (org.motechproject.mds.domain.Entity)97 Test (org.junit.Test)35 Field (org.motechproject.mds.domain.Field)33 MdsEntity (org.motechproject.mds.domain.MdsEntity)32 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)32 Transactional (org.springframework.transaction.annotation.Transactional)32 ArrayList (java.util.ArrayList)14 Lookup (org.motechproject.mds.domain.Lookup)12 UserPreferences (org.motechproject.mds.domain.UserPreferences)8 Type (org.motechproject.mds.domain.Type)7 AllUserPreferences (org.motechproject.mds.repository.internal.AllUserPreferences)7 EntityDto (org.motechproject.mds.dto.EntityDto)6 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)6 HashMap (java.util.HashMap)5 FieldDto (org.motechproject.mds.dto.FieldDto)5 HashSet (java.util.HashSet)4 EntityDraft (org.motechproject.mds.domain.EntityDraft)4 MotechDataService (org.motechproject.mds.service.MotechDataService)4 Query (javax.jdo.Query)3 FieldSetting (org.motechproject.mds.domain.FieldSetting)3