Search in sources :

Example 31 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddDbTableCommandHandler method storeTableTypesInfoTableCommand.

/**
 ********************************************************************************************
 * Build the command for storing the table type definitions table
 *
 * @return Command for building the specified table
 ********************************************************************************************
 */
protected String storeTableTypesInfoTableCommand() {
    // Build the command to delete the existing table type definitions table and create the new
    // one
    String cmd = "DROP TABLE IF EXISTS " + InternalTable.TABLE_TYPES.getTableName() + "; CREATE TABLE " + InternalTable.TABLE_TYPES.getTableName() + " " + InternalTable.TABLE_TYPES.getColumnCommand(!tableTypeHandler.getTypeDefinitions().isEmpty());
    // Get the index of the default column definitions command (if present)
    int cmdIndex = cmd.indexOf(DefaultColumn.getColumnDefinitions());
    // Check if the default column definitions command is present
    if (cmdIndex != -1) {
        // Remove the default column definitions from the command
        cmd = cmd.substring(0, cmd.indexOf(DefaultColumn.getColumnDefinitions()));
    }
    // Convert the command to a StringBuilder for efficiency
    StringBuilder command = new StringBuilder(cmd);
    // Step through each table type definition
    for (TypeDefinition typeDefn : tableTypeHandler.getTypeDefinitions()) {
        // Step through each column definition
        for (int index = 0; index < typeDefn.getColumnNamesDatabase().length; index++) {
            // Add the command to create the column
            command.append("(" + delimitText(typeDefn.getName()) + ", " + index + ", '" + typeDefn.getColumnNamesDatabase()[index] + "', '" + typeDefn.getColumnNamesUser()[index] + "', " + delimitText(typeDefn.getColumnToolTips()[index]) + ", '" + typeDefn.getInputTypes()[index].getInputName() + "', " + typeDefn.isRowValueUnique()[index] + ", " + typeDefn.isRequired()[index] + ", " + typeDefn.isStructureAllowed()[index] + ", " + typeDefn.isPointerAllowed()[index] + "), ");
        }
    }
    // Replace the trailing comma with a semicolon
    command = CcddUtilities.removeTrailer(command, ", ").append("; ").append(dbControl.buildOwnerCommand(DatabaseObject.TABLE, InternalTable.TABLE_TYPES.getTableName()));
    return command.toString();
}
Also used : TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 32 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddDbTableCommandHandler method loadTableData.

/**
 ********************************************************************************************
 * Perform the database query to load the contents of a database table. The data is sorted in
 * ascending numerical order based on the index (primary key) column
 *
 * @param tablePath
 *            table path in the format rootTable[,dataType1.variable1[,dataType2
 *            .variable2[,...]]]. The table path for a non-structure table is simply the root
 *            table name. For a structure table the root table is the top level structure table
 *            from which this table descends. The first data type/variable name pair is from
 *            the root table, with each succeeding pair coming from the next level down in the
 *            structure's hierarchy
 *
 * @param loadDescription
 *            true to load the table's description
 *
 * @param loadColumnOrder
 *            true to load the table's column order
 *
 * @param loadFieldInfo
 *            true to retrieve the data field information to include with the table
 *            information; false to not load the field information
 *
 * @param parent
 *            GUI component calling this method
 *
 * @return TableInformation class containing the table data from the database. If the error
 *         flag is set the an error occurred and the data is invalid
 ********************************************************************************************
 */
protected TableInformation loadTableData(String tablePath, boolean loadDescription, boolean loadColumnOrder, boolean loadFieldInfo, Component parent) {
    // Create an empty table information class
    TableInformation tableInfo = new TableInformation(tablePath);
    // Strip the variable name, if present, from the table name
    String tableName = tableInfo.getPrototypeName();
    // Convert the table name to lower case. PostgreSQL ignores case; it's done here just to
    // differentiate the table name from the database commands in the event log
    String dbTableName = tableName.toLowerCase();
    try {
        // Check if the table doesn't exist in the database
        if (!isTableExists(dbTableName, parent)) {
            throw new CCDDException("table doesn't exist");
        }
        // Get the table comment
        String[] comment = queryDataTableComment(tableName, parent);
        // Get the table type definition for this table
        TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(comment[TableCommentIndex.TYPE.ordinal()]);
        // Get a comma-separated list of the columns for this table's type
        String columnNames = CcddUtilities.convertArrayToString(typeDefn.getColumnNamesDatabase());
        // Get the table's row information for the specified columns. The table must have all
        // of its table type's columns or else it fails to load
        ResultSet rowData = dbCommand.executeDbQuery("SELECT " + columnNames + " FROM " + dbTableName + " ORDER BY " + DefaultColumn.ROW_INDEX.getDbName() + ";", parent);
        // Create a list to contain the database table rows
        List<String[]> dbRows = new ArrayList<String[]>();
        // Step through each of the query results
        while (rowData.next()) {
            // Create an array to contain the column values
            String[] columnValues = new String[typeDefn.getColumnCountDatabase()];
            // Step through each column in the row
            for (int column = 0; column < typeDefn.getColumnCountDatabase(); column++) {
                // Add the column value to the array. Note that the first column's index in
                // the database is 1, not 0
                columnValues[column] = rowData.getString(column + 1);
                // Check if the value is null
                if (columnValues[column] == null) {
                    // Replace the null with a blank
                    columnValues[column] = "";
                }
            }
            // Add the row data to the list
            dbRows.add(columnValues);
        }
        rowData.close();
        // Create the table information handler for this table
        tableInfo = new TableInformation(comment[TableCommentIndex.TYPE.ordinal()], tablePath, dbRows.toArray(new String[0][0]), (loadColumnOrder ? queryColumnOrder(tablePath, comment[TableCommentIndex.TYPE.ordinal()], parent) : ""), (loadDescription ? queryTableDescription(tablePath, parent) : ""), rootStructures.contains(tablePath), (loadFieldInfo ? retrieveInformationTable(InternalTable.FIELDS, parent).toArray(new String[0][0]) : null));
        // Get the index of the variable name and data type columns
        int varNameIndex = typeDefn.getColumnIndexByInputType(InputDataType.VARIABLE);
        int dataTypeIndex = typeDefn.getColumnIndexByInputType(InputDataType.PRIM_AND_STRUCT);
        // must be loaded
        if (varNameIndex != -1 && dataTypeIndex != -1 && tablePath.contains(",")) {
            // Get the column index for the variable path
            int varPathIndex = typeDefn.getColumnIndexByInputType(InputDataType.VARIABLE_PATH);
            // Check if the variable path column is present
            if (varPathIndex != -1) {
                // Step through each row in the table
                for (int row = 0; row < tableInfo.getData().length; row++) {
                    // Blank the variable path. This prevents the child table from inheriting a
                    // user-defined variable path from the prototype
                    tableInfo.getData()[row][varPathIndex] = "";
                }
            }
            // Place double back slashes before each square brace character in an array index
            // so that the brackets are interpreted correctly in the query's regular expression
            // comparisons
            tablePath = tablePath.replaceAll("\\[(\\d+)\\]", "\\\\\\\\[$1\\\\\\\\]");
            // Get the rows from the custom values table that match the specified parent table
            // and variable path. These values replace those loaded for the prototype of this
            // table
            rowData = dbCommand.executeDbQuery("SELECT * FROM " + InternalTable.VALUES.getTableName() + " WHERE " + ValuesColumn.TABLE_PATH.getColumnName() + " ~ E'^" + tablePath + ",[^,]+$' AND " + ValuesColumn.COLUMN_NAME.getColumnName() + " != '';", parent);
            // Step through each of the query results
            while (rowData.next()) {
                // Get the variable name that will have its value replaced
                String variableName = rowData.getString(ValuesColumn.TABLE_PATH.getColumnName());
                // Get the index of the last data type/variable name separator character (if
                // present)
                int varIndex = variableName.lastIndexOf(".");
                // Check if a variable name exists
                if (varIndex != -1) {
                    // Get the row index for the referenced variable
                    int row = typeDefn.getRowIndexByColumnValue(tableInfo.getData(), variableName.substring(varIndex + 1), varNameIndex);
                    // values table
                    if (row != -1 && tableInfo.getData()[row][dataTypeIndex].equals(variableName.subSequence(variableName.lastIndexOf(",") + 1, varIndex))) {
                        // Get the index of the column that will have its data replaced
                        int column = typeDefn.getColumnIndexByUserName(rowData.getString(ValuesColumn.COLUMN_NAME.getColumnName()));
                        // Check if the table contains the column
                        if (column != -1) {
                            // Replace the value in the table with the one from the custom
                            // values table
                            tableInfo.getData()[row][column] = rowData.getString(ValuesColumn.VALUE.getColumnName());
                        }
                    }
                }
            }
            rowData.close();
        }
    } catch (SQLException | CCDDException se) {
        // Inform the user that loading the table failed
        eventLog.logFailEvent(parent, "Cannot load table '" + tableInfo.getProtoVariableName() + "'; cause '" + se.getMessage() + "'", "<html><b>Cannot load table '</b>" + tableInfo.getProtoVariableName() + "<b>'");
    } catch (Exception e) {
        // Display a dialog providing details on the unanticipated error
        CcddUtilities.displayException(e, parent);
    }
    return tableInfo;
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) TableInformation(CCDD.CcddClassesDataTable.TableInformation) CCDDException(CCDD.CcddClassesDataTable.CCDDException) SQLException(java.sql.SQLException) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 33 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddAssignMessageIDDialog method assignTableMessageIDs.

/**
 ********************************************************************************************
 * Assign message ID values to the structure, command, or other table type message ID columns
 * and data fields
 *
 * @param tabInfo
 *            message ID tab information reference
 *
 * @param tables
 *            list of structure, command, or other tables, with paths
 *
 * @param fieldInformation
 *            list of data field information
 *
 * @return true if a message ID value changed
 ********************************************************************************************
 */
private boolean assignTableMessageIDs(MsgTabInfo type, List<String> tables, List<FieldInformation> fieldInformation) {
    boolean isChanges = false;
    // Get the starting message ID and ID interval values
    int startID = Integer.decode(type.getStartFld().getText());
    int interval = Integer.valueOf(type.getIntervalFld().getText());
    // Step through each table type
    for (TypeDefinition typeDefn : tableTypeHandler.getTypeDefinitions()) {
        // Check if the tab information type and table type match the table type definition
        if ((type.getName().equals(TYPE_STRUCTURE) && typeDefn.isStructure()) || type.getName().equals(TYPE_COMMAND) && typeDefn.isCommand() || (type.getName().equals(TYPE_OTHER) && !typeDefn.isStructure() && !typeDefn.isCommand())) {
            // Get a list of the columns in this table type that are message IDs
            List<Integer> msgIDColumns = typeDefn.getColumnIndicesByInputType(InputDataType.MESSAGE_ID);
            // Check if the table type has any columns that are message IDs
            if (!msgIDColumns.isEmpty()) {
                // Step through each table
                for (String tablePath : tables) {
                    // Load the table's information from the project database
                    TableInformation tableInformation = dbTable.loadTableData(tablePath, false, false, false, CcddAssignMessageIDDialog.this);
                    // the current type definition
                    if (!tableInformation.isErrorFlag() && tableInformation.getType().equals(typeDefn.getName())) {
                        // Create a table editor handler, but without displaying the editor
                        // itself
                        CcddTableEditorHandler editor = new CcddTableEditorHandler(ccddMain, tableInformation, null);
                        // Check if the table arrays aren't expanded
                        if (!editor.isExpanded()) {
                            // Expand the table arrays
                            editor.showHideArrayMembers();
                        }
                        // Get the table's data (again if a name change occurred since changes
                        // were made)
                        Object[][] tableData = editor.getTable().getTableData(false);
                        // Step through each row in the table
                        for (int row = 0; row < editor.getTable().getModel().getRowCount(); row++) {
                            // Step through each column that contains message IDs
                            for (int idColumn : msgIDColumns) {
                                // values should be overwritten or if the cell is empty
                                if (editor.getTable().isCellEditable(editor.getTable().convertRowIndexToView(row), editor.getTable().convertColumnIndexToView(idColumn)) && !tableData[row][idColumn].toString().endsWith(PROTECTED_MSG_ID_IDENT) && (type.getOverwriteCbx().isSelected() || tableData[row][idColumn].toString().isEmpty())) {
                                    // Set the column message ID value to the next unused
                                    // message ID
                                    startID = getNextMessageID(startID, interval);
                                    tableData[row][idColumn] = formatMessageID(startID);
                                }
                            }
                        }
                        // Check if the a message ID in the table was changed
                        if (editor.getTable().isTableChanged(tableData)) {
                            // Load the updated array of data into the table
                            editor.getTable().loadDataArrayIntoTable(tableData, false);
                            // Build the table updates
                            editor.buildUpdates();
                            // Make the table modifications to the project database and to any
                            // open table editors
                            dbTable.modifyTableData(editor.getTableInformation(), editor.getAdditions(), editor.getModifications(), editor.getDeletions(), true, false, false, false, false, null, CcddAssignMessageIDDialog.this);
                        }
                    }
                }
            }
        }
    }
    // Step through each defined data field
    for (int index = 0; index < fieldInformation.size(); index++) {
        // Get the reference to the field information
        FieldInformation fieldInfo = fieldInformation.get(index);
        // blank
        if (fieldInfo.getInputType().equals(InputDataType.MESSAGE_ID) && tables.contains(fieldInfo.getOwnerName()) && !fieldInfo.getValue().endsWith(PROTECTED_MSG_ID_IDENT) && (type.getOverwriteCbx().isSelected() || fieldInfo.getValue().isEmpty())) {
            // Set the message ID data field value to the next unused message ID
            startID = getNextMessageID(startID, interval);
            fieldInfo.setValue(formatMessageID(startID));
            // the database. Step through each table editor dialog
            for (CcddTableEditorDialog editorDialog : ccddMain.getTableEditorDialogs()) {
                boolean isUpdate = false;
                // Step through each table editor in the editor dialog
                for (CcddTableEditorHandler editor : editorDialog.getTableEditors()) {
                    // Get the reference to the table's field handler
                    CcddFieldHandler editorFldHandler = editor.getTableInformation().getFieldHandler();
                    // to the new value
                    if (editorFldHandler.updateField(fieldInfo)) {
                        // Update the committed message ID value
                        editor.getCommittedTableInformation().getFieldHandler().updateField(fieldInfo);
                        // Update the editor data fields
                        editor.updateDataFields();
                        // Set the flag to indicate the table/field combination was located and
                        // stop searching
                        isUpdate = true;
                        break;
                    }
                }
                // Check if this table/field combination has been located
                if (isUpdate) {
                    // Stop searching
                    break;
                }
            }
            // Set the flag to indicate a message ID value is changed
            isChanges = true;
        }
    }
    return isChanges;
}
Also used : TableInformation(CCDD.CcddClassesDataTable.TableInformation) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) FieldInformation(CCDD.CcddClassesDataTable.FieldInformation)

Example 34 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddDataTypeEditorDialog method createDataTypeTable.

/**
 ********************************************************************************************
 * Create the data type table
 *
 * @return Reference to the scroll pane in which the table is placed
 ********************************************************************************************
 */
private JScrollPane createDataTypeTable() {
    // Define the data type editor JTable
    dataTypeTable = new CcddJTableHandler(DefaultPrimitiveTypeInfo.values().length) {

        /**
         ************************************************************************************
         * Allow multiple line display in user name and C name columns
         ************************************************************************************
         */
        @Override
        protected boolean isColumnMultiLine(int column) {
            return column == DataTypeEditorColumnInfo.USER_NAME.ordinal() || column == DataTypeEditorColumnInfo.C_NAME.ordinal();
        }

        /**
         ************************************************************************************
         * Hide the specified column(s)
         ************************************************************************************
         */
        @Override
        protected boolean isColumnHidden(int column) {
            return column == DataTypeEditorColumnInfo.OID.ordinal();
        }

        /**
         ************************************************************************************
         * Override isCellEditable so that all columns can be edited
         ************************************************************************************
         */
        @Override
        public boolean isCellEditable(int row, int column) {
            return true;
        }

        /**
         ************************************************************************************
         * Allow pasting data into the data type cells
         ************************************************************************************
         */
        @Override
        protected boolean isDataAlterable(Object[] rowData, int row, int column) {
            return isCellEditable(convertRowIndexToView(row), convertColumnIndexToView(column));
        }

        /**
         ************************************************************************************
         * Override getCellEditor so that for a base data type column cell the base data type
         * combo box cell editor is returned; for all other cells return the normal cell editor
         *
         * @param row
         *            table view row number
         *
         * @param column
         *            table view column number
         *
         * @return The cell editor for the specified row and column
         ************************************************************************************
         */
        @Override
        public TableCellEditor getCellEditor(int row, int column) {
            // Get the editor for this cell
            TableCellEditor cellEditor = super.getCellEditor(row, column);
            // Convert the row and column indices to the model coordinates
            int modelColumn = convertColumnIndexToModel(column);
            // column
            if (modelColumn == DataTypeEditorColumnInfo.BASE_TYPE.ordinal()) {
                // Select the combo box cell editor that displays the base data types
                cellEditor = baseTypeCellEditor;
            }
            return cellEditor;
        }

        /**
         ************************************************************************************
         * Validate changes to the editable cells
         *
         * @param tableData
         *            list containing the table data row arrays
         *
         * @param row
         *            table model row number
         *
         * @param column
         *            table model column number
         *
         * @param oldValue
         *            original cell contents
         *
         * @param newValue
         *            new cell contents
         *
         * @param showMessage
         *            true to display the invalid input dialog, if applicable
         *
         * @param isMultiple
         *            true if this is one of multiple cells to be entered and checked; false if
         *            only a single input is being entered
         *
         * @return Always returns false
         ************************************************************************************
         */
        @Override
        protected Boolean validateCellContent(List<Object[]> tableData, int row, int column, Object oldValue, Object newValue, Boolean showMessage, boolean isMultiple) {
            // Reset the flag that indicates the last edited cell's content is invalid
            setLastCellValid(true);
            // Create a string version of the old and new values
            String oldValueS = oldValue.toString();
            String newValueS = newValue.toString();
            try {
                // Check if the value isn't blank
                if (!newValueS.isEmpty()) {
                    // Check if the data type user name or C type has been changed
                    if (column == DataTypeEditorColumnInfo.USER_NAME.ordinal() || column == DataTypeEditorColumnInfo.C_NAME.ordinal()) {
                        // avoid creating a duplicate
                        for (int otherRow = 0; otherRow < getRowCount(); otherRow++) {
                            // type name matches the one being added (case insensitive)
                            if (otherRow != row && newValueS.equalsIgnoreCase(CcddDataTypeHandler.getDataTypeName(tableData.get(otherRow)[DataTypeEditorColumnInfo.USER_NAME.ordinal()].toString(), tableData.get(otherRow)[DataTypeEditorColumnInfo.C_NAME.ordinal()].toString()))) {
                                throw new CCDDException("Data type name already in use");
                            }
                        }
                        // Check if the data type user name has been changed
                        if (column == DataTypeEditorColumnInfo.USER_NAME.ordinal()) {
                            // input type
                            if (!newValueS.matches(InputDataType.ALPHANUMERIC.getInputMatch())) {
                                throw new CCDDException("Illegal character(s) in data type name");
                            }
                        } else // The data type C type has been changed
                        {
                            // Initialize the C type name matching regular expression
                            String match = InputDataType.ALPHANUMERIC_MULTI.getInputMatch();
                            // Check if the base data type is a pointer
                            if (tableData.get(row)[DataTypeEditorColumnInfo.BASE_TYPE.ordinal()].equals(BaseDataTypeInfo.POINTER.getName())) {
                                // asterisk
                                if (!newValueS.endsWith("*")) {
                                    // Append an asterisk to the C type name
                                    newValueS += "*";
                                    tableData.get(row)[column] = newValueS;
                                }
                                // Add the ending asterisk to the matching regular expression
                                match += "\\*+";
                            } else // Check if the base type is blank
                            if (tableData.get(row)[DataTypeEditorColumnInfo.BASE_TYPE.ordinal()].toString().isEmpty()) {
                                // Add the optional ending asterisk to the matching regular
                                // expression
                                match += "\\*?+";
                            }
                            // alphanumeric input type (with an asterisk if this is a pointer)
                            if (!newValueS.matches(match)) {
                                throw new CCDDException("Illegal character(s) in data type C type name");
                            }
                        }
                    } else // Check if this is the data type size column
                    if (column == DataTypeEditorColumnInfo.SIZE.ordinal()) {
                        // Check if the data type size is not a positive integer
                        if (!newValueS.matches(InputDataType.INT_POSITIVE.getInputMatch())) {
                            throw new CCDDException("Data type size must be a positive integer");
                        }
                        // Remove any unneeded characters and store the cleaned number
                        tableData.get(row)[column] = Integer.valueOf(newValueS.replaceAll(InputDataType.INT_POSITIVE.getInputMatch(), "$1"));
                    } else // Check if this is the data type base type column
                    if (column == DataTypeEditorColumnInfo.BASE_TYPE.ordinal()) {
                        // Get the C type name
                        String cType = tableData.get(row)[DataTypeEditorColumnInfo.C_NAME.ordinal()].toString();
                        // Check if the base type changed from a non-pointer to a pointer
                        if (newValueS.equals(BaseDataTypeInfo.POINTER.getName())) {
                            // an asterisk
                            if (!cType.isEmpty() && !cType.endsWith("*")) {
                                // Append an asterisk to the C type name
                                tableData.get(row)[DataTypeEditorColumnInfo.C_NAME.ordinal()] += "*";
                            }
                        } else // the base type had been empty
                        if (oldValueS.equals(BaseDataTypeInfo.POINTER.getName()) || oldValueS.isEmpty()) {
                            // Remove any asterisks from the C type name
                            tableData.get(row)[DataTypeEditorColumnInfo.C_NAME.ordinal()] = cType.replaceAll("\\*", "");
                        }
                    }
                    // a non-integer
                    if (!oldValueS.isEmpty() && ((column == DataTypeEditorColumnInfo.SIZE.ordinal() && Integer.valueOf(newValueS) < Integer.valueOf(oldValueS) && (tableData.get(row)[DataTypeEditorColumnInfo.BASE_TYPE.ordinal()].equals(BaseDataTypeInfo.SIGNED_INT.getName()) || tableData.get(row)[DataTypeEditorColumnInfo.BASE_TYPE.ordinal()].equals(BaseDataTypeInfo.UNSIGNED_INT.getName()))) || (column == DataTypeEditorColumnInfo.BASE_TYPE.ordinal() && (oldValueS.equals(BaseDataTypeInfo.SIGNED_INT.getName()) || oldValueS.equals(BaseDataTypeInfo.UNSIGNED_INT.getName())) && !(newValueS.equals(BaseDataTypeInfo.SIGNED_INT.getName()) || newValueS.equals(BaseDataTypeInfo.UNSIGNED_INT.getName()))))) {
                        // Get the data type's index
                        String index = tableData.get(row)[DataTypeEditorColumnInfo.OID.ordinal()].toString();
                        // Step through the committed data types
                        for (int commRow = 0; commRow < committedData.length; commRow++) {
                            // Check if the index matches that for the committed data type
                            if (index.equals(committedData[commRow][DataTypeEditorColumnInfo.OID.ordinal()])) {
                                List<String> tableNames = new ArrayList<String>();
                                // Get the data type name. Use the committed name (in place of
                                // the current name in the editor, in case it's been changed)
                                // since this is how the data type is referenced in the data
                                // tables
                                String dataTypeName = CcddDataTypeHandler.getDataTypeName(CcddUtilities.convertObjectToString(committedData[commRow]));
                                // prototype tables
                                for (String dataTypeRef : getDataTypeReferences(dataTypeName).getReferences()) {
                                    // Split the reference into table name, column name, table
                                    // type, and context
                                    String[] tblColDescAndCntxt = dataTypeRef.split(TABLE_DESCRIPTION_SEPARATOR, 4);
                                    String refComment = tblColDescAndCntxt[SearchResultsQueryColumn.COMMENT.ordinal()];
                                    // Extract the viewable name and type of the table, and the
                                    // name of the column containing the data type. Separate
                                    // the column string into the individual column values
                                    String[] refNameAndType = refComment.split(",");
                                    String[] refColumns = CcddUtilities.splitAndRemoveQuotes(tblColDescAndCntxt[SearchResultsQueryColumn.CONTEXT.ordinal()]);
                                    // Use the type and column to get the column's input data
                                    // type
                                    TypeDefinition typeDefn = ccddMain.getTableTypeHandler().getTypeDefinition(refNameAndType[1]);
                                    // Get the index of the bit length column, if present
                                    int bitLengthIndex = typeDefn.getColumnIndexByInputType(InputDataType.BIT_LENGTH);
                                    // Check if the byte size changed
                                    if (column == DataTypeEditorColumnInfo.SIZE.ordinal()) {
                                        // have a conflict
                                        if (bitLengthIndex != -1 && !refColumns[bitLengthIndex].isEmpty() && Integer.valueOf(newValueS) * 8 < Integer.valueOf(refColumns[bitLengthIndex]) && !tableNames.contains(refNameAndType[0])) {
                                            // The bit length is now too large; add the
                                            // affected table name to the list
                                            tableNames.add(refNameAndType[0]);
                                        }
                                    } else // The base type changed
                                    {
                                        // to have a conflict
                                        if (bitLengthIndex != -1 && !refColumns[bitLengthIndex].isEmpty() && !tableNames.contains(refNameAndType[0])) {
                                            // A bit length is not valid with the new data
                                            // type; add the affected table name to the list
                                            tableNames.add(refNameAndType[0]);
                                        }
                                        // Get the enumeration column index(ices), if present
                                        List<Integer> enumerationIndices = typeDefn.getColumnIndicesByInputType(InputDataType.ENUMERATION);
                                        // Step through each enumeration column
                                        for (int enumIndex : enumerationIndices) {
                                            // already been found to have a conflict
                                            if (!refColumns[enumIndex].isEmpty() && !tableNames.contains(refNameAndType[0])) {
                                                // An enumeration is not valid with the new
                                                // data type; add the affected table name to
                                                // the list
                                                tableNames.add(refNameAndType[0]);
                                            }
                                        }
                                    }
                                }
                                // were found
                                if (!tableNames.isEmpty()) {
                                    // Check if the byte size changed
                                    if (column == DataTypeEditorColumnInfo.SIZE.ordinal()) {
                                        throw new CCDDException("Bit length exceeds the size of the data type in table(s) '</b>" + dbTable.getShortenedTableNames(tableNames.toArray(new String[0])) + "<b>'");
                                    } else // The base type changed
                                    {
                                        throw new CCDDException("Base data type inconsistent with data type usage in table(s) '</b>" + dbTable.getShortenedTableNames(tableNames.toArray(new String[0])) + "<b>'");
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            } catch (CCDDException ce) {
                // Set the flag that indicates the last edited cell's content is invalid
                setLastCellValid(false);
                // Check if the input error dialog should be displayed
                if (showMessage) {
                    // Inform the user that the input value is invalid
                    new CcddDialogHandler().showMessageDialog(CcddDataTypeEditorDialog.this, "<html><b>" + ce.getMessage(), "Invalid Input", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
                }
                // Restore the cell contents to its original value and pop the edit from the
                // stack
                tableData.get(row)[column] = oldValue;
                dataTypeTable.getUndoManager().undoRemoveEdit();
            }
            return false;
        }

        /**
         ************************************************************************************
         * Load the table data types into the table and format the table cells
         ************************************************************************************
         */
        @Override
        protected void loadAndFormatData() {
            // Place the data into the table model along with the column names, set up the
            // editors and renderers for the table cells, set up the table grid lines, and
            // calculate the minimum width required to display the table information
            setUpdatableCharacteristics(committedData, DataTypeEditorColumnInfo.getColumnNames(), null, DataTypeEditorColumnInfo.getToolTips(), true, true, true);
        }

        /**
         ************************************************************************************
         * Override prepareRenderer to allow adjusting the background colors of table cells
         ************************************************************************************
         */
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            JComponent comp = (JComponent) super.prepareRenderer(renderer, row, column);
            // invalid highlighting, if applicable)
            if (!(isFocusOwner() && isRowSelected(row) && (isColumnSelected(column) || !getColumnSelectionAllowed()))) {
                boolean found = true;
                // Convert the column to model coordinates
                int modelColumn = dataTypeTable.convertColumnIndexToModel(column);
                // Check if both the user name and C type columns are blank
                if ((modelColumn == DataTypeEditorColumnInfo.USER_NAME.ordinal() || modelColumn == DataTypeEditorColumnInfo.C_NAME.ordinal()) && dataTypeTable.getValueAt(row, DataTypeEditorColumnInfo.USER_NAME.ordinal()).toString().isEmpty() && dataTypeTable.getValueAt(row, DataTypeEditorColumnInfo.C_NAME.ordinal()).toString().isEmpty()) {
                    // Set the flag indicating that the cell value is invalid
                    found = false;
                } else // Check if the cell is required and is empty
                if (DataTypeEditorColumnInfo.values()[modelColumn].isRequired() && dataTypeTable.getValueAt(row, column).toString().isEmpty()) {
                    // Set the flag indicating that the cell value is invalid
                    found = false;
                }
                // Check if the cell value is invalid
                if (!found) {
                    // Change the cell's background color
                    comp.setBackground(ModifiableColorInfo.REQUIRED_BACK.getColor());
                }
            }
            return comp;
        }

        /**
         ************************************************************************************
         * Override the CcddJTableHandler method to produce an array containing empty values
         * for a new row in this table
         *
         * @return Array containing blank cell values for a new row
         ************************************************************************************
         */
        @Override
        protected Object[] getEmptyRow() {
            return DataTypeEditorColumnInfo.getEmptyRow();
        }

        /**
         ************************************************************************************
         * Handle a change to the table's content
         ************************************************************************************
         */
        @Override
        protected void processTableContentChange() {
            // Add or remove the change indicator based on whether any unstored changes exist
            setTitle(DIALOG_TITLE + (dataTypeTable.isTableChanged(committedData) ? "*" : ""));
            // Force the table to redraw so that changes to the cells are displayed
            repaint();
        }
    };
    // Place the table into a scroll pane
    JScrollPane scrollPane = new JScrollPane(dataTypeTable);
    // Disable storage of edit operations during creation of the table
    dataTypeTable.getUndoHandler().setAllowUndo(false);
    // Set common table parameters and characteristics
    dataTypeTable.setFixedCharacteristics(scrollPane, true, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, TableSelectionMode.SELECT_BY_CELL, false, ModifiableColorInfo.TABLE_BACK.getColor(), true, true, ModifiableFontInfo.DATA_TABLE_CELL.getFont(), true);
    // Re-enable storage of edit operations
    dataTypeTable.getUndoHandler().setAllowUndo(true);
    return scrollPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) TableCellRenderer(javax.swing.table.TableCellRenderer) CCDDException(CCDD.CcddClassesDataTable.CCDDException) JComponent(javax.swing.JComponent) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) List(java.util.List) ArrayList(java.util.ArrayList) TableCellEditor(javax.swing.table.TableCellEditor) Component(java.awt.Component) JComponent(javax.swing.JComponent)

Example 35 with TypeDefinition

use of CCDD.CcddTableTypeHandler.TypeDefinition in project CCDD by nasa.

the class CcddPatchHandler method updateTableTypesTable.

/**
 ********************************************************************************************
 * Update the internal table __types to the new name __table_types, delete the primitive_only
 * column, and add the structure allowed and pointer allowed columns. If successful, the
 * original table (__types) is renamed, preserving the original information and preventing
 * subsequent conversion attempts. The project database is first backed up to the file
 * <projectName>_<timeStamp>.dbu. Older versions of CCDD are not compatible with the project
 * database after applying this patch
 *
 * @throws CCDDException
 *             If the user elects to not install the patch or an error occurs while applying
 *             the patch
 ********************************************************************************************
 */
private void updateTableTypesTable() throws CCDDException {
    CcddDbTableCommandHandler dbTable = ccddMain.getDbTableCommandHandler();
    // Check if the old table exists
    if (dbTable.isTableExists("__types", ccddMain.getMainFrame())) {
        // Check if the user elects to not apply the patch
        if (new CcddDialogHandler().showMessageDialog(ccddMain.getMainFrame(), "<html><b>Apply patch to update the table types " + "table?<br><br></b>Creates the new " + "__table_types table from the old __types " + "table.<br><b><i>Older versions of CCDD " + "will be incompatible with this project " + "database after applying the patch", "Apply Patch #01262017", JOptionPane.QUESTION_MESSAGE, DialogOption.OK_CANCEL_OPTION) != OK_BUTTON) {
            throw new CCDDException("user elected to not install patch (#01262017)");
        }
        CcddDbControlHandler dbControl = ccddMain.getDbControlHandler();
        CcddDbCommandHandler dbCommand = ccddMain.getDbCommandHandler();
        CcddEventLogDialog eventLog = ccddMain.getSessionEventLog();
        CcddTableTypeHandler tableTypeHandler = ccddMain.getTableTypeHandler();
        try {
            // Back up the project database before applying the patch
            dbControl.backupDatabase(dbControl.getDatabaseName(), new FileEnvVar(ModifiablePathInfo.DATABASE_BACKUP_PATH.getPath() + File.separator + dbControl.getDatabaseName() + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()) + FileExtension.DBU.getExtension()));
            // Create lists to contain the old and new table types table items
            List<String[]> oldTableData = new ArrayList<String[]>();
            List<String[]> newTableData = new ArrayList<String[]>();
            // Read the contents of the old table types table
            ResultSet infoData = dbCommand.executeDbQuery("SELECT * FROM __types ORDER BY OID;", ccddMain.getMainFrame());
            // Step through each of the query results
            while (infoData.next()) {
                // Create an array to contain the column values
                String[] columnValues = new String[infoData.getMetaData().getColumnCount()];
                // Step through each column in the row
                for (int column = 0; column < infoData.getMetaData().getColumnCount(); column++) {
                    // Add the column value to the array. Note that the first column's index in
                    // the database is 1, not 0
                    columnValues[column] = infoData.getString(column + 1);
                    // Check if the value is null
                    if (columnValues[column] == null) {
                        // Replace the null with a blank
                        columnValues[column] = "";
                    }
                }
                // Add the row data to the list
                oldTableData.add(columnValues);
            }
            infoData.close();
            // Indicate in the log that the old data successfully loaded
            eventLog.logEvent(SUCCESS_MSG, "__types retrieved");
            // Step through the old table types column definitions
            for (String[] oldColumnDefn : oldTableData) {
                boolean isFound = false;
                // Create storage for the new column definition
                String[] newColumnDefn = new String[InternalTable.TABLE_TYPES.getNumColumns()];
                // Step through each of the old columns (the new table has one extra column)
                for (int index = 0; index < TableTypesColumn.values().length - 1; index++) {
                    // Copy the old columns definition to the new column definition
                    newColumnDefn[index] = oldColumnDefn[index];
                }
                // Get the default type definition for this table type name
                TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(oldColumnDefn[TableTypesColumn.TYPE_NAME.ordinal()]);
                // Check if the type exists in the default definitions
                if (typeDefn != null) {
                    // Get the index of the column
                    int column = typeDefn.getColumnIndexByDbName(oldColumnDefn[TableTypesColumn.COLUMN_NAME_DB.ordinal()]);
                    // Check if the column exists in the default type definition
                    if (column != -1) {
                        // Use the default definition to set the structure and pointer allowed
                        // flags
                        newColumnDefn[TableTypesColumn.STRUCTURE_ALLOWED.ordinal()] = typeDefn.isStructureAllowed()[column] ? "t" : "f";
                        newColumnDefn[TableTypesColumn.POINTER_ALLOWED.ordinal()] = typeDefn.isPointerAllowed()[column] ? "t" : "f";
                        isFound = true;
                    }
                }
                // Check if this column isn't in the default column definitions
                if (!isFound) {
                    // Assume that this column is valid for a structures and pointers
                    newColumnDefn[TableTypesColumn.STRUCTURE_ALLOWED.ordinal()] = "t";
                    newColumnDefn[TableTypesColumn.POINTER_ALLOWED.ordinal()] = "t";
                }
                // Add the column definition to the list
                newTableData.add(newColumnDefn);
            }
            // Delete the default column definitions
            tableTypeHandler.getTypeDefinitions().clear();
            // Step through the updated table types column definitions
            for (String[] newColumnDefn : newTableData) {
                // Get the type definition associated with this column
                TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(newColumnDefn[TableTypesColumn.TYPE_NAME.ordinal()]);
                // Check if the type is not defined
                if (typeDefn == null) {
                    // Create the type and add it to the list. THis creates the primary key and
                    // row index columns
                    typeDefn = tableTypeHandler.createTypeDefinition(newColumnDefn[TableTypesColumn.TYPE_NAME.ordinal()], new String[0][0], newColumnDefn[TableTypesColumn.COLUMN_DESCRIPTION.ordinal()]);
                }
                // these were created previously
                if (!newColumnDefn[TableTypesColumn.COLUMN_NAME_DB.ordinal()].equals(DefaultColumn.PRIMARY_KEY.getDbName()) && !newColumnDefn[TableTypesColumn.COLUMN_NAME_DB.ordinal()].equals(DefaultColumn.ROW_INDEX.getDbName())) {
                    // Add the column names, description, input type, and flags to the type
                    // definition
                    typeDefn.addColumn(Integer.parseInt(newColumnDefn[TableTypesColumn.INDEX.ordinal()].toString()), newColumnDefn[TableTypesColumn.COLUMN_NAME_DB.ordinal()].toString(), newColumnDefn[TableTypesColumn.COLUMN_NAME_VISIBLE.ordinal()].toString(), newColumnDefn[TableTypesColumn.COLUMN_DESCRIPTION.ordinal()].toString(), InputDataType.getInputTypeByName(newColumnDefn[TableTypesColumn.INPUT_TYPE.ordinal()].toString()), newColumnDefn[TableTypesColumn.ROW_VALUE_UNIQUE.ordinal()].equals("t") ? true : false, newColumnDefn[TableTypesColumn.COLUMN_REQUIRED.ordinal()].equals("t") ? true : false, newColumnDefn[TableTypesColumn.STRUCTURE_ALLOWED.ordinal()].equals("t") ? true : false, newColumnDefn[TableTypesColumn.POINTER_ALLOWED.ordinal()].equals("t") ? true : false);
                }
            }
            // Store the updated table type definitions in the project database and change the
            // old table types table name so that the conversion doesn't take place again
            dbCommand.executeDbCommand(dbTable.storeTableTypesInfoTableCommand() + "ALTER TABLE __types RENAME TO __types_backup;", ccddMain.getMainFrame());
            // Inform the user that converting the table types completed
            eventLog.logEvent(EventLogMessageType.SUCCESS_MSG, "Table types conversion complete");
            // Reopen the database
            dbControl.openDatabase(dbControl.getProjectName());
        } catch (Exception e) {
            // Inform the user that converting the table types table failed
            eventLog.logFailEvent(ccddMain.getMainFrame(), "Cannot convert table types table to new format; cause '" + e.getMessage() + "'", "<html><b>Cannot convert table types table to new " + "format (project database will be closed)");
            throw new CCDDException();
        }
    }
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) FileEnvVar(CCDD.CcddClassesComponent.FileEnvVar) ArrayList(java.util.ArrayList) CCDDException(CCDD.CcddClassesDataTable.CCDDException) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) ResultSet(java.sql.ResultSet) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

TypeDefinition (CCDD.CcddTableTypeHandler.TypeDefinition)64 TableInformation (CCDD.CcddClassesDataTable.TableInformation)30 ArrayList (java.util.ArrayList)24 CCDDException (CCDD.CcddClassesDataTable.CCDDException)18 AssociatedColumns (CCDD.CcddClassesDataTable.AssociatedColumns)10 SQLException (java.sql.SQLException)9 JSONArray (org.json.simple.JSONArray)8 JSONObject (org.json.simple.JSONObject)8 TableTypeDefinition (CCDD.CcddClassesDataTable.TableTypeDefinition)6 IOException (java.io.IOException)6 TableDefinition (CCDD.CcddClassesDataTable.TableDefinition)5 BackgroundCommand (CCDD.CcddBackgroundCommand.BackgroundCommand)4 ArrayListMultiple (CCDD.CcddClassesComponent.ArrayListMultiple)4 FileEnvVar (CCDD.CcddClassesComponent.FileEnvVar)3 FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)3 Component (java.awt.Component)3 GridBagConstraints (java.awt.GridBagConstraints)3 GridBagLayout (java.awt.GridBagLayout)3 Insets (java.awt.Insets)3 ResultSet (java.sql.ResultSet)3