]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/impl/MemoryHistory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / impl / MemoryHistory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.history.impl;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.logging.Logger;
19
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;
32
33 public class MemoryHistory implements HistoryManager {
34
35         Map<String, Item> items = new HashMap<String, Item>();
36         
37         /** Logger */
38         static Logger logger = Logger.getLogger( MemoryHistory.class.getName() );
39         
40         @Override
41         public void create(Bean... descs) throws HistoryException {
42                 for (Bean desc : descs) {
43                         try {
44                                 String id = (String) desc.getField("id");
45                                 Datatype format = (Datatype) desc.getField("format");
46                                 
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();
52                                 item.stream = sa;
53                                 item.desc = desc.clone();
54                                 item.format = format;
55                                 items.put(id, item);                            
56                         } catch (BindingException e) {
57                                 throw new HistoryException("Invalid history item description", e);
58                         }
59                 }
60         }
61
62         @Override
63         public void delete(String... itemIds) throws HistoryException {
64                 for (String itemId : itemIds) {
65                         try {
66                                 Item i = items.remove( itemId );
67                                 if (i==null) throw new HistoryException("Null item");
68                                 i.stream.close();
69                         } catch (AccessorException e) {
70                                 throw new HistoryException();
71                         }
72                 }
73         }
74
75         @Override
76         public void modify(Bean... descs) throws HistoryException {
77                 for (Bean desc : descs) {
78                         try {
79                                 String id = (String) desc.getField("id");
80                                 Item i = items.get( id );
81                                 if ( i == null ) {
82                                         create(desc);
83                                 } else {
84                                         
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");
89                                         }
90                                         // Write new metadata bean 
91                                         //i.desc = desc.clone();
92                                         // Workaround clone -- we cannot clone referable records ( as there is no adaptation context )
93                                         try {
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);
103                                         }
104                                 }
105                         } catch (BindingException e) {
106                                 throw new HistoryException(e);
107                         }
108                 }
109         }
110
111         @Override
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");
115                 return i.desc;
116         }
117
118         @Override
119         public Bean[] getItems() throws HistoryException {
120                 Bean[] result = new Bean[ items.size() ];
121                 int ix=0; 
122                 for (Item i : items.values()) result[ix++] = i.desc;
123                 return result;
124         }
125
126         @Override
127         public void close() {           
128         }
129
130         @Override
131         public StreamAccessor openStream(String itemId, String mode) throws HistoryException {
132                 Item i = items.get(itemId);
133                 if ( i == null ) {
134                         throw new HistoryException(itemId+" does not exist");
135                 }
136                 return i.stream;
137         }
138
139         @Override
140         public synchronized boolean exists(String itemId) throws HistoryException {
141                 return items.containsKey(itemId);
142         }
143         
144         static class Item {
145                 JavaArray stream;
146                 Bean desc;
147                 Datatype format;
148         }
149
150 }