/* * Copyright (C) 2020 Christian Pierre MOMON * * This file is part of AgirStatool, simple key value database. * * AgirStatool 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. * * AgirStatool 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 AgirStatool. If not, see . */ package org.april.agirstatool.core; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.april.agirstatool.charts.DateCount; import org.april.agirstatool.charts.DateCountList; import org.april.agirstatool.charts.DateCountMap; import org.april.agirstatool.core.pages.ProjectPage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringsUtils; /** * The Class AgirStatoolUtils. */ public class AgirStatoolUtils { private static Logger logger = LoggerFactory.getLogger(ProjectPage.class); public static final DateTimeFormatter PATTERN_SHORTDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy"); public static final DateTimeFormatter PATTERN_LONGDATE = DateTimeFormatter.ofPattern("dd/MM/yyyy hh'h'mm"); /** * Normalise week date. * * @param source * the source * @return the local date */ public static LocalDate normaliseWeekDate(final LocalDate source) { LocalDate result; if (source == null) { result = source; } else { result = source.minusDays(source.getDayOfWeek().getValue() - 1); } // return result; } /** * Normalized week count list. * * @param source * the source * @param start * the start * @param end * the end * @return the date count list */ public static DateCountList normalizedWeekCountList(final DateCountList source, final LocalDate start, final LocalDate end) { DateCountList result; result = new DateCountList(); LocalDate normalizedEnd = normaliseWeekDate(end); LocalDate date = normaliseWeekDate(start); int index = source.indexOf(start.format(DateTimeFormatter.ofPattern("yyyy-M-w"))); if (index == -1) { index = 0; } while (date.isBefore(normalizedEnd) || date.isEqual(normalizedEnd)) { String dateToken = date.format(DateTimeFormatter.ofPattern("yyyy-M-w")); long count; if (index < source.size()) { DateCount current = source.get(index); AgirStatoolUtils.logger.info("===> " + dateToken + " " + current.getDate()); if (StringUtils.equals(current.getDate(), dateToken)) { count = current.getCount(); index += 1; } else { count = 0; } } else { count = 0; } result.add(new DateCount(dateToken, count)); date = date.plusWeeks(1); } // return result; } /** * Builds the week created count list. * * @param source * the source * @param start * the start * @return the date count list */ public static DateCountList normalizedWeekCountList(final DateCountMap source, final LocalDate start) { DateCountList result; result = new DateCountList(); LocalDate date = normaliseWeekDate(start); LocalDate end = normaliseWeekDate(LocalDate.now()); long count = 0; while (date.isBefore(end) || date.isEqual(end)) { String dateToken = date.format(DateTimeFormatter.ofPattern("yyyy-M-w")); DateCount current = source.get(dateToken); if (current != null) { AgirStatoolUtils.logger.info("xxx> " + dateToken + " " + current.getDate()); count += current.getCount(); } else { AgirStatoolUtils.logger.info("xxx> " + dateToken + " " + null); } result.add(new DateCount(dateToken, count)); date = date.plusWeeks(1); } // return result; } /** * Gets the current time in long format. * * @return the long */ public static long now() { return new Date().getTime(); } /** * Select stat indicator. * * @param value * the value * @return the string */ public static String selectStatIndicator(final long value) { String result; if (value < 10) { result = null; } else if (value < 20) { result = "caution"; } else if (value < 50) { result = "warning"; } else { result = "alert"; } // return result; } /** * Select unassigned indicator. * * @param value * the value * @return the string */ public static String selectUnassignedIndicator(final long value) { String result; if (value == 0) { result = null; } else { result = "alert"; } // return result; } /** * To human long. * * @param value * the value * @return the string */ public static String toHumanLong(final LocalDateTime value) { String result; result = toHumanLong(value, null); // return result; } /** * To human long. * * @param value * the value * @param defaultValue * the default value * @return the string */ public static String toHumanLong(final LocalDateTime value, final String defaultValue) { String result; if (value == null) { result = null; } else { result = value.format(PATTERN_LONGDATE); } // return result; } /** * To human short. * * @param value * the value * @return the string */ public static String toHumanShort(final LocalDateTime value) { String result; result = toHumanShort(value, null); // return result; } /** * @param value * @param defaultValue * @return */ public static String toHumanShort(final LocalDateTime value, final String defaultValue) { String result; if (value == null) { result = null; } else { result = value.format(PATTERN_SHORTDATE); } // return result; } public static Integer toInteger(final String value) { Integer result; if ((value == null) || (!NumberUtils.isDigits(value))) { result = null; } else { result = Integer.parseInt(value); } // return result; } /** * To Json numbers. * * @param source * the source * @return the string */ public static String toJSonNumbers(final StringList source) { String result; result = StringsUtils.toString(source, "[", ",", "]"); // return result; } /** * To Json strings. * * @param source * the source * @return the string */ public static String toJSonStrings(final StringList source) { String result; StringList target = new StringList(); target.append("["); for (String string : source) { target.append("'"); target.append(string); target.append("'"); target.append(","); } target.removeLast(); target.append("]"); result = target.toString(); // return result; } /** * To date time. * * @param source * the source * @return the local date time */ public static LocalDateTime toLocaleDateTime(final java.sql.Timestamp source) { LocalDateTime result; if (source == null) { result = null; } else { result = LocalDateTime.ofEpochSecond(source.getTime() / 1000, 0, ZoneOffset.UTC); } // return result; } }