# Path: gloss/gloss_service_base.py
# class GlossService(object):
#     def __init__(self, receiver, importer, subscribers, issuing_source):
#         self.receiver = receiver
#         self.importer = importer
#         self.subscribers = subscribers
#         self.issuing_source = issuing_source
# 
#     def notify_subscribers(self, message_container):
#         for subscriber in self.subscribers:
#             subscriber(message_container, self)
# 
# Path: gloss/receivers/mllp_multi_service.py
# class MultiMLLPServer(object):
#     def __init__(self, ports, host):
#         self.ports = ports
#         self.host = host
# 
#     def make_service(self, gloss_service):
#         """Construct a server using MLLPFactory.
# 
#         :rtype: :py:class:`twisted.application.internet.StreamServerEndpointService`
#         """
#         from twisted.internet import reactor
#         from txHL7.mllp import MLLPFactory
#         factory = MLLPFactory(OhcReceiver(gloss_service))
#         multi_service = MultiService()
# 
#         for port_number in self.ports:
#             port = "tcp:interface={0}:port={1}".format(self.host, port_number,)
#             endpoint = endpoints.serverFromString(reactor, port)
#             server = internet.StreamServerEndpointService(endpoint, factory)
#             server.setName(u"gloss-mllp-{0}".format(port_number))
#             multi_service.addService(server)
#         return multi_service
# 
# Path: gloss/importers/hl7_importer.py
# class HL7Importer(SafelImporter):
#     def import_message(self, msg, gloss_service):
#         hl7 = hl7_translator.HL7Translator.translate(msg)
#         importer = HL7TranslationToMessage.get_for_hl7(hl7)
#         if importer:
#             processed_messages = importer.import_hl7()
#             hospital_number = importer.get_hospital_number()
#             return messages.MessageContainer(
#                 messages=processed_messages,
#                 hospital_number=hospital_number,
#                 issuing_source=gloss_service.issuing_source
#             )
# 
# Path: sites/uch/subscribe/production.py
# class NotifyOpalWhenSubscribed(BaseSubscriber):
#     """ checks whether we're subscribed, sends a message to an opal
#         application if so
#     """
#     def notify(self, message_container, gloss_service):
#         sub_classes = itersubclasses(self.__class__)
#         message_classes = set(i.__class__ for i in message_container.messages)
# 
#         for sub_class in sub_classes:
#             cares_about = getattr(sub_class, "message_types", [])
#             relevent = message_classes.intersection(set(cares_about))
#             if relevent:
#                 sc = sub_class()
#                 sc.notify(message_container, gloss_service)
# 
# Path: gloss/subscribers/send_all_messages.py
# class SendAllMessages(BaseSubscriber):
#     def __init__(self, *args, **kwargs):
#         self.end_point = kwargs.pop("end_point")
#         super(SendAllMessages, self).__init__(*args, **kwargs)
# 
#     @atomic_method
#     def notify(self, message_container, gloss_service, session):
#         send_to_opal(message_container, self.end_point)


from gloss.gloss_service_base import GlossService
from gloss.receivers.mllp_multi_service import MultiMLLPServer
from gloss.importers.hl7_importer import HL7Importer
from sites.uch.subscribe.production import NotifyOpalWhenSubscribed
from gloss.subscribers.send_all_messages import SendAllMessages


GLOSS_SERVICE = GlossService(
    receiver=MultiMLLPServer(ports=[2574, 2575], host="localhost").make_service,
    importer=HL7Importer().import_and_notify,
    subscribers=[
        NotifyOpalWhenSubscribed().notify,

# Path: gloss/gloss_service_base.py
# class GlossService(object):
#     def __init__(self, receiver, importer, subscribers, issuing_source):
#         self.receiver = receiver
#         self.importer = importer
#         self.subscribers = subscribers
#         self.issuing_source = issuing_source
# 
#     def notify_subscribers(self, message_container):
#         for subscriber in self.subscribers:
#             subscriber(message_container, self)
# 
# Path: gloss/receivers/mllp_multi_service.py
# class MultiMLLPServer(object):
#     def __init__(self, ports, host):
#         self.ports = ports
#         self.host = host
# 
#     def make_service(self, gloss_service):
#         """Construct a server using MLLPFactory.
# 
#         :rtype: :py:class:`twisted.application.internet.StreamServerEndpointService`
#         """
#         from twisted.internet import reactor
#         from txHL7.mllp import MLLPFactory
#         factory = MLLPFactory(OhcReceiver(gloss_service))
#         multi_service = MultiService()
# 
#         for port_number in self.ports:
#             port = "tcp:interface={0}:port={1}".format(self.host, port_number,)
#             endpoint = endpoints.serverFromString(reactor, port)
#             server = internet.StreamServerEndpointService(endpoint, factory)
#             server.setName(u"gloss-mllp-{0}".format(port_number))
#             multi_service.addService(server)
#         return multi_service
# 
# Path: gloss/importers/hl7_importer.py
# class HL7Importer(SafelImporter):
#     def import_message(self, msg, gloss_service):
#         hl7 = hl7_translator.HL7Translator.translate(msg)
#         importer = HL7TranslationToMessage.get_for_hl7(hl7)
#         if importer:
#             processed_messages = importer.import_hl7()
#             hospital_number = importer.get_hospital_number()
#             return messages.MessageContainer(
#                 messages=processed_messages,
#                 hospital_number=hospital_number,
#                 issuing_source=gloss_service.issuing_source
#             )
# 
# Path: sites/uch/subscribe/production.py
# class NotifyOpalWhenSubscribed(BaseSubscriber):
#     """ checks whether we're subscribed, sends a message to an opal
#         application if so
#     """
#     def notify(self, message_container, gloss_service):
#         sub_classes = itersubclasses(self.__class__)
#         message_classes = set(i.__class__ for i in message_container.messages)
# 
#         for sub_class in sub_classes:
#             cares_about = getattr(sub_class, "message_types", [])
#             relevent = message_classes.intersection(set(cares_about))
#             if relevent:
#                 sc = sub_class()
#                 sc.notify(message_container, gloss_service)
# 
# Path: gloss/subscribers/send_all_messages.py
# class SendAllMessages(BaseSubscriber):
#     def __init__(self, *args, **kwargs):
#         self.end_point = kwargs.pop("end_point")
#         super(SendAllMessages, self).__init__(*args, **kwargs)
# 
#     @atomic_method
#     def notify(self, message_container, gloss_service, session):
#         send_to_opal(message_container, self.end_point)


# Path: sites/uch/gloss_service.py
from gloss.gloss_service_base import GlossService
from gloss.receivers.mllp_multi_service import MultiMLLPServer
from gloss.importers.hl7_importer import HL7Importer
from sites.uch.subscribe.production import NotifyOpalWhenSubscribed
from gloss.subscribers.send_all_messages import SendAllMessages



GLOSS_SERVICE = GlossService(
    receiver=MultiMLLPServer(ports=[2574, 2575], host="localhost").make_service,
    importer=HL7Importer().import_and_notify,
    subscribers=[
        NotifyOpalWhenSubscribed().notify,

        SendAllMessages(end_point="http://127.0.0.1:8000/glossapi/v0.1/glossapi/").notify