Search in sources :

Example 1 with RestOptionsDto

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

the class EntityControllerTest method shouldGetAdvancedSettingsForEntity.

@Test
public void shouldGetAdvancedSettingsForEntity() throws Exception {
    AdvancedSettingsDto expected = new AdvancedSettingsDto();
    RestOptionsDto restOptions = new RestOptionsDto();
    List<String> fields = new LinkedList<>();
    fields.add("field1");
    fields.add("field2");
    restOptions.setCreate(true);
    restOptions.setFieldNames(fields);
    expected.setId(1L);
    expected.setEntityId(7L);
    expected.setRestOptions(restOptions);
    controller.perform(get("/entities/7/advanced")).andExpect(status().isOk()).andExpect(content().string(new ObjectMapper().writeValueAsString(expected)));
}
Also used : AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) Matchers.anyString(org.mockito.Matchers.anyString) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LinkedList(java.util.LinkedList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 2 with RestOptionsDto

use of org.motechproject.mds.dto.RestOptionsDto 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 3 with RestOptionsDto

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

the class RestOptions method toDto.

public RestOptionsDto toDto() {
    RestOptionsDto dto = new RestOptionsDto();
    dto.setId(id);
    dto.setCreate(allowCreate);
    dto.setRead(allowRead);
    dto.setUpdate(allowUpdate);
    dto.setDelete(allowDelete);
    dto.setModifiedByUser(modifiedByUser);
    for (Lookup lookup : getLookups()) {
        dto.addLookup(lookup.getLookupName());
    }
    for (Field field : getFields()) {
        dto.addField(field.getName());
    }
    return dto;
}
Also used : RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto)

Example 4 with RestOptionsDto

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

the class Entity method advancedSettingsDto.

@NotPersistent
public AdvancedSettingsDto advancedSettingsDto() {
    AdvancedSettingsDto advancedSettingsDto = new AdvancedSettingsDto();
    RestOptionsDto restDto;
    if (null == restOptions) {
        restDto = new RestOptions(this).toDto();
    } else {
        restDto = restOptions.toDto();
    }
    List<LookupDto> indexes = new ArrayList<>();
    for (Lookup lookup : getLookups()) {
        indexes.add(lookup.toDto());
    }
    Tracking trackingMapping = getTracking();
    TrackingDto trackingDto = (trackingMapping == null) ? new TrackingDto() : trackingMapping.toDto();
    advancedSettingsDto.setIndexes(indexes);
    advancedSettingsDto.setEntityId(getId());
    advancedSettingsDto.setBrowsing(getBrowsingSettings().toDto());
    advancedSettingsDto.setRestOptions(restDto);
    advancedSettingsDto.setTracking(trackingDto);
    return advancedSettingsDto;
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) TrackingDto(org.motechproject.mds.dto.TrackingDto) NotPersistent(javax.jdo.annotations.NotPersistent)

Example 5 with RestOptionsDto

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

the class EntityServiceContextIT method shouldUpdateRestOptions.

@Test
public void shouldUpdateRestOptions() throws IOException {
    // given
    EntityDto entityDto = new EntityDto();
    entityDto.setName("RestTestEnt");
    entityDto = entityService.createEntity(entityDto);
    FieldDto boolField = FieldTestHelper.fieldDto("boolField", Boolean.class);
    FieldDto dtField = FieldTestHelper.fieldDto("dtField", DateTime.class);
    FieldDto strField = FieldTestHelper.fieldDto("strField", String.class);
    entityService.addFields(entityDto, asList(boolField, dtField, strField));
    List<LookupFieldDto> lookupFieldDtos = lookupFieldsFromNames("boolField", "dtField", "strField");
    LookupDto lookup = new LookupDto("lookup", false, false, lookupFieldDtos, true);
    List<LookupFieldDto> strLookupFieldDtos = lookupFieldsFromNames("strField");
    LookupDto strLookup = new LookupDto("strLookup", false, false, strLookupFieldDtos, true);
    entityService.addLookups(entityDto.getId(), asList(lookup, strLookup));
    RestOptionsDto restOptionsDto = new RestOptionsDto(true, false, true, false, false);
    restOptionsDto.addField("boolField");
    restOptionsDto.addField("strField");
    restOptionsDto.addLookup("strLookup");
    // when
    entityService.updateRestOptions(entityDto.getId(), restOptionsDto);
    // then
    AdvancedSettingsDto advancedSettingsDto = entityService.getAdvancedSettings(entityDto.getId(), true);
    RestOptionsDto fromDb = advancedSettingsDto.getRestOptions();
    assertTrue(fromDb.isCreate());
    assertFalse(fromDb.isRead());
    assertTrue(fromDb.isUpdate());
    assertFalse(fromDb.isDelete());
    assertEquals(asList("strLookup"), fromDb.getLookupNames());
    assertEquals(asList("boolField", "strField"), fromDb.getFieldNames());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) AdvancedSettingsDto(org.motechproject.mds.dto.AdvancedSettingsDto) RestOptionsDto(org.motechproject.mds.dto.RestOptionsDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Aggregations

RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)11 FieldDto (org.motechproject.mds.dto.FieldDto)6 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)5 EntityDto (org.motechproject.mds.dto.EntityDto)5 Test (org.junit.Test)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)4 ArrayList (java.util.ArrayList)2 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)2 TrackingDto (org.motechproject.mds.dto.TrackingDto)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Arrays.asList (java.util.Arrays.asList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 NotPersistent (javax.jdo.annotations.NotPersistent)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Before (org.junit.Before)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Entity (org.motechproject.mds.annotations.Entity)1