statoolinfosweb/src/fr/devinsy/statoolinfos/htmlize/ServicePage.java

265 lines
12 KiB
Java

/*
* Copyright (C) 2020 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of StatoolInfos, simple service statistics tool.
*
* StatoolInfos is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* StatoolInfos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with StatoolInfos. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.htmlize;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.catgenerator.core.BirdGenerator;
import fr.devinsy.statoolinfos.HtmlizerContext;
import fr.devinsy.statoolinfos.core.Metric;
import fr.devinsy.statoolinfos.core.Organization;
import fr.devinsy.statoolinfos.core.Service;
import fr.devinsy.statoolinfos.core.StatoolInfosException;
import fr.devinsy.statoolinfos.crawl.CrawlCache;
import fr.devinsy.statoolinfos.htmlize.charts.BarMonthsChartView;
import fr.devinsy.statoolinfos.htmlize.charts.ChartColor;
import fr.devinsy.xidyn.XidynException;
import fr.devinsy.xidyn.data.DisplayMode;
import fr.devinsy.xidyn.data.TagDataManager;
import fr.devinsy.xidyn.presenters.PresenterUtils;
/**
* The Class ServicePage.
*/
public class ServicePage
{
private static Logger logger = LoggerFactory.getLogger(ServicePage.class);
/**
* Builds the view.
*
* @throws IOException
* @throws StatoolInfosException
*/
public static void build(final Service service) throws IOException, StatoolInfosException
{
CrawlCache cache = HtmlizerContext.instance().getCache();
File htmlizeDirectory = HtmlizerContext.instance().getHtmlizeDirectory();
// Manage the logo file.
logger.info("Htmlize service logo: {}.", service.getName());
htmlizeServiceLogo(service, cache, htmlizeDirectory);
//
logger.info("Htmlize service properties file: {}.", service.getName());
FileUtils.copyFile(service.getInputFile(),
new File(htmlizeDirectory, service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + ".properties"));
//
logger.info("Htmlize service page: {}.", service.getName());
String page = ServicePage.htmlize(service.getOrganization(), service);
FileUtils.write(new File(htmlizeDirectory, service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml"), page, StandardCharsets.UTF_8);
}
/**
* Builds the all.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws StatoolInfosException
* the statool infos exception
*/
public static void buildAll() throws IOException, StatoolInfosException
{
for (Service service : HtmlizerContext.instance().getFederation().getAllServices())
{
build(service);
}
}
/**
* Builds the.
*
* @param service
* the service
* @return the string
* @throws StatoolInfosException
* the statool infos exception
*/
public static String htmlize(final Organization organization, final Service service) throws StatoolInfosException
{
String result;
try
{
logger.debug("Building organization page {}…", service.get("organization.name"));
TagDataManager data = new TagDataManager();
data.setAttribute("serviceLogo", "src", service.getLogoFileName());
data.setEscapedContent("serviceName", service.getName());
data.setAttribute("serviceName", "href", service.getWebsite());
data.setEscapedContent("serviceURL", service.getWebsite());
data.setAttribute("serviceURL", "href", service.getWebsite());
data.setContent("serviceDescription", StringUtils.defaultIfBlank(service.getDescription(), "n/a"));
data.setContent("serviceStartDate", StringUtils.defaultIfBlank(service.getStartDate(), "n/a"));
data.setContent("serviceEndDate", StringUtils.defaultIfBlank(service.getEndDate(), "n/a"));
data.setAttribute("serviceStatusImg", "src", "status-" + service.getStatus().toString().toLowerCase() + ".png");
data.setAttribute("serviceStatusImg", "title", StringUtils.defaultIfBlank(service.getStatusDescription(), service.getStatus().toString()));
data.setAttribute("rawLink", "href", organization.getTechnicalName() + "-" + service.getTechnicalName() + ".properties");
data.setAttribute("rawCheckLink", "href", organization.getTechnicalName() + "-" + service.getTechnicalName() + "-check.xhtml");
if (StringUtils.isNotBlank(service.getLegalWebsite()))
{
data.setAttribute("legalLink", "href", service.getLegalWebsite());
data.setAttribute("legalLinkImg", "class", "");
data.getIdData("legalLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getContactWebsite()))
{
data.setAttribute("contactLink", "href", service.getContactWebsite());
data.setAttribute("contactLinkImg", "class", "");
data.getIdData("contactLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getContactEmail()))
{
data.setAttribute("emailLink", "href", "mailto:" + service.getContactEmail());
data.setAttribute("emailLinkImg", "class", "");
data.getIdData("emailLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getUserDocWebsite()))
{
data.setAttribute("userDocLink", "href", service.getUserDocWebsite());
data.setAttribute("userDocLinkImg", "class", "");
data.getIdData("userDocLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getTechnicalDocWebsite()))
{
data.setAttribute("technicalDocLink", "href", service.getTechnicalDocWebsite());
data.setAttribute("technicalDocLinkImg", "class", "");
data.getIdData("technicalDocLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
//
if (service.isRegistrationNone())
{
data.setAttribute("registrationNoneImg", "class", "");
data.getIdData("registrationNoneImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (service.isRegistrationFree())
{
data.setAttribute("registrationFreeImg", "class", "");
data.getIdData("registrationFreeImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (service.isRegistrationMember())
{
data.setAttribute("registrationMemberImg", "class", "");
data.getIdData("registrationMemberImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (service.isRegistrationClient())
{
data.setAttribute("registrationClientImg", "class", "");
data.getIdData("registrationClientImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
//
data.setContent("softwareName", StringUtils.defaultIfBlank(service.getSoftwareName(), "n/a"));
data.setContent("softwareVersion", StringUtils.defaultIfBlank(service.getSoftwareVersion(), "n/a"));
data.setContent("softwareLicenseName", StringUtils.defaultIfBlank(service.getSoftwareLicenseName(), "n/a"));
if (StringUtils.isNotBlank(service.getSoftwareWebsite()))
{
data.setAttribute("softwareWebsiteLink", "href", service.getSoftwareWebsite());
data.setAttribute("softwareWebsiteLinkImg", "class", "");
data.getIdData("softwareWebsiteLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getSoftwareLicenseWebpage()))
{
data.setAttribute("softwareLicenseLink", "href", service.getSoftwareLicenseWebpage());
data.setAttribute("softwareLicenseLinkImg", "class", "");
data.getIdData("softwareLicenseLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
if (StringUtils.isNotBlank(service.getSoftwareSourceWebsite()))
{
data.setAttribute("softwareSourceLink", "href", service.getSoftwareSourceWebsite());
data.setAttribute("softwareSourceLinkImg", "class", "");
data.getIdData("softwareSourceLinkImg").getAttribute("class").setMode(DisplayMode.REPLACE);
}
//
int graphicIndex = 0;
// data.setContent("fooChart", graphicIndex++,
// LineMonthsChartView.build());
service.getPrefixes();
Metric metric = service.getMetric("metrics.http.hits");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric));
}
metric = service.getMetric("metrics.http.errors");
if ((metric != null) && (!metric.isEmpty()))
{
data.setContent("fooChart", graphicIndex++, BarMonthsChartView.build(metric, ChartColor.RED));
}
//
String content = PresenterUtils.dynamize("/fr/devinsy/statoolinfos/htmlize/service.xhtml", data).toString();
BreadcrumbTrail trail = new BreadcrumbTrail();
trail.add(service.getOrganization().getName(), service.getOrganization().getTechnicalName() + ".xhtml");
trail.add(service.getName(), service.getOrganization().getTechnicalName() + "-" + service.getTechnicalName() + ".xhtml");
result = WebCharterView.build(content, trail);
}
catch (XidynException exception)
{
throw new StatoolInfosException("Error building service page: " + exception.getMessage(), exception);
}
//
return result;
}
/**
* @param service
* @param cache
* @param htmlizeDirectory
* @throws IOException
*/
private static void htmlizeServiceLogo(final Service service, final CrawlCache cache, final File htmlizeDirectory) throws IOException
{
logger.info("Htmlize organization logo.");
File target = new File(htmlizeDirectory, service.getLogoFileName());
File logoFile = cache.restoreFile(service.getLogoURL());
if (logoFile == null)
{
logger.info("BirdGeneratoring cat avatar: {}", target.getAbsoluteFile());
BirdGenerator.buildAvatarTo(service.getTechnicalName(), target);
}
else
{
FileUtils.copyFile(logoFile, target);
}
}
}