Search in sources :

Example 61 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class ViewDefinitionParserImplTest method init.

@Before
public void init() throws Exception {
    applicationContext = mock(ApplicationContext.class);
    dataDefinitionService = mock(DataDefinitionService.class);
    translationService = mock(TranslationService.class);
    RibbonParserService ribbonService = new RibbonParserService();
    contextualHelpService = mock(ContextualHelpService.class);
    securityRolesService = mock(SecurityRolesService.class);
    viewDefinitionService = new ViewDefinitionServiceImpl();
    hookFactory = new HookFactory();
    setField(hookFactory, "applicationContext", applicationContext);
    viewDefinitionParser = new ViewDefinitionParserImpl();
    setField(viewDefinitionParser, "dataDefinitionService", dataDefinitionService);
    setField(viewDefinitionParser, "viewDefinitionService", viewDefinitionService);
    setField(viewDefinitionParser, "hookFactory", hookFactory);
    setField(viewDefinitionParser, "translationService", translationService);
    setField(viewDefinitionParser, "applicationContext", applicationContext);
    setField(viewDefinitionParser, "contextualHelpService", contextualHelpService);
    setField(viewDefinitionParser, "viewComponentsResolver", viewComponentsResolver);
    setField(viewDefinitionParser, "ribbonService", ribbonService);
    setField(viewDefinitionParser, "securityRolesService", securityRolesService);
    userRoleMock = mock(SecurityRole.class);
    given(securityRolesService.getRoleByIdentifier("ROLE_USER")).willReturn(userRoleMock);
    adminRoleMock = mock(SecurityRole.class);
    given(securityRolesService.getRoleByIdentifier("ROLE_ADMIN")).willReturn(adminRoleMock);
    xml1 = "view/test1.xml";
    xml2 = "view/test2.xml";
    given(applicationContext.getBean(CustomViewService.class)).willReturn(new CustomViewService());
    dataDefinitionA = mock(DataDefinition.class);
    dataDefinitionB = mock(DataDefinition.class);
    FieldDefinition nameA = mock(FieldDefinition.class, "nameA");
    FieldDefinition nameB = mock(FieldDefinition.class, "nameB");
    FieldDefinition hasManyB = mock(FieldDefinition.class, "hasManyB");
    FieldDefinition belongToA = mock(FieldDefinition.class, "belongsToA");
    HasManyType hasManyBType = mock(HasManyType.class);
    BelongsToType belongToAType = mock(BelongsToType.class);
    given(nameA.getDataDefinition()).willReturn(dataDefinitionA);
    given(nameA.getType()).willReturn(new StringType());
    given(nameB.getType()).willReturn(new StringType());
    given(nameB.getDataDefinition()).willReturn(dataDefinitionA);
    given(hasManyB.getType()).willReturn(hasManyBType);
    given(hasManyB.getDataDefinition()).willReturn(dataDefinitionB);
    given(belongToA.getType()).willReturn(belongToAType);
    given(belongToA.getDataDefinition()).willReturn(dataDefinitionB);
    given(hasManyBType.getDataDefinition()).willReturn(dataDefinitionB);
    given(belongToAType.getDataDefinition()).willReturn(dataDefinitionA);
    given(dataDefinitionA.getField("beansB")).willReturn(hasManyB);
    given(dataDefinitionA.getField("name")).willReturn(nameA);
    given(dataDefinitionB.getField("activeA")).willReturn(nameA);
    given(dataDefinitionB.getField("activeB")).willReturn(nameA);
    given(dataDefinitionB.getField("activeC")).willReturn(nameA);
    given(dataDefinitionB.getField("beanA")).willReturn(belongToA);
    given(dataDefinitionB.getField("beanM")).willReturn(belongToA);
    given(dataDefinitionB.getField("beanB")).willReturn(belongToA);
    given(dataDefinitionB.getField("name")).willReturn(nameB);
    given(dataDefinitionA.getName()).willReturn("beanA");
    given(dataDefinitionB.getName()).willReturn("beanB");
    given(dataDefinitionA.getFields()).willReturn(ImmutableMap.of("name", nameA, "beansB", hasManyB));
    given(dataDefinitionB.getFields()).willReturn(ImmutableMap.of("name", nameB, "beanA", belongToA));
    given(dataDefinitionService.get("sample", "beanA")).willReturn(dataDefinitionA);
    given(dataDefinitionService.get("sample", "beanB")).willReturn(dataDefinitionB);
}
Also used : SecurityRole(com.qcadoo.security.api.SecurityRole) CustomViewService(com.qcadoo.view.beans.sample.CustomViewService) StringType(com.qcadoo.model.internal.types.StringType) ViewDefinitionServiceImpl(com.qcadoo.view.internal.ViewDefinitionServiceImpl) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinitionService(com.qcadoo.model.api.DataDefinitionService) DataDefinition(com.qcadoo.model.api.DataDefinition) RibbonParserService(com.qcadoo.view.internal.ribbon.RibbonParserService) ApplicationContext(org.springframework.context.ApplicationContext) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) TranslationService(com.qcadoo.localization.api.TranslationService) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) Before(org.junit.Before)

Example 62 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class ExpressionUtilTest method shouldReturnJoinedStringRepresentationsOfMultipleFieldWithoutExpression.

@Test
public void shouldReturnJoinedStringRepresentationsOfMultipleFieldWithoutExpression() throws Exception {
    // given
    Entity entity = new DefaultEntity(null, 1L);
    entity.setField("name", "Mr T");
    entity.setField("age", 33);
    entity.setField("sex", "F");
    FieldDefinition fieldDefinitionName = new FieldDefinitionImpl(null, "name").withType(new StringType());
    FieldDefinition fieldDefinitionAge = new FieldDefinitionImpl(null, "age").withType(new IntegerType());
    FieldDefinition fieldDefinitionSex = new FieldDefinitionImpl(null, "sex").withType(new StringType());
    // when
    String value = expressionService.getValue(entity, Lists.newArrayList(fieldDefinitionName, fieldDefinitionAge, fieldDefinitionSex), Locale.ENGLISH);
    // then
    assertEquals("Mr T, 33, F", value);
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) StringType(com.qcadoo.model.internal.types.StringType) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) FieldDefinitionImpl(com.qcadoo.model.internal.FieldDefinitionImpl) Test(org.junit.Test)

Example 63 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class GridComponentFilterUtils method addMultiSearchFilter.

public static void addMultiSearchFilter(GridComponentMultiSearchFilter multiSearchFilter, Map<String, GridComponentColumn> columns, DataDefinition dataDefinition, SearchCriteriaBuilder criteria) throws GridComponentFilterException {
    LinkedList<SearchCriterion> searchRules = Lists.newLinkedList();
    for (GridComponentMultiSearchFilterRule rule : multiSearchFilter.getRules()) {
        String field = getFieldNameByColumnName(columns, rule.getField());
        if (field != null) {
            try {
                FieldDefinition fieldDefinition = getFieldDefinition(dataDefinition, field);
                if ("".equals(rule.getData()) && !ISNULL.equals(rule.getFilterOperator())) {
                    continue;
                }
                field = addAliases(criteria, field, JoinType.LEFT);
                SearchCriterion searchRule;
                if (fieldDefinition != null && String.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    searchRule = createStringCriterion(rule.getFilterOperator(), rule.getData(), field);
                } else if (fieldDefinition != null && Boolean.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    searchRule = createSimpleCriterion(rule.getFilterOperator(), "1".equals(rule.getData()), field);
                } else if (fieldDefinition != null && Date.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    searchRule = createDateCriterion(rule.getFilterOperator(), rule.getData(), field);
                } else if (fieldDefinition != null && BigDecimal.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    searchRule = createDecimalCriterion(rule.getFilterOperator(), rule.getData(), field);
                } else if (fieldDefinition != null && Integer.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    searchRule = createIntegerCriterion(rule.getFilterOperator(), rule.getData(), field);
                } else {
                    searchRule = createSimpleCriterion(rule.getFilterOperator(), rule.getData(), field);
                }
                searchRules.add(searchRule);
            } catch (Exception pe) {
                throw new GridComponentFilterException(rule.getData());
            }
        }
    }
    SearchCriterion groupedRules = null;
    if (searchRules.size() == 1) {
        groupedRules = searchRules.pollFirst();
    } else if (searchRules.size() == 2) {
        if (multiSearchFilter.getGroupOperator() == GridComponentFilterGroupOperator.AND) {
            groupedRules = SearchRestrictions.and(searchRules.pollFirst(), searchRules.pollFirst());
        } else if (multiSearchFilter.getGroupOperator() == GridComponentFilterGroupOperator.OR) {
            groupedRules = SearchRestrictions.or(searchRules.pollFirst(), searchRules.pollFirst());
        }
    } else if (searchRules.size() > 2) {
        SearchCriterion firstRule = searchRules.pollFirst();
        SearchCriterion secondRule = searchRules.pollFirst();
        SearchCriterion[] otherRules = new SearchCriterion[searchRules.size()];
        searchRules.toArray(otherRules);
        if (multiSearchFilter.getGroupOperator() == GridComponentFilterGroupOperator.AND) {
            groupedRules = SearchRestrictions.and(firstRule, secondRule, otherRules);
        } else if (multiSearchFilter.getGroupOperator() == GridComponentFilterGroupOperator.OR) {
            groupedRules = SearchRestrictions.or(firstRule, secondRule, otherRules);
        }
    }
    if (groupedRules != null) {
        criteria.add(groupedRules);
    }
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) SearchCriterion(com.qcadoo.model.api.search.SearchCriterion) Date(java.util.Date) ParseException(java.text.ParseException)

Example 64 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class GridComponentFilterUtils method addFilters.

public static void addFilters(final Map<String, String> filters, final Map<String, GridComponentColumn> columns, final DataDefinition dataDefinition, final SearchCriteriaBuilder criteria) throws GridComponentFilterException {
    for (Map.Entry<String, String> filter : filters.entrySet()) {
        String field = getFieldNameByColumnName(columns, filter.getKey());
        if (field != null) {
            try {
                FieldDefinition fieldDefinition = getFieldDefinition(dataDefinition, field);
                Map.Entry<GridComponentFilterOperator, String> filterValue = parseFilterValue(filter.getValue());
                if ("".equals(filterValue.getValue()) && !ISNULL.equals(filterValue.getKey())) {
                    continue;
                }
                field = addAliases(criteria, field, JoinType.LEFT);
                if (fieldDefinition != null && String.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    addStringFilter(criteria, filterValue, field, fieldDefinition.getType());
                } else if (fieldDefinition != null && Boolean.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    addSimpleFilter(criteria, filterValue, field, "1".equals(filterValue.getValue()));
                } else if (fieldDefinition != null && Date.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    addDateFilter(criteria, filterValue, field);
                } else if (fieldDefinition != null && BigDecimal.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    addDecimalFilter(criteria, filterValue, field);
                } else if (fieldDefinition != null && Integer.class.isAssignableFrom(fieldDefinition.getType().getType())) {
                    addIntegerFilter(criteria, filterValue, field);
                } else {
                    addSimpleFilter(criteria, filterValue, field, filterValue.getValue());
                }
            } catch (ParseException pe) {
                throw new GridComponentFilterException(filter.getValue());
            }
        }
    }
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition) ParseException(java.text.ParseException) Map(java.util.Map) Date(java.util.Date)

Example 65 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class GridComponentPattern method getFilterValuesForColumn.

private JSONArray getFilterValuesForColumn(final GridComponentColumn column, final Locale locale) throws JSONException {
    if (column.getFields().size() != 1) {
        return null;
    }
    String field = GridComponentFilterUtils.getFieldNameByColumnName(columns, column.getName());
    if (field == null) {
        return null;
    }
    FieldDefinition fieldDefinition = GridComponentFilterUtils.getFieldDefinition(getDataDefinition(), field);
    if (fieldDefinition == null) {
        return null;
    }
    JSONArray values = new JSONArray();
    if (fieldDefinition.getType() instanceof EnumeratedType) {
        EnumeratedType type = (EnumeratedType) fieldDefinition.getType();
        Map<String, String> sortedValues = type.values(locale);
        for (Map.Entry<String, String> value : sortedValues.entrySet()) {
            JSONObject obj = new JSONObject();
            obj.put("key", value.getKey());
            obj.put("value", value.getValue());
            values.put(obj);
        }
    } else if (fieldDefinition.getType().getType().equals(Boolean.class)) {
        JSONObject obj1 = new JSONObject();
        obj1.put("key", "1");
        obj1.put("value", getTranslationService().translate("qcadooView.true", locale));
        values.put(obj1);
        JSONObject obj0 = new JSONObject();
        obj0.put("key", "0");
        obj0.put("value", getTranslationService().translate("qcadooView.false", locale));
        values.put(obj0);
    }
    if (values.length() > 0) {
        return values;
    } else {
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) EnumeratedType(com.qcadoo.model.api.types.EnumeratedType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) JSONArray(org.json.JSONArray) Map(java.util.Map)

Aggregations

FieldDefinition (com.qcadoo.model.api.FieldDefinition)142 Test (org.junit.Test)92 DataDefinition (com.qcadoo.model.api.DataDefinition)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 Entity (com.qcadoo.model.api.Entity)32 BelongsToType (com.qcadoo.model.api.types.BelongsToType)19 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)15 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)13 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)12 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)11 HasManyType (com.qcadoo.model.api.types.HasManyType)9 StringType (com.qcadoo.model.internal.types.StringType)9 JSONObject (org.json.JSONObject)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)6 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)6 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)6 Before (org.junit.Before)6 EntityList (com.qcadoo.model.api.EntityList)5