Search in sources :

Example 1 with TableTypeDefinition

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

the class CcddJSONHandler 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> tableTypeDefinitions = new ArrayList<TableTypeDefinition>();
        tableDefinitions = new ArrayList<TableDefinition>();
        // Flags indicating if importing should continue after an input error is detected
        boolean continueOnTableTypeError = false;
        boolean continueOnDataTypeError = false;
        boolean continueOnMacroError = false;
        boolean continueOnReservedMsgIDError = false;
        boolean continueOnColumnError = false;
        boolean continueOnDataFieldError = false;
        boolean continueOnTableTypeFieldError = false;
        // Create a JSON parser and use it to parse the import file contents
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader(importFile));
        // Get the table type definitions JSON object
        Object defn = jsonObject.get(JSONTags.TABLE_TYPE_DEFN.getTag());
        // Check if the table type definitions exist
        if (defn != null && defn instanceof JSONArray) {
            // Step through each table type definition
            for (JSONObject tableTypeJO : parseJSONArray(defn)) {
                // Get the table type definition components
                String typeName = getString(tableTypeJO, JSONTags.TABLE_TYPE_NAME.getTag());
                String typeDesc = getString(tableTypeJO, JSONTags.TABLE_TYPE_DESCRIPTION.getTag());
                Object typeColumn = getObject(tableTypeJO, JSONTags.TABLE_TYPE_COLUMN.getTag());
                // Check if the expected inputs are present
                if (!typeName.isEmpty() && typeColumn != null && typeColumn instanceof JSONArray) {
                    // Create a new table type definition
                    TableTypeDefinition tableTypeDefn = new TableTypeDefinition(typeName, typeDesc);
                    int columnNumber = 0;
                    // Step through each table type column definition
                    for (JSONObject typeJO : parseJSONArray(typeColumn)) {
                        // Check if the expected input is present
                        if (typeJO.keySet().size() == 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), getString(typeJO, TableTypeEditorColumnInfo.NAME.getColumnName()), getString(typeJO, TableTypeEditorColumnInfo.DESCRIPTION.getColumnName()), getString(typeJO, TableTypeEditorColumnInfo.INPUT_TYPE.getColumnName()), getString(typeJO, TableTypeEditorColumnInfo.UNIQUE.getColumnName()), getString(typeJO, TableTypeEditorColumnInfo.REQUIRED.getColumnName()), getString(typeJO, CcddUtilities.removeHTMLTags(TableTypeEditorColumnInfo.STRUCTURE_ALLOWED.getColumnName())), getString(typeJO, CcddUtilities.removeHTMLTags(TableTypeEditorColumnInfo.POINTER_ALLOWED.getColumnName())) }, 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 '" + typeName + "' 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);
                        }
                    }
                    // Get the data fields for this table type
                    Object typeField = getObject(tableTypeJO, JSONTags.TABLE_TYPE_FIELD.getTag());
                    // Check if any data fields exists for this table type
                    if (typeField != null) {
                        // Step through each table type data field definition
                        for (JSONObject typeJO : parseJSONArray(typeField)) {
                            // Add the data field definition, checking for (and if possible,
                            // correcting) errors
                            continueOnTableTypeFieldError = addImportedDataFieldDefinition(continueOnTableTypeFieldError, tableTypeDefn, new String[] { CcddFieldHandler.getFieldTypeName(tableTypeDefn.getTypeName()), getString(typeJO, FieldEditorColumnInfo.NAME.getColumnName()), getString(typeJO, FieldEditorColumnInfo.DESCRIPTION.getColumnName()), getString(typeJO, FieldEditorColumnInfo.SIZE.getColumnName()), getString(typeJO, FieldEditorColumnInfo.INPUT_TYPE.getColumnName()), getString(typeJO, FieldEditorColumnInfo.REQUIRED.getColumnName()), getString(typeJO, FieldEditorColumnInfo.APPLICABILITY.getColumnName()), getString(typeJO, FieldEditorColumnInfo.VALUE.getColumnName()) }, importFile.getAbsolutePath(), parent);
                        }
                    }
                    // Add the table type definition to the list
                    tableTypeDefinitions.add(tableTypeDefn);
                }
            }
        }
        // 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(tableTypeDefinitions, fieldHandler);
        // Check if a table type isn't new and doesn't match an existing one with the 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) {
            List<String[]> dataTypeDefns = new ArrayList<String[]>();
            List<String[]> macroDefns = new ArrayList<String[]>();
            List<String[]> reservedMsgIDDefns = new ArrayList<String[]>();
            // Get the data type definitions JSON object
            defn = jsonObject.get(JSONTags.DATA_TYPE_DEFN.getTag());
            // Check if the data type definitions exist
            if (defn != null && defn instanceof JSONArray) {
                // Step through each data type definition
                for (JSONObject typeJO : parseJSONArray(defn)) {
                    // Get the data type definition components
                    String userName = getString(typeJO, DataTypeEditorColumnInfo.USER_NAME.getColumnName());
                    String cName = getString(typeJO, DataTypeEditorColumnInfo.C_NAME.getColumnName());
                    String size = getString(typeJO, DataTypeEditorColumnInfo.SIZE.getColumnName());
                    String baseType = getString(typeJO, DataTypeEditorColumnInfo.BASE_TYPE.getColumnName());
                    // Check if the expected inputs are present
                    if ((!userName.isEmpty() || !cName.isEmpty()) && !size.isEmpty() && !baseType.isEmpty() && typeJO.keySet().size() < DataTypeEditorColumnInfo.values().length) {
                        // Add the data type definition (add a blank to represent the OID)
                        dataTypeDefns.add(new String[] { userName, cName, size, baseType, "" });
                    } 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);
                    }
                }
            }
            // Get the macro definitions JSON object
            defn = jsonObject.get(JSONTags.MACRO_DEFN.getTag());
            // Check if the macro definitions exist
            if (defn != null && defn instanceof JSONArray) {
                // Step through each macro definition
                for (JSONObject macroJO : parseJSONArray(defn)) {
                    // Get the macro definition components
                    String name = getString(macroJO, MacroEditorColumnInfo.NAME.getColumnName());
                    String value = getString(macroJO, MacroEditorColumnInfo.VALUE.getColumnName());
                    // Check if the expected inputs are present
                    if (!name.isEmpty() && macroJO.keySet().size() < MacroEditorColumnInfo.values().length) {
                        // Add the macro definition (add a blank to represent the OID)
                        macroDefns.add(new String[] { name, value, "" });
                    } 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);
                    }
                }
            }
            // Get the reserved message ID definitions JSON object
            defn = jsonObject.get(JSONTags.RESERVED_MSG_ID_DEFN.getTag());
            // Check if the reserved message ID definitions exist
            if (defn != null && defn instanceof JSONArray) {
                // Step through each reserved message ID definition
                for (JSONObject reservedMsgIDJO : parseJSONArray(defn)) {
                    // Get the reserved message ID definition components
                    String name = getString(reservedMsgIDJO, ReservedMsgIDEditorColumnInfo.MSG_ID.getColumnName());
                    String value = getString(reservedMsgIDJO, ReservedMsgIDEditorColumnInfo.DESCRIPTION.getColumnName());
                    // Check if the expected inputs are present
                    if (!name.isEmpty() && reservedMsgIDJO.keySet().size() < ReservedMsgIDEditorColumnInfo.values().length) {
                        // Add the reserved message ID definition (add a blank to represent the
                        // OID)
                        reservedMsgIDDefns.add(new String[] { name, value, "" });
                    } 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 reserved message ID", "Ignore this and any remaining invalid reserved message IDs", "Stop importing", parent);
                    }
                }
            }
            // 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);
            // 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);
            // Add the reserved message ID definition if it's new
            rsvMsgIDHandler.updateReservedMsgIDs(reservedMsgIDDefns);
            // Check if a macro isn't new and doesn't match an existing one with the same name
            if (badDefn != null) {
                throw new CCDDException("Imported macro '" + badDefn + "' doesn't match the existing definition");
            }
        }
        // Get the table definitions JSON object
        defn = jsonObject.get(JSONTags.TABLE_DEFN.getTag());
        // Check if the table definitions exist
        if (defn != null && defn instanceof JSONArray) {
            // Step through each table definition
            for (JSONObject tableJO : parseJSONArray(defn)) {
                // Get the table definition components
                String tableName = getString(tableJO, JSONTags.TABLE_NAME.getTag());
                String tableType = getString(tableJO, JSONTags.TABLE_TYPE.getTag());
                String tableDesc = getString(tableJO, JSONTags.TABLE_DESCRIPTION.getTag());
                Object tableDataJA = getObject(tableJO, JSONTags.TABLE_DATA.getTag());
                Object dataFieldsJA = getObject(tableJO, JSONTags.TABLE_FIELD.getTag());
                // Check if the expected inputs are present
                if (!tableName.isEmpty() && tableDataJA != null && tableDataJA instanceof JSONArray && (dataFieldsJA == null || dataFieldsJA instanceof JSONArray)) {
                    // Create a new table type definition
                    TableDefinition tableDefn = new TableDefinition(tableName, tableDesc);
                    // Get the table's type definition
                    TypeDefinition typeDefn = tableTypeHandler.getTypeDefinition(tableType);
                    // Check if the table type doesn't exist
                    if (typeDefn == null) {
                        throw new CCDDException("Unknown table type '" + tableType + "'");
                    }
                    // Store the table's type name
                    tableDefn.setTypeName(tableType);
                    // Get the number of expected columns (the hidden columns, primary key and
                    // row index, should not be included in the JSON file)
                    int numColumns = typeDefn.getColumnCountVisible();
                    // Create storage for the row of cell data
                    String[] rowData = new String[numColumns];
                    // Step through each row of data
                    for (JSONObject rowDataJO : parseJSONArray(tableDataJA)) {
                        // Initialize the column values to blanks
                        Arrays.fill(rowData, null);
                        // Step through each key (column name)
                        for (Object columnName : rowDataJO.keySet()) {
                            // Get the column index based on the column name
                            int column = typeDefn.getVisibleColumnIndexByUserName(columnName.toString());
                            // Check if a column by this name exists
                            if (column != -1) {
                                // Get the value from the JSON input, if present; use a blank
                                // if a value for this column doesn't exist
                                rowData[column] = getString(rowDataJO, typeDefn.getColumnNamesVisible()[column]);
                            } 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>" + tableName + "<b>' column name '</b>" + columnName + "<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);
                            }
                        }
                        // Add the row of data read in from the file to the cell data list
                        tableDefn.addData(rowData);
                    }
                    // defined
                    if (dataFieldsJA != null) {
                        // Step through each data field definition
                        for (JSONObject dataFieldJO : parseJSONArray(dataFieldsJA)) {
                            // Add the data field definition, checking for (and if possible,
                            // correcting) errors
                            continueOnDataFieldError = addImportedDataFieldDefinition(continueOnDataFieldError, tableDefn, new String[] { tableName, getString(dataFieldJO, FieldEditorColumnInfo.NAME.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.DESCRIPTION.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.SIZE.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.INPUT_TYPE.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.REQUIRED.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.APPLICABILITY.getColumnName()), getString(dataFieldJO, FieldEditorColumnInfo.VALUE.getColumnName()) }, importFile.getAbsolutePath(), parent);
                        }
                    }
                    // 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;
                }
            }
        }
    } catch (ParseException pe) {
        // Inform the user that the file cannot be closed
        new CcddDialogHandler().showMessageDialog(parent, "<html><b>Cannot parse import file<br>'</b>" + importFile.getAbsolutePath() + "<b>'; cause '" + pe.getMessage() + "'", "File Warning", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
    } 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<br>'</b>" + importFile.getAbsolutePath() + "<b>'", "File Warning", JOptionPane.WARNING_MESSAGE, DialogOption.OK_OPTION);
        }
    }
}
Also used : CCDDException(CCDD.CcddClassesDataTable.CCDDException) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) IOException(java.io.IOException) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) TypeDefinition(CCDD.CcddTableTypeHandler.TypeDefinition) JSONObject(org.json.simple.JSONObject) BufferedReader(java.io.BufferedReader) TableDefinition(CCDD.CcddClassesDataTable.TableDefinition) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException)

Example 2 with TableTypeDefinition

use of CCDD.CcddClassesDataTable.TableTypeDefinition 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 3 with TableTypeDefinition

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

the class CcddTableTypeHandler method updateTableTypes.

/**
 ********************************************************************************************
 * Check if the specified table types are new or match an existing one. If new then add the
 * table type. If the table type name matches then compare the type definitions to ensure the
 * two are the same (ignoring the column descriptions)
 *
 * @param tableTypeDefinitions
 *            list of table type definitions
 *
 * @param fieldHandler
 *            reference to a data field handler
 *
 * @return null if all of the table types are created or match existing ones; the name of the
 *         table type that matches an existing one but the type definitions differ
 ********************************************************************************************
 */
protected String updateTableTypes(List<TableTypeDefinition> tableTypeDefinitions, CcddFieldHandler fieldHandler) {
    boolean isNewStruct = false;
    String badType = null;
    isNewField = false;
    // Step through each table type definition
    for (TableTypeDefinition tableTypeDefn : tableTypeDefinitions) {
        // Determine if the table type is new or matches an existing one with the same name
        TableTypeUpdate typeUpdate = updateTableTypes(tableTypeDefn, fieldHandler);
        // Check if the type name matches an existing one but the type definition differs
        if (typeUpdate == TableTypeUpdate.MISMATCH) {
            // Store the type name that mismatched and stop processing the table type
            // definitions
            badType = tableTypeDefn.getTypeName();
            break;
        }
        // Check if the table type is new and represents a structure
        if (typeUpdate == TableTypeUpdate.NEW && getTypeDefinition(tableTypeDefn.getTypeName()).isStructure()) {
            // Set the flag to indicate a structure table type was added
            isNewStruct = true;
        }
    }
    // Clear the table type definitions since they have been incorporated
    tableTypeDefinitions.clear();
    // Check if no mismatches occurred
    if (badType == null) {
        // Check if the deleted type represents a structure
        if (isNewStruct) {
            // Update the database functions that collect structure table members and
            // structure-defining column data
            dbControl.createStructureColumnFunctions();
        }
        // Check if the number of rate columns changed due to the type update
        if (ccddMain.getRateParameterHandler().setRateInformation()) {
            // Store the rate parameters in the project database
            dbTable.storeRateParameters(ccddMain.getMainFrame());
        }
        // Check if a data field was created for a table type
        if (isNewField) {
            // Store the data field table with the additional fields
            dbTable.storeInformationTable(InternalTable.FIELDS, fieldHandler.getFieldDefinitions(), null, ccddMain.getMainFrame());
        }
    }
    return badType;
}
Also used : TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) TableTypeUpdate(CCDD.CcddConstants.TableTypeUpdate)

Example 4 with TableTypeDefinition

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

the class CcddTableTypeHandler method updateTableTypes.

/**
 ********************************************************************************************
 * Check if specified table type is new or matches an existing one. If new then add the table
 * type. If the table type name matches then compare the type definitions to ensure the two are
 * the same (ignoring the column descriptions)
 *
 * @param tableTypeDefn
 *            table type definition
 *
 * @param fieldHandler
 *            reference to a data field handler
 *
 * @return TableTypeUpdate.NEW if the table type is new, TableTypeUpdate.MATCH if the table
 *         type matches an existing one, or TableTypeUpdate.MISMATCH if the table type name
 *         matches an existing one but the type definition differs
 ********************************************************************************************
 */
private TableTypeUpdate updateTableTypes(TableTypeDefinition tableTypeDefn, CcddFieldHandler fieldHandler) {
    TableTypeUpdate typeUpdate = TableTypeUpdate.MATCH;
    // Get the type definition based on the type name
    TypeDefinition typeDefn = getTypeDefinition(tableTypeDefn.getTypeName());
    // Check if the table type doesn't already exist
    if (typeDefn == null) {
        // Set the flag indicating the table type is new
        typeUpdate = TableTypeUpdate.NEW;
        // Add the new table type
        createTypeDefinition(tableTypeDefn.getTypeName(), tableTypeDefn.getColumns().toArray(new Object[0][0]), tableTypeDefn.getDescription());
        // Check if a data field is associated with the new table type
        if (tableTypeDefn.getDataFields().size() != 0) {
            // Add the table type's data field definitions, if any, to the existing field
            // definitions
            fieldHandler.getFieldDefinitions().addAll(tableTypeDefn.getDataFields());
            fieldHandler.buildFieldInformation(null);
            isNewField = true;
        }
        // Check if the table type editor is open
        if (ccddMain.getTableTypeEditor() != null && ccddMain.getTableTypeEditor().isShowing()) {
            // Add the new table type tab to the editor
            ccddMain.getTableTypeEditor().addTypePanes(new String[] { tableTypeDefn.getTypeName() }, tableTypeDefn.getDataFields());
        }
    } else // A table type with this name already exists
    {
        // Add the table type with a different name and get a reference to it
        TypeDefinition altTypeDefn = createTypeDefinition(tableTypeDefn.getTypeName() + "_TEMP", tableTypeDefn.getColumns().toArray(new Object[0][0]), tableTypeDefn.getDescription());
        // Ignore the column description (tool tip text) when comparing
        if (!(CcddUtilities.isArraySetsEqual(typeDefn.getColumnNamesUser(), altTypeDefn.getColumnNamesUser()) && CcddUtilities.isArraySetsEqual(typeDefn.getInputTypes(), altTypeDefn.getInputTypes()) && CcddUtilities.isArraySetsEqual(typeDefn.isRowValueUnique(), altTypeDefn.isRowValueUnique()) && CcddUtilities.isArraySetsEqual(typeDefn.isRequired(), altTypeDefn.isRequired()) && CcddUtilities.isArraySetsEqual(typeDefn.isStructureAllowed(), altTypeDefn.isStructureAllowed()) && CcddUtilities.isArraySetsEqual(typeDefn.isPointerAllowed(), altTypeDefn.isPointerAllowed()))) {
            // Set the flag indicating a mismatch exists
            typeUpdate = TableTypeUpdate.MISMATCH;
        }
        // Step through each table type data field
        for (String[] dataField : tableTypeDefn.getDataFields()) {
            // Get the reference to the data field from the existing field information
            FieldInformation fieldInfo = fieldHandler.getFieldInformationByName(dataField[FieldsColumn.OWNER_NAME.ordinal()], dataField[FieldsColumn.FIELD_NAME.ordinal()]);
            // Check if this is a new field
            if (fieldInfo == null) {
                // Add the field
                fieldHandler.getFieldDefinitions().add(dataField);
                isNewField = true;
            } else // value don't match (the description and size are allowed to differ)
            if (!dataField[FieldsColumn.FIELD_TYPE.ordinal()].equals(fieldInfo.getInputType().getInputName()) || !dataField[FieldsColumn.FIELD_REQUIRED.ordinal()].equalsIgnoreCase(Boolean.toString(fieldInfo.isRequired())) || !dataField[FieldsColumn.FIELD_APPLICABILITY.ordinal()].equals(fieldInfo.getApplicabilityType().getApplicabilityName()) || !dataField[FieldsColumn.FIELD_VALUE.ordinal()].equals(fieldInfo.getValue())) {
                // Set the flag indicating a mismatch exists
                typeUpdate = TableTypeUpdate.MISMATCH;
                break;
            }
        }
        // Delete the added type definition
        getTypeDefinitions().remove(altTypeDefn);
    }
    return typeUpdate;
}
Also used : TableTypeUpdate(CCDD.CcddConstants.TableTypeUpdate) TableTypeDefinition(CCDD.CcddClassesDataTable.TableTypeDefinition) FieldInformation(CCDD.CcddClassesDataTable.FieldInformation)

Aggregations

TableTypeDefinition (CCDD.CcddClassesDataTable.TableTypeDefinition)4 CCDDException (CCDD.CcddClassesDataTable.CCDDException)2 TableDefinition (CCDD.CcddClassesDataTable.TableDefinition)2 TableTypeUpdate (CCDD.CcddConstants.TableTypeUpdate)2 TypeDefinition (CCDD.CcddTableTypeHandler.TypeDefinition)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 FieldInformation (CCDD.CcddClassesDataTable.FieldInformation)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1