package org.fiteagle.core.repository.dm;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.jms.JMSContext;
import javax.jms.Message;
import javax.jms.Topic;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.fiteagle.api.core.IMessageBus;
import org.fiteagle.api.core.MessageUtil;
import org.fiteagle.api.core.OntologyModelUtil;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * This Class sends the ontology Model over message bus on startup
 */
public class ResourceRepoMDBSender implements ServletContextListener{
 
  @Inject
  private JMSContext context;
  @Resource(mappedName = IMessageBus.TOPIC_CORE_NAME)
  private Topic topic;
  
  private static Logger LOGGER = Logger.getLogger(ResourceRepoMDBSender.class.toString());
  
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    try {
      Model model = OntologyModelUtil.loadModel("ontologies/fiteagle/ontology.ttl", IMessageBus.SERIALIZATION_TURTLE);
      Model messageModel = MessageUtil.createMsgInform(model);
      String serializedRDF = MessageUtil.serializeModel(messageModel);
      
      final Message eventMessage = this.context.createMessage();
      
      eventMessage.setStringProperty(IMessageBus.METHOD_TYPE, IMessageBus.TYPE_INFORM);
      eventMessage.setStringProperty(IMessageBus.RDF, serializedRDF);
      eventMessage.setStringProperty(IMessageBus.SERIALIZATION, IMessageBus.SERIALIZATION_DEFAULT);
      LOGGER.log(Level.INFO, "Sending Ontology Model as Inform Message");
      this.context.createProducer().send(topic, eventMessage);
    } catch (Exception e) {
      LOGGER.log(Level.SEVERE, e.getMessage());
    }
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
  }
}

@@ -372,16 +372,70 @@
 ageBus;%0A
+import org.fiteagle.api.core.MessageBusOntologyModel;%0A
 import o
@@ -556,16 +556,55 @@
 l.Model;
+%0Aimport com.hp.hpl.jena.vocabulary.RDF;
 %0A%0A/**%0A *
@@ -1169,62 +1169,115 @@
     
-M
+m
 odel
- messageModel = MessageUtil.createMsgInform(model
+.add(MessageBusOntologyModel.internalMessage, RDF.type, MessageBusOntologyModel.propertyFiteagleInform
 );%0A 
@@ -1336,15 +1336,8 @@
 el(m
-essageM
 odel

update for new api version