/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.webdetails.cdv.notifications;

import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import pt.webdetails.cpf.persistence.Persistable;

/**
 *
 * @author pdpi
 */
public class Alert implements Persistable {

    public enum Level {

        ALL, OK, WARNING, ERROR, CRITICAL
    }
    private Level level;
    private String msg, group;
    private Date timestamp;

    public Alert(Level level, String group, String msg) {
        init(level, group, msg, new Date());
    }

    public Alert(String level, String group, String msg) {
        init(Level.valueOf(level.toUpperCase()), group, msg, new Date());
    }

    private Alert(JSONObject json) {
        try {
            Level lvl = Level.valueOf(json.getString("level").toUpperCase());
            String g = json.getString("group"),
                    m = json.getString("message");
            init(lvl, g, m, new Date());
        } catch (JSONException e) {
        }
    }

    public static Alert fromJSON(JSONObject json) {
        Alert alert = new Alert(json);
        try {
            alert.timestamp = new Date(json.getLong("timestamp"));
        } catch (JSONException jse) {
            // Do nothing
        }
        return alert;
    }

    private void init(Level level, String group, String msg, Date date) {
        this.level = level;
        this.msg = msg;
        this.group = group;
        this.timestamp = date;
    }

    public Level getLevel() {
        return level;
    }

    public String getGroup() {
        return group;
    }

    public String getMessage() {
        return msg;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public String getPersistenceClass() {
        return "Alert";
    }

    public JSONObject getKey() {
        JSONObject json = new JSONObject();
        try {
            json.put("level", level.toString());
            json.put("group", group);
            json.put("timestamp", timestamp.getTime());
        } catch (JSONException jse) {
        }
        return json;
    }

    public JSONObject toJSON() {
        JSONObject json = getKey();
        try {
            json.put("message", msg);
        } catch (JSONException jse) {
        }
        return json;
    }
}

@@ -463,24 +463,30 @@
 g msg, group
+, name
 ;%0A    privat
@@ -539,32 +539,45 @@
 l, String group,
+ String name,
  String msg) %7B%0A 
@@ -593,32 +593,38 @@
 it(level, group,
+ name,
  msg, new Date()
@@ -669,32 +669,45 @@
 l, String group,
+ String name,
  String msg) %7B%0A 
@@ -760,16 +760,22 @@
 , group,
+ name,
  msg, ne
@@ -969,16 +969,65 @@
 roup%22),%0A
+                    n = json.getString(%22group%22),%0A
         
@@ -1093,16 +1093,19 @@
 (lvl, g,
+ n,
  m, new 
@@ -1481,32 +1481,45 @@
 l, String group,
+ String name,
  String msg, Dat
@@ -1576,24 +1576,50 @@
 .msg = msg;%0A
+        this.name = name;%0A
         this
@@ -1909,32 +1909,90 @@
 mestamp;%0A    %7D%0A%0A
+    public String getName() %7B%0A        return name;%0A    %7D%0A%0A
     public Strin
@@ -2226,16 +2226,52 @@
 group);%0A
+            json.put(%22name%22, name);%0A
         

Add name field to Alert