]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/UndoMetadata.java
APIs for skipping state restoration for non-undo synchronization events
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / UndoMetadata.java
1 package org.simantics.db.common;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.ObjectInputStream;
6 import java.io.ObjectOutputStream;
7 import java.util.TreeMap;
8
9 import org.simantics.db.Session;
10 import org.simantics.db.common.utils.Logger;
11 import org.simantics.db.common.utils.Serializers;
12
13 public class UndoMetadata extends ACommentMetadata {
14     private boolean redo; // False if this was a undo operation.
15     private long begin; // Identifies first change set that was reverted.
16     private long end; // Identifies last change set that was reverted.
17     public UndoMetadata(byte[] comments, boolean redo, long begin, long end) {
18         super(comments);
19         this.redo = redo;
20         this.begin = begin;
21         this.end = end;
22     }
23     public UndoMetadata(TreeMap<String, byte[]> metadata) {
24         super((byte[])null);
25         if (null != metadata) {
26             byte[] data = metadata.get(UndoMetadata.class.getName());
27             initialize(data);
28         }
29     }
30     public UndoMetadata(byte[] data) {
31         super((byte[])null);
32         initialize(data);
33     }
34
35     private void initialize(byte[] data) {
36         if (null != data) {
37             try {
38                 UndoMetadata um = deserialise(null, data);
39                 comments = um.comments;
40                 redo = um.redo;
41                 begin = um.begin;
42                 end = um.end;
43             } catch (Exception e) {
44                 Logger.defaultLogError(e);
45             }
46         }
47     }
48
49     @Override
50     public byte[] serialise(Session session) {
51         try {
52             ByteArrayOutputStream os = new ByteArrayOutputStream();
53             ObjectOutputStream oos = new ObjectOutputStream(os);
54             byte[] bytes = Serializers.serializeStrings(comments.toArray(new String[comments.size()]));
55             oos.writeInt(bytes.length);
56             oos.write(bytes);
57             oos.writeBoolean(redo);
58             oos.writeLong(begin);
59             oos.writeLong(end);
60             oos.close();
61             return os.toByteArray();
62         } catch (Exception e) {
63             if (DEBUG)
64                 e.printStackTrace();
65             Logger.defaultLogError(e.toString());
66         }
67         return new byte[0];
68     }
69
70     public static UndoMetadata deserialise(Session session, byte[] input) {
71         try {
72             if (null == input)
73                 return new UndoMetadata(null, false, 0, 0);
74             ByteArrayInputStream is = new ByteArrayInputStream(input);
75             ObjectInputStream ois = new ObjectInputStream(is);
76             int len = ois.readInt();
77             if (len < 0)
78                 throw new Error("Deserialisation error. Illegal string length=" + len);
79             byte[] bytes = new byte[len];
80             ois.readFully(bytes);
81             boolean redo = ois.readBoolean();
82             long begin = ois.readLong();
83             long end = ois.readLong();
84             ois.close();
85             return new UndoMetadata(bytes, redo, begin, end);
86         } catch (Throwable e) {
87             Logger.defaultLogError(e);
88         }
89         return new UndoMetadata(null, false, 0, 0);
90     }
91     public boolean isRedo() {
92         return redo;
93     }
94     public long getBeginCSId() {
95         return begin;
96     }
97     public long getEndCSId() {
98         return end;
99     }
100     public void setTypeAndRange(UndoMetadata um) {
101         redo = ! um.redo;
102         begin = um.begin;
103         end = um.end;
104     }
105     public void setTypeAndRange(boolean redo, long begin, long end) {
106         this.redo = redo;
107         this.begin = begin;
108         this.end = end;
109     }
110     public String getType() {
111         return redo ? "Redo" : "Undo";
112     }
113     public String getRange() {
114         return "[" + begin + "," + end + "]";
115     }
116     public String getHeader() {
117         return getType() + getRange();
118     }
119     
120     /**
121      * Returns true when no change sets are included.
122      */
123     public boolean isEmpty() {
124         return begin == 0 && end == 0;
125     }
126 }