From 539edbf75cf7c1a827e3bd70b9d807acca92cfcd Mon Sep 17 00:00:00 2001 From: izabel <izabel> Date: Mon, 15 Mar 2010 10:05:27 +0000 Subject: [PATCH] [LMS-1429] add "transpose" option to TSV viewer SVN: 15138 --- .../AbstractFileTableReportingPlugin.java | 30 +++++ .../standard/TSVViewReportingPlugin.java | 8 +- .../AbstractFileTableReportingPluginTest.java | 103 ++++++++++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPluginTest.java diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPlugin.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPlugin.java index 49945ca5538..752632a855b 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPlugin.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPlugin.java @@ -161,4 +161,34 @@ abstract public class AbstractFileTableReportingPlugin extends AbstractDatastore } return tableBuilder.getTableModel(); } + + protected static TableModel createTransposedTableModel(DatasetFileLines lines) + { + int columns = lines.getHeaderTokens().length; + int rows = lines.getDataLines().size() + 1; + String[][] all = new String[columns][rows]; + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < columns; c++) + { + all[c][r] = + (r == 0) ? lines.getHeaderTokens()[c] : lines.getDataLines().get(r - 1)[c]; + } + } + SimpleTableModelBuilder tableBuilder = new SimpleTableModelBuilder(); + for (int r = 0; r < rows; r++) + { + tableBuilder.addHeader(all[0][r]); + } + for (int c = 1; c < columns; c++) + { + List<ISerializableComparable> row = new ArrayList<ISerializableComparable>(); + for (int r = 0; r < rows; r++) + { + row.add(TableCellUtil.createTableCell(all[c][r])); + } + tableBuilder.addRow(row); + } + return tableBuilder.getTableModel(); + } } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/TSVViewReportingPlugin.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/TSVViewReportingPlugin.java index 7b3180788fc..e4ca18d6348 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/TSVViewReportingPlugin.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/TSVViewReportingPlugin.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Properties; import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.common.utilities.PropertyUtils; import ch.systemsx.cisd.openbis.dss.generic.server.AutoResolveUtils; import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.DatasetFileLines; import ch.systemsx.cisd.openbis.generic.shared.basic.dto.TableModel; @@ -36,9 +37,14 @@ public class TSVViewReportingPlugin extends AbstractFileTableReportingPlugin { private static final long serialVersionUID = 1L; + private static final String TRANSPOSE_KEY = "transpose"; + + private final boolean transpose; + public TSVViewReportingPlugin(Properties properties, File storeRoot) { super(properties, storeRoot, TAB_SEPARATOR); + this.transpose = PropertyUtils.getBoolean(properties, TRANSPOSE_KEY, false); } public TableModel createReport(List<DatasetDescription> datasets) @@ -52,7 +58,7 @@ public class TSVViewReportingPlugin extends AbstractFileTableReportingPlugin if (fileToOpenOrNull != null && fileToOpenOrNull.isFile() && fileToOpenOrNull.exists()) { DatasetFileLines lines = loadFromFile(dataset, fileToOpenOrNull); - return createTableModel(lines); + return transpose ? createTransposedTableModel(lines) : createTableModel(lines); } throw UserFailureException.fromTemplate("Main TSV file could not be found."); } diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPluginTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPluginTest.java new file mode 100644 index 00000000000..541c32c1860 --- /dev/null +++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractFileTableReportingPluginTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard; + +import java.io.File; +import java.util.Arrays; + +import org.testng.AssertJUnit; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.openbis.dss.generic.server.plugins.tasks.DatasetFileLines; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.TableModel; +import ch.systemsx.cisd.openbis.generic.shared.dto.DatasetDescription; + +/** + * Test cases for {@link AbstractFileTableReportingPlugin}. + * + * @author Izabela Adamczyk + */ +public class AbstractFileTableReportingPluginTest extends AssertJUnit +{ + + @Test + public void testCreateTransposedTableModel() throws Exception + { + String[][] lines = + { + { "A", "B", "C", "D", "E" }, + { "A1", "B1", "C1", "D1", "E1" }, + { "A2", "B2", "C2", "D2", "E2" }, }; + DatasetFileLines dataSetFileLines = + new DatasetFileLines(createFile(), createDataSetDescription(), Arrays.asList(lines)); + TableModel model = + AbstractFileTableReportingPlugin.createTransposedTableModel(dataSetFileLines); + + assertEquals(3, model.getHeader().size()); + + assertEquals("A", getHeaderValue(model, 0)); + assertEquals("A1", getHeaderValue(model, 1)); + assertEquals("A2", getHeaderValue(model, 2)); + + assertEquals(4, model.getRows().size()); + + assertEquals(3, getRowSize(model, 0)); + assertEquals(3, getRowSize(model, 1)); + assertEquals(3, getRowSize(model, 2)); + assertEquals(3, getRowSize(model, 3)); + + assertEquals("B", getDataRowColumnValue(model, 0, 0)); + assertEquals("C", getDataRowColumnValue(model, 1, 0)); + assertEquals("D", getDataRowColumnValue(model, 2, 0)); + assertEquals("E", getDataRowColumnValue(model, 3, 0)); + + assertEquals("B1", getDataRowColumnValue(model, 0, 1)); + assertEquals("C1", getDataRowColumnValue(model, 1, 1)); + assertEquals("D1", getDataRowColumnValue(model, 2, 1)); + assertEquals("E1", getDataRowColumnValue(model, 3, 1)); + + assertEquals("B2", getDataRowColumnValue(model, 0, 2)); + assertEquals("C2", getDataRowColumnValue(model, 1, 2)); + assertEquals("D2", getDataRowColumnValue(model, 2, 2)); + assertEquals("E2", getDataRowColumnValue(model, 3, 2)); + } + + private String getDataRowColumnValue(TableModel model, int row, int column) + { + return model.getRows().get(row).getValues().get(column).toString(); + } + + private int getRowSize(TableModel model, int index) + { + return model.getRows().get(index).getValues().size(); + } + + private String getHeaderValue(TableModel model, int index) + { + return model.getHeader().get(index).toString(); + } + + private File createFile() + { + return new File("file.tsv"); + } + + private DatasetDescription createDataSetDescription() + { + return new DatasetDescription("Cod", "Loc", "Sam", "Spa", "Pro", "Exp", null, null, "Ins"); + } +} -- GitLab