package automately.core.file; import automately.core.data.IdentifiedDataTypeFactory; import com.hazelcast.nio.ObjectDataInput; import com.hazelcast.nio.ObjectDataOutput; import io.jsync.app.core.persistence.IdentifiedDataType; 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 IdentifiedDataType { public String name; public String userToken; public Date created; public Date updated; public long size; public String type; public String pathAlias; public boolean isPublic; private String token; public boolean isDirectory; public VirtualFile(){ this(true); } public VirtualFile(boolean initialize){ if(initialize){ name = ""; userToken = ""; //Empty by default created = new Date(); updated = new Date(); size = 0; type = ""; pathAlias = "/"; isPublic = false; token = Token.generateToken().toHex(); isDirectory = false; } } @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); dataOutput.writeBoolean(isDirectory); } @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(); isDirectory = 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); if(json.getValue("size") instanceof JsonObject && json.getObject("size").containsField("$numberLong")){ json.putValue("size", Long.valueOf(json.getObject("size", new JsonObject()).getValue("$numberLong"))); } 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); this.isDirectory = json.getBoolean("isDirectory", this.isDirectory); // 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("id"); return ret; } /** * This will create a new VirtualFile that is a copy of the given file. * @param file * @return */ public static VirtualFile copy(VirtualFile file){ VirtualFile newFile = new VirtualFile(false); newFile.isPublic = file.isPublic; newFile.name = file.name; newFile.pathAlias = file.pathAlias; newFile.created = file.created; newFile.updated = file.updated; newFile.size = file.size; newFile.isDirectory = file.isDirectory; newFile.type = file.type; newFile.userToken = file.userToken; newFile.token = Token.generateToken().toHex(); return newFile; } @Override public int getFactoryId() { return IdentifiedDataTypeFactory.FACTORY_ID; } @Override public int getId() { return IdentifiedDataTypeFactory.VIRTUALFILE_TYPE; } }