package won.matcher.service.common.event;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;

import org.apache.jena.query.Dataset;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.LangBuilder;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;

import won.matcher.service.common.service.sparql.SparqlService;

/**
 * This event is used in the matching service to indicate that a new atom has
 * been found. It includes the URIs of the atom and the won node and optionally
 * the serialized resource User: hfriedrich Date: 04.06.2015
 */
public class AtomEvent implements Serializable {
    private String uri;
    private String wonNodeUri;
    private String serializedAtomResource;
    private String serializationLangName;
    private String serializationLangContentType;
    private long crawlDate;
    private TYPE eventType;

    public static enum TYPE {
        ACTIVE, INACTIVE
    }

    public AtomEvent(String uri, String wonNodeUri, TYPE eventType, long crawlDate, String resource, Lang format) {
        this.uri = uri;
        this.wonNodeUri = wonNodeUri;
        this.eventType = eventType;
        this.crawlDate = crawlDate;
        serializedAtomResource = resource;
        serializationLangName = format.getName();
        serializationLangContentType = format.getContentType().getContentType();
    }

    public AtomEvent(String uri, String wonNodeUri, TYPE eventType, long crawlDate, Dataset ds) {
        this.uri = uri;
        this.wonNodeUri = wonNodeUri;
        this.eventType = eventType;
        this.crawlDate = crawlDate;
        StringWriter sw = new StringWriter();
        RDFDataMgr.write(sw, ds, RDFFormat.TRIG.getLang());
        serializedAtomResource = sw.toString();
        serializationLangName = RDFFormat.TRIG.getLang().getName();
        serializationLangContentType = RDFFormat.TRIG.getLang().getContentType().getContentType();
    }

    public String getUri() {
        return uri;
    }

    public String getWonNodeUri() {
        return wonNodeUri;
    }

    public TYPE getEventType() {
        return eventType;
    }

    public String getSerializedAtomResource() {
        return serializedAtomResource;
    }

    public Lang getSerializationFormat() {
        Lang format = LangBuilder.create(serializationLangName, serializationLangContentType).build();
        return format;
    }

    public long getCrawlDate() {
        return crawlDate;
    }

    public Dataset deserializeAtomDataset() throws IOException {
        return SparqlService.deserializeDataset(serializedAtomResource, getSerializationFormat());
    }

    @Override
    public AtomEvent clone() {
        AtomEvent e = new AtomEvent(uri, wonNodeUri, eventType, crawlDate, serializedAtomResource,
                        getSerializationFormat());
        return e;
    }

    @Override
    public String toString() {
        return getUri();
    }
}

@@ -312,24 +312,92 @@
 RDFFormat;%0A%0A
+import won.matcher.service.common.mailbox.PriorityAtomEventMailbox;%0A
 import won.m
@@ -969,16 +969,966 @@
 entType;
+%0A    private Priority priority;%0A%0A    /**%0A     * Indicates the priority this event should be given by matchers. The priority%0A     * should be given based on how the event was generated. Used in the%0A     * %7B@link PriorityAtomEventMailbox%7D; lower priority values means more important.%0A     */%0A    public static enum Priority %7B%0A        PUSHED(0), // we received a push about this Atom from a WoN node. React fast!%0A        SCHEDULED_FOR_REMATCH(5), // we decided it's time to re-match for this Atom. Should be handled before%0A                                  // events generated in crawling%0A        CRAWLED(10), // we found this Atom during crawling. Handle when we have nothing else to do%0A        ;%0A        private int priority;%0A%0A        private Priority(int prio) %7B%0A            this.priority = prio;%0A        %7D%0A%0A        public int getPriority() %7B%0A            return priority;%0A        %7D%0A%0A        public static int LOWEST_PRIORTY = Integer.MAX_VALUE;%0A    %7D
 %0A%0A    pu
@@ -2095,16 +2095,55 @@
 g format
+,%0A                    Priority priority
 ) %7B%0A    
@@ -2547,16 +2547,35 @@
 taset ds
+, Priority priority
 ) %7B%0A    
@@ -3216,32 +3216,100 @@
 entType;%0A    %7D%0A%0A
+    public Priority getPriority() %7B%0A        return priority;%0A    %7D%0A%0A
     public Strin
@@ -3987,24 +3987,34 @@
 tionFormat()
+, priority
 );%0A        r

Add priority to AtomEvent