/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocelotds.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.Session;

/**
 *
 * @author hhfrancois
 */
@ApplicationScoped
public class SessionManager {
	Map<String, Collection<Session>> httpid_wss = new HashMap<>();
	Map<String, String> wsid_httpid = new HashMap<>();
	
	public void linkWsToHttp(Session ws, String httpid) {
		if(ws != null && httpid != null) {
			Collection<Session> wss = getOpenSessions(httpid);
			wss.add(ws);
			wsid_httpid.put(ws.getId(), httpid);
			httpid_wss.put(httpid, wss);
		}
	}

	public void unlinkWs(String wsid) {
		if(wsid != null && wsid_httpid.containsKey(wsid)) {
			String httpid = wsid_httpid.get(wsid);
			Collection<Session> wss = getOpenSessions(httpid);
			if(!wss.isEmpty()) {
				httpid_wss.put(httpid, wss);
			} else {
				httpid_wss.remove(httpid);
			}
		}
	}
	
	Collection<Session> getOpenSessions(String httpid) {
		Collection<Session> wss = new ArrayList();
		if(httpid_wss.containsKey(httpid)) {
			Collection<Session> befores = httpid_wss.get(httpid);
			for (Session before : befores) {
				if(before.isOpen()) {
					wss.add(before);
				} else {
					wsid_httpid.remove(before.getId());
				}
			}
		}
		return wss;
	}
	
	public Collection<Session> getUserSessions(String httpid) {
		return Collections.unmodifiableCollection(getOpenSessions(httpid));
	}
	
	
}

@@ -356,24 +356,61 @@
 a.util.Map;%0A
+import java.util.function.Predicate;%0A
 import javax
@@ -479,16 +479,138 @@
 Session;
+%0Aimport org.ocelotds.web.ws.predicates.IsOpenAndDifferentPredicate;%0Aimport org.ocelotds.web.ws.predicates.IsOpenPredicate;
 %0A%0A/**%0A *
@@ -803,16 +803,69 @@
 %3C%3E();%0A%09%0A
+%09Predicate%3CSession%3E ISOPEN = new IsOpenPredicate();%0A%0A
 %09public 
@@ -976,36 +976,37 @@
 n%3E wss = get
-Open
+Valid
 Sessions
 (httpid);%0A%09%09
@@ -989,31 +989,49 @@
 alidSessions
-(httpid
+ElseRemove(httpid, ISOPEN
 );%0A%09%09%09wss.ad
@@ -1239,19 +1239,22 @@
 _httpid.
-get
+remove
 (wsid);%0A
@@ -1260,172 +1260,267 @@
 %0A%09%09%09
-Collection%3CSession%3E wss = getOpenSessions(httpid);%0A%09%09%09if(!wss.isEmpty()) %7B%0A%09%09%09%09httpid_wss.put(httpid, wss);%0A%09%09%09%7D else %7B%0A%09%09%09%09httpid_wss.remove(httpid);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09
+getValidSessionsElseRemove(httpid, new IsOpenAndDifferentPredicate(wsid));%0A%09%09%7D%0A%09%7D%0A%09%0A%09/**%0A%09 * Return all sessions for httpid and for the predicate%0A%09 * if some session negate the predicate, its are removed%0A%09 * @param httpid%0A%09 * @param predicat%0A%09 * @return %0A%09 */
 %0A%09Co
@@ -1540,28 +1540,29 @@
 ion%3E get
-Open
+Valid
 Sessions
 (String 
@@ -1545,32 +1545,42 @@
 getValidSessions
+ElseRemove
 (String httpid) 
@@ -1573,24 +1573,53 @@
 tring httpid
+, Predicate%3CSession%3E predicat
 ) %7B%0A%09%09Collec
@@ -1798,22 +1798,28 @@
 %09if(
-before.isOpen(
+predicat.test(before
 )) %7B
@@ -1841,16 +1841,62 @@
 efore);%0A
+%09%09%09%09%09wsid_httpid.put(before.getId(), httpid);%0A
 %09%09%09%09%7D el
@@ -1952,16 +1952,120 @@
 %09%7D%0A%09%09%09%7D%0A
+%09%09%09if(wss.isEmpty()) %7B%0A%09%09%09%09httpid_wss.remove(httpid);%0A%09%09%09%7D else %7B%0A%09%09%09%09httpid_wss.put(httpid, wss);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09re
@@ -2191,20 +2191,21 @@
 (get
-Open
+Valid
 Sessions
 (htt
@@ -2200,23 +2200,41 @@
 Sessions
-(httpid
+ElseRemove(httpid, ISOPEN
 ));%0A%09%7D%0A%09

fix linkWs and unlinkWS