Content of file DefaultHourlyReportGenerator.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>DefaultHourlyReportGenerator.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueHour - UI - Commons</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-accounting-model</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.accounting.model.impl</a> &gt; <span class="el_source">DefaultHourlyReportGenerator.java</span></div><h1>DefaultHourlyReportGenerator.java</h1><pre class="source lang-java linenums">/*
 * #%L
 * *********************************************************************************************************************
 *
 * blueHour
 * http://bluehour.tidalwave.it - git clone git@bitbucket.org:tidalwave/bluehour-src.git
 * %%
 * Copyright (C) 2013 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.accounting.model.impl;

import javax.annotation.Nonnull;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import it.tidalwave.dci.annotation.DciRole;
import it.tidalwave.accounting.model.HourlyReportGenerator;
import it.tidalwave.accounting.model.HourlyReport;
import it.tidalwave.accounting.model.JobEvent;
import it.tidalwave.accounting.model.JobEventGroup;
import it.tidalwave.accounting.model.spi.JobEventSpi;
import it.tidalwave.accounting.model.spi.ProjectSpi;
import lombok.RequiredArgsConstructor;
import static java.util.Comparator.*;
import static it.tidalwave.accounting.model.spi.util.Formatters.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L52">@DciRole(datumType = ProjectSpi.class) @RequiredArgsConstructor</span>
public class DefaultHourlyReportGenerator implements HourlyReportGenerator
  {
    private static final String SEPARATOR = &quot;===========================================================================&quot;;

    private static final String PATTERN = &quot;| %-12s | %-30s | %-8s | %-12s |%n&quot;;
    private static final String PATTERN2 = &quot;| %12s | %-30s | %8s | %12s |%n&quot;;
    private static final String PATTERN3 = &quot;  %12s   %-30s   %8s   %12s  %n&quot;;

    @Nonnull
    private final ProjectSpi project;

    @Override @Nonnull
    public HourlyReport createReport()
      {
<span class="nc" id="L67">        final var sw = new StringWriter();</span>
<span class="nc" id="L68">        makeReport(sw);</span>
<span class="nc" id="L69">        return new HourlyReport(sw.toString());</span>
      }

    private void makeReport (@Nonnull final Writer w)
      {
<span class="nc" id="L74">        final var pw = new PrintWriter(w);</span>
<span class="nc" id="L75">        System.err.println(&quot;CREATE REPORT &quot; + project);</span>

<span class="nc" id="L77">        pw.print(SEPARATOR + &quot;\n&quot;);</span>
<span class="nc" id="L78">        pw.printf(PATTERN, &quot;Date&quot;, &quot;Description&quot;, &quot;Time&quot;, &quot;Cost&quot;);</span>
<span class="nc" id="L79">        pw.print(SEPARATOR + &quot;\n&quot;);</span>

        // TODO: quick and dirty - refactor with visitor, lambdas
<span class="nc" id="L82">        final List&lt;JobEventSpi&gt; jobEvents = new ArrayList&lt;&gt;();</span>
<span class="nc" id="L83">        addAll(jobEvents, project.findChildren().results());</span>
<span class="nc" id="L84">        jobEvents.stream().sorted(comparing(JobEventSpi::getDateTime))</span>
<span class="nc" id="L85">                          .forEach(event -&gt; pw.printf(PATTERN2, DATE_FORMATTER.format(event.getDateTime()),</span>
<span class="nc" id="L86">                                                                event.getName(),</span>
<span class="nc" id="L87">                                                                DURATION_FORMATTER.format(event.getDuration()),</span>
<span class="nc" id="L88">                                                                MONEY_FORMATTER.format(event.getEarnings())));</span>
<span class="nc" id="L89">        pw.print(SEPARATOR + &quot;\n&quot;);</span>
<span class="nc" id="L90">        pw.printf(PATTERN3, &quot;&quot;, &quot;&quot;, DURATION_FORMATTER.format(project.getDuration()),</span>
<span class="nc" id="L91">                                    MONEY_FORMATTER.format(project.getEarnings()));</span>

        // FIXME: rename getAmount() -&gt; getBudget()
        // FIXME: introduce getBudgetDuration()
introduce getBudgetDuration()
<span class="nc" id="L95"> final var duration = Duration.ofHours((long)project.getBudget().divided(project.getHourlyRate()));</span> <span class="nc" id="L96"> pw.printf(&quot;BUDGET: %s%n&quot;, MONEY_FORMATTER.format(project.getBudget()));</span> <span class="nc" id="L97"> pw.printf(&quot;HOURLY RATE: %s%n&quot;, MONEY_FORMATTER.format(project.getHourlyRate()));</span> <span class="nc" id="L98"> pw.printf(&quot;DURATION: %s%n&quot;, DURATION_FORMATTER.format(duration));</span> <span class="nc" id="L99"> pw.printf(&quot;REMAINING BUDGET: %s%n&quot;,</span> <span class="nc" id="L100"> MONEY_FORMATTER.format(project.getBudget().subtract(project.getEarnings())));</span> <span class="nc" id="L101"> pw.printf(&quot;REMAINING TIME: %s%n&quot;, DURATION_FORMATTER.format(duration.minus(project.getDuration())));</span> <span class="nc" id="L102"> pw.flush();</span> <span class="nc" id="L103"> }</span> private void addAll (@Nonnull final List&lt;? super JobEventSpi&gt; results, @Nonnull final List&lt;? extends JobEvent&gt; jobEvents) { <span class="nc bnc" id="L108" title="All 2 branches missed."> for (final JobEvent jobEvent : jobEvents)</span> { <span class="nc bnc" id="L110" title="All 2 branches missed."> if (jobEvent instanceof JobEventGroup)</span> { <span class="nc" id="L112"> addAll(results, ((JobEventGroup)jobEvent).findChildren().results());</span> } else { <span class="nc" id="L116"> results.add((JobEventSpi)jobEvent);</span> } <span class="nc" id="L118"> }</span> <span class="nc" id="L119"> }</span> } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>