Search in sources :

Example 6 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceTest method mockLookups.

private void mockLookups() {
    LookupDto lookup = new LookupDto(TestDataService.LOOKUP_1_NAME, true, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), true, "singleObject", asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(lookup);
    Map<String, FieldDto> mapping = new HashMap<>();
    mapping.put("strField", FieldTestHelper.fieldDto(1L, "strField", String.class.getName(), "String field", "Default"));
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(mapping);
    lookup = new LookupDto(TestDataService.LOOKUP_2_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), false, "multiObject", asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(lookup);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(mapping);
    lookup = new LookupDto(TestDataService.NULL_EXPECTING_LOOKUP_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(3L, "dtField")), false, "nullParamExpected", asList("dtField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(lookup);
    mapping = new HashMap<>();
    mapping.put("dtField", FieldTestHelper.fieldDto(3L, "dtField", DateTime.class.getName(), "DateTime field", null));
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(mapping);
}
Also used : HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) Matchers.anyString(org.mockito.Matchers.anyString) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 7 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceTest method mockAnotherEntityFields.

private void mockAnotherEntityFields() {
    FieldDto relatedField = FieldTestHelper.fieldDto(2L, "testClasses", OneToManyRelationship.class.getName(), "Test Classes", null);
    relatedField.addMetadata(new MetadataDto(Constants.MetadataKeys.RELATED_CLASS, TestClass.class.getName()));
    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID)).thenReturn(asList(FieldTestHelper.fieldDto(1L, "id", Long.class.getName(), "Id", null), relatedField));
}
Also used : OneToManyRelationship(org.motechproject.mds.domain.OneToManyRelationship) Matchers.anyLong(org.mockito.Matchers.anyLong) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 8 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityProcessor method updateUiChangedFields.

private void updateUiChangedFields(Collection<FieldDto> fieldsToUpdate, String entityClassName) {
    if (getSchemaHolder().getEntityByClassName(entityClassName) != null) {
        List<FieldDto> currentFields = getSchemaHolder().getFields(entityClassName);
        for (FieldDto field : fieldsToUpdate) {
            FieldDto currentField = getCurrentField(currentFields, field.getBasic().getName());
            if (currentField != null && currentField.isUiChanged()) {
                field.setUiFilterable(currentField.isUiFilterable());
                field.setUiChanged(currentField.isUiChanged());
            }
        }
    }
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto)

Example 9 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class EntityProcessor method process.

@Override
protected void process(AnnotatedElement element) {
    BundleHeaders bundleHeaders = new BundleHeaders(getBundle());
    EntityProcessorOutput entityProcessorOutput = new EntityProcessorOutput();
    Class clazz = (Class) element;
    Class<Entity> ann = ReflectionsUtil.getAnnotationClass(clazz, Entity.class);
    Annotation annotation = AnnotationUtils.findAnnotation(clazz, ann);
    if (null != annotation) {
        String className = clazz.getName();
        String name = ReflectionsUtil.getAnnotationValue(annotation, NAME, ClassName.getSimpleName(className));
        String module = ReflectionsUtil.getAnnotationValue(annotation, MODULE, bundleHeaders.getName(), bundleHeaders.getSymbolicName());
        String bundleSymbolicName = getBundle().getSymbolicName();
        String namespace = ReflectionsUtil.getAnnotationValue(annotation, NAMESPACE);
        String tableName = ReflectionsUtil.getAnnotationValue(annotation, TABLE_NAME);
        boolean recordHistory = Boolean.parseBoolean(ReflectionsUtil.getAnnotationValue(annotation, HISTORY));
        boolean nonEditable = Boolean.parseBoolean(ReflectionsUtil.getAnnotationValue(annotation, NON_EDITABLE));
        EntityDto entity = getSchemaHolder().getEntityByClassName(className);
        RestOptionsDto restOptions = new RestOptionsDto();
        TrackingDto tracking = new TrackingDto();
        Collection<FieldDto> fields;
        if (entity == null) {
            LOGGER.debug("Creating DDE for {}", className);
            entity = new EntityDto(null, className, name, module, namespace, tableName, recordHistory, SecurityMode.EVERYONE, null, null, null, clazz.getSuperclass().getName(), Modifier.isAbstract(clazz.getModifiers()), false, bundleSymbolicName);
        } else {
            LOGGER.debug("DDE for {} already exists, updating if necessary", className);
            AdvancedSettingsDto advancedSettings = getSchemaHolder().getAdvancedSettings(className);
            restOptions = advancedSettings.getRestOptions();
            tracking = advancedSettings.getTracking();
            entity.setBundleSymbolicName(bundleSymbolicName);
            entity.setModule(module);
        }
        if (!tracking.isModifiedByUser()) {
            tracking.setRecordHistory(recordHistory);
            tracking.setNonEditable(nonEditable);
        }
        setSecurityOptions(element, entity);
        // per entity maxFetchDepth that will be passed to the Persistence Manager
        setMaxFetchDepth(entity, annotation);
        entityProcessorOutput.setEntityProcessingResult(entity);
        fields = findFields(clazz, entity);
        String versionField = getVersionFieldName(clazz);
        addVersionMetadata(fields, versionField);
        addDefaultFields(entity, fields);
        restOptions = processRestOperations(clazz, restOptions);
        restOptions = findRestFields(clazz, restOptions, fields);
        updateUiChangedFields(fields, className);
        updateResults(entityProcessorOutput, clazz, fields, restOptions, tracking, versionField);
        add(entity);
        processingResult.add(entityProcessorOutput);
        MotechClassPool.registerDDE(entity.getClassName());
    } else {
        LOGGER.debug("Did not find Entity annotation in class: {}", clazz.getName());
    }
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Entity(org.motechproject.mds.annotations.Entity) BundleHeaders(org.motechproject.osgi.web.util.BundleHeaders) Annotation(java.lang.annotation.Annotation) TrackingDto(org.motechproject.mds.dto.TrackingDto) EntityDto(org.motechproject.mds.dto.EntityDto) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 10 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class FieldProcessor method getFieldByName.

private FieldDto getFieldByName(String className, String fieldName) {
    if (!StringUtils.equals(cachedClassname, className)) {
        EntityDto entityDto = getSchemaHolder().getEntityByClassName(className);
        if (entityDto != null) {
            cachedFields = getSchemaHolder().getFields(entityDto);
        } else {
            cachedFields = new ArrayList<>();
        }
        cachedClassname = className;
    }
    for (FieldDto field : cachedFields) {
        if (StringUtils.equals(field.getBasic().getName(), fieldName)) {
            return field;
        }
    }
    return null;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7