X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FCommandMetadata.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2FCommandMetadata.java;h=6d980165f5e120adfa2371047054f4f16fdcba97;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/CommandMetadata.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/CommandMetadata.java new file mode 100644 index 000000000..6d980165f --- /dev/null +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/CommandMetadata.java @@ -0,0 +1,93 @@ +package org.simantics.db.common; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.serialization.SerializationException; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.db.Metadata; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; + +public class CommandMetadata implements Metadata { + + public static final boolean DEBUG = false; + public static final String RESET_COMMAND = "// RESET"; + + private static final Binding BINDING = + Bindings.getBindingUnchecked(CommandMetadata.class); + private static final Serializer SERIALIZER = + Bindings.getSerializerUnchecked(BINDING); + + public List commands; + + public static class Command { + public long modelId; + public String command; + + public Command() { + } + + public Command(long modelId, String command) { + super(); + this.modelId = modelId; + this.command = command; + } + } + + public CommandMetadata() { + } + + @Override + public byte[] serialise(Session session) { + try { + return SERIALIZER.serialize(this); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + } + + public static CommandMetadata deserialise(Session session, byte[] input) { + if(input == null) { + CommandMetadata metadata = new CommandMetadata(); + metadata.commands = new ArrayList(); + return metadata; + } + try { + return (CommandMetadata)SERIALIZER.deserialize(input); + } catch (SerializationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public CommandMetadata add(Command command) { + commands.add(command); + return this; + } + + public List getCommands() { + return commands; + } + + public static void add(WriteGraph graph, long modelId, String command) throws DatabaseException { + if(DEBUG) { + System.out.println("-------------------------------------------------------------"); + System.out.println(command); + } + graph.addMetadata(graph.getMetadata(CommandMetadata.class).add( + new Command(modelId, command))); + } + + public static void addReset(WriteGraph graph, long modelId) throws DatabaseException { + graph.addMetadata(graph.getMetadata(CommandMetadata.class).add( + new Command(modelId, RESET_COMMAND))); + } +}