package org.loklak.harvester;

import java.util.Date;
import org.json.JSONObject;
import org.loklak.objects.ObjectEntry;

/**
 * @author vibhcool (Vibhor Verma)
 * @version 0.1
 * @since 07.06.2017
 *
 * Post abstract class for data objects.
 */
public class Post extends JSONObject implements ObjectEntry {

    protected long timestamp = 0;
    protected String postId;
    private boolean wrapper = false;

    public Post() {
        this.setTimestamp();
    }

    /*
     * If wrapper=true, then this object will be used just to as wrapper of data
     * with no post-id and timestamp
     */
    public Post(boolean wrapper) {
        if(!wrapper) {
            this.setTimestamp();
        } else {
            this.wrapper = true;
        }
    }


    public Post(JSONObject json) {
        this();
    }

    public Post(String data, String query) {
        super(data);
        this.setTimestamp();
        this.setPostId(query);
    }

    protected Post(long timestamp) {
        this.setTimestamp(timestamp);
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.put("timestamp", timestamp);
        this.timestamp = timestamp;
    }

    public void setTimestamp() {
        if(this.has("timestamp")) {
            long timestampValue = Long.parseLong(String.valueOf(this.get("timestamp")));
            this.setTimestamp(timestampValue);
        } else {
            long timestamp = System.currentTimeMillis();
            this.setTimestamp(timestamp);
        }
    }

    public Date getTimestampDate() {
        return new Date(this.timestamp);
    }

    private void setPostId() {
        if(this.has("post_id")) {
            this.setPostId(String.valueOf(this.get("post_id")));
        } else {
            this.setPostId(String.valueOf(this.getTimestamp()));
        }
    }

    public void setPostId(String postId) {
        this.put("post_id", postId);
        this.postId = postId;
    }

    public String getPostId() {
        return this.postId;
    }

    public boolean isWrapper() {
        return this.wrapper;
    }

    public String toString(){
        return super.toString();
    }

    public JSONObject toJSON() {
        return this;
    }
}


@@ -75,16 +75,44 @@
 Object;%0A
+import org.loklak.data.DAO;%0A
 import o
@@ -2290,11 +2290,246 @@
 ;%0A    %7D%0A
-%7D
 %0A
+    protected String%5B%5D getStreamChannels() %7B%0A        return new String%5B%5D %7B%0A            %22all%22%0A        %7D;%0A    %7D%0A%0A    public final void publishToMQTT() %7B%0A        DAO.mqttPublisher.publish(this.getStreamChannels(), this.toString());%0A    %7D%0A%7D
 %0A

Fix #1413: Add MQTT publish hook in Post object (#1414)