]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/CommandMetadata.java
Add logging by default to adapters exception-methods
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / CommandMetadata.java
1 package org.simantics.db.common;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.binding.Binding;
9 import org.simantics.databoard.serialization.SerializationException;
10 import org.simantics.databoard.serialization.Serializer;
11 import org.simantics.db.Metadata;
12 import org.simantics.db.Session;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.exception.DatabaseException;
15
16 public class CommandMetadata implements Metadata {
17
18     public static final boolean DEBUG = false;
19     public static final String RESET_COMMAND = "// RESET";
20     
21     private static final Binding BINDING = 
22             Bindings.getBindingUnchecked(CommandMetadata.class);
23     private static final Serializer SERIALIZER = 
24             Bindings.getSerializerUnchecked(BINDING);
25     
26     public List<Command> commands;
27     
28     public static class Command {
29         public long modelId;
30         public String command;
31         
32         public Command() {         
33         }
34         
35         public Command(long modelId, String command) {
36             super();
37             this.modelId = modelId;
38             this.command = command;
39         }
40     }
41     
42     public CommandMetadata() {
43     }
44
45     @Override
46     public byte[] serialise(Session session) {
47         try {
48             return SERIALIZER.serialize(this);
49         } catch (IOException e) {
50             e.printStackTrace();
51             throw new RuntimeException(e);
52         }
53     }
54     
55     public static CommandMetadata deserialise(Session session, byte[] input) {
56         if(input == null) {
57             CommandMetadata metadata = new CommandMetadata();
58             metadata.commands = new ArrayList<Command>();
59             return metadata;
60         }
61         try {
62             return (CommandMetadata)SERIALIZER.deserialize(input);
63         } catch (SerializationException e) {
64             e.printStackTrace();
65         } catch (IOException e) {
66             e.printStackTrace();
67         }
68         return null;
69     }
70
71     public CommandMetadata add(Command command) {
72         commands.add(command);
73         return this;
74     }
75     
76     public List<Command> getCommands() {
77         return commands;
78     }
79
80     public static void add(WriteGraph graph, long modelId, String command) throws DatabaseException {
81         if(DEBUG) {
82             System.out.println("-------------------------------------------------------------");
83             System.out.println(command);
84         }
85         graph.addMetadata(graph.getMetadata(CommandMetadata.class).add(
86                 new Command(modelId, command)));
87     }
88     
89     public static void addReset(WriteGraph graph, long modelId) throws DatabaseException {
90         graph.addMetadata(graph.getMetadata(CommandMetadata.class).add(
91                 new Command(modelId, RESET_COMMAND)));
92     }
93 }