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 20, 2022
1 parent 47ff2e7 commit dae0817
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 528 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 @@ -147,14 +149,7 @@ protected void renderStatRow( String textKey, int statCount, boolean forceSucces
{
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 +160,18 @@ protected void renderStatRow( String textKey, int statCount, boolean forceSucces
sink.tableRow_();
}

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 +218,89 @@ 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 )
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_();
}

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 )
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_();
}

protected void renderStatus( AbstractVersionDetails details )
{
if ( newestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null )
{
renderWarningIcon();
Expand Down Expand Up @@ -336,109 +331,31 @@ 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 )
}

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
*/
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 Down Expand Up @@ -466,4 +383,5 @@ protected String getLabel( ArtifactVersion version, AbstractVersionDetails detai

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ protected void renderDependencyDetail( Dependency artifact, ArtifactVersions det
renderDependencyDetailTable( artifact, details, true );
sink.section2_();
}

}
Loading

0 comments on commit dae0817

Please sign in to comment.