1 package org.simantics.db.common;
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;
9 import org.simantics.db.Session;
10 import org.simantics.db.common.utils.Logger;
11 import org.simantics.db.common.utils.Serializers;
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) {
23 public UndoMetadata(TreeMap<String, byte[]> metadata) {
25 if (null != metadata) {
26 byte[] data = metadata.get(UndoMetadata.class.getName());
30 public UndoMetadata(byte[] data) {
35 private void initialize(byte[] data) {
38 UndoMetadata um = deserialise(null, data);
39 comments = um.comments;
43 } catch (Exception e) {
44 Logger.defaultLogError(e);
50 public byte[] serialise(Session session) {
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);
57 oos.writeBoolean(redo);
61 return os.toByteArray();
62 } catch (Exception e) {
65 Logger.defaultLogError(e.toString());
70 public static UndoMetadata deserialise(Session session, byte[] 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();
78 throw new Error("Deserialisation error. Illegal string length=" + len);
79 byte[] bytes = new byte[len];
81 boolean redo = ois.readBoolean();
82 long begin = ois.readLong();
83 long end = ois.readLong();
85 return new UndoMetadata(bytes, redo, begin, end);
86 } catch (Throwable e) {
87 Logger.defaultLogError(e);
89 return new UndoMetadata(null, false, 0, 0);
91 public boolean isRedo() {
94 public long getBeginCSId() {
97 public long getEndCSId() {
100 public void setTypeAndRange(UndoMetadata um) {
105 public void setTypeAndRange(boolean redo, long begin, long end) {
110 public String getType() {
111 return redo ? "Redo" : "Undo";
113 public String getRange() {
114 return "[" + begin + "," + end + "]";
116 public String getHeader() {
117 return getType() + getRange();