]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.history/src/org/simantics/history/ItemManager.java
Rest API for Historian data
[simantics/platform.git] / bundles / org.simantics.history / src / org / simantics / history / ItemManager.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;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.TreeMap;
20
21 import org.simantics.databoard.Bindings;
22 import org.simantics.databoard.binding.Binding;
23 import org.simantics.databoard.binding.error.BindingConstructionException;
24 import org.simantics.databoard.binding.error.BindingException;
25 import org.simantics.databoard.type.Datatype;
26 import org.simantics.databoard.util.Bean;
27
28 /**
29  * This utility class helps in adding, removing and searching of history items.
30  * The input and result if convertible with Item[] configuration.
31  *  
32  * @author toni.kalajainen
33  */
34 public class ItemManager {
35         
36         Map<String, Bean> items = new TreeMap<String, Bean>();
37
38         public static <Item extends Bean> ItemManager createUnchecked( @SuppressWarnings("unchecked") Item...items ) {
39                 try {
40                         return new ItemManager(items);
41                 } catch (HistoryException e) {
42                         throw new RuntimeException(e);
43                 }
44         }
45
46         public <Item extends Bean> ItemManager( @SuppressWarnings("unchecked") Item...items ) throws HistoryException
47         {
48                 try {
49                         for (Item item : items) {
50                                 String id = (String) item.getField("id");
51                                 this.items.put( id, item );
52                         }
53                 } catch (BindingException e) {
54                         throw new HistoryException( e );
55                 }
56         }
57         
58         public <Item extends Bean> ItemManager( List<Item> items ) throws HistoryException
59         {
60                 try {
61                         for (Item item : items) {
62                                 String id = (String) item.getField("id");
63                                 this.items.put( id, item );
64                         }
65                 } catch (BindingException e) {
66                         throw new HistoryException( e );
67                 }
68         }
69         
70         public boolean exists(String id)
71         {
72                 return items.containsKey(id);
73         }
74         
75         public Bean get(String id) 
76         {
77                 return items.get( id );
78         }
79         
80         public Set<String> ids()
81         {
82                 return items.keySet();
83         }
84
85         public Collection<Bean> values()
86         {
87                 return items.values();
88         }
89         
90         public Bean[] toArray() 
91         {
92                 return items.values().toArray( new Bean[ items.size() ] );
93         }
94         
95         public String[] toIdArray()
96         {
97                 return items.keySet().toArray( new String[ items.size() ] );
98         }
99         
100         public void add(Bean item) throws BindingException 
101         {
102                 items.put( (String) item.getIdentifier(), item.clone()); 
103         }
104         
105         public Bean remove(String id)
106         {
107                 return items.remove(id);
108         }
109         
110         public void remove(Bean...items)
111         {
112                 for (Bean item : items)
113                 {
114                         try {
115                                 this.items.remove( (String) item.getIdentifier() );
116                         } catch (BindingException e) {
117                         }
118                 }
119         }
120         
121         public List<Bean> search(String fieldName1, Object expected1) throws BindingException
122         {
123                 try {
124                         Binding binding1 = getBinding(expected1);
125                         List<Bean> result = new ArrayList<Bean>();
126                         for (Bean item : items.values()) {
127                                 Binding cb1 = item.getFieldBinding(fieldName1);
128                                 Object cv1 = item.getField(fieldName1);
129                                 
130                                 boolean matches1 = 
131                                                 (expected1==null && cv1==null) || 
132                                                 (cv1!=null && cb1!=null && binding1!=null && expected1!=null &&
133                                                  Bindings.equals(cb1, cv1, binding1, expected1));
134                                 
135                                 if ( matches1 ) result.add(item);                               
136                         }
137                         return result;
138                 } catch (BindingConstructionException bce) {
139                         throw new BindingException(bce);
140                 }
141         }
142
143         public List<Bean> search(String fieldName1, Object expected1, String fieldName2, Object expected2) throws BindingException
144         {
145                 try {
146                         List<Bean> result = new ArrayList<Bean>();
147                         Binding binding1 = getBinding(expected1);
148                         Binding binding2 = getBinding(expected2);
149                         for (Bean item : items.values()) {
150                                 Binding cb1 = item.getFieldBinding(fieldName1);
151                                 Object cv1 = item.getField(fieldName1);
152                                 boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));
153                                 
154                                 Binding cb2 = item.getFieldBinding(fieldName2);
155                                 Object cv2 = item.getField(fieldName2);
156                                 boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));
157
158                                 if ( matches1 && matches2 ) result.add(item);                           
159                         }
160                         return result;
161                 } catch (BindingConstructionException bce) {
162                         throw new BindingException(bce);
163                 }
164         }
165
166         public List<Bean> search(
167                         String fieldName1, Object expected1,
168                         String fieldName2, Object expected2,
169                         String fieldName3, Object expected3)
170                                         throws BindingException
171         {
172                 try {
173                         List<Bean> result = new ArrayList<Bean>();
174                         Binding binding1 = getBinding(expected1);
175                         Binding binding2 = getBinding(expected2);
176                         Binding binding3 = getBinding(expected3);
177                         for (Bean item : items.values()) {
178                                 Binding cb1 = item.getFieldBinding(fieldName1);
179                                 Object cv1 = item.getField(fieldName1);
180                                 boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));
181                                 
182                                 Binding cb2 = item.getFieldBinding(fieldName2);
183                                 Object cv2 = item.getField(fieldName2);
184                                 boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));
185
186                                 Binding cb3 = item.getFieldBinding(fieldName3);
187                                 Object cv3 = item.getField(fieldName3);
188                                 boolean matches3 = (expected3==null && cv3==null) || (Bindings.equals(cb3, cv3, binding3, expected3));
189
190                                 if ( matches1 && matches2 && matches3 ) result.add(item);
191                         }
192                         return result;
193                 } catch (BindingConstructionException bce) {
194                         throw new BindingException(bce);
195                 }
196         }
197         
198         Binding getBinding(Object data) throws BindingConstructionException {
199                 if ( data==null ) return null;
200                 Class<?> clazz = data.getClass();
201                 if ( Datatype.class.isAssignableFrom(clazz) ) return Bindings.getBinding( Datatype.class );
202                 return Bindings.getBinding(clazz);
203         }
204
205         public List<Bean> search(
206                         String fieldName1, Object expected1, 
207                         String fieldName2, Object expected2, 
208                         String fieldName3, Object expected3, 
209                         String fieldName4, Object expected4, 
210                         String fieldName5, Object expected5, 
211                         String fieldName6, Object expected6, 
212                         String fieldName7, Object expected7
213                         ) throws BindingException
214         {
215                 try {
216                         List<Bean> result = new ArrayList<Bean>();
217                         Binding binding1 = getBinding(expected1);
218                         Binding binding2 = getBinding(expected2);
219                         Binding binding3 = getBinding(expected3);
220                         Binding binding4 = getBinding(expected4);
221                         Binding binding5 = getBinding(expected5);
222                         Binding binding6 = getBinding(expected6);
223                         Binding binding7 = getBinding(expected7);
224                         for (Bean item : items.values()) {
225                                 Binding cb1 = item.getFieldBinding(fieldName1);
226                                 Object cv1 = item.getField(fieldName1);
227                                 boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));
228                                 
229                                 Binding cb2 = item.getFieldBinding(fieldName2);
230                                 Object cv2 = item.getField(fieldName2);
231                                 boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));
232
233                                 Binding cb3 = item.getFieldBinding(fieldName3);
234                                 Object cv3 = item.getField(fieldName3);
235                                 boolean matches3 = (expected3==null && cv3==null) || (Bindings.equals(cb3, cv3, binding3, expected3));
236
237                                 Binding cb4 = item.getFieldBinding(fieldName4);
238                                 Object cv4 = item.getField(fieldName4);
239                                 boolean matches4 = (expected4==null && cv4==null) || (Bindings.equals(cb4, cv4, binding4, expected4));
240
241                                 Binding cb5 = item.getFieldBinding(fieldName5);
242                                 Object cv5 = item.getField(fieldName5);
243                                 boolean matches5 = (expected5==null && cv5==null) || (Bindings.equals(cb5, cv5, binding5, expected5));
244
245                                 Binding cb6 = item.getFieldBinding(fieldName6);
246                                 Object cv6 = item.getField(fieldName6);
247                                 boolean matches6 = (expected6==null && cv6==null) || (Bindings.equals(cb6, cv6, binding6, expected6));
248
249                                 Binding cb7 = item.getFieldBinding(fieldName7);
250                                 Object cv7 = item.getField(fieldName7);
251                                 boolean matches7 = (expected7==null && cv7==null) || (Bindings.equals(cb7, cv7, binding7, expected7));
252                                 
253 //                              if (expected2.equals("/RL10F001/XB_02#BINARY_VALUE") && cv2.equals("/RL10F001/XB_02#BINARY_VALUE"));
254 //                                      System.out.println("debug here");
255
256                                 if ( matches1 && matches2 & matches3 & matches4 & matches5 & matches6 & matches7 ) result.add(item);                            
257                         }
258                         /*
259                         if (result.isEmpty()) {
260                                 System.out.println("No match for "+expected2);
261                         } else {
262                                 if (result.size()>1) System.out.println("many matches for "+expected2); else
263                                 System.out.println("match for "+expected2+" is "+result.iterator().next().getField(fieldName2));
264                         }*/
265                         return result;
266                 } catch (BindingConstructionException bce) {
267                         throw new BindingException(bce);
268                 }
269         }
270         
271
272 }