Search in sources :

Example 1 with AttributeMappingParameters

use of org.cytoscape.tableimport.internal.reader.AttributeMappingParameters in project cytoscape-impl by cytoscape.

the class LoadTableReaderTask method loadAnnotation.

private void loadAnnotation(TaskMonitor tm) {
    tm.setProgress(0.0);
    TextTableReader reader = this.reader;
    AttributeMappingParameters readerAMP = (AttributeMappingParameters) reader.getMappingParameter();
    String primaryKey = readerAMP.getAttributeNames()[readerAMP.getKeyIndex()];
    tm.setProgress(0.1);
    final CyTableFactory tableFactory = serviceRegistrar.getService(CyTableFactory.class);
    final CyTable table = tableFactory.createTable("AttrTable " + inputName.substring(inputName.lastIndexOf('/') + 1) + " " + Integer.toString(numImports++), primaryKey, String.class, true, true);
    cyTables = new CyTable[] { table };
    tm.setProgress(0.3);
    try {
        this.reader.readTable(table);
    } catch (IOException e) {
        e.printStackTrace();
    }
    tm.setProgress(1.0);
}
Also used : CyTableFactory(org.cytoscape.model.CyTableFactory) CyTable(org.cytoscape.model.CyTable) IOException(java.io.IOException) TextTableReader(org.cytoscape.tableimport.internal.reader.TextTableReader) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)

Example 2 with AttributeMappingParameters

use of org.cytoscape.tableimport.internal.reader.AttributeMappingParameters in project cytoscape-impl by cytoscape.

the class ImportTablePanel method getAttributeMappingParameters.

public AttributeMappingParameters getAttributeMappingParameters() throws Exception {
    final String sourceName = getPreviewPanel().getSourceName();
    final String[] attrNames = getPreviewPanel().getAttributeNames();
    final SourceColumnSemantic[] types = getPreviewPanel().getTypes();
    if (!isAttributeNamesValid(attrNames, types))
        return null;
    final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);
    final AttributeDataType[] dataTypes = getPreviewPanel().getDataTypes();
    final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);
    final String[] listDelimiters = getPreviewPanel().getListDelimiters();
    final String[] listDelimitersCopy = Arrays.copyOf(listDelimiters, listDelimiters.length);
    int startLineNumber = getStartLineNumber();
    String commentChar = null;
    if (!getCommentLinePrefix().isEmpty())
        commentChar = getCommentLinePrefix();
    // Build mapping parameter object.
    final List<String> del = checkDelimiter();
    final int keyInFile = getPreviewPanel().getColumnIndex(KEY);
    final AttributeMappingParameters mapping = new AttributeMappingParameters(sourceName, del, listDelimitersCopy, keyInFile, attrNames, dataTypesCopy, typesCopy, startLineNumber, commentChar);
    return mapping;
}
Also used : SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)

Example 3 with AttributeMappingParameters

use of org.cytoscape.tableimport.internal.reader.AttributeMappingParameters in project cytoscape-impl by cytoscape.

the class LoadTableReaderTask method run.

@Override
public void run(final TaskMonitor tm) throws Exception {
    tm.setTitle("Loading table data");
    tm.setProgress(0.0);
    tm.setStatusMessage("Loading table...");
    List<String> attrNameList = new ArrayList<>();
    int colCount;
    String[] attributeNames;
    Workbook workbook = null;
    // Load Spreadsheet data for preview.
    try {
        if (fileType != null && (fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) && workbook == null) {
            try {
                workbook = WorkbookFactory.create(isStart);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
                throw new IllegalArgumentException("Could not read Excel file.  Maybe the file is broken?");
            } finally {
                if (isStart != null)
                    isStart.close();
            }
        }
    } catch (Exception ioe) {
        tm.showMessage(TaskMonitor.Level.ERROR, "Unable to read table: " + ioe.getMessage());
        return;
    }
    if (startLoadRow > 0)
        startLoadRow--;
    final int startLoadRowTemp = firstRowAsColumnNames ? 0 : startLoadRow;
    previewPanel.updatePreviewTable(workbook, fileType, inputName, isStart, delimiters.getSelectedValues(), null, startLoadRowTemp);
    colCount = previewPanel.getPreviewTable().getColumnModel().getColumnCount();
    Object curName = null;
    if (firstRowAsColumnNames) {
        previewPanel.setFirstRowAsColumnNames();
        startLoadRow++;
    }
    final String sourceName = previewPanel.getSourceName();
    final SourceColumnSemantic[] types = previewPanel.getTypes();
    for (int i = 0; i < colCount; i++) {
        curName = previewPanel.getPreviewTable().getColumnModel().getColumn(i).getHeaderValue();
        if (attrNameList.contains(curName)) {
            int dupIndex = 0;
            for (int idx = 0; idx < attrNameList.size(); idx++) {
                if (curName.equals(attrNameList.get(idx))) {
                    dupIndex = idx;
                    break;
                }
            }
            if (!TypeUtil.allowsDuplicateName(ImportType.TABLE_IMPORT, types[i], types[dupIndex])) {
                // TODO add message to user
                return;
            }
        }
        if (curName == null)
            attrNameList.add("Column " + i);
        else
            attrNameList.add(curName.toString());
    }
    attributeNames = attrNameList.toArray(new String[0]);
    final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);
    final AttributeDataType[] dataTypes = previewPanel.getDataTypes();
    final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);
    AttributeDataType[] tunableDataTypes = null;
    if (dataTypeList != null && !dataTypeList.trim().isEmpty())
        tunableDataTypes = TypeUtil.parseDataTypeList(dataTypeList);
    if (tunableDataTypes != null && tunableDataTypes.length > 0)
        System.arraycopy(tunableDataTypes, 0, dataTypesCopy, 0, Math.min(tunableDataTypes.length, dataTypesCopy.length));
    String[] listDelimiters = previewPanel.getListDelimiters();
    if (listDelimiters == null || listDelimiters.length == 0) {
        listDelimiters = new String[dataTypes.length];
        if (delimitersForDataList.getSelectedValue() != null)
            Arrays.fill(listDelimiters, delimitersForDataList.getSelectedValue());
    }
    if (keyColumnIndex > 0)
        keyColumnIndex--;
    amp = new AttributeMappingParameters(sourceName, delimiters.getSelectedValues(), listDelimiters, keyColumnIndex, attributeNames, dataTypesCopy, typesCopy, startLoadRow, null);
    if (this.fileType.equalsIgnoreCase(SupportedFileType.EXCEL.getExtension()) || this.fileType.equalsIgnoreCase(SupportedFileType.OOXML.getExtension())) {
        // Fixed bug# 1668, Only load data from the first sheet, ignore the rest sheets
        // UPDATE: From the user perspective it makes more sense to get the selected tab/sheet than the first one.
        final Sheet sheet = workbook.getSheet(sourceName);
        if (sheet != null) {
            reader = new ExcelAttributeSheetReader(sheet, amp, serviceRegistrar);
            loadAnnotation(tm);
        }
    } else {
        reader = new DefaultAttributeTableReader(null, amp, this.isEnd, serviceRegistrar);
        loadAnnotation(tm);
    }
}
Also used : ArrayList(java.util.ArrayList) ExcelAttributeSheetReader(org.cytoscape.tableimport.internal.reader.ExcelAttributeSheetReader) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Workbook(org.apache.poi.ss.usermodel.Workbook) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) DefaultAttributeTableReader(org.cytoscape.tableimport.internal.reader.DefaultAttributeTableReader) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 4 with AttributeMappingParameters

use of org.cytoscape.tableimport.internal.reader.AttributeMappingParameters in project cytoscape-impl by cytoscape.

the class ImportAttributeTableReaderTask method loadAnnotation.

private void loadAnnotation(TaskMonitor tm) {
    tm.setProgress(0.0);
    final TextTableReader reader = this.reader;
    final AttributeMappingParameters readerAMP = (AttributeMappingParameters) reader.getMappingParameter();
    final String primaryKey = readerAMP.getAttributeNames()[readerAMP.getKeyIndex()];
    final AttributeDataType dataType = readerAMP.getDataTypes()[readerAMP.getKeyIndex()];
    final Class<?> keyType;
    switch(dataType) {
        case TYPE_INTEGER:
            keyType = Integer.class;
            break;
        case TYPE_LONG:
            keyType = Long.class;
            break;
        default:
            keyType = String.class;
    }
    tm.setProgress(0.1);
    final CyTable table = serviceRegistrar.getService(CyTableFactory.class).createTable("AttrTable " + inputName.substring(inputName.lastIndexOf('/') + 1) + " " + Integer.toString(numImports++), primaryKey, keyType, true, true);
    cyTables = new CyTable[] { table };
    tm.setProgress(0.3);
    try {
        this.reader.readTable(table);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    tm.setProgress(1.0);
}
Also used : CyTableFactory(org.cytoscape.model.CyTableFactory) CyTable(org.cytoscape.model.CyTable) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) IOException(java.io.IOException) TextTableReader(org.cytoscape.tableimport.internal.reader.TextTableReader) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)

Aggregations

AttributeMappingParameters (org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)4 IOException (java.io.IOException)3 AttributeDataType (org.cytoscape.tableimport.internal.util.AttributeDataType)3 CyTable (org.cytoscape.model.CyTable)2 CyTableFactory (org.cytoscape.model.CyTableFactory)2 TextTableReader (org.cytoscape.tableimport.internal.reader.TextTableReader)2 SourceColumnSemantic (org.cytoscape.tableimport.internal.util.SourceColumnSemantic)2 ArrayList (java.util.ArrayList)1 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 DefaultAttributeTableReader (org.cytoscape.tableimport.internal.reader.DefaultAttributeTableReader)1 ExcelAttributeSheetReader (org.cytoscape.tableimport.internal.reader.ExcelAttributeSheetReader)1