/*
* The MIT License (MIT)
*
* Copyright (c) 2014 México Abierto
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* For more information visit https://github.com/mxabierto/avisos.
*/
package mx.org.cedn.avisosconagua.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.mongodb.BasicDBObject;
/**
* Utility class with common operation methods.
* @author hasdai
*/
public class Utils {
public static final TimeZone tz = TimeZone.getTimeZone("America/Mexico_City");
public static final TimeZone utc = TimeZone.getTimeZone("UTC");
public static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
private static final SimpleDateFormat sdfjd = new SimpleDateFormat("dd/MM/yyyy");
public static final SimpleDateFormat diaformater = new SimpleDateFormat("dd 'de' MMMMM 'del' yyyy", Locale.forLanguageTag("es-mx"));
public static final SimpleDateFormat horaformater = new SimpleDateFormat("HH:mm", Locale.forLanguageTag("es-mx"));
public static final SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssXXX");
public static final SimpleDateFormat orderformater = new SimpleDateFormat("YYYY-MM-dd HH:mm");
static {
diaformater.setTimeZone(tz);
horaformater.setTimeZone(tz);
isoformater.setTimeZone(tz);
sdfjd.setTimeZone(tz);
sdf.setTimeZone(tz);
}
/**
* Parses a date string.
* @param date date String
* @return Date object from the parsed string
* @throws ParseException
*/
public static Date getDateFromString(String date) throws ParseException {
return sdfjd.parse(date);
}
/**
* Gets a formetted string representation for the day and time of a date.
* @param date date string
* @return string representation for the day and time
*/
public static String getDiaText(String date) {
String fecha = "";
try {
Date fec = getDateFromString(date);
fecha = diaformater.format(fec);
} catch (ParseException pe) {
pe.printStackTrace();
}
return fecha;
}
/**
* ISO formats a date string.
* @param date date string
* @return date string in ISO format
*/
public static String getISODate(String date){
String fecha = "";
try {
Date fec = sdf.parse(date);
fecha = isoformater.format(fec);
} catch (ParseException pe){
pe.printStackTrace();
}
return fecha;
}
/**
* Formats a date string for sort purposes.
* @param date date string
* @return formatted date string
*/
public static String getOrderDate(String date){
String fecha = "";
try {
Date fec = sdf.parse(date);
fecha = orderformater.format(fec);
} catch (ParseException pe){
pe.printStackTrace();
}
return fecha;
}
/**
* Gets the string title for the HTML representation of the advice
* @param type advice type. One of pacdp|atldp|pacht|atlht.
* @param dist distance threshold
* @return string title for the HTML advice
*/
public static String getTituloBoletinHtml(String type, String dist){
String titulo = "";
switch (dist) {
case "lessthan500km":
titulo = "Aviso ";
break;
case "morethan500km":
titulo = "Aviso ";
break;
case "land":
titulo = "Aviso ";
break;
}
switch (type) {
case "pacdp":
titulo = titulo + "de zonas de baja presión en el Pacífico con potencial ciclónico";
break;
case "pacht":
titulo = titulo + "de Ciclón Tropical en el Pacífico";
break;
case "atldp":
titulo = titulo + "de zonas de baja presión en el Atlántico con potencial ciclónico";
break;
case "atlht":
titulo = titulo + "de Ciclón Tropical en el Atlántico";
break;
}
return titulo;
}
/**
* Gets the advice title for the generated files.
* @param type advice type. One of pacdp|atldp|pacht|atlht
* @return advice title
*/
public static String getTituloBoletin(String type){
String titulo = "";
switch (type) {
case "pacdp":
titulo = "Aviso de zonas de baja presión en el Pacífico con potencial ciclónico";
break;
case "pacht":
titulo = "Aviso de Ciclón Tropical en el Pacífico";
break;
case "atldp":
titulo = "Aviso de zonas de baja presión en el Atlántico con potencial ciclónico";
break;
case "atlht":
titulo = "Aviso de Ciclón Tropical en el Atlántico";
break;
}
return titulo;
}
/**
* Gets the region from the advice type
* @param type advice type. One of pacdp|atldp|pacht|atlht
* @return String regionName
*/
public static String getRegionFromAdviceType(String type){
if (type.startsWith("pac")) {
return "el Pacífico";
} else if (type.startsWith("atl")) {
return "el Atlántico";
}
return "México";
}
/**
* Gets a String value from a map or an empty String.
*
* @param map Map to get value from
* @param key Key
* @return String value for the given key or an empty String.
*/
public static String getValidFieldFromHash(HashMap<String, String> map, String key) {
String ret = map.get(key);
if (ret == null) {
ret = "";
}
return ret;
}
/**
* Escapes quotes in a string using the corresponding HTML entity.
* @param string original string
* @return escaped string
*/
public static String escapeQuote(String string){
return string.replaceAll("\"", """);
}
/**
* Tokenizes a string splitting in base to the provided regexp.
* @param source Source string to tokenize.
* @param regex Regular expression for token matching.
* @return List of tokens.
*/
public static ArrayList<String> tokenize(String source, String regex) {
ArrayList<String> ret = new ArrayList<String>();
Pattern logEntry = Pattern.compile(regex, Pattern.DOTALL);
Matcher matchPattern = logEntry.matcher(source);
while(matchPattern.find()) {
ret.add(matchPattern.group(1));
}
return ret;
}
/**
* Finds out whether advisory has areas defined or nicht
* @param aviso Yet another DB object (typically an advisory)
* @return Boolean can I has Areaz?
*/
public static Boolean canIHasAreaz(BasicDBObject aviso) {
if (null != aviso) {
BasicDBObject init = (BasicDBObject) aviso.get("init");
for (String key : init.keySet())
if (key.startsWith("area-"))
return true;
}
return false;
}
}