DataSetLookupConstraints extends DataSetConstraints<DataSetLookupConstraints> { public DataSetLookup newDataSetLookup(DataSetMetadata metatada) { DataSetLookupBuilder<DataSetLookupBuilderImpl> builder = DataSetLookupFactory.newDataSetLookupBuilder(); builder.dataset(metatada.getUUID()); Set<Integer> exclude = new HashSet<Integer>(); int startIndex = 0; if (groupRequired) { int groupIdx = getGroupColumn(metatada); if (groupIdx == -1) { throw new IllegalStateException("The data set does not contains group-able columns (label or date)"); } exclude.add(groupIdx); builder.group(metatada.getColumnId(groupIdx)); builder.column(metatada.getColumnId(groupIdx)); startIndex = 1; } ColumnType[] types = getColumnTypes(); if (types == null || types.length == 0) { if (minColumns > 0 && minColumns < metatada.getNumberOfColumns()) { types = new ColumnType[minColumns]; } else { types = new ColumnType[metatada.getNumberOfColumns()]; } for (int i = 0; i < types.length; i++) { types[i] = metatada.getColumnType(i); } } for (int i=startIndex; i<types.length; i++) { ColumnType targetType = types[i]; int idx = getTargetColumn(metatada, targetType, exclude); if (idx == -1) { idx = getTargetColumn(metatada, exclude); } String columnId = metatada.getColumnId(idx); ColumnType columnType = metatada.getColumnType(idx); exclude.add(idx); DataSetLookup currentLookup = builder.buildLookup(); String uniqueColumnId = buildUniqueColumnId(currentLookup, columnId); String uniqueCountId = buildUniqueColumnId(currentLookup, "#items"); if (ColumnType.NUMBER.equals(targetType)) { if (groupRequired || functionRequired) { if (ColumnType.NUMBER.equals(columnType)) { builder.column(columnId, AggregateFunctionType.SUM, uniqueColumnId); } else { builder.column(AggregateFunctionType.COUNT, uniqueCountId); } } else { builder.column(columnId, uniqueColumnId); } } else { if (functionRequired) { builder.column(AggregateFunctionType.COUNT, uniqueCountId); } else { builder.column(columnId, uniqueColumnId); } } } return builder.buildLookup(); }  boolean isUniqueColumnIds(); DataSetLookupConstraints setUniqueColumnIds(boolean uniqueColumnIds); boolean isFilterAllowed(); DataSetLookupConstraints setFilterAllowed(boolean filterAllowed); boolean isGroupAllowed(); DataSetLookupConstraints setGroupAllowed(boolean groupAllowed); boolean isGroupRequired(); DataSetLookupConstraints setGroupRequired(boolean groupRequired); int getMaxGroups(); DataSetLookupConstraints setMaxGroups(int maxGroups); String getGroupsTitle(); DataSetLookupConstraints setGroupsTitle(String groupsTitle); String getColumnsTitle(); DataSetLookupConstraints setColumnsTitle(String columnsTitle); DataSetLookupConstraints setColumnTitle(Integer index, String title); String getColumnTitle(Integer index); boolean isGroupColumn(); DataSetLookupConstraints setGroupColumn(boolean groupColumn); boolean isFunctionRequired(); DataSetLookupConstraints setFunctionRequired(boolean functionRequired); ValidationError check(DataSetLookup lookup); ValidationError check(DataSetLookup lookup, DataSetMetadata metadata); DataSetLookup newDataSetLookup(DataSetMetadata metatada); String buildUniqueColumnId(DataSetLookup lookup, String targetId); String buildUniqueColumnId(DataSetLookup lookup, GroupFunction column); static final int ERROR_GROUP_NUMBER; static final int ERROR_GROUP_NOT_ALLOWED; static final int ERROR_GROUP_REQUIRED; static final int ERROR_DUPLICATED_COLUMN_ID; }
@Test public void testTwoColumns() { DataSetLookup lookup = TWO_COLUMNS_GROUPED.newDataSetLookup(METADATA); assertEquals(lookup.getDataSetUUID(), "test"); List<DataSetOp> opList = lookup.getOperationList(); assertEquals(opList.size(), 1); List<DataSetGroup> groupList = lookup.getOperationList(DataSetGroup.class); assertEquals(groupList.size(), 1); DataSetGroup groupOp = groupList.get(0); assertNotNull(groupOp); assertEquals(groupOp.getGroupFunctions().size(), 2); ColumnGroup cg = groupOp.getColumnGroup(); assertNotNull(groupOp); assertEquals(cg.getSourceId(), OFFICE); assertEquals(cg.getColumnId(), OFFICE); assertEquals(cg.getStrategy(), GroupStrategy.DYNAMIC); GroupFunction gf1 = groupOp.getGroupFunction(OFFICE); assertNotNull(gf1); assertNull(gf1.getFunction()); assertEquals(gf1.getSourceId(), OFFICE); assertEquals(gf1.getColumnId(), OFFICE); GroupFunction gf2 = groupOp.getGroupFunction(AMOUNT); assertNotNull(gf2); assertNotNull(gf2.getFunction()); assertEquals(gf2.getSourceId(), AMOUNT); assertEquals(gf2.getColumnId(), AMOUNT); }
@Test public void testMultipleColumns() { DataSetLookup lookup = MULTIPLE_COLUMNS.newDataSetLookup(METADATA); assertEquals(lookup.getDataSetUUID(), "test"); List<DataSetOp> opList = lookup.getOperationList(); assertEquals(opList.size(), 1); List<DataSetGroup> groupList = lookup.getOperationList(DataSetGroup.class); assertEquals(groupList.size(), 1); DataSetGroup groupOp = groupList.get(0); assertNotNull(groupOp); ColumnGroup cg = groupOp.getColumnGroup(); assertNull(cg); assertEquals(groupOp.getGroupFunctions().size(), MULTIPLE_COLUMNS.getMinColumns()); for (int i = 0; i < MULTIPLE_COLUMNS.getMinColumns(); i++) { GroupFunction gf = groupOp.getGroupFunction(METADATA.getColumnId(i)); assertNotNull(gf); assertEquals(gf.getSourceId(), METADATA.getColumnId(i)); assertEquals(gf.getColumnId(), METADATA.getColumnId(i)); assertNull(gf.getFunction()); } }