]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.history/src/org/simantics/history/impl/MemoryHistory.java b/bundles/org.simantics.history/src/org/simantics/history/impl/MemoryHistory.java
new file mode 100644 (file)
index 0000000..753e6d5
--- /dev/null
@@ -0,0 +1,150 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history.impl;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.logging.Logger;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.accessor.StreamAccessor;\r
+import org.simantics.databoard.accessor.error.AccessorException;\r
+import org.simantics.databoard.accessor.impl.AccessorParams;\r
+import org.simantics.databoard.accessor.java.JavaArray;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.binding.impl.ArrayListBinding;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.Bean;\r
+import org.simantics.history.HistoryException;\r
+import org.simantics.history.HistoryManager;\r
+\r
+public class MemoryHistory implements HistoryManager {\r
+\r
+       Map<String, Item> items = new HashMap<String, Item>();\r
+       \r
+       /** Logger */\r
+       static Logger logger = Logger.getLogger( MemoryHistory.class.getName() );\r
+       \r
+       @Override\r
+       public void create(Bean... descs) throws HistoryException {\r
+               for (Bean desc : descs) {\r
+                       try {\r
+                               String id = (String) desc.getField("id");\r
+                               Datatype format = (Datatype) desc.getField("format");\r
+                               \r
+                               Binding sampleBinding = Bindings.getBinding( format );\r
+                               ArrayListBinding arrayBinding = new ArrayListBinding( sampleBinding );\r
+                               ArrayList<?> array = new ArrayList<Object>();\r
+                               JavaArray sa = new JavaArray(null, arrayBinding, array, AccessorParams.DEFAULT );\r
+                               Item item = new Item();\r
+                               item.stream = sa;\r
+                               item.desc = desc.clone();\r
+                               item.format = format;\r
+                               items.put(id, item);                            \r
+                       } catch (BindingException e) {\r
+                               throw new HistoryException("Invalid history item description", e);\r
+                       }\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void delete(String... itemIds) throws HistoryException {\r
+               for (String itemId : itemIds) {\r
+                       try {\r
+                               Item i = items.remove( itemId );\r
+                               if (i==null) throw new HistoryException("Null item");\r
+                               i.stream.close();\r
+                       } catch (AccessorException e) {\r
+                               throw new HistoryException();\r
+                       }\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void modify(Bean... descs) throws HistoryException {\r
+               for (Bean desc : descs) {\r
+                       try {\r
+                               String id = (String) desc.getField("id");\r
+                               Item i = items.get( id );\r
+                               if ( i == null ) {\r
+                                       create(desc);\r
+                               } else {\r
+                                       \r
+                                       if (desc.equalContents(i.desc)) continue;\r
+                                       Datatype format = (Datatype) desc.getField("format");\r
+                                       if (!i.format.equals(format)) {\r
+                                               throw new HistoryException("Format conversion is not supported");\r
+                                       }\r
+                                       // Write new metadata bean \r
+                                       //i.desc = desc.clone();\r
+                                       // Workaround clone -- we cannot clone referable records ( as there is no adaptation context )\r
+                                       try {\r
+                                               byte[] data = desc.serialize();\r
+                                               i.desc = desc.getClass().newInstance();\r
+                                               i.desc.deserialize(data);\r
+                                       } catch (IOException e) {\r
+                                               throw new HistoryException(e);\r
+                                       } catch (InstantiationException e) {\r
+                                               throw new HistoryException(e);\r
+                                       } catch (IllegalAccessException e) {\r
+                                               throw new HistoryException(e);\r
+                                       }\r
+                               }\r
+                       } catch (BindingException e) {\r
+                               throw new HistoryException(e);\r
+                       }\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public Bean getItem(String itemId) throws HistoryException {\r
+               Item i = items.get( itemId );\r
+               if ( i==null ) throw new HistoryException(itemId+" does not exist");\r
+               return i.desc;\r
+       }\r
+\r
+       @Override\r
+       public Bean[] getItems() throws HistoryException {\r
+               Bean[] result = new Bean[ items.size() ];\r
+               int ix=0; \r
+               for (Item i : items.values()) result[ix++] = i.desc;\r
+               return result;\r
+       }\r
+\r
+       @Override\r
+       public void close() {           \r
+       }\r
+\r
+       @Override\r
+       public StreamAccessor openStream(String itemId, String mode) throws HistoryException {\r
+               Item i = items.get(itemId);\r
+               if ( i == null ) {\r
+                       throw new HistoryException(itemId+" does not exist");\r
+               }\r
+               return i.stream;\r
+       }\r
+\r
+       @Override\r
+       public synchronized boolean exists(String itemId) throws HistoryException {\r
+               return items.containsKey(itemId);\r
+       }\r
+       \r
+       static class Item {\r
+               JavaArray stream;\r
+               Bean desc;\r
+               Datatype format;\r
+       }\r
+\r
+}\r