Search in sources :

Example 31 with LookupFieldDto

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

the class SchemaComparatorTest method shouldCompareLookupsWithDifferentFields.

@Test
public void shouldCompareLookupsWithDifferentFields() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupFieldDto lookupFieldDto = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "relatedName");
    lookupDto.setLookupFields(asList(lookupFieldDto));
    LookupDto lookupDto2 = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupFieldDto lookupFieldDto2 = new LookupFieldDto(1L, "name", LookupFieldType.RANGE, "customOperator", true, "differentRelatedName");
    lookupDto2.setLookupFields(asList(lookupFieldDto2));
    entityLookups = asList(lookupDto);
    newLookups = asList(lookupDto2);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertTrue(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 32 with LookupFieldDto

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

the class SchemaComparatorTest method shouldCompareTheSameLookups.

@Test
public void shouldCompareTheSameLookups() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    entityLookups = asList(lookupDto);
    newLookups = asList(lookupDto);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertFalse(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 33 with LookupFieldDto

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

the class SchemaComparatorTest method shouldCompareOnlyReadOnlyLookups.

@Test
public void shouldCompareOnlyReadOnlyLookups() {
    LookupDto lookupDto = new LookupDto("lookupName", true, true, new ArrayList<LookupFieldDto>(), true, "methodName", new ArrayList<String>());
    LookupDto lookupDto2 = new LookupDto();
    lookupDto2.setReadOnly(false);
    entityLookups = asList(lookupDto, lookupDto2);
    newLookups = asList(lookupDto);
    when(entityService.getEntityLookups(ENTITY_ID)).thenReturn(entityLookups);
    boolean lookupsDiffer = schemaComparator.lookupsDiffer(ENTITY_ID, newLookups);
    assertFalse(lookupsDiffer);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 34 with LookupFieldDto

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

the class LookupProcessorTest method shouldProcessMethodWithRangeParam.

@Test
public void shouldProcessMethodWithRangeParam() throws NoSuchMethodException {
    FieldDto arg0Field = new FieldDto("arg0Field", "Arg 0 Field", TypeDto.BOOLEAN);
    FieldDto rangeField = new FieldDto("rangeField", "Range Field", TypeDto.STRING);
    FieldDto regularFieldField = new FieldDto("regularField", "Regular Field", TypeDto.BOOLEAN);
    FieldDto rangeFieldField = new FieldDto("rangeFieldDouble", "Range Field Double", TypeDto.DOUBLE);
    EntityProcessorOutput eop = mockEntityProcessorOutput(new EntityDto(TestClass.class.getName()), Arrays.asList(arg0Field, rangeField, regularFieldField, rangeFieldField));
    lookupProcessor.setEntityProcessingResult(Arrays.asList(eop));
    LookupFieldDto[][] expectedFields = { { lookupFieldDto("arg0"), lookupFieldDto("range", RANGE) }, { lookupFieldDto("regularField"), lookupFieldDto("rangeField", RANGE) } };
    String[][] expectedFieldsOrder = { { "arg0", "range" }, { "regularField", "rangeField" } };
    // test two methods, one with @LookupField annotations, second without
    for (int i = 0; i < 2; i++) {
        Method method = getTestMethodWithRangeParam(i);
        when(paranamer.lookupParameterNames(method)).thenReturn(new String[] { "arg0", "range" });
        LookupDto expectedLookup = new LookupDto("Test Method With Range Param " + i, false, false, asList(expectedFields[i]), true, "testMethodWithRangeParam" + i, asList(expectedFieldsOrder[i]), true);
        lookupProcessor.process(method);
        Map<String, List<LookupDto>> elements = lookupProcessor.getProcessingResult();
        assertTrue(elements.containsKey(TEST_CLASS_NAME));
        List<LookupDto> list = elements.get(TEST_CLASS_NAME);
        assertEquals(1, list.size());
        assertEquals(expectedLookup, list.get(0));
        assertEquals(asList(VALUE, RANGE), extract(list.get(0).getLookupFields(), on(LookupFieldDto.class).getType()));
        lookupProcessor.clear();
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) Method(java.lang.reflect.Method) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) FieldTestHelper.lookupFieldDto(org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) Test(org.junit.Test)

Example 35 with LookupFieldDto

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

the class LookupBuilder method body.

private String body() {
    StringBuilder body = new StringBuilder();
    body.append("java.util.List properties = new java.util.ArrayList();");
    Map<String, String> jdoQueryVariables = new HashMap<>();
    for (String lookupFieldName : lookup.getFieldsOrder()) {
        boolean appendJdoVariableName = false;
        FieldDto field = getLookupField(lookupFieldName);
        // don't use lookupFieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        if (lookupFieldName.contains(".")) {
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(lookupFieldName));
        }
        TypeDto type = field.getType();
        String typeClassName = resolveClassName(type, relationField);
        body.append("properties.add(");
        if (type.isCombobox()) {
            ComboboxHolder holder = new ComboboxHolder(entity, field);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
        } else if (relationField != null && relationField.getType().isCombobox()) {
            RelationshipHolder relationshipHolder = new RelationshipHolder(field);
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(relationshipHolder.getRelatedClass());
            ComboboxHolder holder = new ComboboxHolder(relatedEntity, relationField);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = needsJdoVariable(type);
        } else if (relationField != null && (type.getTypeClass().equals(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass()) || type.getTypeClass().equals(TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass()))) {
            // related class collection
            body.append(PropertyBuilder.class.getName());
            body.append(".createRelationProperty");
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = true;
        } else {
            body.append(PropertyBuilder.class.getName());
            body.append(".create");
        }
        body.append(buildPropertyParameters(appendJdoVariableName, lookupFieldName, lookupField, typeClassName, jdoQueryVariables));
    }
    body.append(buildReturn());
    return body.toString();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) HashMap(java.util.HashMap) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) PropertyBuilder(org.motechproject.mds.query.PropertyBuilder)

Aggregations

LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)36 LookupDto (org.motechproject.mds.dto.LookupDto)25 FieldDto (org.motechproject.mds.dto.FieldDto)24 EntityDto (org.motechproject.mds.dto.EntityDto)18 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)13 Method (java.lang.reflect.Method)5 TypeDto (org.motechproject.mds.dto.TypeDto)5 Arrays.asList (java.util.Arrays.asList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)4 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)4 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)4 Before (org.junit.Before)3 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)3 LinkedList (java.util.LinkedList)2 Field (org.motechproject.mds.domain.Field)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)2