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);
}
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);
}
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);
}
}
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());
}
}
}
}
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;
}
}
Aggregations