/* * 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.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.commons.lang3.math.NumberUtils; import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringsUtils; /** * The Class AgirStatoolUtils. */ public class AgirStatoolUtils { 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"); /** * 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; } }