Skip to content
Snippets Groups Projects
Commit cb9e1df9 authored by tpylak's avatar tpylak
Browse files

LMS-1204 Calculated Columns: proper columns sorting for integers, longs and strings

SVN: 12957
parent 30db1b35
No related branches found
No related tags found
No related merge requests found
Showing
with 140 additions and 21 deletions
......@@ -20,6 +20,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.columns
import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.columns.framework.IColumnDefinitionUI;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.GridCustomColumnInfo;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* Column definition for a grid custom column.
......@@ -52,13 +53,19 @@ public class GridCustomColumnDefinition<T> implements IColumnDefinitionUI<T>
public Comparable<?> getComparableValue(GridRowModel<T> rowModel)
{
return getValue(rowModel);
return getPrimitiveValue(rowModel).getComparableValue();
}
public String getValue(GridRowModel<T> rowModel)
private PrimitiveValue getPrimitiveValue(GridRowModel<T> rowModel)
{
String columnId = columnMetadata.getCode();
return rowModel.findColumnValue(columnId);
PrimitiveValue value = rowModel.findColumnValue(columnId);
return value;
}
public String getValue(GridRowModel<T> rowModel)
{
return getPrimitiveValue(rowModel).toString();
}
public String getHeader()
......
......@@ -35,6 +35,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ParameterWithValue
import ch.systemsx.cisd.openbis.generic.client.web.client.exception.UserFailureException;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.GridCustomColumn;
/**
......@@ -120,12 +121,13 @@ public class GridExpressionUtils
}
for (T rowData : allRows)
{
HashMap<String, String> customColumnValues = new HashMap<String, String>();
HashMap<String, PrimitiveValue> customColumnValues =
new HashMap<String, PrimitiveValue>();
for (GridCustomColumn customColumn : customColumns)
{
String columnId = customColumn.getCode();
RowCalculator<T> calculator = calculators.get(columnId);
String value = evalCustomColumn(rowData, customColumn, calculator);
PrimitiveValue value = evalCustomColumn(rowData, customColumn, calculator);
customColumnValues.put(columnId, value);
}
result.add(new GridRowModel<T>(rowData, customColumnValues));
......@@ -133,7 +135,8 @@ public class GridExpressionUtils
return result;
}
private static List<GridCustomColumnInfo> extractColumnInfos(List<GridCustomColumn> customColumns)
private static List<GridCustomColumnInfo> extractColumnInfos(
List<GridCustomColumn> customColumns)
{
List<GridCustomColumnInfo> result = new ArrayList<GridCustomColumnInfo>();
for (GridCustomColumn column : customColumns)
......@@ -146,7 +149,7 @@ public class GridExpressionUtils
return result;
}
private static <T> String evalCustomColumn(T rowData, GridCustomColumn customColumn,
private static <T> PrimitiveValue evalCustomColumn(T rowData, GridCustomColumn customColumn,
RowCalculator<T> calculator)
{
// NOTE: we do not allow a calculated column to reference other calculated columns. It's
......@@ -155,14 +158,15 @@ public class GridExpressionUtils
// dependencies create a DAG. Then the columns should be evaluated in a topological
// order.
GridRowModel<T> rowDataWithEmptyCustomColumns =
new GridRowModel<T>(rowData, new HashMap<String, String>());
new GridRowModel<T>(rowData, new HashMap<String, PrimitiveValue>());
try
{
calculator.setRowData(rowDataWithEmptyCustomColumns);
return calculator.evalAsString();
return calculator.getTypedResult();
} catch (Exception ex)
{
return createCustomColumnErrorMessage(customColumn, ex);
return new PrimitiveValue(createCustomColumnErrorMessage(customColumn, ex));
}
}
......
......@@ -27,6 +27,7 @@ import ch.systemsx.cisd.common.evaluator.EvaluatorException;
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ParameterWithValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* @author Franz-Josef Elmer
......@@ -61,6 +62,21 @@ class RowCalculator<T>
row.setRowData(rowData);
}
public PrimitiveValue getTypedResult()
{
Object value = evaluator.eval();
if (value instanceof Long)
{
return new PrimitiveValue((Long) value);
} else if (value instanceof Double)
{
return new PrimitiveValue((Double) value);
} else
{
return new PrimitiveValue(value.toString());
}
}
public boolean evalToBoolean() throws EvaluatorException
{
return evaluator.evalToBoolean();
......
......@@ -27,7 +27,7 @@ public class GridCustomColumnValue implements IsSerializable
{
private String columnId;
private String value;
private PrimitiveValue value;
public String getColumnId()
{
......@@ -39,12 +39,12 @@ public class GridCustomColumnValue implements IsSerializable
this.columnId = columnId;
}
public String getValue()
public PrimitiveValue getValue()
{
return value;
}
public void setValue(String value)
public void setValue(PrimitiveValue value)
{
this.value = value;
}
......
......@@ -23,7 +23,6 @@ import java.util.Map.Entry;
import com.google.gwt.user.client.rpc.IsSerializable;
/**
* Stores the original object which will be a basis to calculate a grid row together with calculated
* all custom columns values.
......@@ -38,16 +37,17 @@ public class GridRowModel<T> implements IsSerializable
// displaying serialization warnings in GWT 1.5. It was fixed in GWT 1.6
private List<GridCustomColumnValue> calculatedColumnValues;
public GridRowModel(T originalObject, HashMap<String, String> calculatedColumnMap)
public GridRowModel(T originalObject,
HashMap<String, PrimitiveValue> calculatedColumnMap)
{
this.originalObject = originalObject;
this.calculatedColumnValues = asList(calculatedColumnMap);
}
private List<GridCustomColumnValue> asList(HashMap<String, String> map)
private List<GridCustomColumnValue> asList(HashMap<String, PrimitiveValue> map)
{
List<GridCustomColumnValue> result = new ArrayList<GridCustomColumnValue>();
for (Entry<String, String> entry : map.entrySet())
for (Entry<String, PrimitiveValue> entry : map.entrySet())
{
GridCustomColumnValue column = new GridCustomColumnValue();
column.setColumnId(entry.getKey());
......@@ -74,7 +74,7 @@ public class GridRowModel<T> implements IsSerializable
this.calculatedColumnValues = calculatedColumnValues;
}
public String findColumnValue(String columnId)
public PrimitiveValue findColumnValue(String columnId)
{
for (GridCustomColumnValue value : calculatedColumnValues)
{
......
/*
* Copyright 2009 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.generic.shared.basic;
import com.google.gwt.user.client.rpc.IsSerializable;
/**
* Stores one primitive value: Double, Long or String.
* <p>
* Such a type is needed becaus GWT does not support serialization fields of Object or Serializable
* type.
* </p>
*
* @author Tomasz Pylak
*/
public class PrimitiveValue implements IsSerializable
{
private Double doubleValueOrNull;
private Long longValueOrNull;
private String stringValueOrNull;
public PrimitiveValue(Double value)
{
doubleValueOrNull = value;
}
public PrimitiveValue(Long value)
{
longValueOrNull = value;
}
public PrimitiveValue(String value)
{
stringValueOrNull = value;
}
public Comparable<?> getComparableValue()
{
if (doubleValueOrNull != null)
{
return doubleValueOrNull;
} else if (longValueOrNull != null)
{
return longValueOrNull;
} else
{
return stringValueOrNull;
}
}
@Override
public String toString()
{
if (doubleValueOrNull != null)
{
return doubleValueOrNull.toString();
} else if (longValueOrNull != null)
{
return longValueOrNull.toString();
} else
{
return stringValueOrNull;
}
}
// GWT only
@SuppressWarnings("unused")
private PrimitiveValue()
{
}
}
......@@ -30,6 +30,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.columns
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ParameterWithValue;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* @author Franz-Josef Elmer
......@@ -235,6 +236,6 @@ public class RowCalculatorTest extends AssertJUnit
{
Data data = new Data();
data.setValue(value);
return new GridRowModel<Data>(data, new HashMap<String, String>());
return new GridRowModel<Data>(data, new HashMap<String, PrimitiveValue>());
}
}
......@@ -30,6 +30,7 @@ import org.testng.annotations.Test;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* @author Franz-Josef Elmer
......@@ -230,7 +231,8 @@ public class RowTest extends AssertJUnit
private GridRowModel<Data> createData(double value)
{
Data originalObject = new Data(value);
return new GridRowModel<Data>(originalObject, new HashMap<String, String>());
return new GridRowModel<Data>(originalObject,
new HashMap<String, PrimitiveValue>());
}
@Test
......
......@@ -28,6 +28,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.GridCustomColumnIn
import ch.systemsx.cisd.openbis.generic.client.web.client.dto.GridRowModels;
import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
/**
* Tests of {@link TSVRenderer}
......@@ -54,7 +55,7 @@ public class TSVRendererTest
List<GridRowModel<T>> list = new ArrayList<GridRowModel<T>>();
for (T entity : entities)
{
list.add(new GridRowModel<T>(entity, new HashMap<String, String>()));
list.add(new GridRowModel<T>(entity, new HashMap<String, PrimitiveValue>()));
}
return new GridRowModels<T>(list, new ArrayList<GridCustomColumnInfo>());
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment