From 5822c9260a3b8f624929b987ca9ccb18a1c529e4 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 25 Jan 2021 02:00:48 +0100 Subject: [PATCH] Improved date display in organization page. --- src/fr/devinsy/statoolinfos/core/Factory.java | 1 + .../statoolinfos/core/Organization.java | 67 ++++++++++ .../statoolinfos/core/StatoolInfosUtils.java | 124 +++++++++++++++++- .../htmlize/OrganizationPage.java | 17 ++- .../statoolinfos/htmlize/organization.xhtml | 3 +- .../statoolinfos/properties/PathProperty.java | 2 +- 6 files changed, 210 insertions(+), 4 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/core/Factory.java b/src/fr/devinsy/statoolinfos/core/Factory.java index 6f82d9a..8e918b7 100644 --- a/src/fr/devinsy/statoolinfos/core/Factory.java +++ b/src/fr/devinsy/statoolinfos/core/Factory.java @@ -175,6 +175,7 @@ public class Factory Organization organization = loadOrganization(inputURL, cache); if (organization != null) { + organization.setFederation(result); result.getOrganizations().add(organization); } } diff --git a/src/fr/devinsy/statoolinfos/core/Organization.java b/src/fr/devinsy/statoolinfos/core/Organization.java index 008c7f3..28770df 100644 --- a/src/fr/devinsy/statoolinfos/core/Organization.java +++ b/src/fr/devinsy/statoolinfos/core/Organization.java @@ -21,6 +21,7 @@ package fr.devinsy.statoolinfos.core; import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.time.LocalDate; import java.time.LocalDateTime; import org.apache.commons.codec.digest.DigestUtils; @@ -62,6 +63,24 @@ public class Organization extends PathPropertyList this.services = new Services(); } + /** + * Gets the age. + * + * @return the age + */ + public String getAge() + { + String result; + + LocalDate startDate = StatoolInfosUtils.parseDate(getStartDate()); + LocalDate endDate = StatoolInfosUtils.parseDate(getEndDate()); + + result = StatoolInfosUtils.toHumanDuration(startDate, endDate); + + // + return result; + } + /** * Gets the contact email. * @@ -287,6 +306,54 @@ public class Organization extends PathPropertyList return result; } + /** + * Gets the member age. + * + * @return the member age + */ + public String getMemberAge() + { + String result; + + LocalDate startDate = StatoolInfosUtils.parseDate(getMemberStartDate()); + LocalDate endDate = StatoolInfosUtils.parseDate(getMemberEndDate()); + + result = StatoolInfosUtils.toHumanDuration(startDate, endDate); + + // + return result; + } + + /** + * Gets the member end date. + * + * @return the member end date + */ + public String getMemberEndDate() + { + String result; + + result = get("organization.memberof." + this.federation.getName() + ".enddate"); + + // + return result; + } + + /** + * Gets the member start date. + * + * @return the member start date + */ + public String getMemberStartDate() + { + String result; + + result = get("organization.memberof." + this.federation.getName() + ".startdate"); + + // + return result; + } + /** * Gets the mobilizon webpage. * diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java index 7db42c7..fb07214 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfosUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Christian Pierre MOMON + * Copyright (C) 2020-2021 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -25,8 +25,10 @@ import java.net.URL; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.Period; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Iterator; import java.util.Locale; @@ -150,6 +152,49 @@ public class StatoolInfosUtils return new Date().getTime(); } + /** + * Parses the date. + * + * @param date + * the date + * @return the local date + */ + public static LocalDate parseDate(final String date) + { + LocalDate result; + + try + { + if (date == null) + { + result = null; + } + else if (date.matches("^\\d{1,2}/\\d{1,2}/\\d{4}$")) + { + result = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd/MM/yyyy")); + } + else if (date.matches("^\\d{4}-\\d{1,2}-\\d{1,2}/$")) + { + result = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")); + } + else if (date.matches("^\\d{1,2}/\\d{4}$")) + { + result = LocalDate.parse("01/" + date, DateTimeFormatter.ofPattern("dd/MM/yyyy")); + } + else + { + result = null; + } + } + catch (DateTimeParseException exception) + { + result = null; + } + + // + return result; + } + /** * Split day values. * @@ -261,6 +306,83 @@ public class StatoolInfosUtils return result; } + /** + * To duration. + * + * @param startDate + * the start date + * @param endDate + * the end date + * @return the string + */ + public static String toHumanDuration(final LocalDate startDate, final LocalDate endDate) + { + String result; + + if ((startDate == null) && (endDate == null)) + { + result = null; + } + else if ((startDate == null) && (endDate != null)) + { + result = toHumanDuration(LocalDate.now(), endDate); + } + else if ((startDate != null) && (endDate == null)) + { + result = toHumanDuration(startDate, LocalDate.now()); + } + else if ((startDate != null) && (endDate != null)) + { + Period period = Period.between(startDate, LocalDate.now()); + + if (period.getYears() == 0) + { + if (period.getMonths() == 0) + { + result = String.format("%d jours", period.getDays()); + } + else + { + result = String.format("%d mois", period.getMonths()); + } + } + else if (period.getYears() == 1) + { + period.minusYears(1); + + if (period.getMonths() == 0) + { + result = "1 an"; + } + else + { + result = String.format("1 an et %d mois", period.getMonths()); + } + } + else + { + long years = period.getYears(); + period.minusYears(period.getYears()); + + if (period.getMonths() == 0) + { + result = years + " ans"; + } + else + { + result = String.format("%d ans et %d mois", years, period.getMonths()); + } + } + } + else + { + result = null; + } + + // + return result; + } + /** * To human long. * diff --git a/src/fr/devinsy/statoolinfos/htmlize/OrganizationPage.java b/src/fr/devinsy/statoolinfos/htmlize/OrganizationPage.java index e5aaa85..b75175f 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/OrganizationPage.java +++ b/src/fr/devinsy/statoolinfos/htmlize/OrganizationPage.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Christian Pierre MOMON + * Copyright (C) 2020-2021 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -113,8 +113,23 @@ public class OrganizationPage data.setEscapedAttribute("organizationURL", "href", organization.getWebsite()); data.setEscapedContent("organizationDescription", organization.get("organization.description")); + data.setContent("organizationStartDate", StringUtils.defaultIfBlank(organization.getStartDate(), "n/a")); data.setContent("organizationEndDate", StringUtils.defaultIfBlank(organization.getEndDate(), "n/a")); + data.setContent("organizationAge", StringUtils.defaultIfBlank(organization.getAge(), "n/a")); + if (StringUtils.isBlank(organization.getEndDate())) + { + data.setAttribute("organizationEndDateData", "style", "display: none;"); + } + + data.setContent("organizationMemberStartDate", StringUtils.defaultIfBlank(organization.getMemberStartDate(), "n/a")); + data.setContent("organizationMemberEndDate", StringUtils.defaultIfBlank(organization.getMemberEndDate(), "n/a")); + data.setContent("organizationMemberAge", StringUtils.defaultIfBlank(organization.getMemberAge(), "n/a")); + if (StringUtils.isBlank(organization.getMemberEndDate())) + { + data.setAttribute("organizationMemberEndDateData", "style", "display: none;"); + } + data.setContent("serviceCount", organization.getServices().size()); data.setAttribute("rawLink", "href", organization.getTechnicalName() + ".properties"); diff --git a/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml b/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml index b27150b..31f6cfb 100644 --- a/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml +++ b/src/fr/devinsy/statoolinfos/htmlize/organization.xhtml @@ -22,7 +22,8 @@

Description absente…

-
Date d'entrée : n/a – Date de sortie : n/a
+
Date d'entrée : n/a – Date de sortie : n/a (n/a)
+
Date de création : n/a – Date d'arrêt : n/a (n/a)