Search in sources :

Example 21 with TypeDefinition

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

the class CcddDataTypeEditorDialog method initialize.

/**
 ********************************************************************************************
 * Create the data type editor dialog. This is executed in a separate thread since it can take
 * a noticeable amount time to complete, and by using a separate thread the GUI is allowed to
 * continue to update. The GUI menu commands, however, are disabled until the telemetry
 * scheduler initialization completes execution
 ********************************************************************************************
 */
private void initialize() {
    // Build the data type editor dialog in the background
    CcddBackgroundCommand.executeInBackground(ccddMain, new BackgroundCommand() {

        // Create panels to hold the components of the dialog
        JPanel editorPnl = new JPanel(new GridBagLayout());

        JPanel buttonPnl = new JPanel();

        JButton btnClose;

        /**
         ************************************************************************************
         * Build the data type editor dialog
         ************************************************************************************
         */
        @Override
        protected void execute() {
            modifications = new ArrayList<TableModification>();
            loadedReferences = new ArrayList<DataTypeReference>();
            // Set the initial layout manager characteristics
            GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0);
            // Create a copy of the data type data so it can be used to determine if changes
            // are made
            committedData = dataTypeHandler.getDataTypeDataArray();
            // Define the panel to contain the table and place it in the editor
            JPanel tablePnl = new JPanel();
            tablePnl.setLayout(new BoxLayout(tablePnl, BoxLayout.X_AXIS));
            tablePnl.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
            tablePnl.add(createDataTypeTable());
            editorPnl.add(tablePnl, gbc);
            editorPnl.setBorder(BorderFactory.createEmptyBorder());
            // Create the cell editor for base data types
            createBasePrimitiveTypeCellEditor();
            // Set the modal undo manager and table references in the keyboard handler while
            // the data type editor is active
            ccddMain.getKeyboardHandler().setModalDialogReference(dataTypeTable.getUndoManager(), dataTypeTable);
            // New button
            JButton btnInsertRow = CcddButtonPanelHandler.createButton("Ins Row", INSERT_ICON, KeyEvent.VK_I, "Insert a new row into the table");
            // Create a listener for the Insert Row button
            btnInsertRow.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Insert a new row into the table at the selected location
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    dataTypeTable.insertEmptyRow(true);
                }
            });
            // Delete button
            JButton btnDeleteRow = CcddButtonPanelHandler.createButton("Del Row", DELETE_ICON, KeyEvent.VK_D, "Delete the selected row(s) from the table");
            // Create a listener for the Delete row button
            btnDeleteRow.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Delete the selected row(s) from the table
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    // Step through each row selected for deletion
                    for (int row : dataTypeTable.getSelectedRows()) {
                        // Get the data type name
                        String dataType = CcddDataTypeHandler.getDataTypeName(dataTypeTable.getValueAt(row, DataTypesColumn.USER_NAME.ordinal()).toString(), dataTypeTable.getValueAt(row, DataTypesColumn.C_NAME.ordinal()).toString());
                        // Check if the data type name is present
                        if (!dataType.isEmpty()) {
                            boolean isInUse = false;
                            // type, and not just text matching the data type's name
                            for (String dataTypeRef : getDataTypeReferences(dataType).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()];
                                // Check if the match is within a sizeof() call
                                if (CcddVariableSizeAndConversionHandler.hasSizeof(tblColDescAndCntxt[SearchResultsQueryColumn.CONTEXT.ordinal()], dataType)) {
                                    // Set the flag to indicate the reference is to the data
                                    // type, and stop searching
                                    isInUse = true;
                                    break;
                                }
                                // 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 indices for all columns that can reference a data
                                // type
                                List<Integer> primColumns = typeDefn.getColumnIndicesByInputType(InputDataType.PRIMITIVE);
                                primColumns.addAll(typeDefn.getColumnIndicesByInputType(InputDataType.PRIM_AND_STRUCT));
                                // Step through each of the data type columns
                                for (int column : primColumns) {
                                    // Check if the column contents matches the data type name
                                    if (refColumns[column].equals(dataType)) {
                                        // Set the flag to indicate the reference is to the
                                        // data type, and stop searching
                                        isInUse = true;
                                        break;
                                    }
                                }
                                // Check if the data type has a valid reference
                                if (isInUse) {
                                    // Stop searching
                                    break;
                                }
                            }
                            // Check if the data type is in use by a data table
                            if (isInUse) {
                                // Deselect the data type
                                dataTypeTable.removeRowSelectionInterval(row, row);
                                // Inform the user that the data type can't be deleted
                                new CcddDialogHandler().showMessageDialog(CcddDataTypeEditorDialog.this, "<html><b>Cannot delete data type '" + dataType + "'; data type is referenced by a data table", "Delete Data Type", JOptionPane.QUESTION_MESSAGE, DialogOption.OK_OPTION);
                            }
                        }
                    }
                    dataTypeTable.deleteRow(true);
                }
            });
            // Move Up button
            JButton btnMoveUp = CcddButtonPanelHandler.createButton("Up", UP_ICON, KeyEvent.VK_U, "Move the selected row(s) up");
            // Create a listener for the Move Up button
            btnMoveUp.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Move the selected row(s) up in the table
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    dataTypeTable.moveRowUp();
                }
            });
            // Move Down button
            JButton btnMoveDown = CcddButtonPanelHandler.createButton("Down", DOWN_ICON, KeyEvent.VK_W, "Move the selected row(s) down");
            // Create a listener for the Move Down button
            btnMoveDown.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Move the selected row(s) down in the table
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    dataTypeTable.moveRowDown();
                }
            });
            // Undo button
            JButton btnUndo = CcddButtonPanelHandler.createButton("Undo", UNDO_ICON, KeyEvent.VK_Z, "Undo the last edit");
            // Create a listener for the Undo button
            btnUndo.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Undo the last cell edit
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    dataTypeTable.getUndoManager().undo();
                }
            });
            // Redo button
            JButton btnRedo = CcddButtonPanelHandler.createButton("Redo", REDO_ICON, KeyEvent.VK_Y, "Redo the last undone edit");
            // Create a listener for the Redo button
            btnRedo.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Redo the last cell edit that was undone
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    dataTypeTable.getUndoManager().redo();
                }
            });
            // Store the data types button
            JButton btnStore = CcddButtonPanelHandler.createButton("Store", STORE_ICON, KeyEvent.VK_S, "Store the data type(s)");
            // Create a listener for the Store button
            btnStore.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Store the data types
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    // the action
                    if (dataTypeTable.isTableChanged(committedData) && !checkForMissingColumns() && new CcddDialogHandler().showMessageDialog(CcddDataTypeEditorDialog.this, "<html><b>Store changes in project database?", "Store Changes", JOptionPane.QUESTION_MESSAGE, DialogOption.OK_CANCEL_OPTION) == OK_BUTTON) {
                        // Get a list of the data type modifications
                        buildUpdates();
                        // Update the tables affected by the changes to the data type(s)
                        dbTable.modifyTablePerDataTypeOrMacroChanges(modifications, getUpdatedData(), CcddDataTypeEditorDialog.this);
                    }
                }
            });
            // Close button
            btnClose = CcddButtonPanelHandler.createButton("Close", CLOSE_ICON, KeyEvent.VK_C, "Close the data type editor");
            // Create a listener for the Close button
            btnClose.addActionListener(new ValidateCellActionListener(dataTypeTable) {

                /**
                 ****************************************************************************
                 * Close the data type editor dialog
                 ****************************************************************************
                 */
                @Override
                protected void performAction(ActionEvent ae) {
                    windowCloseButtonAction();
                }
            });
            // Add buttons in the order in which they'll appear (left to right, top to bottom)
            buttonPnl.add(btnInsertRow);
            buttonPnl.add(btnMoveUp);
            buttonPnl.add(btnUndo);
            buttonPnl.add(btnStore);
            buttonPnl.add(btnDeleteRow);
            buttonPnl.add(btnMoveDown);
            buttonPnl.add(btnRedo);
            buttonPnl.add(btnClose);
            // Distribute the buttons across two rows
            setButtonRows(2);
        }

        /**
         ************************************************************************************
         * Data type editor dialog creation complete
         ************************************************************************************
         */
        @Override
        protected void complete() {
            // Display the data type editor dialog
            showOptionsDialog(ccddMain.getMainFrame(), editorPnl, buttonPnl, btnClose, DIALOG_TITLE, true);
        }
    });
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) ArrayList(java.util.ArrayList) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) ValidateCellActionListener(CCDD.CcddClassesComponent.ValidateCellActionListener) BackgroundCommand(CCDD.CcddBackgroundCommand.BackgroundCommand)

Example 22 with TypeDefinition

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

the class CcddDbVerificationHandler method verifyTableTypes.

/**
 ********************************************************************************************
 * Check that the tables are consistent with their type definitions. If any inconsistencies are
 * detected then get user approval to alter the table(s)
 *
 * @param tableResult
 *            metadata for all tables
 ********************************************************************************************
 */
private void verifyTableTypes(ResultSet tableResult) {
    String tableNameDb = "";
    try {
        // Get the column order information from the column order table
        List<String[]> orders = dbTable.retrieveInformationTable(InternalTable.ORDERS, ccddMain.getMainFrame());
        // Initialize the progress bar within-step value counters
        int count = 0;
        int startProgress = progBar.getValue();
        // Get the total number of rows in the result set
        tableResult.last();
        int total = tableResult.getRow();
        // Start before the first row in the result set
        tableResult.beforeFirst();
        // Step through each database table
        while (tableResult.next()) {
            // Check if the user canceled verification
            if (canceled) {
                break;
            }
            // Get the table name
            tableNameDb = tableResult.getString("TABLE_NAME");
            // Check if this is a data table
            if (!tableNameDb.startsWith(INTERNAL_TABLE_PREFIX)) {
                String tableName = "";
                // Get the comment array for this table
                String[] comment = dbTable.getTableComment(tableNameDb, comments);
                // Get the table name as seen by the user
                tableName = comment[TableCommentIndex.NAME.ordinal()];
                // Get the table's type definition
                TypeDefinition typeDefinition = tableTypeHandler.getTypeDefinition(comment[TableCommentIndex.TYPE.ordinal()]);
                // Check if the type definition is defined in the table type definitions
                if (typeDefinition != null) {
                    // Step through each table in the column order table
                    for (int index = 0; index < orders.size() && !canceled; index++) {
                        // doesn't match the number of columns for this table's type
                        if (orders.get(index)[OrdersColumn.TABLE_PATH.ordinal()].matches("^" + tableName + "(,|$)") && orders.get(index)[OrdersColumn.COLUMN_ORDER.ordinal()].split(":").length != typeDefinition.getColumnCountDatabase()) {
                            // Column order table has an invalid entry for this table
                            issues.add(new TableIssue("Incorrect number of columns indicated for table '" + orders.get(index)[OrdersColumn.TABLE_PATH.ordinal()] + "' in the column order table for user '" + orders.get(index)[OrdersColumn.USER_NAME.ordinal()] + "'", "Modify column order table", "UPDATE " + InternalTable.ORDERS.getTableName() + " SET " + OrdersColumn.COLUMN_ORDER.getColumnName() + " = '" + tableTypeHandler.getDefaultColumnOrder(typeDefinition.getName()) + "' WHERE " + OrdersColumn.USER_NAME.getColumnName() + " = '" + orders.get(index)[OrdersColumn.USER_NAME.ordinal()] + "' AND " + OrdersColumn.TABLE_PATH.getColumnName() + " = '" + orders.get(index)[OrdersColumn.TABLE_PATH.ordinal()] + "'; "));
                        }
                    }
                    // Get the table's column metadata
                    ResultSet columnResult = dbCommand.getConnection().getMetaData().getColumns(null, null, tableNameDb, null);
                    // Create storage for flags indicating which columns have been matched
                    boolean[] isFound = new boolean[typeDefinition.getColumnCountDatabase()];
                    // Step through each column in the table
                    while (columnResult.next()) {
                        // Check if the user canceled verification
                        if (canceled) {
                            break;
                        }
                        // Get the column name and data type
                        String columnName = columnResult.getString("COLUMN_NAME");
                        String columnType = columnResult.getString("TYPE_NAME");
                        // Get the index of the column based on its name
                        int columnIndex = typeDefinition.getColumnIndexByDbName(columnName);
                        // Check if the column name is defined in the type definition
                        if (columnIndex != -1) {
                            // the database
                            if (!columnType.startsWith(DefaultColumn.getColumnDbType(columnIndex).substring(0, 3))) {
                                // Column's data type is incorrect
                                issues.add(new TableIssue("Table '" + tableName + "' column '" + columnName + "' data type is invalid (" + columnType + ")", "Modify data type", "ALTER TABLE " + tableNameDb + " ALTER COLUMN " + columnName + " TYPE " + DefaultColumn.getColumnDbType(columnIndex) + "; "));
                            }
                            // Set the flag to indicate the column exists for this table
                            isFound[columnIndex] = true;
                        } else // The column in the database table is not defined in the type
                        // definition
                        {
                            // Column name is unknown
                            issues.add(new TableIssue("Table '" + tableName + "' has an unknown column (" + columnName + ")", "Delete column", "ALTER TABLE " + tableNameDb + " DROP COLUMN " + columnName + "; "));
                        }
                    }
                    columnResult.close();
                    // Step through the column found flags
                    for (int index = 0; index < isFound.length && !canceled; index++) {
                        // Check if the column wasn't located in the table
                        if (!isFound[index]) {
                            // Column is missing
                            issues.add(new TableIssue("Table '" + tableName + "' is missing column '" + typeDefinition.getColumnNamesUser()[index] + "'", "Add missing column", "ALTER TABLE " + tableNameDb + " ADD COLUMN " + typeDefinition.getColumnNamesDatabase()[index] + " " + DefaultColumn.getColumnDbType(index) + " DEFAULT ''; "));
                        }
                    }
                } else // The table type definition is unknown
                {
                    // Table type is unknown
                    issues.add(new TableIssue("Table '" + tableName + "' is an unknown type (" + comment[TableCommentIndex.TYPE.ordinal()] + ")", "Delete table", "DROP TABLE " + tableNameDb + "; "));
                }
            }
            // Update the within-step progress value
            progBar.setValue(startProgress + (numDivisionPerStep * count / total));
        }
        tableResult.close();
    } catch (SQLException se) {
        // Inform the user that obtaining the table metadata failed
        eventLog.logFailEvent(ccddMain.getMainFrame(), "Error obtaining metadata for table '" + tableNameDb + "'; cause '" + se.getMessage() + "'", "<html><b>Error obtaining metadata for table '</b>" + tableNameDb + "<b>'");
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 23 with TypeDefinition

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

the class CcddDbVerificationHandler method checkForDuplicates.

/**
 ********************************************************************************************
 * Check if a column marked as unique for this table type has duplicate values in one or more
 * rows
 *
 * @param tableInfo
 *            reference to the table information
 ********************************************************************************************
 */
private void checkForDuplicates(TableInformation tableInfo) {
    String[] columnValues = new String[tableInfo.getData().length];
    // Get the comment array for this table
    String[] comment = dbTable.getTableComment(tableInfo.getProtoVariableName().toLowerCase(), comments);
    // Get the table's type definition
    TypeDefinition typeDefinition = tableTypeHandler.getTypeDefinition(comment[TableCommentIndex.TYPE.ordinal()]);
    // Step through each column in the table
    for (int column = 0; column < tableInfo.getData()[0].length && !canceled; column++) {
        // Check if the values in this column must be unique
        if (typeDefinition.isRowValueUnique()[column]) {
            // Step through each row in the table
            for (int row = 0; row < tableInfo.getData().length - 1 && !canceled; row++) {
                // Store the column value in the temporary column value array, expanding any
                // macros in the value. The temporary column values are stored so that macro
                // expansion need only be done once per table cell, which speeds the comparison
                // below
                columnValues[row] = !tableInfo.getData()[row][column].isEmpty() ? macroHandler.getMacroExpansion(tableInfo.getData()[row][column]) : "";
            }
            // Step through each row in the table
            for (int row = 0; row < tableInfo.getData().length - 1 && !canceled; row++) {
                // Step through the remaining rows in the table
                for (int otherRow = row + 1; otherRow < tableInfo.getData().length && !canceled; otherRow++) {
                    // values aren't blank
                    if (!columnValues[row].isEmpty() && columnValues[row].equals(columnValues[otherRow])) {
                        // Duplicate item exists in a column designated as having unique values
                        issues.add(new TableIssue("Table '" + tableInfo.getProtoVariableName() + "' column '" + typeDefinition.getColumnNamesUser()[column] + "' rows " + (row + 1) + " and " + (otherRow + 1) + " have duplicate values", "Replace with a blank", otherRow, column, "", tableInfo));
                    }
                }
            }
        }
    }
}
Also used : TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition)

Example 24 with TypeDefinition

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

the class CcddCSVHandler method exportToFile.

/**
 ********************************************************************************************
 * Export the project in CSV format to the specified file
 *
 * @param exportFile
 *            reference to the user-specified output file
 *
 * @param tableNames
 *            array of table names to export
 *
 * @param replaceMacros
 *            true to replace any embedded macros with their corresponding values
 *
 * @param includeReservedMsgIDs
 *            true to include the contents of the reserved message ID table in the export file
 *
 * @param includeVariablePaths
 *            true to include the variable path for each variable in a structure table, both in
 *            application format and using the user-defined separator characters
 *
 * @param variableHandler
 *            variable handler class reference; null if includeVariablePaths is false
 *
 * @param separators
 *            string array containing the variable path separator character(s), show/hide data
 *            types flag ('true' or 'false'), and data type/variable name separator
 *            character(s); null if includeVariablePaths is false
 *
 * @param extraInfo
 *            unused
 *
 * @return true if an error occurred preventing exporting the project to the file
 ********************************************************************************************
 */
@Override
public boolean exportToFile(FileEnvVar exportFile, String[] tableNames, boolean replaceMacros, boolean includeReservedMsgIDs, boolean includeVariablePaths, CcddVariableSizeAndConversionHandler variableHandler, String[] separators, Object... extraInfo) {
    boolean errorFlag = false;
    boolean addLineFeed = false;
    FileWriter fw = null;
    BufferedWriter bw = null;
    PrintWriter pw = null;
    try {
        List<String> referencedTableTypes = new ArrayList<String>();
        List<String> referencedDataTypes = new ArrayList<String>();
        List<String> referencedMacros = new ArrayList<String>();
        List<String[]> variablePaths = new ArrayList<String[]>();
        // Output the table data to the selected file. Multiple writers are needed in case
        // tables are appended to an existing file
        fw = new FileWriter(exportFile, true);
        bw = new BufferedWriter(fw);
        pw = new PrintWriter(bw);
        // Step through each table
        for (String tblName : tableNames) {
            // Get the information from the database for the specified table
            TableInformation tableInfo = ccddMain.getDbTableCommandHandler().loadTableData(tblName, true, false, true, parent);
            // Check if the table's data successfully loaded
            if (!tableInfo.isErrorFlag()) {
                // Output the file creation information (for the first pass only)
                pw.printf((!addLineFeed ? "# Created " + new Date().toString() + " : project = " + dbControl.getDatabaseName() + " : host = " + dbControl.getServer() + " : user = " + dbControl.getUser() + "\n" : "") + "\n");
                // Get the table type definition based on the type name
                TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(tableInfo.getType());
                // Check if this table type is not already output
                if (!referencedTableTypes.contains(tableInfo.getType())) {
                    // Add the table type to the list of those referenced
                    referencedTableTypes.add(tableInfo.getType());
                }
                // Get the visible column names based on the table's type
                String[] columnNames = typeDefn.getColumnNamesVisible();
                // Check if the flag is set that indicates macros should be replaced
                if (replaceMacros) {
                    // Replace all macro names with their corresponding values
                    tableInfo.setData(macroHandler.replaceAllMacros(tableInfo.getData()));
                }
                String systemName = fieldHandler.getFieldValue(tblName, InputDataType.SYSTEM_PATH);
                // Check if the system name exists
                if (systemName != null && !systemName.isEmpty()) {
                    // Store the system name
                    systemName = ",\"" + systemName + "\"";
                }
                // Output the table path (if applicable) and name, table type, and system name
                // (if provided)
                pw.printf(CSVTags.NAME_TYPE.getTag() + "\n%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(tableInfo.getTablePath(), tableInfo.getType(), systemName));
                // Check if the table has a description
                if (!tableInfo.getDescription().isEmpty()) {
                    // Output the table description tag and description
                    pw.printf(CSVTags.DESCRIPTION.getTag() + "\n%s\n", CcddUtilities.addEmbeddedQuotes(tableInfo.getDescription()));
                }
                // Output the column data tag and column names
                pw.printf(CSVTags.COLUMN_NAMES.getTag() + "\n%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(columnNames));
                // Step through each row in the table
                for (int row = 0; row < tableInfo.getData().length; row++) {
                    // Output the table row data, skipping the hidden columns
                    pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(Arrays.copyOfRange(tableInfo.getData()[row], NUM_HIDDEN_COLUMNS, tableInfo.getData()[row].length)));
                    // Step through each column in the row, skipping the hidden columns
                    for (int column = NUM_HIDDEN_COLUMNS; column < columnNames.length; column++) {
                        List<Integer> dataTypeColumns = new ArrayList<Integer>();
                        // Get the column indices for all columns that can contain a primitive
                        // data type
                        dataTypeColumns.addAll(typeDefn.getColumnIndicesByInputType(InputDataType.PRIM_AND_STRUCT));
                        dataTypeColumns.addAll(typeDefn.getColumnIndicesByInputType(InputDataType.PRIMITIVE));
                        // Step through each data type column
                        for (int dataTypeColumn : dataTypeColumns) {
                            // Get the value in the data type column
                            String dataTypeName = tableInfo.getData()[row][dataTypeColumn].toString();
                            // list
                            if (dataTypeHandler.isPrimitive(dataTypeName) && !referencedDataTypes.contains(dataTypeName)) {
                                // Add the data type name to the list of references data types
                                referencedDataTypes.add(dataTypeName);
                            }
                        }
                        // Get the names of the macros referenced in the cell and add them to
                        // the list
                        referencedMacros.addAll(macroHandler.getReferencedMacros(tableInfo.getData()[row][column].toString()));
                        // represents a structure
                        if (includeVariablePaths && typeDefn.isStructure()) {
                            // Get the variable path
                            String variablePath = tableInfo.getTablePath() + "," + tableInfo.getData()[row][typeDefn.getColumnIndexByInputType(InputDataType.PRIM_AND_STRUCT)] + "." + tableInfo.getData()[row][typeDefn.getColumnIndexByInputType(InputDataType.VARIABLE)];
                            // Add the path, in both application and user-defined formats, to
                            // the list to be output
                            variablePaths.add(new String[] { variablePath, variableHandler.getFullVariableName(variablePath, separators[0], Boolean.parseBoolean(separators[1]), separators[2]) });
                        }
                    }
                }
                // Get the table's data field information
                List<FieldInformation> fieldInformation = tableInfo.getFieldHandler().getFieldInformation();
                // Check if the table contains any data fields
                if (!fieldInformation.isEmpty()) {
                    // Output the data field marker
                    pw.printf(CSVTags.DATA_FIELD.getTag() + "\n");
                    // Step through each data field
                    for (FieldInformation fieldInfo : fieldInformation) {
                        // Output the field information
                        pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(fieldInfo.getFieldName(), fieldInfo.getDescription(), Integer.toString(fieldInfo.getSize()), fieldInfo.getInputType().getInputName(), Boolean.toString(fieldInfo.isRequired()), fieldInfo.getApplicabilityType().getApplicabilityName(), fieldInfo.getValue()));
                    }
                }
                addLineFeed = true;
            }
        }
        // Check if any table types are referenced
        if (!referencedTableTypes.isEmpty()) {
            // Step through each referenced table type
            for (String tableType : referencedTableTypes) {
                // Get the table type definition based on the type name
                TypeDefinition tableTypeDefn = tableTypeHandler.getTypeDefinition(tableType);
                // Output the table type tag, and the type name and description
                pw.printf("\n" + CSVTags.TABLE_TYPE.getTag() + "\n%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(tableTypeDefn.getName(), tableTypeDefn.getDescription()));
                // key and row index columns
                for (int column = NUM_HIDDEN_COLUMNS; column < tableTypeDefn.getColumnCountDatabase(); column++) {
                    // Output the column definition
                    pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(tableTypeDefn.getColumnNamesUser()[column], tableTypeDefn.getColumnToolTips()[column], tableTypeDefn.getInputTypes()[column].getInputName(), tableTypeDefn.isRowValueUnique()[column].toString(), tableTypeDefn.isRequired()[column].toString(), tableTypeDefn.isStructureAllowed()[column].toString(), tableTypeDefn.isPointerAllowed()[column].toString()));
                }
                // Build the data field information for this table type
                fieldHandler.buildFieldInformation(CcddFieldHandler.getFieldTypeName(tableType));
                List<FieldInformation> fieldInformation = fieldHandler.getFieldInformation();
                // Check if the table type contains any data fields
                if (!fieldInformation.isEmpty()) {
                    // Output the data field marker
                    pw.printf(CSVTags.TABLE_TYPE_DATA_FIELD.getTag() + "\n");
                    // Step through each data field
                    for (FieldInformation fieldInfo : fieldInformation) {
                        // Output the field information
                        pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(fieldInfo.getFieldName(), fieldInfo.getDescription(), Integer.toString(fieldInfo.getSize()), fieldInfo.getInputType().getInputName(), Boolean.toString(fieldInfo.isRequired()), fieldInfo.getApplicabilityType().getApplicabilityName(), fieldInfo.getValue()));
                    }
                }
            }
        }
        // Check if any primitive data types are referenced
        if (!referencedDataTypes.isEmpty()) {
            // Output the data type marker
            pw.printf("\n" + CSVTags.DATA_TYPE.getTag() + "\n");
            // Step through each data type
            for (String[] dataType : dataTypeHandler.getDataTypeData()) {
                // Check if the data type is referenced in the table
                if (referencedDataTypes.contains(CcddDataTypeHandler.getDataTypeName(dataType))) {
                    // Output the data type definition
                    pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(dataType[DataTypesColumn.USER_NAME.ordinal()], dataType[DataTypesColumn.C_NAME.ordinal()], dataType[DataTypesColumn.SIZE.ordinal()], dataType[DataTypesColumn.BASE_TYPE.ordinal()]));
                }
            }
        }
        // Check if any macros are referenced
        if (!referencedMacros.isEmpty()) {
            // Output the macro marker
            pw.printf("\n" + CSVTags.MACRO.getTag() + "\n");
            // Step through each macro
            for (String[] macro : macroHandler.getMacroData()) {
                // Check if the macro is referenced in the table
                if (referencedMacros.contains(macro[MacrosColumn.MACRO_NAME.ordinal()])) {
                    // Output the macro definition
                    pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(macro[MacrosColumn.MACRO_NAME.ordinal()], macro[MacrosColumn.VALUE.ordinal()]));
                }
            }
        }
        // reserved message IDs defined
        if (includeReservedMsgIDs && !rsvMsgIDHandler.getReservedMsgIDData().isEmpty()) {
            // Output the reserved message ID marker
            pw.printf("\n" + CSVTags.RESERVED_MSG_IDS.getTag() + "\n");
            // Step through each reserved message ID
            for (String[] reservedMsgID : rsvMsgIDHandler.getReservedMsgIDData()) {
                // Output the reserved message ID definition
                pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(reservedMsgID[ReservedMsgIDsColumn.MSG_ID.ordinal()], reservedMsgID[ReservedMsgIDsColumn.DESCRIPTION.ordinal()]));
            }
        }
        // Check if variable paths are to be output and that any exist
        if (includeVariablePaths && !variablePaths.isEmpty()) {
            // Output the variable path marker
            pw.printf("\n" + CSVTags.VARIABLE_PATHS.getTag() + "\n");
            // Step through each variable path
            for (String[] variablePath : variablePaths) {
                // Output the variable path in application and user-defined formats
                pw.printf("%s\n", CcddUtilities.addEmbeddedQuotesAndCommas(variablePath[0], variablePath[1]));
            }
        }
    } catch (IOException ioe) {
        // Inform the user that the data file cannot be written to
        new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot write to export file '</b>" + exportFile.getAbsolutePath() + "<b>'", "File Error", JOptionPane.ERROR_MESSAGE, DialogOption.OK_OPTION);
        errorFlag = true;
    } catch (Exception e) {
        // Display a dialog providing details on the unanticipated error
        CcddUtilities.displayException(e, parent);
        errorFlag = true;
    } finally {
        // Check if the PrintWriter was opened
        if (pw != null) {
            // Close the file
            pw.close();
        }
        try {
            // Check if the BufferedWriter was opened
            if (bw != null) {
                // Close the file
                bw.close();
            }
            // Check if the FileWriter was opened
            if (fw != null) {
                // Close the file
                fw.close();
            }
        } catch (IOException ioe) {
            // Inform the user that the data file cannot be closed
            new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot close export file '</b>" + exportFile.getAbsolutePath() + "<b>'", "File Warning", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
        }
    }
    return errorFlag;
}
Also used : FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Date(java.util.Date) CCDDException(CCDD.CcddClassesDataTable.CCDDException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) TableInformation(CCDD.CcddClassesDataTable.TableInformation) PrintWriter(java.io.PrintWriter) FieldInformation(CCDD.CcddClassesDataTable.FieldInformation)

Example 25 with TypeDefinition

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

the class CcddCSVHandler method importFromFile.

/**
 ********************************************************************************************
 * Build the information from the table definition(s) in the current file
 *
 * @param importFile
 *            import file reference
 *
 * @param importAll
 *            ImportType.IMPORT_ALL to import the table type, data type, and macro definitions,
 *            and the data from all the table definitions; ImportType.FIRST_DATA_ONLY to load
 *            only the data for the first table defined
 *
 * @throws CCDDException
 *             If a data is missing, extraneous, or in error in the import file
 *
 * @throws IOException
 *             If an import file I/O error occurs
 *
 * @throws Exception
 *             For any unanticipated errors
 ********************************************************************************************
 */
@Override
public void importFromFile(FileEnvVar importFile, ImportType importType) throws CCDDException, IOException, Exception {
    BufferedReader br = null;
    try {
        List<TableTypeDefinition> tableTypeDefns = new ArrayList<TableTypeDefinition>();
        List<String[]> dataTypeDefns = new ArrayList<String[]>();
        List<String[]> macroDefns = new ArrayList<String[]>();
        List<String[]> reservedMsgIDDefns = new ArrayList<String[]>();
        tableDefinitions = new ArrayList<TableDefinition>();
        // macros, then a second pass to read the table data and fields
        for (int loop = 1; loop <= 2; loop++) {
            int columnNumber = 0;
            // Create a buffered reader to read the file
            br = new BufferedReader(new FileReader(importFile));
            // Flags indicating if importing should continue after an input error is detected
            boolean continueOnTableTypeError = false;
            boolean continueOnDataTypeError = false;
            boolean continueOnMacroError = false;
            boolean continueOnColumnError = false;
            boolean continueOnDataFieldError = false;
            boolean continueOnReservedMsgIDError = false;
            boolean continueOnTableTypeFieldError = false;
            // Initialize the input tag
            CSVTags importTag = null;
            // Read first line in file
            String line = br.readLine();
            // outer while loop accounts for multiple table definitions within a single file
            while (line != null) {
                TableTypeDefinition tableTypeDefn = null;
                // Initialize the table information
                int numColumns = 0;
                String tablePath = "";
                // Create empty table information and table type definition references
                TypeDefinition typeDefn = null;
                // Storage for column indices
                int[] columnIndex = null;
                // Initialize the number of matching columns and the cell data storage
                String[] columnValues = null;
                // Create a table definition to contain the table's information
                TableDefinition tableDefn = new TableDefinition();
                // Flag that indicates if a table type row is the type name and description or
                // a column definition
                boolean isTypeName = false;
                // inner while loop reads the information for a single table in the file
                while (line != null) {
                    // Remove any trailing commas, empty quotes, and leading/trailing white
                    // space characters from the row. If the CSV file is generated from a
                    // spreadsheet application then extra commas are appended to a row if
                    // needed for the number of columns to be equal with the other rows. These
                    // empty trailing columns are ignored
                    line = line.replaceAll("(?:[,\\s*]|\\\"\\s*\\\")*$", "");
                    // character)
                    if (!line.isEmpty() && !line.startsWith("#")) {
                        // Parse the import data. The values are comma- separated; however,
                        // commas within quotes are ignored - this allows commas to be included
                        // in the data values
                        columnValues = CcddUtilities.splitAndRemoveQuotes(line);
                        // Remove any leading/trailing white space characters from the first
                        // column value
                        String firstColumn = columnValues[0].trim();
                        // Check if this is the table name and table type tag
                        if (firstColumn.equalsIgnoreCase(CSVTags.NAME_TYPE.getTag())) {
                            // Set the input type to look for the table name and table type
                            importTag = CSVTags.NAME_TYPE;
                            // information
                            if (loop == 2 && !tablePath.isEmpty()) {
                                // to beginning another one
                                break;
                            }
                        } else // type are defined
                        if (firstColumn.equalsIgnoreCase(CSVTags.COLUMN_NAMES.getTag()) && !tablePath.isEmpty()) {
                            // Set the input type to look for the table column names
                            importTag = CSVTags.COLUMN_NAMES;
                        } else // type are defined
                        if (firstColumn.equalsIgnoreCase(CSVTags.DESCRIPTION.getTag()) && !tablePath.isEmpty()) {
                            // Set the input type to look for the table description
                            importTag = CSVTags.DESCRIPTION;
                        } else // are defined
                        if (firstColumn.equalsIgnoreCase(CSVTags.DATA_FIELD.getTag()) && !tablePath.isEmpty()) {
                            // Set the input type to look for the data field(s)
                            importTag = CSVTags.DATA_FIELD;
                        } else // Check if this is the table type tag
                        if (firstColumn.equalsIgnoreCase(CSVTags.TABLE_TYPE.getTag())) {
                            // Set the input type to look for the table type definition
                            importTag = CSVTags.TABLE_TYPE;
                            // Set the flag so that the next row is treated as the table type
                            // name and description
                            isTypeName = true;
                        } else // is defined
                        if (firstColumn.equalsIgnoreCase(CSVTags.TABLE_TYPE_DATA_FIELD.getTag()) && tableTypeDefn != null) {
                            // Set the input type to look for the table type data field(s)
                            importTag = CSVTags.TABLE_TYPE_DATA_FIELD;
                        } else // Check if this is the data type tag
                        if (firstColumn.equalsIgnoreCase(CSVTags.DATA_TYPE.getTag())) {
                            // Set the input type to look for the data type(s)
                            importTag = CSVTags.DATA_TYPE;
                        } else // Check if this is the macro tag
                        if (firstColumn.equalsIgnoreCase(CSVTags.MACRO.getTag())) {
                            // Set the input type to look for the macro(s)
                            importTag = CSVTags.MACRO;
                        } else // Check if this is the reserved message IDs tag
                        if (firstColumn.equalsIgnoreCase(CSVTags.RESERVED_MSG_IDS.getTag())) {
                            // Set the input type to look for the reserved IDs
                            importTag = CSVTags.RESERVED_MSG_IDS;
                        } else // Not a tag (or no table name and type are defined); read in the
                        // information based on the last tag read
                        {
                            // Check if this is the first pass
                            if (loop == 1) {
                                switch(importTag) {
                                    case TABLE_TYPE:
                                        // Check if this is the table type name and description
                                        if (isTypeName) {
                                            // Reset the flag so that subsequent rows are
                                            // treated as column definitions
                                            isTypeName = false;
                                            columnNumber = NUM_HIDDEN_COLUMNS;
                                            // present
                                            if (columnValues.length == 2 || columnValues.length == 1) {
                                                // Add the table type definition
                                                tableTypeDefn = new TableTypeDefinition(columnValues[0], (columnValues.length == 2 ? columnValues[1] : ""));
                                                tableTypeDefns.add(tableTypeDefn);
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnTableTypeError = getErrorResponse(continueOnTableTypeError, "<html><b>Missing table type name in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Table Type Error", "Ignore this table type", "Ignore this and any remaining invalid table types", "Stop importing", parent);
                                            }
                                        } else // This is a column definition
                                        {
                                            // present
                                            if (columnValues.length == TableTypeEditorColumnInfo.values().length - 1) {
                                                // Add the table type column definition,
                                                // checking for (and if possible, correcting)
                                                // errors
                                                continueOnTableTypeError = addImportedTableTypeDefinition(continueOnTableTypeError, tableTypeDefn, new String[] { String.valueOf(columnNumber), columnValues[TableTypeEditorColumnInfo.NAME.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.DESCRIPTION.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.INPUT_TYPE.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.UNIQUE.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.REQUIRED.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.STRUCTURE_ALLOWED.ordinal() - 1], columnValues[TableTypeEditorColumnInfo.POINTER_ALLOWED.ordinal() - 1] }, importFile.getAbsolutePath(), parent);
                                                // Update the column index number for the next
                                                // column definition
                                                columnNumber++;
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnTableTypeError = getErrorResponse(continueOnTableTypeError, "<html><b>Table type '" + tableTypeDefn.getTypeName() + "' definition has missing or extra " + "input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Table Type Error", "Ignore this table type", "Ignore this and any remaining invalid table types", "Stop importing", parent);
                                            }
                                        }
                                        break;
                                    case TABLE_TYPE_DATA_FIELD:
                                        // Check if all definitions are to be loaded
                                        if (importType == ImportType.IMPORT_ALL) {
                                            // present
                                            if (columnValues.length == FieldsColumn.values().length - 1 || columnValues.length == FieldsColumn.values().length - 2) {
                                                // Append empty columns as needed to fill out
                                                // the expected number of inputs
                                                columnValues = CcddUtilities.appendArrayColumns(columnValues, FieldsColumn.values().length - 1 - columnValues.length);
                                                // Add the data field definition, checking for
                                                // (and if possible, correcting) errors
                                                continueOnTableTypeFieldError = addImportedDataFieldDefinition(continueOnTableTypeFieldError, tableTypeDefn, new String[] { CcddFieldHandler.getFieldTypeName(tableTypeDefn.getTypeName()), columnValues[FieldsColumn.FIELD_NAME.ordinal() - 1], columnValues[FieldsColumn.FIELD_DESC.ordinal() - 1], columnValues[FieldsColumn.FIELD_SIZE.ordinal() - 1], columnValues[FieldsColumn.FIELD_TYPE.ordinal() - 1], columnValues[FieldsColumn.FIELD_REQUIRED.ordinal() - 1], columnValues[FieldsColumn.FIELD_APPLICABILITY.ordinal() - 1], columnValues[FieldsColumn.FIELD_VALUE.ordinal() - 1] }, importFile.getAbsolutePath(), parent);
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnTableTypeFieldError = getErrorResponse(continueOnTableTypeFieldError, "<html><b>Table type '</b>" + tableTypeDefn.getTypeName() + "<b>' has missing or extra data field " + "input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Data Field Error", "Ignore this invalid data field", "Ignore this and any remaining invalid data fields", "Stop importing", parent);
                                            }
                                        }
                                        break;
                                    case DATA_TYPE:
                                        // Check if all definitions are to be loaded
                                        if (importType == ImportType.IMPORT_ALL) {
                                            // present
                                            if (columnValues.length == 4) {
                                                // Add the data type definition (add a blank to
                                                // represent the OID)
                                                dataTypeDefns.add(new String[] { columnValues[DataTypesColumn.USER_NAME.ordinal()], columnValues[DataTypesColumn.C_NAME.ordinal()], columnValues[DataTypesColumn.SIZE.ordinal()], columnValues[DataTypesColumn.BASE_TYPE.ordinal()], "" });
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnDataTypeError = getErrorResponse(continueOnDataTypeError, "<html><b>Missing or extra data type definition " + "input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Data Type Error", "Ignore this data type", "Ignore this and any remaining invalid data types", "Stop importing", parent);
                                            }
                                        }
                                        break;
                                    case MACRO:
                                        // Check if all definitions are to be loaded
                                        if (importType == ImportType.IMPORT_ALL) {
                                            // present
                                            if (columnValues.length == 2 || columnValues.length == 1) {
                                                // Add the macro definition (add a blank to
                                                // represent the OID)
                                                macroDefns.add(new String[] { columnValues[0], (columnValues.length == 2 ? columnValues[1] : ""), "" });
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnMacroError = getErrorResponse(continueOnMacroError, "<html><b>Missing or extra macro definition " + "input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Macro Error", "Ignore this macro", "Ignore this and any remaining invalid macros", "Stop importing", parent);
                                            }
                                        }
                                        break;
                                    case RESERVED_MSG_IDS:
                                        // Check if all definitions are to be loaded
                                        if (importType == ImportType.IMPORT_ALL) {
                                            // present
                                            if (columnValues.length == 2 || columnValues.length == 1) {
                                                // Append empty columns as needed to fill out
                                                // the expected number of inputs
                                                columnValues = CcddUtilities.appendArrayColumns(columnValues, 2 - columnValues.length);
                                                // Add the reserved message ID definition (add
                                                // a blank to represent the OID)
                                                reservedMsgIDDefns.add(new String[] { columnValues[ReservedMsgIDsColumn.MSG_ID.ordinal()], columnValues[ReservedMsgIDsColumn.DESCRIPTION.ordinal()], "" });
                                            } else // The number of inputs is incorrect
                                            {
                                                // Check if the error should be ignored or the
                                                // import canceled
                                                continueOnReservedMsgIDError = getErrorResponse(continueOnReservedMsgIDError, "<html><b>Missing or extra reserved message ID " + "definition input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Reserved Message ID Error", "Ignore this data type", "Ignore this and any remaining invalid reserved message IDs", "Stop importing", parent);
                                            }
                                        }
                                        break;
                                    case CELL_DATA:
                                    case COLUMN_NAMES:
                                    case DATA_FIELD:
                                    case DESCRIPTION:
                                    case NAME_TYPE:
                                        break;
                                    default:
                                        // before other data
                                        throw new CCDDException("Import file '</b>" + importFile.getAbsolutePath() + "<b>' information missing");
                                }
                            } else // This is the second pass
                            {
                                switch(importTag) {
                                    case NAME_TYPE:
                                        // not used)
                                        if (columnValues.length == 2 || columnValues.length == 3) {
                                            // Use the table name (with path, if applicable)
                                            // and type to build the parent, path, and type for
                                            // the table information class
                                            tablePath = columnValues[0];
                                            tableDefn.setName(tablePath);
                                            tableDefn.setTypeName(columnValues[1]);
                                            // Get the table's type definition
                                            typeDefn = tableTypeHandler.getTypeDefinition(tableDefn.getTypeName());
                                            // Check if the table type doesn't exist
                                            if (typeDefn == null) {
                                                throw new CCDDException("Unknown table type '" + tableDefn.getTypeName() + "'");
                                            }
                                            // Get the number of expected columns (the hidden
                                            // columns, primary key and row index, should not
                                            // be included in the CSV file)
                                            numColumns = typeDefn.getColumnCountVisible();
                                        } else // Incorrect number of inputs
                                        {
                                            throw new CCDDException("Too many/few table name and type inputs");
                                        }
                                        break;
                                    case DESCRIPTION:
                                        // Store the table description
                                        tableDefn.setDescription(columnValues[0]);
                                        break;
                                    case COLUMN_NAMES:
                                        // Check if any column names exist
                                        if (columnValues.length != 0) {
                                            // Number of columns in an import file that match
                                            // the target table
                                            int numValidColumns = 0;
                                            // Create storage for the column indices
                                            columnIndex = new int[columnValues.length];
                                            // Step through each column name
                                            for (int index = 0; index < columnValues.length; index++) {
                                                // Get the index for this column name
                                                columnIndex[index] = typeDefn.getVisibleColumnIndexByUserName(columnValues[index]);
                                                // that of a column in the table
                                                if (columnIndex[index] != -1) {
                                                    // Increment the counter that tracks the
                                                    // number of matched columns
                                                    numValidColumns++;
                                                } else // The number of inputs is incorrect
                                                {
                                                    // Check if the error should be ignored or
                                                    // the import canceled
                                                    continueOnColumnError = getErrorResponse(continueOnColumnError, "<html><b>Table '</b>" + tableDefn.getName() + "<b>' column name '</b>" + columnValues[index] + "<b>' unrecognized in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Column Error", "Ignore this invalid column name", "Ignore this and any remaining invalid column names", "Stop importing", parent);
                                                }
                                            }
                                            // in the table
                                            if (numValidColumns == 0) {
                                                throw new CCDDException("No columns in import file '</b>" + importFile.getAbsolutePath() + "<b>' match those in the target table", JOptionPane.WARNING_MESSAGE);
                                            }
                                        } else // The file contains no column data
                                        {
                                            throw new CCDDException("Format invalid for import file '</b>" + importFile.getAbsolutePath() + "<b>'");
                                        }
                                        // Set the input type to look for cell data
                                        importTag = CSVTags.CELL_DATA;
                                        break;
                                    case CELL_DATA:
                                        // Create storage for the row of cell data and
                                        // initialize the values to nulls (a null indicates
                                        // that the pasted cell value won't overwrite the
                                        // current table value if overwriting; if inserting the
                                        // pasted value is changed to a space)
                                        String[] rowData = new String[numColumns];
                                        Arrays.fill(rowData, null);
                                        // Step through each column in the row
                                        for (int index = 0; index < columnValues.length; index++) {
                                            // Check if the column exists
                                            if (index < columnIndex.length && columnIndex[index] != -1) {
                                                // Store the cell data in the column matching
                                                // the one in the target table
                                                rowData[columnIndex[index]] = columnValues[index];
                                            }
                                        }
                                        // Add the row of data read in from the file to the
                                        // cell data list
                                        tableDefn.addData(rowData);
                                        break;
                                    case DATA_FIELD:
                                        // Check if the expected number of inputs is present
                                        if (columnValues.length == FieldsColumn.values().length - 1 || columnValues.length == FieldsColumn.values().length - 2) {
                                            // Append empty columns as needed to fill out the
                                            // expected number of inputs
                                            columnValues = CcddUtilities.appendArrayColumns(columnValues, FieldsColumn.values().length - 1 - columnValues.length);
                                            // Add the data field definition, checking for (and
                                            // if possible, correcting) errors
                                            continueOnDataFieldError = addImportedDataFieldDefinition(continueOnDataFieldError, tableDefn, new String[] { tableDefn.getName(), columnValues[FieldsColumn.FIELD_NAME.ordinal() - 1], columnValues[FieldsColumn.FIELD_DESC.ordinal() - 1], columnValues[FieldsColumn.FIELD_SIZE.ordinal() - 1], columnValues[FieldsColumn.FIELD_TYPE.ordinal() - 1], columnValues[FieldsColumn.FIELD_REQUIRED.ordinal() - 1], columnValues[FieldsColumn.FIELD_APPLICABILITY.ordinal() - 1], columnValues[FieldsColumn.FIELD_VALUE.ordinal() - 1] }, importFile.getAbsolutePath(), parent);
                                        } else // The number of inputs is incorrect
                                        {
                                            // Check if the error should be ignored or the
                                            // import canceled
                                            continueOnDataFieldError = getErrorResponse(continueOnDataFieldError, "<html><b>Table '</b>" + tableDefn.getName() + "<b>' has missing or extra data field " + "input(s) in import file '</b>" + importFile.getAbsolutePath() + "<b>'; continue?", "Data Field Error", "Ignore this invalid data field", "Ignore this and any remaining invalid data fields", "Stop importing", parent);
                                        }
                                        break;
                                    case DATA_TYPE:
                                    case MACRO:
                                    case TABLE_TYPE:
                                    case TABLE_TYPE_DATA_FIELD:
                                    case RESERVED_MSG_IDS:
                                        break;
                                    default:
                                        // before other data
                                        throw new CCDDException("Import file '</b>" + importFile.getAbsolutePath() + "<b>' information missing");
                                }
                            }
                        }
                    }
                    // Read next line in file
                    line = br.readLine();
                }
                // Check if this is the second pass
                if (loop == 2) {
                    // Add the table's definition to the list
                    tableDefinitions.add(tableDefn);
                    // Check if only the data from the first table is to be read
                    if (importType == ImportType.FIRST_DATA_ONLY) {
                        // Stop reading table definitions
                        break;
                    }
                }
            }
            // Check if this is the first pass
            if (loop == 1) {
                // Add the table type if it's new or match it to an existing one with the same
                // name if the type definitions are the same
                String badDefn = tableTypeHandler.updateTableTypes(tableTypeDefns, fieldHandler);
                // same name
                if (badDefn != null) {
                    throw new CCDDException("Imported table type '" + badDefn + "' doesn't match the existing definition");
                }
                // Check if all definitions are to be loaded
                if (importType == ImportType.IMPORT_ALL) {
                    // Add the data type if it's new or match it to an existing one with the
                    // same name if the type definitions are the same
                    badDefn = dataTypeHandler.updateDataTypes(dataTypeDefns);
                    // the same name
                    if (badDefn != null) {
                        throw new CCDDException("Imported data type '" + badDefn + "' doesn't match the existing definition");
                    }
                    // Add the macro if it's new or match it to an existing one with the same
                    // name if the values are the same
                    badDefn = macroHandler.updateMacros(macroDefns);
                    // same name
                    if (badDefn != null) {
                        throw new CCDDException("Imported macro '" + badDefn + "' doesn't match the existing definition");
                    }
                    // Add the reserved message ID if it's new
                    rsvMsgIDHandler.updateReservedMsgIDs(reservedMsgIDDefns);
                }
            }
        }
    } finally {
        try {
            // Check that the buffered reader exists
            if (br != null) {
                // Close the file
                br.close();
            }
        } catch (IOException ioe) {
            // Inform the user that the file cannot be closed
            new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot close import file '</b>" + importFile.getAbsolutePath() + "<b>'", "File Warning", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
        }
    }
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) ArrayList(java.util.ArrayList) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) IOException(java.io.IOException) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) BufferedReader(java.io.BufferedReader) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) FileReader(java.io.FileReader)

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