package com.onaro.util.jfc.grouping; import com.onaro.sanscreen.util.Constant; import java.util.Date; /** * Copies a value from a latest row in the group, based on a given column name that has the endtime (usually called 'EndTime') */ public class CopyLatestSummarizer implements Summarizer { /** * The column from which the value is copied and to which it is copied. */ private int column; /** * Index of the column that should be checked when selecting the latest row */ private int timeColumn; private final boolean excludeEndDate; /** * Creates a new summarizer * @param column index of the column to summarize * @param timeColumn the index of the column to look for the date. *

based on this column, the summarizer will selected the latest row and copy the value from that row. */ public CopyLatestSummarizer(int column,int timeColumn) { this(column, timeColumn, false); } /** * Creates a new summarizer * @param column index of the column to summarize * @param timeColumn the index of the column to look for the date. * @param excludeEndDate true if the constant date long of 5000000000000 should not be considered when calculating latest time *

based on this column, the summarizer will selected the latest row and copy the value from that row. */ public CopyLatestSummarizer(int column,int timeColumn, boolean excludeEndDate) { this.column = column; this.timeColumn = timeColumn; this.excludeEndDate = excludeEndDate; } /** * Update the summary of a node and all its descendants. * @param node the node to update */ public void updateSummary(Node node) { int childCount = node.getChildCount(); if (childCount > 0) { Object commonValue = getValueOfLatestRow(node, childCount); if (commonValue != null) { node.setValueAt(column, commonValue); } } } /** * Checks if a given time stamp can be considered later than the given max time. * [for #22677 joshuae]

* * @param testTime time to check * @param currentMaxTime the maximum or latest time * @return true if this summarizer should consider the test time later than the max time */ protected boolean isLaterThan(long testTime, long currentMaxTime) { // Exclude EndDate values? if (excludeEndDate && testTime == Constant.END_DATE) { return false; } // A simple comparison: return testTime > currentMaxTime; } /** * Iterates through all the nodes in the group, selecting the latest row (row with the biggest value in the timeColumn) and returning it's value */ private Object getValueOfLatestRow(Node node, int childCount) { long currMaxValue = 0; Object valueToCopy = null; for (int childIndex = 0 ; childIndex < childCount; ++childIndex) { Node child = node.getChild(childIndex); Object tmpValue = child.getValueAt(column); long tmpTimeValue = getTime(child); if (isLaterThan(tmpTimeValue, currMaxValue)) { currMaxValue = tmpTimeValue; valueToCopy = tmpValue; } } return valueToCopy; } /** * Get the time from timeColumn, as a long * If the cell is empty, it will be regarded as an "open" row - meaning, no end time. and therefor the 'latest' row. */ private long getTime(Node node) { Object valueAt = node.getValueAt(timeColumn); //if the value is missing, the row is open and this is the latest - return the max if (valueAt instanceof Date){ return ((Date) valueAt).getTime(); } else { return Long.MAX_VALUE; } } }