/* 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;

import org.ocelotds.annotations.DataService;
import org.ocelotds.annotations.JsCacheRemove;
import org.ocelotds.annotations.JsCacheResult;
import org.ocelotds.annotations.TransientDataService;
import org.ocelotds.core.MethodWithSessionInjection;
import org.ocelotds.core.SessionManager;
import org.ocelotds.core.UpdatedCacheManager;
import org.ocelotds.i18n.Locale;
import org.ocelotds.i18n.ThreadLocalContextHolder;
import java.util.Collection;
import java.util.Map;
import javax.inject.Inject;
import javax.websocket.Session;
import org.ocelotds.annotations.JsTopic;
import org.ocelotds.annotations.JsTopicName;
import org.ocelotds.logger.OcelotLogger;
import org.slf4j.Logger;

/**
 *
 * @author hhfrancois
 */
@DataService(resolver = Constants.Resolver.CDI)
public class OcelotServices {

	@Inject
	@OcelotLogger
	private Logger logger;

	@Inject
	private UpdatedCacheManager updatedCacheManager;
	
	@Inject
	private SessionManager sessionManager;
	
	@MethodWithSessionInjection
	@JsCacheRemove(cls = OcelotServices.class, methodName = "getLocale")
	public void setLocale(Locale locale) {
	}

	@TransientDataService
	public void setLocale(Locale l, Session session) {
		java.util.Locale locale = new java.util.Locale(l.getLanguage(), l.getCountry());
		logger.debug("Receive setLocale({}) call from client.", locale);
		session.getUserProperties().put(Constants.LOCALE, locale);
		ThreadLocalContextHolder.put(Constants.LOCALE, locale);
	}

	@MethodWithSessionInjection
	@JsCacheResult(year = 1)
	public Locale getLocale() {
		return null;
	}

	@TransientDataService
	public Locale getLocale(Session session) {
		logger.debug("Receive getLocale call from client.");
		java.util.Locale locale = (java.util.Locale) session.getUserProperties().get(Constants.LOCALE);
		Locale l = new Locale();
		l.setLanguage(locale.getLanguage());
		l.setCountry(locale.getCountry());
		logger.debug("getLocale() = {}", l);
		return l;
	}
	
	public Collection<String> getOutDatedCache(Map<String, Long> states) {
		return updatedCacheManager.getOutDatedCache(states);
	}
	
	@MethodWithSessionInjection
	public void subscribe(String topic) {
	}

	@MethodWithSessionInjection
	public void unsubscribe(String topic) {
	}

	@JsTopic
	@TransientDataService
	public Integer subscribe(Session session, @JsTopicName(prefix=Constants.Topic.SUBSCRIBERS) String topic) throws IllegalAccessException {
		return sessionManager.registerTopicSession(topic, session);
	}

	@JsTopic
	@TransientDataService
	public Integer unsubscribe(Session session, @JsTopicName(prefix=Constants.Topic.SUBSCRIBERS) String topic) {
		return sessionManager.unregisterTopicSession(topic, session);
	}

	public Integer getNumberSubscribers(String topic) {
		return sessionManager.getNumberSubscribers(topic);
	}
}

@@ -570,42 +570,8 @@
 r;%0D%0A
-import org.ocelotds.i18n.Locale;%0D%0A
 impo
@@ -644,24 +644,50 @@
 ollection;%0D%0A
+import java.util.Locale;%0D%0A
 import java.
@@ -885,24 +885,252 @@
 lotLogger;%0D%0A
+import org.ocelotds.marshallers.LocaleMarshaller;%0D%0Aimport org.ocelotds.marshallers.LocaleUnmarshaller;%0D%0Aimport org.ocelotds.marshalling.annotations.JsonMarshaller;%0D%0Aimport org.ocelotds.marshalling.annotations.JsonUnmarshaller;%0D%0A
 import org.s
@@ -1457,32 +1457,147 @@
 ssionInjection%0D%0A
+%09public void setLocale(@JsonUnmarshaller(LocaleUnmarshaller.class) Locale locale) %7B%0D%0A%09%7D%0D%0A%0D%0A%09@TransientDataService%0D%0A
 %09@JsCacheRemove(
@@ -1648,16 +1648,27 @@
 tLocale%22
+, keys = %7B%7D
 )%0D%0A%09publ
@@ -1689,87 +1689,65 @@
 ale(
-Locale locale) %7B%0D%0A%09%7D%0D%0A%0D%0A%09@TransientDataService%0D%0A%09public void set
+@JsonUnmarshaller(LocaleUnmarshaller.class) 
 Locale
-(L
+ l
 ocale
- l
 , Se
@@ -1768,92 +1768,8 @@
  %7B%0D%0A
-%09%09java.util.Locale locale = new java.util.Locale(l.getLanguage(), l.getCountry());%0D%0A
 %09%09lo
@@ -1993,35 +1993,8 @@
 on%0D%0A
-%09@JsCacheResult(year = 1)%0D%0A
 %09pub
@@ -2057,32 +2057,101 @@
 entDataService%0D%0A
+%09@JsCacheResult(year = 1)%0D%0A%09@JsonMarshaller(LocaleMarshaller.class)%0D%0A
 %09public Locale g
@@ -2241,45 +2241,16 @@
 %0D%0A%09%09
-java.util.Locale locale = (java.util.
+return (
 Loca
@@ -2309,167 +2309,8 @@
 );%0D%0A
-%09%09Locale l = new Locale();%0D%0A%09%09l.setLanguage(locale.getLanguage());%0D%0A%09%09l.setCountry(locale.getCountry());%0D%0A%09%09logger.debug(%22getLocale() = %7B%7D%22, l);%0D%0A%09%09return l;%0D%0A
 %09%7D%0D%0A

use native Locale with LocaleMarshaller and Unmarshaller