package won.protocol.message;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import won.protocol.util.RdfUtils;
import won.protocol.util.WonRdfUtils;


/**
 * User: ypanchenko
 * Date: 04.08.2014
 */
public class WonMessage
{

  private Dataset messageContent;
  //private Model messageMetadata;
  //private URI messageEventURI;
  private MessageEvent messageEvent;


  //private Resource msgBnode;
  // private Signature signature;


  public WonMessage() {
    initMessageMetadata();
  }

  public WonMessage(MessageEvent messageEvent, Dataset messageContent) {
    //this.messageEventURI = messageEventURI;
    this.messageEvent = messageEvent;
    this.messageContent = messageContent;
    //initMessageMetadata();
  }

  private void initMessageMetadata() {
    Model messageMetadata = ModelFactory.createDefaultModel();
    messageMetadata.setNsPrefix(WONMSG.DEFAULT_PREFIX, WONMSG.BASE_URI);
    Resource msgBnode = messageMetadata.createResource();
    Resource messageEventResource = messageMetadata.createResource(messageEvent.getMessageURI().toString());
    msgBnode.addProperty(WONMSG.MESSAGE_POINTER_PROPERTY, messageEventResource);
    //TODO own message event signature
  }


  public Dataset getMessageContent() {
    return messageContent;
  }

  public Model getMessageContent(String contentResourceUri) {
    String ngName = getNamedGraphNameForUri(contentResourceUri);
    return messageContent.getNamedModel(ngName);
  }

  public Dataset getMessageWithSignature(String contentResourceUri) {
    Dataset dataset = DatasetFactory.createMem();
    String ngName = getNamedGraphNameForUri(contentResourceUri);
    dataset.addNamedModel(ngName, messageContent.getNamedModel(ngName));
    RdfUtils.addPrefixMapping(dataset.getDefaultModel(), messageContent.getNamedModel(ngName));
    RdfUtils.addPrefixMapping(dataset.getDefaultModel(), messageContent.getDefaultModel());
    //TODO signature into default graph
    return dataset;
  }

  private String getNamedGraphNameForUri(final String resourceUri) {
    String ngName = resourceUri;
    // we commonly use resource url + #data for the name of named graph
    // with this resource content
    if (messageContent.getNamedModel(resourceUri) == null) {
      ngName = resourceUri + WonRdfUtils.NAMED_GRAPH_SUFFIX;
    }
    return ngName;
  }

  public void setMessageContent(Dataset messageContent) {
    this.messageContent = messageContent;
  }

  public MessageEvent getMessageEvent() {
    return messageEvent;
  }

  public void setMessageEvent(final MessageEvent messageEvent) {
    this.messageEvent = messageEvent;
  }
}

@@ -237,16 +237,73 @@
 source;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import w
@@ -368,16 +368,152 @@
 Utils;%0A%0A
+import java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A%0A
 %0A/**%0A * 
@@ -580,16 +580,78 @@
 sage%0A%7B%0A%0A
+  final Logger logger = LoggerFactory.getLogger(getClass());%0A%0A
   privat
@@ -2726,24 +2726,500 @@
 gName;%0A  %7D%0A%0A
+  /**%0A   * Returns a list of all the URIs of the message contents%0A   * (not the corresponding graphs)%0A   *%0A   * @return List of strings each representing one of the requested URLs%0A   */%0A  public List%3CString%3E getMessageContentURIs()%0A  %7B%0A    List%3CString%3E result = new ArrayList%3CString%3E();%0A%0A    Iterator%3CString%3E graphNames = messageContent.listNames();%0A    while (graphNames.hasNext()) %7B%0A        result.add(graphNames.next().replaceAll(%22#.*%22, %22%22));%0A    %7D%0A    return result;%0A  %7D%0A%0A
   public voi

add method to retrieve message content URIs from WonMessage