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> > <a href="../index.html" class="el_bundle">it-tidalwave-accounting-model</a> > <a href="index.source.html" class="el_package">it.tidalwave.accounting.model.impl</a> > <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 "License"); 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 "AS IS" 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 = "===========================================================================";
private static final String PATTERN = "| %-12s | %-30s | %-8s | %-12s |%n";
private static final String PATTERN2 = "| %12s | %-30s | %8s | %12s |%n";
private static final String PATTERN3 = " %12s %-30s %8s %12s %n";
@Nonnull
private final ProjectSpi project;
@Override @Nonnull
public HourlyReport createReport()
{
<span class="nc" id="L67"> final StringWriter 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 PrintWriter pw = new PrintWriter(w);</span>
<span class="nc" id="L75"> System.err.println("CREATE REPORT " + project);</span>
<span class="nc" id="L77"> pw.print(SEPARATOR + "\n");</span>
<span class="nc" id="L78"> pw.printf(PATTERN, "Date", "Description", "Time", "Cost");</span>
<span class="nc" id="L79"> pw.print(SEPARATOR + "\n");</span>
// TODO: quick and dirty - refactor with visitor, lambdas
| quick and dirty - refactor with visitor, lambdas | |
<span class="nc" id="L82"> final List<JobEventSpi> jobEvents = new ArrayList<>();</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 -> 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 + "\n");</span>
<span class="nc" id="L90"> pw.printf(PATTERN3, "", "", DURATION_FORMATTER.format(project.getDuration()),</span>
<span class="nc" id="L91"> MONEY_FORMATTER.format(project.getEarnings()));</span>
// FIXME: rename getAmount() -> getBudget()
// FIXME: introduce getBudgetDuration()
<span class="nc" id="L95"> final Duration duration = Duration.ofHours((long)project.getBudget().divided(project.getHourlyRate()));</span>
<span class="nc" id="L96"> pw.printf("BUDGET: %s%n", MONEY_FORMATTER.format(project.getBudget()));</span>
<span class="nc" id="L97"> pw.printf("HOURLY RATE: %s%n", MONEY_FORMATTER.format(project.getHourlyRate()));</span>
<span class="nc" id="L98"> pw.printf("DURATION: %s%n", DURATION_FORMATTER.format(duration));</span>
<span class="nc" id="L99"> pw.printf("REMAINING BUDGET: %s%n",</span>
<span class="nc" id="L100"> MONEY_FORMATTER.format(project.getBudget().subtract(project.getEarnings())));</span>
<span class="nc" id="L101"> pw.printf("REMAINING TIME: %s%n", 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<JobEventSpi> results,
@Nonnull final List<? extends JobEvent> 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.6.202009150832</span></div></body></html>