mx.org.cedn.avisosconagua.mongo.Statistics.java Source code

Java tutorial

Introduction

Here is the source code for mx.org.cedn.avisosconagua.mongo.Statistics.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Mxico 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.mongo;

import com.mongodb.BasicDBObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Wrapper to store advice information.
 * @author serch
 */
public class Statistics {

    /** Advice number **/
    private String aviso = "";
    /** Issue date **/
    private String fecha = "";
    /** Latitude **/
    private String latitud = "";
    /** Longitude **/
    private String longitud = "";
    /** Distance **/
    private String distancia = "";
    /** Wind **/
    private String viento = "";
    /** Category **/
    private String categoria = "";
    /** Development **/
    private String avance = "";

    /**
     * Constructor. Creates a new Statistics object with the given information
     * @param aviso advice number
     * @param fecha issue date
     * @param latitud event latitude
     * @param longitud event longitude
     * @param distancia event distance
     * @param viento event winds
     * @param categoria event category
     * @param avance event development
     */
    public Statistics(String aviso, String fecha, String latitud, String longitud, String distancia, String viento,
            String categoria, String avance) {
        this.aviso = aviso;
        this.fecha = fecha;
        this.latitud = latitud;
        this.longitud = longitud;
        this.distancia = distancia;
        this.viento = viento;
        this.categoria = categoria;
        this.avance = avance;
    }

    /**
     * Constructor. Creates a new Statistics from the given BasicDBObject
     * @param aviso DBObject for the advice
     */
    public Statistics(BasicDBObject aviso) {
        BasicDBObject capInfo = (BasicDBObject) aviso.get("capInfo");
        BasicDBObject init = (BasicDBObject) aviso.get("init");
        this.aviso = capInfo.getString("issueNumber");
        String tmp = init.getString("issueLocalTime");
        StringBuilder builder = new StringBuilder();
        int i = 0;
        for (; i < tmp.length(); i++) {
            if ((tmp.charAt(i) >= '0' && tmp.charAt(i) <= '9') || tmp.charAt(i) == ':') {
                builder.append(tmp.charAt(i));
            }
            if (tmp.charAt(i) == 'a' || tmp.charAt(i) == 'A') {
                builder.append(" am");
                break;
            }
            if (tmp.charAt(i) == 'p' || tmp.charAt(i) == 'P') {
                builder.append(" pm");
                break;
            }
            if (tmp.charAt(i) == 'h' || tmp.charAt(i) == 'H' || tmp.charAt(i) == '(') {
                break;
            }
        }
        String pattern = "dd/MM/yyyyHH:mm";
        String hora = builder.toString();
        if (hora.contains("am") || hora.contains("pm")) {
            pattern = "dd/MM/yyyyhh:mm aa";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String fechaStr = capInfo.getString("issueDate") + hora;
        Date date = new Date();
        try {
            date = sdf.parse(fechaStr);
            //System.out.println("fecha: " + fechaStr + " : " + date);
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
        SimpleDateFormat sdf2 = new SimpleDateFormat("dd MMM '/' HH:mm", Locale.forLanguageTag("es-mx"));
        this.fecha = sdf2.format(date);
        this.latitud = init.getString("eventCLat");
        this.longitud = init.getString("eventCLon");
        this.distancia = init.getString("eventDistance");
        this.viento = init.getString("eventWindSpeedSust") + " / " + init.getString("eventWindSpeedMax");
        this.categoria = capInfo.getString("eventCategory");
        this.avance = init.getString("eventCurrentPath");
    }

    /**
     * Gets the advice field
     * @return 
     */
    public String getAviso() {
        return aviso;
    }

    /**
     * Gets the date field
     * @return 
     */
    public String getFecha() {
        return fecha;
    }

    /**
     * Gets the latitud field
     * @return 
     */
    public String getLatitud() {
        return latitud;
    }

    /**
     * Gets the longitud field
     * @return 
     */
    public String getLongitud() {
        return longitud;
    }

    /**
     * Gets the distancia field
     * @return 
     */
    public String getDistancia() {
        return distancia;
    }

    /**
     * Gets the viento field
     * @return 
     */
    public String getViento() {
        return viento;
    }

    /**
     * Gets the categoria field
     * @return 
     */
    public String getCategoria() {
        return categoria;
    }

    /**
     * Gets the avance field
     * @return 
     */
    public String getAvance() {
        return avance;
    }

    /**
     * Creates a string representation of the Statistics object.
     * @return JSON-like string for the object
     */
    public String toString() {
        return "{\"aviso\":\"" + aviso + "\",\"fecha\":\"" + fecha + "\",\"latitud\":\"" + latitud
                + "\",\"longitud\":\"" + longitud + "\",\"distancia\":\"" + escapeQuote(distancia)
                + "\",\"viento\":\"" + viento + "\",\"categoria\":\"" + escapeQuote(categoria) + "\",\"avance\":\""
                + escapeQuote(avance) + "\"}";
    }

    /**
     * Escapes quote character in a String
     * @param string source string
     * @return string with escaped quotes
     */
    private String escapeQuote(String string) {
        return string.replaceAll("\"", "\\\\\"");
    }

}