Search in sources :

Example 21 with TableInformation

use of CCDD.CcddClassesDataTable.TableInformation in project CCDD by nasa.

the class CcddScriptHandler method getDataAndExecuteScript.

/**
 ********************************************************************************************
 * Get the table information array from the table data used by the script script
 * association(s), then execute the script(s)
 *
 * @param tree
 *            table tree of the table instances (parent tables with their child tables); null
 *            if the tree should be loaded
 *
 * @param associations
 *            list of script associations to execute
 *
 * @param parent
 *            GUI component calling this method; null if none (e.g., if called via the command
 *            line)
 *
 * @return Array containing flags that indicate, for each association, if the association did
 *         not complete successfully
 ********************************************************************************************
 */
protected boolean[] getDataAndExecuteScript(CcddTableTreeHandler tree, List<Object[]> associations, Component parent) {
    int assnIndex = 0;
    CcddTableTreeHandler tableTree = tree;
    // Create an array to indicate if an association has a problem that prevents its execution
    boolean[] isBad = new boolean[associations.size()];
    // Check if the script execution was initiated via command line command
    if (parent == null) {
        // Get the system environment variables map
        envVarMap = new HashMap<String, String>(System.getenv());
    }
    // Check if no table tree was provided
    if (tableTree == null) {
        // Build the table tree
        tableTree = new CcddTableTreeHandler(ccddMain, TableTreeType.INSTANCE_TABLES, parent);
    }
    // Create storage for the individual tables' data and table path+names
    List<TableInformation> tableInformation = new ArrayList<TableInformation>();
    loadedTablePaths = new ArrayList<String>();
    // Get the link assignment information, if any
    CcddLinkHandler linkHandler = new CcddLinkHandler(ccddMain, parent);
    // Load the data field information from the database
    CcddFieldHandler fieldHandler = new CcddFieldHandler(ccddMain, null, parent);
    // Load the group information from the database
    CcddGroupHandler groupHandler = new CcddGroupHandler(ccddMain, null, parent);
    // Get the list of the table paths in the order of appearance in the table tree. This
    // is used to sort the association table paths
    final List<String> allTablePaths = tableTree.getTableTreePathList(null);
    // once. Step through each script association definition
    for (Object[] assn : associations) {
        try {
            // Get the list of association table paths
            List<String> tablePaths = getAssociationTablePaths(assn[AssociationsColumn.MEMBERS.ordinal()].toString(), groupHandler, parent);
            // Check if at least one table is assigned to this script association
            if (!tablePaths.isEmpty()) {
                // Sort the table paths
                Collections.sort(tablePaths, new Comparator<String>() {

                    /**
                     ************************************************************************
                     * Sort the table paths so that the root tables are in alphabetical order
                     * and the child tables appear in the order defined by their table type
                     * definition
                     ************************************************************************
                     */
                    @Override
                    public int compare(String path1, String path2) {
                        int result = 0;
                        // Get the indices of the two paths within the table tree
                        int index1 = allTablePaths.indexOf(path1);
                        int index2 = allTablePaths.indexOf(path2);
                        // the lowest index first
                        if (index1 > index2) {
                            result = 1;
                        } else if (index2 > index1) {
                            result = -1;
                        }
                        return result;
                    }
                });
                // Step through each table path+name
                for (String tablePath : tablePaths) {
                    // Initialize the array for each of the tables to load from the database
                    combinedData = new String[0][0];
                    // Read the table and child table data from the database and store the
                    // results from the last table loaded
                    TableInformation tableInfo = readTable(tablePath, parent);
                    // Check if the table hasn't already been loaded
                    if (tableInfo != null) {
                        // Read the table and child table data from the database
                        tableInformation.add(tableInfo);
                        // Check if an error occurred loading the table data
                        if (tableInfo.isErrorFlag()) {
                            throw new CCDDException("table '" + tableInfo.getProtoVariableName() + "' (or one of its children) failed to load");
                        } else // The table loaded successfully
                        {
                            // Store the data for the table and its child table(s)
                            tableInfo.setData(combinedData);
                            // Get the type definition based on the table type name
                            TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(tableInfo.getType());
                            // Check if the type exists
                            if (typeDefn != null) {
                                // Check if this table represents a structure
                                if (typeDefn.isStructure()) {
                                    // Set the table type to indicate a structure
                                    tableInfo.setType(TYPE_STRUCTURE);
                                } else // Check if this table represents a command table
                                if (typeDefn.isCommand()) {
                                    // Set the table type to indicate a command table
                                    tableInfo.setType(TYPE_COMMAND);
                                }
                            } else // The table's type is invalid
                            {
                                throw new CCDDException("table '" + tableInfo.getProtoVariableName() + "' has unknown type '" + tableInfo.getType() + "'");
                            }
                        }
                    }
                }
            }
        } catch (CCDDException ce) {
            // Inform the user that script execution failed
            logScriptError(FileEnvVar.expandEnvVars(assn[AssociationsColumn.SCRIPT_FILE.ordinal()].toString(), envVarMap), assn[AssociationsColumn.MEMBERS.ordinal()].toString(), ce.getMessage(), parent);
            // Set the flag for this association indicating it can't be executed
            isBad[assnIndex] = true;
        } catch (Exception e) {
            // Display a dialog providing details on the unanticipated error
            CcddUtilities.displayException(e, ccddMain.getMainFrame());
        }
        assnIndex++;
    }
    assnIndex = 0;
    // execute it. Step through each script association definition
    for (Object[] assn : associations) {
        // Check that an error didn't occur loading the data for this association
        if (!isBad[assnIndex]) {
            TableInformation[] combinedTableInfo = null;
            List<String> groupNames = new ArrayList<String>();
            // Get the list of association table paths
            List<String> tablePaths = getAssociationTablePaths(assn[AssociationsColumn.MEMBERS.ordinal()].toString(), groupHandler, parent);
            String[] members = assn[AssociationsColumn.MEMBERS.ordinal()].toString().split(Pattern.quote(ASSN_TABLE_SEPARATOR));
            // Step through each table path+name or group
            for (String member : members) {
                // Check if this is a reference to a group
                if (member.startsWith(GROUP_DATA_FIELD_IDENT)) {
                    // Add the group name to the list of referenced groups
                    groupNames.add(member.substring(GROUP_DATA_FIELD_IDENT.length()));
                }
            }
            // Check if at least one table is assigned to this script association
            if (!tablePaths.isEmpty()) {
                // Create storage for the table types used by this script association
                List<String> tableTypes = new ArrayList<String>();
                // information instance
                for (TableInformation tableInfo : tableInformation) {
                    // Check if this table is a member of the association
                    if (tablePaths.contains(tableInfo.getTablePath())) {
                        // Check if the type for this table is not already in the list
                        if (!tableTypes.contains(tableInfo.getType())) {
                            // Add the table type to the list
                            tableTypes.add(tableInfo.getType());
                        }
                    }
                }
                // Create storage for the combined table data
                combinedTableInfo = new TableInformation[tableTypes.size()];
                // through each table type represented in this association
                for (int typeIndex = 0; typeIndex < tableTypes.size(); typeIndex++) {
                    String tableName = "";
                    String[][] allTableData = new String[0][0];
                    // Step through each table information instance
                    for (TableInformation tableInfo : tableInformation) {
                        // Check if this table is a member of the association
                        if (tablePaths.contains(tableInfo.getTablePath())) {
                            // Check if the table types match
                            if (tableTypes.get(typeIndex).equals(tableInfo.getType())) {
                                // Check if the name hasn't been stored
                                if (tableName.isEmpty()) {
                                    // Assign the name of the first table of this type as this
                                    // type's table name
                                    tableName += tableInfo.getTablePath();
                                }
                                // Append the table data to the combined data array
                                allTableData = CcddUtilities.concatenateArrays(allTableData, tableInfo.getData());
                            }
                        }
                    }
                    // Create the table information from the table data obtained from the
                    // database
                    combinedTableInfo[typeIndex] = new TableInformation(tableTypes.get(typeIndex), tableName, allTableData, null, null, false, new String[0][0]);
                }
            } else // No table is assigned to this script association
            {
                // Create a table information class in order to load and parse the data fields,
                // and to allow access to the field methods
                combinedTableInfo = new TableInformation[1];
                combinedTableInfo[0] = new TableInformation("", "", new String[0][0], null, null, false, new String[0][0]);
            }
            // Get the script file name with any environment variables expanded
            String scriptFileName = FileEnvVar.expandEnvVars(assn[AssociationsColumn.SCRIPT_FILE.ordinal()].toString(), envVarMap);
            try {
                // Execute the script using the indicated table data
                executeScript(scriptFileName, combinedTableInfo, groupNames, linkHandler, fieldHandler, groupHandler, parent);
            } catch (CCDDException ce) {
                // Inform the user that script execution failed
                logScriptError(scriptFileName, assn[AssociationsColumn.MEMBERS.ordinal()].toString(), ce.getMessage(), parent);
                // Set the flag for this association indicating it can't be executed
                isBad[assnIndex] = true;
            } catch (Exception e) {
                // Display a dialog providing details on the unanticipated error
                CcddUtilities.displayException(e, ccddMain.getMainFrame());
            }
        }
        assnIndex++;
    }
    return isBad;
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) ArrayList(java.util.ArrayList) CCDDException(CCDD.CcddClassesDataTable.CCDDException) ScriptException(javax.script.ScriptException) FileNotFoundException(java.io.FileNotFoundException) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) TableInformation(CCDD.CcddClassesDataTable.TableInformation)

Example 22 with TableInformation

use of CCDD.CcddClassesDataTable.TableInformation in project CCDD by nasa.

the class CcddScriptHandler method readTable.

/**
 ********************************************************************************************
 * Recursive method to load a table, and all the tables referenced within it and its child
 * tables. The data is combined into a single array
 *
 * @param tablePath
 *            table path
 *
 * @param parent
 *            GUI component calling this method
 *
 * @return A reference to the TableInformation for the parent table; null if the table has
 *         already been loaded. The error flag for the table data handler is set if an error
 *         occurred loading the data
 ********************************************************************************************
 */
private TableInformation readTable(String tablePath, Component parent) {
    TableInformation tableInfo = null;
    // Check if the table is not already stored in the list
    if (!loadedTablePaths.contains(tablePath)) {
        // Add the table path to the list so that it is not reloaded
        loadedTablePaths.add(tablePath);
        // Read the table's data from the database
        tableInfo = dbTable.loadTableData(tablePath, false, false, false, parent);
        // isn't empty
        if (!tableInfo.isErrorFlag() && tableInfo.getData().length != 0) {
            // Get the table's type definition
            TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(tableInfo.getType());
            // Get the data and place it in an array for reference below. Add columns to
            // contain the table type and path
            String[][] data = CcddUtilities.appendArrayColumns(tableInfo.getData(), 2);
            int typeColumn = data[0].length - TYPE_COLUMN_DELTA;
            int pathColumn = data[0].length - PATH_COLUMN_DELTA;
            // Get the index of the column containing the data type for this table if it has
            // one
            int dataTypeColumn = typeDefn.getColumnIndexByInputType(InputDataType.PRIM_AND_STRUCT);
            // Step through each row
            for (int row = 0; row < data.length && !tableInfo.isErrorFlag(); row++) {
                // Use the index column to store the table path and type for reference during
                // script execution
                data[row][typeColumn] = tableInfo.getType();
                data[row][pathColumn] = tablePath;
                // Store the data from the table in the combined storage array
                combinedData = CcddUtilities.concatenateArrays(combinedData, new String[][] { data[row] });
                // not contain a primitive data type)
                if (dataTypeColumn != -1 && !dataTypeHandler.isPrimitive(data[row][dataTypeColumn])) {
                    // Get the column containing the variable name for this table
                    int varNameColumn = typeDefn.getColumnIndexByInputType(InputDataType.VARIABLE);
                    // Check that a variable name column was found
                    if (varNameColumn != -1) {
                        // Get the column containing the array size for this table
                        int arraySizeColumn = typeDefn.getColumnIndexByInputType(InputDataType.ARRAY_INDEX);
                        // information for this data type structure
                        if ((!data[row][dataTypeColumn].isEmpty() || !data[row][varNameColumn].isEmpty()) && (arraySizeColumn == -1 || data[row][arraySizeColumn].isEmpty() || ArrayVariable.isArrayMember(data[row][varNameColumn]))) {
                            // Get the variable in the format dataType.variableName, prepend a
                            // comma to separate the new variable from the preceding variable
                            // path, then break down the child table
                            TableInformation childInfo = readTable(tablePath + "," + data[row][dataTypeColumn] + "." + data[row][varNameColumn], parent);
                            // Check if an error occurred loading the child table
                            if (childInfo != null && childInfo.isErrorFlag()) {
                                // Set the error flag and stop processing this table
                                tableInfo.setErrorFlag();
                                break;
                            }
                        }
                    } else // Table has no variable name column
                    {
                        // Set the error flag and stop processing this table
                        tableInfo.setErrorFlag();
                        break;
                    }
                }
            }
        }
    }
    return tableInfo;
}
Also used : TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 23 with TableInformation

use of CCDD.CcddClassesDataTable.TableInformation in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getCommandArgArraySize.

/**
 ********************************************************************************************
 * Get the argument array size for the command argument specified at the specified row in the
 * command data. Macro expansion is controlled by the input flag
 *
 * @param argumentNumber
 *            command argument index. The first argument is 0
 *
 * @param row
 *            table data row index
 *
 * @param expandMacros
 *            true to replace any macros with their corresponding value; false to return the
 *            data with any macro names in place
 *
 * @return Argument array size for the command argument specified at the specified row in the
 *         command data; null if the argument number or row index is invalid
 ********************************************************************************************
 */
private String getCommandArgArraySize(int argumentNumber, int row, boolean expandMacros) {
    String argArraySize = null;
    // Get the table type definition for the command table referenced in the specified row
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getCommandTypeNameByRow(row));
    // Check if the table type exists and represents a command
    if (typeDefn != null && typeDefn.isCommand()) {
        // Get the list of command arguments associated with this command table type
        List<AssociatedColumns> commandArguments = typeDefn.getAssociatedCommandArgumentColumns(false);
        // Check if the argument number is valid and that the argument array size exists
        if (argumentNumber < commandArguments.size() && commandArguments.get(argumentNumber).getArraySize() != -1) {
            // Get the reference to the table information
            TableInformation tableInfo = getTableInformation(TYPE_COMMAND);
            // Get the argument array size
            argArraySize = tableInfo.getData()[row][commandArguments.get(argumentNumber).getArraySize()];
            // Check if any macros should be expanded
            if (expandMacros) {
                // Expand any macros
                argArraySize = macroHandler.getMacroExpansion(argArraySize);
            }
        }
    }
    return argArraySize;
}
Also used : AssociatedColumns(CCDD.CcddClassesDataTable.AssociatedColumns) TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 24 with TableInformation

use of CCDD.CcddClassesDataTable.TableInformation in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getCommandCode.

/**
 ********************************************************************************************
 * Get the command code (as a string) at the specified row in the command data. Macro expansion
 * is controlled by the input flag
 *
 * @param row
 *            table data row index
 *
 * @param expandMacros
 *            true to replace any macros with their corresponding value; false to return the
 *            data with any macro names in place
 *
 * @return Command code (as a string) at the specified row in the command data; null if the row
 *         index is invalid
 ********************************************************************************************
 */
private String getCommandCode(int row, boolean expandMacros) {
    String commandCode = null;
    // Get the table type definition for the command table referenced in the specified row
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getCommandTypeNameByRow(row));
    // Check if the table type exists and represents a command
    if (typeDefn != null && typeDefn.isCommand()) {
        // Get the reference to the table information
        TableInformation tableInfo = getTableInformation(TYPE_COMMAND);
        // Get the command code
        commandCode = tableInfo.getData()[row][typeDefn.getColumnIndexByInputType(InputDataType.COMMAND_CODE)];
        // Check if any macros should be expanded
        if (expandMacros) {
            // Expand any macros
            commandCode = macroHandler.getMacroExpansion(commandCode);
        }
    }
    return commandCode;
}
Also used : TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 25 with TableInformation

use of CCDD.CcddClassesDataTable.TableInformation in project CCDD by nasa.

the class CcddScriptDataAccessHandler method getStructureUnits.

/**
 ********************************************************************************************
 * Get the variable units at the specified row in the structure data. Macro expansion is
 * controlled by the input flag
 *
 * @param row
 *            table data row index
 *
 * @param expandMacros
 *            true to replace any macros with their corresponding value; false to return the
 *            data with any macro names in place
 *
 * @return Variable units at the specified row in the structure data; null if the row index is
 *         invalid or no column has the 'Units' input type
 ********************************************************************************************
 */
private String getStructureUnits(int row, boolean expandMacros) {
    String units = null;
    // Get the table type definition for the structure table referenced in the specified row
    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(getStructureTypeNameByRow(row));
    // Check if the table type exists and represents a structure
    if (typeDefn != null && typeDefn.isStructure()) {
        // Get the reference to the table information
        TableInformation tableInfo = getTableInformation(TYPE_STRUCTURE);
        // Get the index of the first column with the 'Units' input type
        int column = typeDefn.getColumnIndexByInputType(InputDataType.UNITS);
        // Check if the column exists
        if (column != -1) {
            // Get the units
            units = tableInfo.getData()[row][column];
            // Check if any macros should be expanded
            if (expandMacros) {
                // Expand any macros
                units = macroHandler.getMacroExpansion(units);
            }
        }
    }
    return units;
}
Also used : TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Aggregations

TableInformation (CCDD.CcddClassesDataTable.TableInformation)43 TypeDefinition (CCDD.CcddTableTypeHandler.TypeDefinition)30 ArrayList (java.util.ArrayList)14 AssociatedColumns (CCDD.CcddClassesDataTable.AssociatedColumns)9 CCDDException (CCDD.CcddClassesDataTable.CCDDException)7 FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)3 GroupInformation (CCDD.CcddClassesDataTable.GroupInformation)2 TableModification (CCDD.CcddClassesDataTable.TableModification)2 SQLException (java.sql.SQLException)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 BackgroundCommand (CCDD.CcddBackgroundCommand.BackgroundCommand)1 ToolTipTreeNode (CCDD.CcddClassesComponent.ToolTipTreeNode)1 TableDefinition (CCDD.CcddClassesDataTable.TableDefinition)1 TableTypeDefinition (CCDD.CcddClassesDataTable.TableTypeDefinition)1 DatabaseObject (CCDD.CcddConstants.DatabaseObject)1 UndoableTextField (CCDD.CcddUndoHandler.UndoableTextField)1 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1