<commit_before>package core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import models.Alarm;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import play.libs.Json;
import play.libs.F.Callback;
import play.libs.F.Callback0;
import play.mvc.WebSocket;

public class MyWebSocketManager {
	
    // collect all websockets here
    private static List<WebSocket.Out<JsonNode>> connections = new ArrayList<WebSocket.Out<JsonNode>>();

    public static void start(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out){
        
        connections.add(out);
        
        in.onMessage(new Callback<JsonNode>(){
            public void invoke(JsonNode event){
            	//MyWebSocketManager.notifyAll(event);
            }
        });
        
        in.onClose(new Callback0(){ // TODO: possibly remove from the connection list
            public void invoke(){
            	//MyWebSocketManager.notifyAll("A connection closed");
            }
        });
    }
    
    // Iterate connection list and write incoming message
    public static void notifyAll(ObjectNode message){
        for (WebSocket.Out<JsonNode> out : connections) {
            out.write(message);
        }
    }
    
    public static void notifyNewAlarm(Alarm al){
    	// TODO: look into automatic jsonfication
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "addAlarm");
    	ObjectNode  alarm = Json.newObject();
    	jsonNotification.put("alarm", alarm);
		alarm.put("id", al.id);
		alarm.put("type", al.type);
		alarm.put("openingDate", al.openingTime.getTime());
		ObjectNode  callee = Json.newObject();
		alarm.put("callee", callee);
		callee.put("id", al.callee.id);
		callee.put("phoneNumber", al.callee.phoneNumber);

		
		MyWebSocketManager.notifyAll(jsonNotification);
    	
    }
    
    public static void addTimeIconToAlarm(long id){
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "addTimeNotification");
    	jsonNotification.put("alarmId", id);
    	
    	MyWebSocketManager.notifyAll(jsonNotification);
    }
    
    public static void notifyCloseAlarm(Alarm al){
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "removeAlarm");
    	jsonNotification.put("alarmId", al.id);<commit_msg>fixed bug with callee name in websocket<commit_after>package core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import models.Alarm;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import play.libs.Json;
import play.libs.F.Callback;
import play.libs.F.Callback0;
import play.mvc.WebSocket;

public class MyWebSocketManager {
	
    // collect all websockets here
    private static List<WebSocket.Out<JsonNode>> connections = new ArrayList<WebSocket.Out<JsonNode>>();

    public static void start(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out){
        
        connections.add(out);
        
        in.onMessage(new Callback<JsonNode>(){
            public void invoke(JsonNode event){
            	//MyWebSocketManager.notifyAll(event);
            }
        });
        
        in.onClose(new Callback0(){ // TODO: possibly remove from the connection list
            public void invoke(){
            	//MyWebSocketManager.notifyAll("A connection closed");
            }
        });
    }
    
    // Iterate connection list and write incoming message
    public static void notifyAll(ObjectNode message){
        for (WebSocket.Out<JsonNode> out : connections) {
            out.write(message);
        }
    }
    
    public static void notifyNewAlarm(Alarm al){
    	// TODO: look into automatic jsonfication
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "addAlarm");
    	ObjectNode  alarm = Json.newObject();
    	jsonNotification.put("alarm", alarm);
		alarm.put("id", al.id);
		alarm.put("type", al.type);
		alarm.put("openingDate", al.openingTime.getTime());
		ObjectNode  callee = Json.newObject();
		alarm.put("callee", callee);
		callee.put("id", al.callee.id);
		callee.put("phoneNumber", al.callee.phoneNumber);
		callee.put("name", al.callee.name);

		
		MyWebSocketManager.notifyAll(jsonNotification);
    	
    }
    
    public static void addTimeIconToAlarm(long id){
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "addTimeNotification");
    	jsonNotification.put("alarmId", id);
    	
    	MyWebSocketManager.notifyAll(jsonNotification);
    }
    
    public static void notifyCloseAlarm(Alarm al){
    	ObjectNode jsonNotification = Json.newObject();
    	ObjectNode  action = Json.newObject();
    	jsonNotification.put("action", action);
    	action.put("action", "removeAlarm");
    	jsonNotification.put("alarmId", al.id);
{'fraction_non_alphanumeric': 0.11923489452985342, 'fraction_numerical': 0.0007150518412584912, 'mean_word_length': 3.2322239031770046, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2113827', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1648, 'n_words': 339}