Search in sources :

Example 6 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class PartNode method process.

public List<DataRendererDefinition> process(FormPreviewPanel fpp, FoundSet fs, Table table, QuerySelect sqlString) throws Exception {
    // Selection model must be in print mode to be able to set the selection to -1  . Otherwise is not allowed by the selectionModel
    ((ISwingFoundSet) fs).getSelectionModel().hideSelectionForPrinting();
    // this is needed because we must keep sql the same in foundset during printing
    FoundSet rootSet = (FoundSet) fs.copy(false);
    foundSets.add(rootSet);
    IApplication app = fpp.getApplication();
    // retval
    List<DataRendererDefinition> list = new ArrayList<DataRendererDefinition>();
    if (part != null && (part.getPartType() == Part.LEADING_SUBSUMMARY || part.getPartType() == Part.TRAILING_SUBSUMMARY || isLeadingAndTrailingSubsummary)) {
        QuerySelect newSQLString = AbstractBaseQuery.deepClone(sqlString);
        IDataServer server = app.getDataServer();
        // build the sql parts  based on sort columns
        ArrayList<IQuerySelectValue> selectCols = new ArrayList<IQuerySelectValue>();
        ArrayList<QueryColumn> groupbyCols = new ArrayList<QueryColumn>();
        ArrayList<QuerySort> sortbyCols = new ArrayList<QuerySort>();
        for (SortColumn element : sortColumns) {
            BaseQueryTable queryTable = sqlString.getTable();
            Relation[] relations = element.getRelations();
            if (relations != null) {
                for (Relation relation : relations) {
                    ISQLTableJoin join = (ISQLTableJoin) sqlString.getJoin(queryTable, relation.getName());
                    if (join == null) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        Debug.log("Missing relation " + relation.getName() + " in join condition for form on table " + table.getName());
                    } else {
                        queryTable = join.getForeignTable();
                    }
                }
            }
            Column column = (Column) element.getColumn();
            QueryColumn queryColumn = column.queryColumn(queryTable);
            selectCols.add(queryColumn);
            groupbyCols.add(queryColumn);
            sortbyCols.add(new QuerySort(queryColumn, element.getSortOrder() == SortColumn.ASCENDING, fs.getFoundSetManager().getSortOptions(column)));
        }
        // make sql
        for (AggregateVariable ag : allAggregates) {
            selectCols.add(new QueryAggregate(ag.getType(), new QueryColumn(newSQLString.getTable(), -1, ag.getColumnNameToAggregate(), ag.getDataProviderType(), ag.getLength(), 0, null, ag.getFlags()), ag.getName()));
        }
        newSQLString.setColumns(selectCols);
        newSQLString.setGroupBy(groupbyCols);
        ArrayList<IQuerySort> oldSort = newSQLString.getSorts();
        // fix the sort (if columns not are selected of used in groupby they cannot be used in sort)
        newSQLString.setSorts(sortbyCols);
        FoundSetManager foundSetManager = ((FoundSetManager) app.getFoundSetManager());
        String transaction_id = foundSetManager.getTransactionID(table.getServerName());
        IDataSet data = server.performQuery(app.getClientID(), table.getServerName(), transaction_id, newSQLString, null, foundSetManager.getTableFilterParams(table.getServerName(), newSQLString), false, 0, foundSetManager.config.pkChunkSize() * 4, IDataServer.PRINT_QUERY);
        // create a new FoundSet with 'data' and with right 'table', 'where','whereArgs'
        SubSummaryFoundSet newSet = new SubSummaryFoundSet(app.getFoundSetManager(), rootSet, sortColumns, allAggregates, data, table);
        // restore the sort for child body parts
        newSQLString.setSorts(oldSort);
        // make new where for use in sub queries
        for (QuerySort sortbyCol : sortbyCols) {
            QueryColumn sc = (QueryColumn) (sortbyCol).getColumn();
            newSQLString.addCondition(SQLGenerator.CONDITION_SEARCH, new CompareCondition(IBaseSQLCondition.EQUALS_OPERATOR, sc, new Placeholder(new TablePlaceholderKey(sc.getTable(), '#' + sc.getName()))));
        }
        int count = newSet.getSize();
        for (int ii = 0; ii < count; ii++) {
            // make copy for setting sort column
            QuerySelect newSQLStringCopy = AbstractBaseQuery.deepClone(newSQLString);
            // handle the child first, this puts the rootset in the right state! for use of related(!) fields in the subsums
            // THIS is EXTREMELY important for correct printing, see also SubSummaryFoundSet.queryForRelatedFoundSet
            List<DataRendererDefinition> childRetval = null;
            IFoundSetInternal curLeafFoundSet = null;
            if (child != null) {
                for (int i = 0; i < sortbyCols.size(); i++) {
                    QueryColumn sc = (QueryColumn) (sortbyCols.get(i)).getColumn();
                    TablePlaceholderKey placeholderKey = new TablePlaceholderKey(sc.getTable(), '#' + sc.getName());
                    if (!newSQLStringCopy.setPlaceholderValue(placeholderKey, data.getRow(ii)[i])) {
                        Debug.error(// $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
                        new RuntimeException("Could not set placeholder " + placeholderKey + " in query " + newSQLStringCopy + "-- continuing"));
                    }
                }
                childRetval = child.process(fpp, rootSet, table, newSQLStringCopy);
                curLeafFoundSet = child.getCurrentLeafFoundSet();
            }
            SubSummaryFoundSet.PrintState state = (SubSummaryFoundSet.PrintState) newSet.getRecord(ii);
            state.setDelegate(curLeafFoundSet);
            if (part.getPartType() == Part.LEADING_SUBSUMMARY) {
                state.doAggregatesLookup();
                list.add(new DataRendererDefinition(fpp, renderParent, part, renderer, state));
            }
            if (childRetval != null) {
                list.addAll(childRetval);
            }
            if (isLeadingAndTrailingSubsummary) {
                state.doAggregatesLookup();
                list.add(new DataRendererDefinition(fpp, renderParent, second_part, second_renderer, state));
            } else if (part.getPartType() == Part.TRAILING_SUBSUMMARY) {
                state.doAggregatesLookup();
                list.add(new DataRendererDefinition(fpp, renderParent, part, renderer, state));
            }
        }
    } else // for handeling (virtual) body part
    {
        rootSet.browseAll(sqlString);
        int count = app.getFoundSetManager().getFoundSetCount(rootSet);
        for (int ii = 0; ii < count; ii++) {
            currentLeafFoundSet = rootSet;
            list.add(new DataRendererDefinition(fpp, renderParent, part, renderer, rootSet, ii));
        }
    }
    return list;
}
Also used : Placeholder(com.servoy.j2db.query.Placeholder) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) ArrayList(java.util.ArrayList) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) IQuerySort(com.servoy.j2db.query.IQuerySort) Relation(com.servoy.j2db.persistence.Relation) ISQLTableJoin(com.servoy.j2db.query.ISQLTableJoin) QueryColumn(com.servoy.j2db.query.QueryColumn) SortColumn(com.servoy.j2db.dataprocessing.SortColumn) Column(com.servoy.j2db.persistence.Column) IQuerySort(com.servoy.j2db.query.IQuerySort) QuerySort(com.servoy.j2db.query.QuerySort) CompareCondition(com.servoy.j2db.query.CompareCondition) QueryAggregate(com.servoy.j2db.query.QueryAggregate) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) TablePlaceholderKey(com.servoy.j2db.query.TablePlaceholderKey) IDataServer(com.servoy.j2db.dataprocessing.IDataServer) SubSummaryFoundSet(com.servoy.j2db.dataprocessing.SubSummaryFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) QuerySelect(com.servoy.j2db.query.QuerySelect) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) IApplication(com.servoy.j2db.IApplication) BaseQueryTable(com.servoy.base.query.BaseQueryTable) QueryColumn(com.servoy.j2db.query.QueryColumn) SubSummaryFoundSet(com.servoy.j2db.dataprocessing.SubSummaryFoundSet) IDataSet(com.servoy.j2db.dataprocessing.IDataSet) IQuerySelectValue(com.servoy.j2db.query.IQuerySelectValue)

Example 7 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class PersistIndex method initDatasourceCache.

protected void initDatasourceCache(String datasource) {
    if ((datasourceToPersist.size() == 0) || (datasource != null && datasourceToPersist.get(datasource) == null)) {
        visit((persist) -> {
            if (persist instanceof TableNode) {
                String tableDs = ((TableNode) persist).getDataSource();
                if (tableDs != null) {
                    ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(tableDs);
                    if (dsMap == null) {
                        dsMap = new ConcurrentHashMap<>(4);
                        dsMap.put(ScriptCalculation.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(TableNode.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(AggregateVariable.class, new ConcurrentHashMap<String, IPersist>(4));
                        dsMap.put(ScriptMethod.class, new ConcurrentHashMap<String, IPersist>(4));
                        datasourceToPersist.put(tableDs, dsMap);
                    }
                    ConcurrentMap<String, IPersist> tableNodeCache = dsMap.get(TableNode.class);
                    Solution solution = (Solution) ((TableNode) persist).getAncestor(IRepository.SOLUTIONS);
                    tableNodeCache.put(solution.getName(), persist);
                    return IPersistVisitor.CONTINUE_TRAVERSAL;
                }
            } else {
                ISupportChilds parent = persist.getParent();
                if (persist instanceof ScriptCalculation) {
                    if (parent instanceof TableNode) {
                        ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                        addInDatasourceCache(dsMap.get(ScriptCalculation.class), persist, datasource);
                    } else {
                        Debug.error("Something wrong with ScriptCalculation " + ((ScriptCalculation) persist).getName() + " should  have table as parent but the parent is: " + parent);
                    }
                } else if (persist instanceof AggregateVariable) {
                    if (parent instanceof TableNode) {
                        ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                        addInDatasourceCache(dsMap.get(AggregateVariable.class), persist, datasource);
                    } else {
                        Debug.error("Something wrong with AggregateVariable " + ((ScriptCalculation) persist).getName() + " should  have table as parent but the parent is: " + parent);
                    }
                } else if (persist instanceof ScriptMethod && parent instanceof TableNode) {
                    ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = datasourceToPersist.get(((TableNode) parent).getDataSource());
                    addInDatasourceCache(dsMap.get(ScriptMethod.class), persist, datasource);
                }
            }
            return persist instanceof Solution ? IPersistVisitor.CONTINUE_TRAVERSAL : IPersistVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
        });
        if (datasource != null && datasourceToPersist.get(datasource) == null) {
            ConcurrentMap<Class<? extends IPersist>, ConcurrentMap<String, IPersist>> dsMap = new ConcurrentHashMap<>(4);
            dsMap.put(ScriptCalculation.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(TableNode.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(AggregateVariable.class, new ConcurrentHashMap<String, IPersist>(4));
            dsMap.put(ScriptMethod.class, new ConcurrentHashMap<String, IPersist>(4));
            datasourceToPersist.put(datasource, dsMap);
        }
    }
}
Also used : ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) ConcurrentMap(java.util.concurrent.ConcurrentMap) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) IPersist(com.servoy.j2db.persistence.IPersist) TableNode(com.servoy.j2db.persistence.TableNode) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Solution(com.servoy.j2db.persistence.Solution)

Example 8 with AggregateVariable

use of com.servoy.j2db.persistence.AggregateVariable in project servoy-client by Servoy.

the class ComponentFormat method getComponentFormat.

public static ComponentFormat getComponentFormat(String format, IDataProvider dataProvider, IServiceProvider application, boolean autoFillMaxLength) {
    int dpType = IColumnTypes.TEXT;
    String formatProperty = format;
    if (dataProvider != null) {
        dpType = dataProvider.getDataProviderType();
        IColumn column = null;
        if (dataProvider instanceof ColumnWrapper) {
            column = ((ColumnWrapper) dataProvider).getColumn();
        } else if (dataProvider instanceof Column) {
            column = (Column) dataProvider;
        } else if (dataProvider instanceof ScriptCalculation) {
            // When it is a stored calculation, the name of the calc is the name of he column
            ScriptCalculation calc = (ScriptCalculation) dataProvider;
            try {
                ITable table = calc.getTable();
                if (table != null) {
                    column = table.getColumn(calc.getName());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
        }
        if (column instanceof AggregateVariable) {
            Column columnToAggregate = null;
            try {
                ITable table = column.getTable();
                if (table != null) {
                    columnToAggregate = table.getColumn(((AggregateVariable) column).getDataProviderIDToAggregate());
                }
            } catch (RepositoryException e) {
                Debug.error(e);
            }
            // Use aggregated column when they are of the same type (so not count(textcolumn))
            if (columnToAggregate != null && column.getDataProviderType() == columnToAggregate.getDataProviderType()) {
                column = columnToAggregate;
            }
        }
        if (column instanceof Column) {
            ColumnInfo ci = ((Column) column).getColumnInfo();
            if (ci != null) {
                if (formatProperty == null || formatProperty.length() == 0) {
                    if (ci.getDefaultFormat() != null && ci.getDefaultFormat().length() > 0) {
                        formatProperty = ci.getDefaultFormat();
                    }
                }
                dpType = application.getFoundSetManager().getConvertedTypeForColumn(column, true);
            }
        }
    }
    ComponentFormat componentFormat = ComponentFormat.getComponentFormat(formatProperty, dpType, application);
    if (autoFillMaxLength && dataProvider != null && dataProvider.getLength() > 0 && componentFormat.parsedFormat != null && componentFormat.parsedFormat.getMaxLength() == null && (dpType == IColumnTypes.TEXT || dpType == IColumnTypes.MEDIA)) {
        componentFormat.parsedFormat.updateMaxLength(Integer.valueOf(dataProvider.getLength()));
    }
    return componentFormat;
}
Also used : ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) IColumn(com.servoy.j2db.persistence.IColumn) IColumn(com.servoy.j2db.persistence.IColumn) Column(com.servoy.j2db.persistence.Column) ColumnWrapper(com.servoy.j2db.persistence.ColumnWrapper) ColumnInfo(com.servoy.j2db.persistence.ColumnInfo) ITable(com.servoy.j2db.persistence.ITable) RepositoryException(com.servoy.j2db.persistence.RepositoryException) AggregateVariable(com.servoy.j2db.persistence.AggregateVariable)

Aggregations

AggregateVariable (com.servoy.j2db.persistence.AggregateVariable)8 BaseQueryTable (com.servoy.base.query.BaseQueryTable)5 ITable (com.servoy.j2db.persistence.ITable)5 Column (com.servoy.j2db.persistence.Column)4 IColumn (com.servoy.j2db.persistence.IColumn)4 QueryColumn (com.servoy.j2db.query.QueryColumn)4 QueryTable (com.servoy.j2db.query.QueryTable)4 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 ScriptCalculation (com.servoy.j2db.persistence.ScriptCalculation)3 Table (com.servoy.j2db.persistence.Table)3 IQuerySelectValue (com.servoy.j2db.query.IQuerySelectValue)3 QueryAggregate (com.servoy.j2db.query.QueryAggregate)3 QuerySelect (com.servoy.j2db.query.QuerySelect)3 BaseQueryColumn (com.servoy.base.query.BaseQueryColumn)2 IPersist (com.servoy.j2db.persistence.IPersist)2 Relation (com.servoy.j2db.persistence.Relation)2 IQuerySort (com.servoy.j2db.query.IQuerySort)2 ISQLTableJoin (com.servoy.j2db.query.ISQLTableJoin)2 QuerySort (com.servoy.j2db.query.QuerySort)2 SafeArrayList (com.servoy.j2db.util.SafeArrayList)2