Skip to content

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 - 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 - 2021 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.
 *
 * *********************************************************************************************************************
 *
 * $Id$
 *
 * *********************************************************************************************************************
 * #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
 * @version $Id$
 *
 **********************************************************************************************************************/
<span class="nc bnc" id="L54" title="All 2 branches missed.">@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="L69">        final StringWriter sw = new StringWriter();</span>
<span class="nc" id="L70">        makeReport(sw);</span>
<span class="nc" id="L71">        return new HourlyReport(sw.toString());</span>
      }

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

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

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

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