Skip to content

Commit

Permalink
Refactoring Reports classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sultan committed Oct 30, 2022
1 parent 5ae680a commit e73286e
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 526 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* under the License.
*/

import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -58,12 +57,15 @@ public abstract class AbstractVersionsReportRenderer<T> extends VersionsReportRe
*/
protected T model;

protected ArtifactVersionsCache newestUpdateCache
protected final ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );

protected ArtifactVersionsCache allUpdatesCache
protected final ArtifactVersionsCache allUpdatesCache
= new ArtifactVersionsCache( AbstractVersionDetails::getAllUpdates );

protected final SinkEventAttributes headerAttributes
= new SinkEventAttributeSet( SinkEventAttributes.WIDTH, "30%" );

/**
* Constructor to be called by the dependency injection framework
* @param i18n i18n object to be injected
Expand Down Expand Up @@ -143,18 +145,17 @@ protected <Q extends OverviewStats> void renderOverviewTableRow( Q stats )
renderStatRow( "report.overview.numNewerMajorAvailable", stats.getMajor(), false );
}

/**
* Renders one table row for the given statistics.
* @param textKey the key of the text to be rendered.
* @param statCount the number of artifacts with the given stat.
* @param forceSuccessIcon if true, the success icon will be rendered regardless.
*/
protected void renderStatRow( String textKey, int statCount, boolean forceSuccessIcon )
{
sink.tableRow();
sink.tableCell();
if ( statCount == 0 || forceSuccessIcon )
{
renderSuccessIcon();
}
else
{
renderWarningIcon();
}
renderIcon( statCount == 0 || forceSuccessIcon );
sink.tableCell_();
sink.tableCell();
sink.text( getText( textKey ) );
Expand All @@ -165,6 +166,22 @@ protected void renderStatRow( String textKey, int statCount, boolean forceSucces
sink.tableRow_();
}

/**
* Renders the success or warning icon.
* @param success if true, the success icon will be rendered, otherwise the warning icon will be rendered.
*/
protected void renderIcon( boolean success )
{
if ( success )
{
renderSuccessIcon();
}
else
{
renderWarningIcon();
}
}

/**
* Computes the {@linkplain OverviewStats} object needed to render the summary table row
* @param <Q> concrete {@linkplain OverviewStats} class
Expand Down Expand Up @@ -211,101 +228,107 @@ protected void renderSummaryTableHeader( boolean hasScope, boolean hasType )
"report.latestIncremental", "report.latestMinor", "report.latestMajor" );
}

protected void renderSummaryTableRow( Dependency artifact, ArtifactVersions details,
boolean includeScope )
protected void renderSummaryTableRow( Dependency artifact, ArtifactVersions details, boolean includeScope )
{
ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() );
boolean upToDate = allUpdates == null || allUpdates.length == 0;

sink.tableRow();

sink.tableCell();
if ( upToDate )
{
renderSuccessIcon();
}
else
{
renderWarningIcon();
}
sink.tableCell_();
sink.tableCell();
sink.text( artifact.getGroupId() );
sink.tableCell_();
sink.tableCell();
sink.text( artifact.getArtifactId() );
sink.tableCell_();
sink.tableCell();
sink.text( artifact.getVersion() );
renderIcon( upToDate );
sink.tableCell_();

renderCells( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
if ( includeScope )
{
sink.tableCell();
sink.text( artifact.getScope() );
sink.tableCell_();
renderCell( artifact.getScope() );
}
sink.tableCell();
sink.text( artifact.getClassifier() );
sink.tableCell_();
sink.tableCell();
sink.text( artifact.getType() );
sink.tableCell_();
renderCells( artifact.getClassifier(), artifact.getType() );
renderNewestVersions( details );

sink.tableCell();
if ( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
safeBold();
sink.text( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ).toString() );
safeBold_();
}
sink.tableCell_();
sink.tableRow_();
}

sink.tableCell();
if ( newestUpdateCache.get( details, of( INCREMENTAL ) ) != null )
/**
* Renders the newest versions for the given artifact.
* @param details the artifact for which to render the newest versions.
*/
protected void renderNewestVersions( AbstractVersionDetails details )
{
renderBoldCell( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) );
renderBoldCell( newestUpdateCache.get( details, of( INCREMENTAL ) ) );
renderBoldCell( newestUpdateCache.get( details, of( MINOR ) ) );
renderBoldCell( newestUpdateCache.get( details, of( MAJOR ) ) );
}

protected void renderDependencyDetailTable( Dependency artifact, ArtifactVersions details, boolean includeScope )
{
ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() );
boolean upToDate = allUpdates == null || allUpdates.length == 0;

sink.table();
sink.tableRows( new int[] { Sink.JUSTIFY_RIGHT, Sink.JUSTIFY_LEFT }, false );

renderTwoCellsRow( "report.status", () -> renderStatus( details ) );
renderTwoCellsRow( "report.groupId", artifact.getGroupId() );
renderTwoCellsRow( "report.artifactId", artifact.getArtifactId() );
renderTwoCellsRow( "report.currentVersion", artifact.getVersion() );
if ( includeScope )
{
safeBold();
sink.text( newestUpdateCache.get( details, of( INCREMENTAL ) ).toString() );
safeBold_();
renderTwoCellsRow( "report.scope", artifact.getScope() );
}
sink.tableCell_();

sink.tableCell();
if ( newestUpdateCache.get( details, of( MINOR ) ) != null )
renderTwoCellsRow( "report.classifier", artifact.getClassifier() );
renderTwoCellsRow( "report.type", artifact.getType() );
if ( !upToDate )
{
safeBold();
sink.text( newestUpdateCache.get( details, of( MINOR ) ).toString() );
safeBold_();
renderTwoCellsRow( "report.updateVersions", () -> renderVersions( allUpdates, details ) );
}
sink.tableCell_();

sink.tableRows_();
sink.table_();
}

/**
* Renders a row of two cells, the first cell being an header and the second cell being a non-header cell.
* @param textKey the key of the text to be rendered.
* @param textValue the value of the text to be rendered.
*/
protected void renderTwoCellsRow( String textKey, String textValue )
{
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( textKey ) );
sink.tableHeaderCell_();
sink.tableCell();
if ( newestUpdateCache.get( details, of( MAJOR ) ) != null )
{
safeBold();
sink.text( newestUpdateCache.get( details, of( MAJOR ) ).toString() );
safeBold_();
}
sink.text( textValue );
sink.tableCell_();

sink.tableRow_();
}

@SuppressWarnings( "checkstyle:MethodLength" )
protected void renderDependencyDetailTable( Dependency artifact, ArtifactVersions details, boolean includeScope )
/**
* Renders a row of two cells, the first cell being an header and the second cell being a non-header cell.
* @param textKey the key of the text to be rendered.
* @param runnable the runnable to be executed to render the second cell content.
*/
protected void renderTwoCellsRow( String textKey, Runnable runnable )
{
ArtifactVersion[] allUpdates = allUpdatesCache.get( details, empty() );
boolean upToDate = allUpdates == null || allUpdates.length == 0;

final SinkEventAttributes headerAttributes = new SinkEventAttributeSet();
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "70%" );
final SinkEventAttributes cellAttributes = new SinkEventAttributeSet();
headerAttributes.addAttribute( SinkEventAttributes.WIDTH, "30%" );
sink.table();
sink.tableRows( new int[] { Sink.JUSTIFY_RIGHT, Sink.JUSTIFY_LEFT }, false );
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.status" ) );
sink.text( getText( textKey ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.tableCell();
runnable.run();
sink.tableCell_();
sink.tableRow_();
}

/**
* Renders the status of the given artifact.
* @param details the artifact for which to render the status.
*/
protected void renderStatus( AbstractVersionDetails details )
{
if ( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
renderWarningIcon();
Expand Down Expand Up @@ -336,111 +359,44 @@ else if ( newestUpdateCache.get( details, of( MAJOR ) ) != null )
sink.nonBreakingSpace();
sink.text( getText( "report.noUpdatesAvailable" ) );
}
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.groupId" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getGroupId() );
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.artifactId" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getArtifactId() );
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.currentVersion" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getVersion() );
sink.tableCell_();
sink.tableRow_();
if ( includeScope )
{
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.scope" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getScope() );
sink.tableCell_();
sink.tableRow_();
}
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.classifier" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getClassifier() );
sink.tableCell_();
sink.tableRow_();
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.type" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
sink.text( artifact.getType() );
sink.tableCell_();
sink.tableRow_();
if ( !upToDate )
}

/**
* Renders the list of versions of the given artifact.
* @param allUpdates the list of all updates available for the given artifact.
* @param details the artifact for which to render the versions.
*/
protected void renderVersions( ArtifactVersion[] allUpdates, ArtifactVersions details )
{
for ( int i = 0; i < allUpdates.length; i++ )
{
sink.tableRow();
sink.tableHeaderCell( headerAttributes );
sink.text( getText( "report.updateVersions" ) );
sink.tableHeaderCell_();
sink.tableCell( cellAttributes );
for ( int i = 0; i < allUpdates.length; i++ )
if ( i > 0 )
{
sink.lineBreak();
}
String label = getLabel( allUpdates[i], details );
if ( label != null )
{
safeBold();
}
sink.text( allUpdates[i].toString() );
if ( label != null )
{
if ( i > 0 )
{
sink.lineBreak();
}
String label = getLabel( allUpdates[i], details );
if ( label != null )
{
safeBold();
}
sink.text( allUpdates[i].toString() );
if ( label != null )
{
safeBold_();
sink.nonBreakingSpace();
safeItalic();
sink.text( label );
safeItalic_();
}
safeBold_();
sink.nonBreakingSpace();
safeItalic();
sink.text( label );
safeItalic_();
}
sink.tableCell_();
sink.tableRow_();
}
sink.tableRows_();
sink.table_();
}

/**
* Renders a table header containing elements denoted by the given keys
* @param keys variable argument list containing keys of the property file to retrieve the
* headers from
* Returns a text label to describe if the given version is a major, minor, incremental or subincremental update.
* @param version the version to describe.
* @param details the artifact for which to render the versions.
* @return a text label to describe if the given version is a major, minor, incremental or subincremental update.
*/
protected void renderTableHeaderCells( String... keys )
{
Arrays.stream( keys )
.map( this::getText )
.forEachOrdered( str ->
{
sink.tableHeaderCell();
sink.text( str );
sink.tableHeaderCell_();
} );
}

protected String getLabel( ArtifactVersion version, AbstractVersionDetails details )
{

Expand All @@ -466,4 +422,5 @@ protected String getLabel( ArtifactVersion version, AbstractVersionDetails detai

return null;
}

}
Loading

0 comments on commit e73286e

Please sign in to comment.