1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.history.impl;
\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
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
33 public class MemoryHistory implements HistoryManager {
\r
35 Map<String, Item> items = new HashMap<String, Item>();
\r
38 static Logger logger = Logger.getLogger( MemoryHistory.class.getName() );
\r
41 public void create(Bean... descs) throws HistoryException {
\r
42 for (Bean desc : descs) {
\r
44 String id = (String) desc.getField("id");
\r
45 Datatype format = (Datatype) desc.getField("format");
\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
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
63 public void delete(String... itemIds) throws HistoryException {
\r
64 for (String itemId : itemIds) {
\r
66 Item i = items.remove( itemId );
\r
67 if (i==null) throw new HistoryException("Null item");
\r
69 } catch (AccessorException e) {
\r
70 throw new HistoryException();
\r
76 public void modify(Bean... descs) throws HistoryException {
\r
77 for (Bean desc : descs) {
\r
79 String id = (String) desc.getField("id");
\r
80 Item i = items.get( id );
\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
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
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
105 } catch (BindingException e) {
\r
106 throw new HistoryException(e);
\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
119 public Bean[] getItems() throws HistoryException {
\r
120 Bean[] result = new Bean[ items.size() ];
\r
122 for (Item i : items.values()) result[ix++] = i.desc;
\r
127 public void close() {
\r
131 public StreamAccessor openStream(String itemId, String mode) throws HistoryException {
\r
132 Item i = items.get(itemId);
\r
134 throw new HistoryException(itemId+" does not exist");
\r
140 public synchronized boolean exists(String itemId) throws HistoryException {
\r
141 return items.containsKey(itemId);
\r
144 static class Item {
\r