Search in sources :

Example 6 with TableDefinition

use of CCDD.CcddClassesDataTable.TableDefinition 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)

Example 7 with TableDefinition

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

the class CcddEDSHandler method importStructureTable.

/**
 ********************************************************************************************
 * Build a structure table from the specified telemetry metadata
 *
 * @param namespace
 *            name space
 *
 * @param parameterSet
 *            reference to the parameter set from which to build the structure table
 *
 * @param table
 *            name table name, including the full system path
 *
 * @param hasCommand
 *            true if the name space also has a command set
 *
 * @throws CCDDException
 *             If an input error is detected
 ********************************************************************************************
 */
private void importStructureTable(NamespaceType namespace, List<InterfaceParameterType> parameterSet, String tableName, boolean hasCommand) throws CCDDException {
    List<DescriptionType> memberList = null;
    // Create a table definition for this structure table. If the name space also includes a
    // command set (which creates a command table) then ensure the two tables have different
    // names
    TableDefinition tableDefn = new TableDefinition(tableName + (hasCommand ? "_tlm" : ""), namespace.getLongDescription());
    // Check if a description exists for this structure table
    if (namespace.getLongDescription() != null && !namespace.getLongDescription().isEmpty()) {
        // Store the table's description
        tableDefn.setDescription(namespace.getLongDescription());
    }
    // Set the new structure table's table type name
    tableDefn.setTypeName(structureTypeDefn.getName());
    // Extract the table's name, minus the path, from the name space name
    String typeName = namespace.getName();
    int index = typeName.lastIndexOf("/");
    if (index != -1) {
        typeName = typeName.substring(index + 1);
    }
    typeName += TYPE;
    // container for this structure
    for (RootDataType parmType : namespace.getDataTypeSet().getArrayDataTypeOrBinaryDataTypeOrBooleanDataType()) {
        // Check if this is the container for the structure's members
        if (parmType instanceof ContainerDataType && parmType.getName().equals(typeName)) {
            // Check if the member list exists
            if (((ContainerDataType) parmType).getEntryList() != null && !((ContainerDataType) parmType).getEntryList().getEntryOrFixedValueEntryOrPaddingEntry().isEmpty()) {
                // Set the reference to the container's member list
                memberList = ((ContainerDataType) parmType).getEntryList().getEntryOrFixedValueEntryOrPaddingEntry();
            }
            // Stop searching since the matching container was found
            break;
        }
    }
    // Step through each telemetry parameter
    for (int parmIndex = 0; parmIndex < parameterSet.size(); parmIndex++) {
        // Get the reference to the parameter in the parameter set
        InterfaceParameterType parm = parameterSet.get(parmIndex);
        // Create a new row of data in the table definition to contain
        // this structure's information. Initialize all columns to
        // blanks except for the variable name
        String[] newRow = new String[structureTypeDefn.getColumnCountVisible()];
        Arrays.fill(newRow, null);
        newRow[variableNameIndex] = parm.getName();
        // definition and that a description exists
        if (descriptionIndex != -1 && parm.getLongDescription() != null) {
            // Store the description for this variable
            newRow[descriptionIndex] = parm.getLongDescription();
        }
        // Add the new row to the table definition
        tableDefn.addData(newRow);
        // name matches the parameter type reference from the parameter set
        for (RootDataType parmType : namespace.getDataTypeSet().getArrayDataTypeOrBinaryDataTypeOrBooleanDataType()) {
            // parameter type set's name
            if (parm.getType().equals(parmType.getName())) {
                String dataType = null;
                String arraySize = null;
                BigInteger bitLength = null;
                long sizeInBytes = 0;
                String enumeration = null;
                String minimum = null;
                String maximum = null;
                Unit units = null;
                // Check if the parameter is an array data type
                if (parmType instanceof ArrayDataType) {
                    arraySize = "";
                    // Store the reference to the array parameter type
                    ArrayDataType arrayType = (ArrayDataType) parmType;
                    // Step through each dimension for the array variable
                    for (DimensionSizeType dim : ((ArrayDataType) parmType).getDimensionList().getDimension()) {
                        // Build the array size string
                        arraySize += String.valueOf(dim.getSize().longValue()) + ",";
                    }
                    arraySize = CcddUtilities.removeTrailer(arraySize, ",");
                    parmType = null;
                    // the parameter type set in order to locate this data type entry
                    for (RootDataType type : namespace.getDataTypeSet().getArrayDataTypeOrBinaryDataTypeOrBooleanDataType()) {
                        // type name
                        if (arrayType.getDataTypeRef().equals(type.getName())) {
                            // Store the reference to the array parameter's data type and stop
                            // searching
                            parmType = type;
                            break;
                        }
                    }
                }
                // locate the data type entry for the individual array members)
                if (parmType != null) {
                    boolean isInteger = false;
                    boolean isUnsigned = false;
                    boolean isFloat = false;
                    boolean isString = false;
                    // Check if the parameter is an integer data type
                    if (parmType instanceof IntegerDataType) {
                        // The 'sizeInBits' references are the integer size for
                        // non-bit-wise parameters, but equal the number of bits
                        // assigned to the parameter for a bit-wise parameter. It
                        // doens't appear that the size of the integer used to contain
                        // the parameter is stored. The assumption is made that the
                        // smallest integer required to store the bits is used.
                        // However, this can alter the originally intended bit-packing
                        // (e.g., a 3-bit and a 9-bit fit within a single 16-bit
                        // integer, but the code below assigns the first to an 8-bit
                        // integer and the second to a 16-bit integer)
                        IntegerDataType itlm = (IntegerDataType) parmType;
                        // Get the number of bits occupied by the parameter
                        bitLength = itlm.getIntegerDataEncoding().getSizeInBits();
                        // Check if units exist for this parameter
                        if (itlm.getSemantics() != null && itlm.getSemantics().getUnit() != null) {
                            // Get the parameter units reference
                            units = itlm.getSemantics().getUnit();
                        }
                        // Check if integer encoding is set to 'unsigned'
                        if (itlm.getIntegerDataEncoding().getEncoding() == IntegerEncodingType.UNSIGNED) {
                            isUnsigned = true;
                        }
                        // Determine the smallest integer size that contains the number
                        // of bits occupied by the parameter
                        sizeInBytes = 8;
                        while (bitLength.longValue() > sizeInBytes) {
                            sizeInBytes *= 2;
                        }
                        sizeInBytes /= 8;
                        // Check if the table's member list exists
                        if (memberList != null) {
                            // Step through each member in the member list
                            for (DescriptionType entry : memberList) {
                                // Check if this is the entry for this parameter
                                if (((EntryType) entry).getName().equals(parm.getName() + TYPE)) {
                                    // Get the minimum and maximum values, if present
                                    DerivedTypeRangeType range = ((EntryType) entry).getValidRange();
                                    // Check if the range information exists
                                    if (range != null) {
                                        // Get the minimum and maximum information
                                        MinMaxRangeType minMaxRange = range.getMinMaxRange();
                                        // Check if the minimum value is specified
                                        if (minMaxRange.getMin() != null) {
                                            // Set the minimum value
                                            minimum = minMaxRange.getMin().toString();
                                        }
                                        // Check if the maximum value is specified
                                        if (minMaxRange.getMax() != null) {
                                            // Set the maximum value
                                            maximum = minMaxRange.getMax().toString();
                                        }
                                    }
                                    // Stop searching since the matching parameter was located
                                    break;
                                }
                            }
                        }
                        isInteger = true;
                    } else // Check if the parameter is a floating point data type
                    if (parmType instanceof FloatDataType) {
                        // Get the float parameter attributes
                        FloatDataType ftlm = (FloatDataType) parmType;
                        switch(ftlm.getFloatDataEncoding().getEncodingAndPrecision()) {
                            case IEEE_754_2008_SINGLE:
                                sizeInBytes = 4;
                                break;
                            case IEEE_754_2008_DOUBLE:
                                sizeInBytes = 8;
                                break;
                            case IEEE_754_2008_QUAD:
                                sizeInBytes = 16;
                                break;
                            default:
                                break;
                        }
                        // Check if units exist for this parameter
                        if (ftlm.getSemantics() != null && ftlm.getSemantics().getUnit() != null) {
                            // Get the parameter units reference
                            units = ftlm.getSemantics().getUnit();
                        }
                        // Check if the table's member list exists
                        if (memberList != null) {
                            // Step through each member in the member list
                            for (DescriptionType entry : memberList) {
                                // Check if this is the entry for this parameter
                                if (((EntryType) entry).getName().equals(parm.getName())) {
                                    // Get the minimum and maximum values, if present
                                    DerivedTypeRangeType range = ((EntryType) entry).getValidRange();
                                    // Check if the range information exists
                                    if (range != null) {
                                        // Get the minimum and maximum information
                                        MinMaxRangeType minMaxRange = range.getMinMaxRange();
                                        // Check if the minimum value is specified
                                        if (minMaxRange.getMin() != null) {
                                            // Set the minimum value
                                            minimum = minMaxRange.getMin().toString();
                                        }
                                        // Check if the maximum value is specified
                                        if (minMaxRange.getMax() != null) {
                                            // Set the maximum value
                                            maximum = minMaxRange.getMax().toString();
                                        }
                                    }
                                    // Stop searching since the matching parameter was located
                                    break;
                                }
                            }
                        }
                        isFloat = true;
                    } else // Check if the parameter is a string data type
                    if (parmType instanceof StringDataType) {
                        // Get the string parameter attributes
                        StringDataType stlm = (StringDataType) parmType;
                        sizeInBytes = stlm.getLength().longValue();
                        // Check if units exist for this parameter
                        if (stlm.getSemantics() != null && stlm.getSemantics().getUnit() != null) {
                            // Get the parameter units reference
                            units = stlm.getSemantics().getUnit();
                        }
                        isString = true;
                    } else // Check if the parameter is an enumerated data type
                    if (parmType instanceof EnumeratedDataType) {
                        // Get the enumeration parameters
                        EnumeratedDataType etlm = (EnumeratedDataType) parmType;
                        EnumerationListType enumList = etlm.getEnumerationList();
                        // Check if any enumeration parameters are defined
                        if (enumList != null) {
                            // Step through each enumeration parameter
                            for (ValueEnumerationType enumType : enumList.getEnumeration()) {
                                // Check if this is the first parameter
                                if (enumeration == null) {
                                    // Initialize the enumeration string
                                    enumeration = "";
                                } else // Not the first parameter
                                {
                                    // Add the separator for the enumerations
                                    enumeration += ",";
                                }
                                // Begin building this enumeration
                                enumeration += enumType.getValue() + " | " + enumType.getLabel();
                            }
                            bitLength = etlm.getIntegerDataEncoding().getSizeInBits();
                            // Check if units exist for this parameter
                            if (etlm.getSemantics() != null && etlm.getSemantics().getUnit() != null) {
                                // Get the parameter units reference
                                units = etlm.getSemantics().getUnit();
                            }
                            // Check if integer encoding is set to 'unsigned'
                            if (etlm.getIntegerDataEncoding().getEncoding() == IntegerEncodingType.UNSIGNED) {
                                isUnsigned = true;
                            }
                            // Determine the smallest integer size that contains the
                            // number of bits occupied by the parameter
                            sizeInBytes = 8;
                            while (bitLength.longValue() > sizeInBytes) {
                                sizeInBytes *= 2;
                            }
                            sizeInBytes /= 8;
                            isInteger = true;
                        }
                    } else // reference
                    if (parmType instanceof ContainerDataType && ((ContainerDataType) parmType).getEntryList() != null) {
                        // Get the reference to the container's base type, which is the name
                        // space path
                        dataType = ((ContainerDataType) parmType).getBaseType();
                        // it begins with a '/'. This beginning '/' is stripped off
                        if (dataType.startsWith("/")) {
                            // Remove the initial '/'
                            dataType = dataType.substring(1);
                        }
                        // The variable name must be stripped from the name space path. Get the
                        // index of the beginning of the variable name
                        int end = dataType.lastIndexOf("/");
                        // Check if the beginning of the variable name was found
                        if (end != -1) {
                            // Strip off the variable name from the path
                            dataType = dataType.substring(0, end);
                        }
                        // Convert the path to a valid structure name, replacing invalid
                        // characters with underscores
                        dataType = convertPathToTableName(dataType);
                    }
                    // Check if the data type isn't a structure reference
                    if (dataType == null) {
                        // Get the name of the data type from the data type table that
                        // matches the base type and size of the parameter
                        dataType = getDataType(dataTypeHandler, sizeInBytes, isInteger, isUnsigned, isFloat, isString);
                    }
                    // Check if a data type exists
                    if (dataType != null) {
                        // Store the data type
                        tableDefn.getData().set(parmIndex * numStructureColumns + dataTypeIndex, dataType);
                    }
                    // Check if a array size exists
                    if (arraySize != null) {
                        // Store the array size
                        tableDefn.getData().set(parmIndex * numStructureColumns + arraySizeIndex, arraySize);
                    }
                    // Check if a bit length exists
                    if (bitLength != null && bitLength.longValue() != sizeInBytes) {
                        // Store the bit length
                        tableDefn.getData().set(parmIndex * numStructureColumns + bitLengthIndex, bitLength.toString());
                    }
                    // Check if a description exists
                    if (parm.getLongDescription() != null) {
                        // Store the description
                        tableDefn.getData().set(parmIndex * numStructureColumns + descriptionIndex, parm.getLongDescription());
                    }
                    // Check if a units exists
                    if (units != null && !units.value().isEmpty()) {
                        // Store the units for this variable
                        tableDefn.getData().set(parmIndex * numStructureColumns + unitsIndex, units.value());
                    }
                }
                // Check if an enumeration exists
                if (enumeration != null) {
                    // Store the enumeration parameters. This accounts only for the
                    // first enumeration for a variable
                    tableDefn.getData().set(parmIndex * numStructureColumns + enumerationIndex, enumeration);
                }
                // Check if a minimum value exists
                if (minimum != null) {
                    // Store the minimum value
                    tableDefn.getData().set(parmIndex * numStructureColumns + minimumIndex, minimum);
                }
                // Check if a maximum value exists
                if (maximum != null) {
                    // Store the maximum value
                    tableDefn.getData().set(parmIndex * numStructureColumns + maximumIndex, maximum);
                }
                break;
            }
        }
    }
    // Add the structure table definition to the list
    tableDefinitions.add(tableDefn);
}
Also used : ValueEnumerationType(org.ccsds.schema.sois.seds.ValueEnumerationType) FloatDataType(org.ccsds.schema.sois.seds.FloatDataType) DescriptionType(org.ccsds.schema.sois.seds.DescriptionType) IntegerDataType(org.ccsds.schema.sois.seds.IntegerDataType) EnumeratedDataType(org.ccsds.schema.sois.seds.EnumeratedDataType) MinMaxRangeType(org.ccsds.schema.sois.seds.MinMaxRangeType) DerivedTypeRangeType(org.ccsds.schema.sois.seds.DerivedTypeRangeType) Unit(org.ccsds.schema.sois.seds.Unit) RootDataType(org.ccsds.schema.sois.seds.RootDataType) EnumerationListType(org.ccsds.schema.sois.seds.EnumerationListType) StringDataType(org.ccsds.schema.sois.seds.StringDataType) EntryType(org.ccsds.schema.sois.seds.EntryType) ContainerDataType(org.ccsds.schema.sois.seds.ContainerDataType) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) BigInteger(java.math.BigInteger) InterfaceParameterType(org.ccsds.schema.sois.seds.InterfaceParameterType) ArrayDataType(org.ccsds.schema.sois.seds.ArrayDataType) DimensionSizeType(org.ccsds.schema.sois.seds.DimensionSizeType)

Example 8 with TableDefinition

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

the class CcddFileIOHandler method importFile.

/**
 ********************************************************************************************
 * Import one or more files, creating new tables and optionally replacing existing ones. The
 * file(s) may contain definitions for more than one table. This method 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 database method completes execution
 *
 * @param dataFile
 *            array of files to import
 *
 * @param backupFirst
 *            true to create a backup of the database before importing tables
 *
 * @param replaceExisting
 *            true to replace a table that already exists in the database
 *
 * @param appendExistingFields
 *            true to append the existing data fields for a table (if any) to the imported ones
 *            (if any). Only valid when replaceExisting is true
 *
 * @param useExistingFields
 *            true to replace an existing data field with the imported ones if the field names
 *            match. Only valid when replaceExisting and appendExistingFields are true
 *
 * @param parent
 *            GUI component calling this method
 ********************************************************************************************
 */
protected void importFile(final FileEnvVar[] dataFile, final boolean backupFirst, final boolean replaceExisting, final boolean appendExistingFields, final boolean useExistingFields, final Component parent) {
    // Create a data field handler
    final CcddFieldHandler fieldHandler = new CcddFieldHandler(ccddMain, null, parent);
    // Store the current table type, data type, macro, reserved message ID, and data field
    // information in case it needs to be restored
    final List<TypeDefinition> originalTableTypes = new ArrayList<TypeDefinition>(tableTypeHandler.getTypeDefinitions());
    final List<String[]> originalDataTypes = new ArrayList<String[]>(dataTypeHandler.getDataTypeData());
    final List<String[]> originalMacros = new ArrayList<String[]>(macroHandler.getMacroData());
    final List<String[]> originalReservedMsgIDs = new ArrayList<String[]>(rsvMsgIDHandler.getReservedMsgIDData());
    final List<String[]> originalDataFields = new ArrayList<String[]>(fieldHandler.getFieldDefinitions());
    // Execute the import operation in the background
    CcddBackgroundCommand.executeInBackground(ccddMain, new BackgroundCommand() {

        List<TableDefinition> allTableDefinitions = new ArrayList<TableDefinition>();

        List<String> duplicateDefinitions = new ArrayList<String>();

        boolean errorFlag = false;

        /**
         ************************************************************************************
         * Import the selected table(s)
         ************************************************************************************
         */
        @Override
        protected void execute() {
            CcddImportExportInterface ioHandler = null;
            // Create a reference to a table editor dialog
            tableEditorDlg = null;
            // Check if the user elected to back up the project before importing tables
            if (backupFirst) {
                // Back up the project database
                backupDatabaseToFile(false);
            }
            // Step through each selected file
            for (FileEnvVar file : dataFile) {
                try {
                    // Check if the file doesn't exist
                    if (!file.exists()) {
                        throw new CCDDException("Cannot locate import file<br>'</b>" + file.getAbsolutePath() + "<b>'");
                    }
                    // Check if the file to import is in CSV format based on the extension
                    if (file.getAbsolutePath().endsWith(FileExtension.CSV.getExtension())) {
                        // Create a CSV handler
                        ioHandler = new CcddCSVHandler(ccddMain, fieldHandler, parent);
                    } else // Check if the file to import is in EDS format based on the extension
                    if (file.getAbsolutePath().endsWith(FileExtension.EDS.getExtension())) {
                        // Create a EDS handler
                        ioHandler = new CcddEDSHandler(ccddMain, fieldHandler, parent);
                    } else // Check if the file to import is in JSON format based on the extension
                    if (file.getAbsolutePath().endsWith(FileExtension.JSON.getExtension())) {
                        // Create a JSON handler
                        ioHandler = new CcddJSONHandler(ccddMain, fieldHandler, parent);
                    } else // Check if the file to import is in XTCE format based on the extension
                    if (file.getAbsolutePath().endsWith(FileExtension.XTCE.getExtension())) {
                        // Create a XTCE handler
                        ioHandler = new CcddXTCEHandler(ccddMain, fieldHandler, parent);
                    } else // The file extension isn't recognized
                    {
                        throw new CCDDException("Cannot import file '" + file.getAbsolutePath() + "'; unrecognized file type");
                    }
                    // Check that no error occurred creating the format conversion handler
                    if (!ioHandler.getErrorStatus()) {
                        // Import the table definition(s) from the file
                        ioHandler.importFromFile(file, ImportType.IMPORT_ALL);
                        // existing ones for a table
                        if (appendExistingFields) {
                            // Step through each table definition
                            for (TableDefinition tableDefn : ioHandler.getTableDefinitions()) {
                                // Build the field information for this table
                                fieldHandler.buildFieldInformation(tableDefn.getName());
                                // Add the imported data field(s) to the table
                                addImportedDataField(fieldHandler, tableDefn, tableDefn.getName(), useExistingFields);
                            }
                        }
                        // Step through each table definition from the import file
                        for (TableDefinition newDefn : ioHandler.getTableDefinitions()) {
                            boolean isFound = false;
                            // Step through each table definition already in the list
                            for (TableDefinition existingDefn : allTableDefinitions) {
                                // Check if the table is already defined in the list
                                if (newDefn.getName().equals(existingDefn.getName())) {
                                    // Add the table name and associated file name to the list
                                    // of duplicates
                                    duplicateDefinitions.add(newDefn.getName() + " (file: " + file.getName() + ")");
                                    // Set the flag indicating the table definition is a
                                    // duplicate and stop searching
                                    isFound = true;
                                    break;
                                }
                            }
                            // Check if the table is not already defined
                            if (!isFound) {
                                // Add the table definition to the list
                                allTableDefinitions.add(newDefn);
                            }
                        }
                    } else // An error occurred creating the format conversion handler
                    {
                        errorFlag = true;
                    }
                } catch (IOException ioe) {
                    // Inform the user that the data file cannot be read
                    new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot read import file<br>'</b>" + file.getAbsolutePath() + "<b>'", "File Error", JOptionPane.ERROR_MESSAGE, DialogOption.OK_OPTION);
                    errorFlag = true;
                } catch (CCDDException ce) {
                    // Check if an error message is provided
                    if (!ce.getMessage().isEmpty()) {
                        // Inform the user that an error occurred reading the import file
                        new CcddDialogHandler().showMessageDialog(parent, "<html><b>" + ce.getMessage(), "Import Error", ce.getMessageType(), DialogOption.OK_OPTION);
                    }
                    errorFlag = true;
                } catch (Exception e) {
                    // Display a dialog providing details on the unanticipated error
                    CcddUtilities.displayException(e, parent);
                    errorFlag = true;
                }
            }
        }

        /**
         ************************************************************************************
         * Import table(s) command complete
         ************************************************************************************
         */
        @Override
        protected void complete() {
            // Check if no errors occurred importing the table(s)
            if (!errorFlag) {
                try {
                    // Enable creation of a save point in case an error occurs while creating
                    // or modifying a table. This prevents committing the changes to the
                    // database until after all database transactions are complete
                    dbCommand.setSavePointEnable(true);
                    // Create the data tables from the imported table definitions from all
                    // files
                    createTablesFromDefinitions(allTableDefinitions, replaceExisting, parent);
                    // Commit the change(s) to the database
                    dbCommand.getConnection().commit();
                } catch (CCDDException | SQLException cse) {
                    errorFlag = true;
                    // message is provided
                    if (cse instanceof CCDDException && !cse.getMessage().isEmpty()) {
                        // Inform the user that an error occurred reading the import file
                        new CcddDialogHandler().showMessageDialog(parent, "<html><b>" + cse.getMessage(), "File Error", ((CCDDException) cse).getMessageType(), DialogOption.OK_OPTION);
                    }
                    try {
                        // Revert the changes to the tables that were successfully updated
                        // prior the current table
                        dbCommand.executeDbCommand("ROLLBACK TO SAVEPOINT " + DB_SAVE_POINT_NAME + ";", parent);
                    } catch (SQLException se) {
                        // Inform the user that the reversion to the save point failed
                        eventLog.logFailEvent(parent, "Cannot revert changes to table(s); cause '" + se.getMessage() + "'", "<html><b>Cannot revert changes to table(s)");
                    }
                } finally {
                    // Reset the flag for creating a save point
                    dbCommand.setSavePointEnable(false);
                }
            }
            // Check if no errors occurred importing and creating the table(s)
            if (!errorFlag) {
                // Store the data file path in the program preferences backing store
                storePath(ccddMain, dataFile[0].getAbsolutePathWithEnvVars(), true, ModifiablePathInfo.TABLE_EXPORT_PATH);
                // Update any open editor's data type columns to include the new table(s), if
                // applicable
                dbTable.updateDataTypeColumns(parent);
                // Update any open editor's message ID names columns to include any new message
                // ID names, if applicable
                dbTable.updateMessageIDNamesColumns(parent);
                eventLog.logEvent(EventLogMessageType.SUCCESS_MSG, "Table import completed successfully");
                // Check if any duplicate table definitions were detected
                if (!duplicateDefinitions.isEmpty()) {
                    // Inform the user that one or more duplicate table definitions were
                    // detected
                    new CcddDialogHandler().showMessageDialog(parent, "<html><b>Ignored the following duplicate table definition(s):</b><br>" + dbTable.getShortenedTableNames(duplicateDefinitions.toArray(new String[0])), "Duplicate Table(s)", JOptionPane.INFORMATION_MESSAGE, DialogOption.OK_OPTION);
                }
            } else // An error occurred while importing the table(s)
            {
                // Restore the table types, data types, macros, reserved message IDs, and data
                // fields to the values prior to the import operation
                tableTypeHandler.setTypeDefinitions(originalTableTypes);
                dataTypeHandler.setDataTypeData(originalDataTypes);
                macroHandler.setMacroData(originalMacros);
                rsvMsgIDHandler.setReservedMsgIDData(originalReservedMsgIDs);
                dbTable.storeInformationTable(InternalTable.FIELDS, originalDataFields, null, parent);
                eventLog.logFailEvent(parent, "Import Error", "Table import completed with errors", "<html><b>Table import completed with errors");
            }
        }
    });
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) FileEnvVar(CCDD.CcddClassesComponent.FileEnvVar) IOException(java.io.IOException) CCDDException(CCDD.CcddClassesDataTable.CCDDException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) BackgroundCommand(CCDD.CcddBackgroundCommand.BackgroundCommand)

Example 9 with TableDefinition

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

the class CcddXTCEHandler method importFromFile.

/**
 ********************************************************************************************
 * Import the the table definitions from an XTCE XML formatted file
 *
 * @param importFile
 *            reference to the user-specified XML input file
 *
 * @param importType
 *            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 {
    try {
        // Import the XML from the specified file
        JAXBElement<?> jaxbElement = (JAXBElement<?>) unmarshaller.unmarshal(importFile);
        // Get the top-level space system
        SpaceSystemType rootSystem = (SpaceSystemType) jaxbElement.getValue();
        tableDefinitions = new ArrayList<TableDefinition>();
        structureTypeDefn = null;
        commandTypeDefn = null;
        // Get the telemetry and command header argument column names for the application ID
        // and the command function code. These are stored as project-level data fields
        ccsdsAppID = fieldHandler.getFieldValue(CcddFieldHandler.getFieldProjectName(), InputDataType.XML_APP_ID);
        ccsdsFuncCode = fieldHandler.getFieldValue(CcddFieldHandler.getFieldProjectName(), InputDataType.XML_FUNC_CODE);
        AncillaryDataSet ancillarySet = rootSystem.getAncillaryDataSet();
        // Check if the root system contains ancillary data
        if (ancillarySet != null) {
            // Step through each ancillary data item
            for (AncillaryData data : ancillarySet.getAncillaryData()) {
                // name indicator
                if (data.getName().equals(ArgumentColumnName.APP_ID.getAncillaryName())) {
                    // Store the item value as the application ID argument column name. Note
                    // that this overrides the value extracted from the project data field
                    ccsdsAppID = data.getValue();
                } else // column name indicator
                if (data.getName().equals(ArgumentColumnName.FUNC_CODE.getAncillaryName())) {
                    // Store the item value as the command function code argument column name.
                    // Note that this overrides the value extracted from the project data field
                    ccsdsFuncCode = data.getValue();
                }
            }
        }
        // import file
        if (ccsdsAppID == null) {
            // Use the default application ID argument column name
            ccsdsAppID = ArgumentColumnName.APP_ID.getDefaultArgColName();
        }
        // the import file
        if (ccsdsFuncCode == null) {
            // Use the default command function code argument column name
            ccsdsFuncCode = ArgumentColumnName.FUNC_CODE.getDefaultArgColName();
        }
        // Create the table type definitions for any new structure and command tables
        createTableTypeDefinitions(rootSystem, importType);
        // Check if at least one structure or command table needs to be built
        if (structureTypeDefn != null || commandTypeDefn != null) {
            // Step through each space system
            for (SpaceSystemType system : rootSystem.getSpaceSystem()) {
                // Recursively step through the XTCE-formatted data and extract the telemetry
                // and command information
                unbuildSpaceSystems(system, rootSystem.getName(), importType);
                // Check if only the data from the first table is to be read
                if (importType == ImportType.FIRST_DATA_ONLY) {
                    // Stop reading table definitions
                    break;
                }
            }
        }
    } catch (JAXBException je) {
        // Inform the user that the database import failed
        new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot import XTCE XML from file<br>'</b>" + importFile.getAbsolutePath() + "<b>'; cause '" + je.getMessage() + "'", "File Error", JOptionPane.ERROR_MESSAGE, DialogOption.OK_OPTION);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) AncillaryDataSet(org.omg.space.xtce.DescriptionType.AncillaryDataSet) AncillaryData(org.omg.space.xtce.DescriptionType.AncillaryDataSet.AncillaryData) JAXBElement(javax.xml.bind.JAXBElement) SpaceSystemType(org.omg.space.xtce.SpaceSystemType)

Example 10 with TableDefinition

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

the class CcddXTCEHandler method importStructureTable.

/**
 ********************************************************************************************
 * Build a structure table from the specified telemetry metadata
 *
 * @param system
 *            space system
 *
 * @param tlmMetaData
 *            reference to the telemetry metadata from which to build the structure table
 *
 * @param table
 *            name table name, including the full system path
 *
 * @throws CCDDException
 *             If an input error is detected
 ********************************************************************************************
 */
private void importStructureTable(SpaceSystemType system, TelemetryMetaDataType tlmMetaData, String tableName) throws CCDDException {
    // Create a table definition for this structure table. If the name space also includes a
    // command metadata (which creates a command table) then ensure the two tables have
    // different names
    TableDefinition tableDefn = new TableDefinition(tableName + (system.getCommandMetaData() == null ? "" : "_tlm"), system.getLongDescription());
    // Check if a description exists for this structure table
    if (system.getLongDescription() != null && !system.getLongDescription().isEmpty()) {
        // Store the table's description
        tableDefn.setDescription(system.getLongDescription());
    }
    // Set the new structure table's table type name
    tableDefn.setTypeName(structureTypeDefn.getName());
    // Get the telemetry information
    ParameterSetType parmSetType = tlmMetaData.getParameterSet();
    ParameterTypeSetType parmTypeSetType = tlmMetaData.getParameterTypeSet();
    // Check if the telemetry information exists
    if (parmSetType != null && parmTypeSetType != null) {
        // Get the references to the parameter set and parameter type set
        List<Object> parmSet = parmSetType.getParameterOrParameterRef();
        List<NameDescriptionType> parmTypeSet = parmTypeSetType.getStringParameterTypeOrEnumeratedParameterTypeOrIntegerParameterType();
        // Step through each telemetry parameter
        for (int parmIndex = 0; parmIndex < parmSet.size(); parmIndex++) {
            // Get the reference to the parameter in the parameter set
            Parameter parm = (Parameter) parmSet.get(parmIndex);
            // Create a new row of data in the table definition to contain this
            // structures's information. Initialize all columns to blanks except for the
            // variable name
            String[] newRow = new String[numStructureColumns];
            Arrays.fill(newRow, null);
            newRow[variableNameIndex] = parm.getName();
            tableDefn.addData(newRow);
            // name matches the parameter type reference from the parameter set
            for (NameDescriptionType parmType : parmTypeSet) {
                // parameter type set's name
                if (parm.getParameterTypeRef().equals(parmType.getName())) {
                    String dataType = null;
                    String arraySize = null;
                    BigInteger bitLength = null;
                    long sizeInBytes = 0;
                    String enumeration = null;
                    String minimum = null;
                    String maximum = null;
                    UnitSet unitSet = null;
                    // Check if the parameter is an array data type
                    if (parmType instanceof ArrayDataTypeType) {
                        // The size of each array dimension is located in a container set.
                        // The array parameter reference containing the dimensions for the
                        // parameter matches the parameter name. Get the container set
                        // reference
                        ContainerSetType containerSet = tlmMetaData.getContainerSet();
                        // Check if the container set exists
                        if (containerSet != null) {
                            // Step through each sequence container in the container set
                            for (SequenceContainerType seqContainer : containerSet.getSequenceContainer()) {
                                // system
                                if (system.getName().equals(seqContainer.getName())) {
                                    // Step through each entry in the sequence
                                    for (SequenceEntryType entry : seqContainer.getEntryList().getParameterRefEntryOrParameterSegmentRefEntryOrContainerRefEntry()) {
                                        // parameter reference matches the target parameter
                                        if (entry instanceof ArrayParameterRefEntryType && parm.getName().equals(((ArrayParameterRefEntryType) entry).getParameterRef())) {
                                            arraySize = "";
                                            // Store the reference to the array parameter
                                            // type
                                            ArrayDataTypeType arrayType = (ArrayDataTypeType) parmType;
                                            parmType = null;
                                            // variable
                                            for (Dimension dim : ((ArrayParameterRefEntryType) entry).getDimensionList().getDimension()) {
                                                // Build the array size string
                                                arraySize += String.valueOf(dim.getEndingIndex().getFixedValue()) + ",";
                                            }
                                            arraySize = CcddUtilities.removeTrailer(arraySize, ",");
                                            // to locate this data type entry
                                            for (NameDescriptionType type : parmTypeSet) {
                                                // name
                                                if (arrayType.getArrayTypeRef().equals(type.getName())) {
                                                    // Store the reference to the array
                                                    // parameter's data type and stop
                                                    // searching
                                                    parmType = type;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    // locate the data type entry for the individual array members)
                    if (parmType != null) {
                        boolean isInteger = false;
                        boolean isUnsigned = false;
                        boolean isFloat = false;
                        boolean isString = false;
                        // Check if the parameter is an integer data type
                        if (parmType instanceof IntegerParameterType) {
                            // The 'sizeInBits' references are the integer size for
                            // non-bit-wise parameters, but equal the number of bits
                            // assigned to the parameter for a bit-wise parameter. It
                            // doens't appear that the size of the integer used to contain
                            // the parameter is stored. The assumption is made that the
                            // smallest integer required to store the bits is used.
                            // However, this can alter the originally intended bit-packing
                            // (e.g., a 3-bit and a 9-bit fit within a single 16-bit
                            // integer, but the code below assigns the first to an 8-bit
                            // integer and the second to a 16-bit integer)
                            IntegerParameterType itlm = (IntegerParameterType) parmType;
                            // Get the number of bits occupied by the parameter
                            bitLength = itlm.getSizeInBits();
                            // Get the parameter units reference
                            unitSet = itlm.getUnitSet();
                            // Check if integer encoding is set to 'unsigned'
                            if (itlm.getIntegerDataEncoding().getEncoding().equalsIgnoreCase("unsigned")) {
                                isUnsigned = true;
                            }
                            // Determine the smallest integer size that contains the number
                            // of bits occupied by the parameter
                            sizeInBytes = 8;
                            while (bitLength.longValue() > sizeInBytes) {
                                sizeInBytes *= 2;
                            }
                            sizeInBytes /= 8;
                            // Get the parameter range
                            IntegerRangeType range = itlm.getValidRange();
                            // Check if the parameter has a range
                            if (range != null) {
                                // Check if the minimum value exists
                                if (range.getMinInclusive() != null) {
                                    // Store the minimum
                                    minimum = range.getMinInclusive();
                                }
                                // Check if the maximum value exists
                                if (range.getMaxInclusive() != null) {
                                    // Store the maximum
                                    maximum = range.getMaxInclusive();
                                }
                            }
                            isInteger = true;
                        } else // Check if the parameter is a floating point data type
                        if (parmType instanceof FloatParameterType) {
                            // Get the float parameter attributes
                            FloatParameterType ftlm = (FloatParameterType) parmType;
                            sizeInBytes = ftlm.getSizeInBits().longValue() / 8;
                            unitSet = ftlm.getUnitSet();
                            // Get the parameter range
                            FloatRangeType range = ftlm.getValidRange();
                            // Check if the parameter has a range
                            if (range != null) {
                                // Check if the minimum value exists
                                if (range.getMinInclusive() != null) {
                                    // Store the minimum
                                    minimum = String.valueOf(range.getMinInclusive());
                                }
                                // Check if the maximum exists
                                if (range.getMaxInclusive() != null) {
                                    // Store the maximum
                                    maximum = String.valueOf(range.getMaxInclusive());
                                }
                            }
                            isFloat = true;
                        } else // Check if the parameter is a string data type
                        if (parmType instanceof StringParameterType) {
                            // Get the string parameter attributes
                            StringParameterType stlm = (StringParameterType) parmType;
                            sizeInBytes = stlm.getCharacterWidth().longValue();
                            unitSet = stlm.getUnitSet();
                            isString = true;
                        } else // Check if the parameter is an enumerated data type
                        if (parmType instanceof EnumeratedParameterType) {
                            // Get the enumeration parameters
                            EnumeratedParameterType etlm = (EnumeratedParameterType) parmType;
                            EnumerationList enumList = etlm.getEnumerationList();
                            // Check if any enumeration parameters are defined
                            if (enumList != null) {
                                // Step through each enumeration parameter
                                for (ValueEnumerationType enumType : enumList.getEnumeration()) {
                                    // Check if this is the first parameter
                                    if (enumeration == null) {
                                        // Initialize the enumeration string
                                        enumeration = "";
                                    } else // Not the first parameter
                                    {
                                        // Add the separator for the enumerations
                                        enumeration += ",";
                                    }
                                    // Begin building this enumeration
                                    enumeration += enumType.getValue() + " | " + enumType.getLabel();
                                }
                                bitLength = etlm.getIntegerDataEncoding().getSizeInBits();
                                unitSet = etlm.getUnitSet();
                                // Check if integer encoding is set to 'unsigned'
                                if (etlm.getIntegerDataEncoding().getEncoding().equalsIgnoreCase("unsigned")) {
                                    isUnsigned = true;
                                }
                                // Determine the smallest integer size that contains the
                                // number of bits occupied by the parameter
                                sizeInBytes = 8;
                                while (bitLength.longValue() > sizeInBytes) {
                                    sizeInBytes *= 2;
                                }
                                sizeInBytes /= 8;
                                isInteger = true;
                            }
                        } else // structure reference
                        if (parmType instanceof AggregateDataType) {
                            // The aggregate type contains a member list of the parameters
                            // belonging to the referenced structure. Each list parameter
                            // has the path to the space system defining the structure;
                            // this path is used to get the structure data type
                            // Get the reference to the aggregate's member list
                            List<Member> memberList = ((AggregateDataType) parmType).getMemberList().getMember();
                            // Check if the member list exists
                            if (!memberList.isEmpty()) {
                                // Get the type reference of the structure's first
                                // parameter which is the path to its space system
                                dataType = memberList.get(0).getTypeRef();
                                // beginning '/' is stripped off
                                if (dataType.startsWith("/")) {
                                    // Remove the initial '/'
                                    dataType = dataType.substring(1);
                                }
                                // The variable name must be stripped from the space system
                                // path. Get the index of the beginning of the variable
                                // name
                                int end = dataType.lastIndexOf("/");
                                // Check if the beginning of the variable name was found
                                if (end != -1) {
                                    // Strip off the variable name from the path
                                    dataType = dataType.substring(0, end);
                                }
                                // Convert the path to a valid structure name, replacing
                                // invalid characters with underscores
                                dataType = convertPathToTableName(dataType);
                            }
                        }
                        // Check if the data type isn't a structure reference
                        if (dataType == null) {
                            // Get the name of the data type from the data type table that
                            // matches the base type and size of the parameter
                            dataType = getDataType(dataTypeHandler, sizeInBytes, isInteger, isUnsigned, isFloat, isString);
                        }
                        // Check if a data type exists
                        if (dataType != null) {
                            // Store the data type
                            tableDefn.getData().set(parmIndex * numStructureColumns + dataTypeIndex, dataType);
                        }
                        // Check if a array size exists
                        if (arraySize != null) {
                            // Store the array size
                            tableDefn.getData().set(parmIndex * numStructureColumns + arraySizeIndex, arraySize);
                        }
                        // Check if a bit length exists
                        if (bitLength != null && bitLength.longValue() != sizeInBytes) {
                            // Store the bit length
                            tableDefn.getData().set(parmIndex * numStructureColumns + bitLengthIndex, bitLength.toString());
                        }
                        // Check if a description exists
                        if (parmType.getLongDescription() != null) {
                            // Store the description
                            tableDefn.getData().set(parmIndex * numStructureColumns + descriptionIndex, parmType.getLongDescription());
                        }
                        // Check if a units exists and
                        if (unitSet != null) {
                            List<UnitType> unitType = unitSet.getUnit();
                            // Check if the units exist
                            if (!unitType.isEmpty()) {
                                // Store the units for this variable
                                tableDefn.getData().set(parmIndex * numStructureColumns + unitsIndex, unitType.get(0).getContent());
                            }
                        }
                        // Check if an enumeration exists
                        if (enumeration != null) {
                            // Store the enumeration parameters. This accounts only for the
                            // first enumeration for a variable
                            tableDefn.getData().set(parmIndex * numStructureColumns + enumerationIndex, enumeration);
                        }
                        // Check if a minimum value exists
                        if (minimum != null) {
                            // Store the minimum value
                            tableDefn.getData().set(parmIndex * numStructureColumns + minimumIndex, minimum);
                        }
                        // Check if a maximum value exists
                        if (maximum != null) {
                            // Store the maximum value
                            tableDefn.getData().set(parmIndex * numStructureColumns + maximumIndex, maximum);
                        }
                    }
                    break;
                }
            }
        }
        ContainerSetType containerSet;
        // Check if the application ID data field name and the container set exist
        if ((containerSet = tlmMetaData.getContainerSet()) != null) {
            // Step through each sequence container in the container set
            for (SequenceContainerType seqContainer : containerSet.getSequenceContainer()) {
                // Check if this is the sequence container for the target system
                if (system.getName().equals(seqContainer.getName())) {
                    BaseContainer baseContainer;
                    RestrictionCriteria restrictionCriteria;
                    ComparisonList comparisonList;
                    // Check if the comparison list exists
                    if (((baseContainer = seqContainer.getBaseContainer()) != null) && ((restrictionCriteria = baseContainer.getRestrictionCriteria()) != null) && ((comparisonList = restrictionCriteria.getComparisonList()) != null)) {
                        // Step through each item in the comparison list
                        for (ComparisonType comparison : comparisonList.getComparison()) {
                            // application ID name
                            if (comparison.getParameterRef().equals(ccsdsAppID)) {
                                // Create a data field for the table containing the application
                                // ID. Once a match is found the search is discontinued
                                tableDefn.addDataField(new String[] { tableName, comparison.getParameterRef(), "Message ID", String.valueOf(comparison.getValue().length()), InputDataType.MESSAGE_ID.getInputName(), String.valueOf(false), ApplicabilityType.ROOT_ONLY.getApplicabilityName(), comparison.getValue() });
                                break;
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    // Add the structure table definition to the list
    tableDefinitions.add(tableDefn);
}
Also used : EnumerationList(org.omg.space.xtce.EnumeratedDataType.EnumerationList) ValueEnumerationType(org.omg.space.xtce.ValueEnumerationType) SequenceContainerType(org.omg.space.xtce.SequenceContainerType) ComparisonType(org.omg.space.xtce.ComparisonType) StringParameterType(org.omg.space.xtce.ParameterTypeSetType.StringParameterType) IntegerParameterType(org.omg.space.xtce.ParameterTypeSetType.IntegerParameterType) IntegerRangeType(org.omg.space.xtce.IntegerRangeType) RestrictionCriteria(org.omg.space.xtce.SequenceContainerType.BaseContainer.RestrictionCriteria) SequenceEntryType(org.omg.space.xtce.SequenceEntryType) NameDescriptionType(org.omg.space.xtce.NameDescriptionType) BaseContainer(org.omg.space.xtce.SequenceContainerType.BaseContainer) FloatParameterType(org.omg.space.xtce.ParameterTypeSetType.FloatParameterType) UnitType(org.omg.space.xtce.UnitType) ArrayParameterRefEntryType(org.omg.space.xtce.ArrayParameterRefEntryType) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) ParameterTypeSetType(org.omg.space.xtce.ParameterTypeSetType) ComparisonList(org.omg.space.xtce.MatchCriteriaType.ComparisonList) Member(org.omg.space.xtce.AggregateDataType.MemberList.Member) ParameterSetType(org.omg.space.xtce.ParameterSetType) Dimension(org.omg.space.xtce.ArrayParameterRefEntryType.DimensionList.Dimension) ArrayDataTypeType(org.omg.space.xtce.ArrayDataTypeType) ContainerSetType(org.omg.space.xtce.ContainerSetType) AggregateDataType(org.omg.space.xtce.AggregateDataType) Parameter(org.omg.space.xtce.ParameterSetType.Parameter) BigInteger(java.math.BigInteger) UnitSet(org.omg.space.xtce.BaseDataType.UnitSet) FloatRangeType(org.omg.space.xtce.FloatRangeType) EnumeratedParameterType(org.omg.space.xtce.ParameterTypeSetType.EnumeratedParameterType)

Aggregations

TableDefinition (CCDD.CcddClassesDataTable.TableDefinition)11 CCDDException (CCDD.CcddClassesDataTable.CCDDException)5 TypeDefinition (CCDD.CcddTableTypeHandler.TypeDefinition)5 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 FileEnvVar (CCDD.CcddClassesComponent.FileEnvVar)2 AssociatedColumns (CCDD.CcddClassesDataTable.AssociatedColumns)2 TableTypeDefinition (CCDD.CcddClassesDataTable.TableTypeDefinition)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 SQLException (java.sql.SQLException)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2 ArrayDataType (org.ccsds.schema.sois.seds.ArrayDataType)2 DimensionSizeType (org.ccsds.schema.sois.seds.DimensionSizeType)2 EnumeratedDataType (org.ccsds.schema.sois.seds.EnumeratedDataType)2 EnumerationListType (org.ccsds.schema.sois.seds.EnumerationListType)2 FloatDataType (org.ccsds.schema.sois.seds.FloatDataType)2 IntegerDataType (org.ccsds.schema.sois.seds.IntegerDataType)2