1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.history.impl;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashMap;
18 import java.util.logging.Logger;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.accessor.StreamAccessor;
22 import org.simantics.databoard.accessor.error.AccessorException;
23 import org.simantics.databoard.accessor.impl.AccessorParams;
24 import org.simantics.databoard.accessor.java.JavaArray;
25 import org.simantics.databoard.binding.Binding;
26 import org.simantics.databoard.binding.error.BindingException;
27 import org.simantics.databoard.binding.impl.ArrayListBinding;
28 import org.simantics.databoard.type.Datatype;
29 import org.simantics.databoard.util.Bean;
30 import org.simantics.history.HistoryException;
31 import org.simantics.history.HistoryManager;
33 public class MemoryHistory implements HistoryManager {
35 Map<String, Item> items = new HashMap<String, Item>();
38 static Logger logger = Logger.getLogger( MemoryHistory.class.getName() );
41 public void create(Bean... descs) throws HistoryException {
42 for (Bean desc : descs) {
44 String id = (String) desc.getField("id");
45 Datatype format = (Datatype) desc.getField("format");
47 Binding sampleBinding = Bindings.getBinding( format );
48 ArrayListBinding arrayBinding = new ArrayListBinding( sampleBinding );
49 ArrayList<?> array = new ArrayList<Object>();
50 JavaArray sa = new JavaArray(null, arrayBinding, array, AccessorParams.DEFAULT );
51 Item item = new Item();
53 item.desc = desc.clone();
56 } catch (BindingException e) {
57 throw new HistoryException("Invalid history item description", e);
63 public void delete(String... itemIds) throws HistoryException {
64 for (String itemId : itemIds) {
66 Item i = items.remove( itemId );
67 if (i==null) throw new HistoryException("Null item");
69 } catch (AccessorException e) {
70 throw new HistoryException();
76 public void modify(Bean... descs) throws HistoryException {
77 for (Bean desc : descs) {
79 String id = (String) desc.getField("id");
80 Item i = items.get( id );
85 if (desc.equalContents(i.desc)) continue;
86 Datatype format = (Datatype) desc.getField("format");
87 if (!i.format.equals(format)) {
88 throw new HistoryException("Format conversion is not supported");
90 // Write new metadata bean
91 //i.desc = desc.clone();
92 // Workaround clone -- we cannot clone referable records ( as there is no adaptation context )
94 byte[] data = desc.serialize();
95 i.desc = desc.getClass().newInstance();
96 i.desc.deserialize(data);
97 } catch (IOException e) {
98 throw new HistoryException(e);
99 } catch (InstantiationException e) {
100 throw new HistoryException(e);
101 } catch (IllegalAccessException e) {
102 throw new HistoryException(e);
105 } catch (BindingException e) {
106 throw new HistoryException(e);
112 public Bean getItem(String itemId) throws HistoryException {
113 Item i = items.get( itemId );
114 if ( i==null ) throw new HistoryException(itemId+" does not exist");
119 public Bean[] getItems() throws HistoryException {
120 Bean[] result = new Bean[ items.size() ];
122 for (Item i : items.values()) result[ix++] = i.desc;
127 public void close() {
131 public StreamAccessor openStream(String itemId, String mode) throws HistoryException {
132 Item i = items.get(itemId);
134 throw new HistoryException(itemId+" does not exist");
140 public synchronized boolean exists(String itemId) throws HistoryException {
141 return items.containsKey(itemId);