All Downloads are FREE. Search and download functionalities are using the official Maven repository.

automately.core.file.VirtualFile Maven / Gradle / Ivy

package automately.core.file;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import io.jcluster.core.data.DataType;
import io.jsync.json.JsonObject;
import io.jsync.json.impl.Json;
import io.jsync.utils.Token;

import java.io.IOException;
import java.util.Date;

/**
 * The VirtualFile class is used by JCluster to store information about user files stored in the cluster.
 * It is used heavily by the VirtualFileService and VirtualFileSystem.
 */
public class VirtualFile extends DataType {

    public String name = "";
    public String userToken = ""; //Empty by default
    public Date created = new Date();
    public Date updated = new Date();
    public long size = 0;
    public String type = "";
    public String pathAlias = "/";
    public boolean isPublic = false;
    private String token = Token.generateToken().toHex();

    @Override
    public void writeData(ObjectDataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(token);
        dataOutput.writeUTF(name);
        dataOutput.writeUTF(userToken);
        dataOutput.writeObject(created);
        dataOutput.writeObject(updated);
        dataOutput.writeLong(size);
        dataOutput.writeUTF(type);
        dataOutput.writeUTF(pathAlias);
        dataOutput.writeBoolean(isPublic);
    }

    @Override
    public void readData(ObjectDataInput dataInput) throws IOException {
        token = dataInput.readUTF();
        name = dataInput.readUTF();
        userToken = dataInput.readUTF();
        created = dataInput.readObject();
        updated = dataInput.readObject();
        size = dataInput.readLong();
        type = dataInput.readUTF();
        pathAlias = dataInput.readUTF();
        isPublic = dataInput.readBoolean();
    }

    public String token() {
        return token;
    }

    @Override
    public void loadJson(JsonObject json) {
        if (json != null) {
            this.token = json.getString("token", this.token);
            this.name = json.getString("name", this.name);
            this.userToken = json.getString("userToken", this.userToken);
            this.size = json.getLong("size", size);
            this.type = json.getString("type", this.type);
            this.pathAlias = json.getString("pathAlias", this.pathAlias);
            this.isPublic = json.getBoolean("isPublic", this.isPublic);

            // For some reason data didn't work out too well
            if(json.getValue("created") instanceof JsonObject &&
                    json.getObject("created", new JsonObject()).containsField("$numberLong")){
                json.putValue("created", Long.valueOf(json.getObject("created", new JsonObject()).getValue("$numberLong")));
            }
            this.created = new Date(json.getLong("created", created.getTime()));

            // For some reason data didn't work out too well
            if(json.getValue("updated") instanceof JsonObject &&
                    json.getObject("updated", new JsonObject()).containsField("$numberLong")){
                json.putValue("updated", Long.valueOf(json.getObject("updated", new JsonObject()).getValue("$numberLong")));
            }
            this.updated = new Date(json.getLong("updated", updated.getTime()));
        }
    }

    @Override
    public JsonObject toJson() {
        JsonObject ret = new JsonObject(Json.encode(this));
        ret.putString("token", token);
        ret.putNumber("created", created.getTime());
        ret.putNumber("updated", updated.getTime());

        ret.removeField("factoryId");
        ret.removeField("internalId");
        ret.removeField("id");
        return ret;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy