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