MigrationStatusReportView.java

  1. /*
  2.  *    Copyright 2010-2022 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.mybatis.maven.mvnmigrate.report;

  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. import java.util.ResourceBundle;

  21. import org.apache.ibatis.migration.Change;
  22. import org.apache.maven.doxia.sink.Sink;
  23. import org.apache.maven.project.MavenProject;

  24. /**
  25.  * View of status report.
  26.  */
  27. public final class MigrationStatusReportView {

  28.   /**
  29.    * Generates the report.
  30.    *
  31.    * @param changes
  32.    *          list of {@link Change}
  33.    * @param sink
  34.    *          the {@link Sink} instance
  35.    * @param bundle
  36.    *          the {@link ResourceBundle} instance
  37.    */
  38.   public void generateReport(Map<MavenProject, List<Change>> changes, Sink sink, ResourceBundle bundle,
  39.       boolean isAggregate) {

  40.     sink.head();
  41.     sink.title();
  42.     sink.text(bundle.getString("migration.status.report.header"));
  43.     sink.title_();
  44.     sink.head_();
  45.     sink.body();

  46.     // Generate title
  47.     sink.section1();
  48.     sink.sectionTitle1();
  49.     sink.text(bundle.getString("migration.status.report.mainTitle"));
  50.     sink.sectionTitle1_();
  51.     sink.section1_();
  52.     sink.lineBreak();

  53.     sink.section2();
  54.     sink.sectionTitle2();
  55.     sink.text(bundle.getString("migration.status.report.secondSectionTitle"));
  56.     sink.sectionTitle2_();

  57.     for (Entry<MavenProject, List<Change>> entries : changes.entrySet()) {
  58.       if (isAggregate) {
  59.         sink.section3();
  60.         sink.sectionTitle3();
  61.         sink.text(bundle.getString("migration.status.report.moduleTitle") + entries.getKey().getName());
  62.         sink.sectionTitle3_();
  63.       }
  64.       generateStatisticsTable(sink, entries.getValue());
  65.     }

  66.     sink.section2_();
  67.     sink.lineBreak();

  68.     sink.section3();
  69.     sink.sectionTitle2();
  70.     sink.text(bundle.getString("migration.status.report.thirdSectionTitle"));
  71.     sink.sectionTitle2_();
  72.     for (Entry<MavenProject, List<Change>> entries : changes.entrySet()) {
  73.       if (isAggregate) {
  74.         sink.section3();
  75.         sink.sectionTitle3();
  76.         sink.text(bundle.getString("migration.status.report.moduleTitle") + entries.getKey().getName());
  77.         sink.sectionTitle3_();
  78.       }
  79.       // Generate Unused declared dependencies:
  80.       generateChangesTable(sink, entries.getValue());
  81.     }

  82.     sink.section3_();

  83.     // Closing the report
  84.     sink.body_();
  85.     sink.flush();
  86.     sink.close();
  87.   }

  88.   /**
  89.    * Generates statistic table.
  90.    *
  91.    * @param sink
  92.    *          the sink
  93.    * @param changes
  94.    *          the changes
  95.    */
  96.   private void generateStatisticsTable(Sink sink, List<Change> changes) {
  97.     sink.table();

  98.     sink.tableRow();
  99.     sink.tableCell();
  100.     sink.text(" Number of migration changes: ");
  101.     sink.tableCell_();

  102.     sink.tableCell();
  103.     sink.text("" + changes.size());
  104.     sink.tableCell_();
  105.     sink.tableRow_();

  106.     sink.tableRow();
  107.     sink.tableCell();
  108.     sink.text(" Number of pending migrations: ");
  109.     sink.tableCell_();

  110.     int nop = numberOfPending(changes);

  111.     sink.tableCell();
  112.     sink.text(nop + "  (" + calcPerc(changes.size(), nop) + ")  ");
  113.     sink.nonBreakingSpace();
  114.     sink.figure();
  115.     sink.figureGraphics(nop == 0 ? "images/icon_success_sml.gif" : "images/icon_warning_sml.gif");
  116.     sink.figure_();
  117.     sink.tableCell_();
  118.     sink.tableRow_();

  119.     sink.table_();
  120.   }

  121.   /**
  122.    * Calculates the percentage.
  123.    *
  124.    * @param tot
  125.    *          the tot
  126.    * @param nop
  127.    *          the nop
  128.    *
  129.    * @return the string
  130.    */
  131.   private String calcPerc(int tot, int nop) {
  132.     return "" + ((100 * nop) / tot) + "%";
  133.   }

  134.   /**
  135.    * Return the number of pending change found.
  136.    *
  137.    * @param changes
  138.    *          list of {@link Change}
  139.    *
  140.    * @return Return the number of pending change found.
  141.    */
  142.   private int numberOfPending(List<Change> changes) {
  143.     int numberOfPending = 0;
  144.     for (Change change : changes) {
  145.       if (change.getAppliedTimestamp() == null) {
  146.         numberOfPending++;
  147.       }
  148.     }
  149.     return numberOfPending;
  150.   }

  151.   /**
  152.    * Generate a table for the given dependencies iterator.
  153.    *
  154.    * @param sink
  155.    *          the {@link Sink} instance
  156.    * @param iter
  157.    *          list of {@link Change}
  158.    */
  159.   public void generateChangesTable(Sink sink, List<Change> iter) {
  160.     sink.table();

  161.     sink.tableRow();
  162.     sink.tableCell();
  163.     sink.bold();
  164.     sink.text("ID");
  165.     sink.bold_();
  166.     sink.tableCell_();

  167.     sink.tableCell();
  168.     sink.bold();
  169.     sink.text("Applied At");
  170.     sink.bold_();
  171.     sink.tableCell_();

  172.     sink.tableCell();
  173.     sink.bold();
  174.     sink.text("Description");
  175.     sink.bold_();
  176.     sink.tableCell_();

  177.     sink.tableCell();
  178.     sink.bold();
  179.     sink.text("Filename");
  180.     sink.bold_();
  181.     sink.tableCell_();

  182.     sink.tableCell();
  183.     sink.bold();
  184.     sink.text("Status");
  185.     sink.bold_();
  186.     sink.tableCell_();

  187.     sink.tableRow_();

  188.     for (Change change : iter) {
  189.       sink.tableRow();

  190.       sink.tableCell();
  191.       sink.text("" + change.getId());
  192.       sink.tableCell_();

  193.       sink.tableCell();
  194.       sink.text(change.getAppliedTimestamp() == null ? " ... pending ... " : change.getAppliedTimestamp());
  195.       sink.tableCell_();

  196.       sink.tableCell();
  197.       sink.text(change.getDescription());
  198.       sink.tableCell_();

  199.       sink.tableCell();
  200.       sink.text(change.getFilename());
  201.       sink.tableCell_();

  202.       sink.tableCell();
  203.       sink.figure();
  204.       sink.figureGraphics(
  205.           change.getAppliedTimestamp() != null ? "images/icon_success_sml.gif" : "images/icon_warning_sml.gif");
  206.       sink.figure_();
  207.       sink.tableCell_();

  208.       sink.tableRow_();
  209.     }

  210.     sink.table_();
  211.     sink.horizontalRule();
  212.   }

  213. }