]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/impl/MemoryHistory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / impl / MemoryHistory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.history.impl;\r
13 \r
14 import java.io.IOException;\r
15 import java.util.ArrayList;\r
16 import java.util.HashMap;\r
17 import java.util.Map;\r
18 import java.util.logging.Logger;\r
19 \r
20 import org.simantics.databoard.Bindings;\r
21 import org.simantics.databoard.accessor.StreamAccessor;\r
22 import org.simantics.databoard.accessor.error.AccessorException;\r
23 import org.simantics.databoard.accessor.impl.AccessorParams;\r
24 import org.simantics.databoard.accessor.java.JavaArray;\r
25 import org.simantics.databoard.binding.Binding;\r
26 import org.simantics.databoard.binding.error.BindingException;\r
27 import org.simantics.databoard.binding.impl.ArrayListBinding;\r
28 import org.simantics.databoard.type.Datatype;\r
29 import org.simantics.databoard.util.Bean;\r
30 import org.simantics.history.HistoryException;\r
31 import org.simantics.history.HistoryManager;\r
32 \r
33 public class MemoryHistory implements HistoryManager {\r
34 \r
35         Map<String, Item> items = new HashMap<String, Item>();\r
36         \r
37         /** Logger */\r
38         static Logger logger = Logger.getLogger( MemoryHistory.class.getName() );\r
39         \r
40         @Override\r
41         public void create(Bean... descs) throws HistoryException {\r
42                 for (Bean desc : descs) {\r
43                         try {\r
44                                 String id = (String) desc.getField("id");\r
45                                 Datatype format = (Datatype) desc.getField("format");\r
46                                 \r
47                                 Binding sampleBinding = Bindings.getBinding( format );\r
48                                 ArrayListBinding arrayBinding = new ArrayListBinding( sampleBinding );\r
49                                 ArrayList<?> array = new ArrayList<Object>();\r
50                                 JavaArray sa = new JavaArray(null, arrayBinding, array, AccessorParams.DEFAULT );\r
51                                 Item item = new Item();\r
52                                 item.stream = sa;\r
53                                 item.desc = desc.clone();\r
54                                 item.format = format;\r
55                                 items.put(id, item);                            \r
56                         } catch (BindingException e) {\r
57                                 throw new HistoryException("Invalid history item description", e);\r
58                         }\r
59                 }\r
60         }\r
61 \r
62         @Override\r
63         public void delete(String... itemIds) throws HistoryException {\r
64                 for (String itemId : itemIds) {\r
65                         try {\r
66                                 Item i = items.remove( itemId );\r
67                                 if (i==null) throw new HistoryException("Null item");\r
68                                 i.stream.close();\r
69                         } catch (AccessorException e) {\r
70                                 throw new HistoryException();\r
71                         }\r
72                 }\r
73         }\r
74 \r
75         @Override\r
76         public void modify(Bean... descs) throws HistoryException {\r
77                 for (Bean desc : descs) {\r
78                         try {\r
79                                 String id = (String) desc.getField("id");\r
80                                 Item i = items.get( id );\r
81                                 if ( i == null ) {\r
82                                         create(desc);\r
83                                 } else {\r
84                                         \r
85                                         if (desc.equalContents(i.desc)) continue;\r
86                                         Datatype format = (Datatype) desc.getField("format");\r
87                                         if (!i.format.equals(format)) {\r
88                                                 throw new HistoryException("Format conversion is not supported");\r
89                                         }\r
90                                         // Write new metadata bean \r
91                                         //i.desc = desc.clone();\r
92                                         // Workaround clone -- we cannot clone referable records ( as there is no adaptation context )\r
93                                         try {\r
94                                                 byte[] data = desc.serialize();\r
95                                                 i.desc = desc.getClass().newInstance();\r
96                                                 i.desc.deserialize(data);\r
97                                         } catch (IOException e) {\r
98                                                 throw new HistoryException(e);\r
99                                         } catch (InstantiationException e) {\r
100                                                 throw new HistoryException(e);\r
101                                         } catch (IllegalAccessException e) {\r
102                                                 throw new HistoryException(e);\r
103                                         }\r
104                                 }\r
105                         } catch (BindingException e) {\r
106                                 throw new HistoryException(e);\r
107                         }\r
108                 }\r
109         }\r
110 \r
111         @Override\r
112         public Bean getItem(String itemId) throws HistoryException {\r
113                 Item i = items.get( itemId );\r
114                 if ( i==null ) throw new HistoryException(itemId+" does not exist");\r
115                 return i.desc;\r
116         }\r
117 \r
118         @Override\r
119         public Bean[] getItems() throws HistoryException {\r
120                 Bean[] result = new Bean[ items.size() ];\r
121                 int ix=0; \r
122                 for (Item i : items.values()) result[ix++] = i.desc;\r
123                 return result;\r
124         }\r
125 \r
126         @Override\r
127         public void close() {           \r
128         }\r
129 \r
130         @Override\r
131         public StreamAccessor openStream(String itemId, String mode) throws HistoryException {\r
132                 Item i = items.get(itemId);\r
133                 if ( i == null ) {\r
134                         throw new HistoryException(itemId+" does not exist");\r
135                 }\r
136                 return i.stream;\r
137         }\r
138 \r
139         @Override\r
140         public synchronized boolean exists(String itemId) throws HistoryException {\r
141                 return items.containsKey(itemId);\r
142         }\r
143         \r
144         static class Item {\r
145                 JavaArray stream;\r
146                 Bean desc;\r
147                 Datatype format;\r
148         }\r
149 \r
150 }\r