--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.history;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.Bean;\r
+\r
+/**\r
+ * This utility class helps in adding, removing and searching of history items.\r
+ * The input and result if convertible with Item[] configuration.\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class ItemManager {\r
+ \r
+ Map<String, Bean> items = new TreeMap<String, Bean>();\r
+\r
+ public static <Item extends Bean> ItemManager createUnchecked( @SuppressWarnings("unchecked") Item...items ) {\r
+ try {\r
+ return new ItemManager(items);\r
+ } catch (HistoryException e) {\r
+ throw new RuntimeException(e);\r
+ }\r
+ }\r
+\r
+ public <Item extends Bean> ItemManager( @SuppressWarnings("unchecked") Item...items ) throws HistoryException\r
+ {\r
+ try {\r
+ for (Item item : items) {\r
+ String id = (String) item.getField("id");\r
+ this.items.put( id, item );\r
+ }\r
+ } catch (BindingException e) {\r
+ throw new HistoryException( e );\r
+ }\r
+ }\r
+ \r
+ public <Item extends Bean> ItemManager( List<Item> items ) throws HistoryException\r
+ {\r
+ try {\r
+ for (Item item : items) {\r
+ String id = (String) item.getField("id");\r
+ this.items.put( id, item );\r
+ }\r
+ } catch (BindingException e) {\r
+ throw new HistoryException( e );\r
+ }\r
+ }\r
+ \r
+ public boolean exists(String id)\r
+ {\r
+ return items.containsKey(id);\r
+ }\r
+ \r
+ public Bean get(String id) \r
+ {\r
+ return items.get( id );\r
+ }\r
+ \r
+ public Set<String> ids()\r
+ {\r
+ return items.keySet();\r
+ }\r
+\r
+ public Collection<Bean> values()\r
+ {\r
+ return items.values();\r
+ }\r
+ \r
+ public Bean[] toArray() \r
+ {\r
+ return items.values().toArray( new Bean[ items.size() ] );\r
+ }\r
+ \r
+ public String[] toIdArray()\r
+ {\r
+ return items.keySet().toArray( new String[ items.size() ] );\r
+ }\r
+ \r
+ public void add(Bean item) throws BindingException \r
+ {\r
+ items.put( (String) item.getIdentifier(), item.clone()); \r
+ }\r
+ \r
+ public Bean remove(String id)\r
+ {\r
+ return items.remove(id);\r
+ }\r
+ \r
+ public void remove(Bean...items)\r
+ {\r
+ for (Bean item : items)\r
+ {\r
+ try {\r
+ this.items.remove( (String) item.getIdentifier() );\r
+ } catch (BindingException e) {\r
+ }\r
+ }\r
+ }\r
+ \r
+ public List<Bean> search(String fieldName1, Object expected1) throws BindingException\r
+ {\r
+ try {\r
+ Binding binding1 = getBinding(expected1);\r
+ List<Bean> result = new ArrayList<Bean>();\r
+ for (Bean item : items.values()) {\r
+ Binding cb1 = item.getFieldBinding(fieldName1);\r
+ Object cv1 = item.getField(fieldName1);\r
+ \r
+ boolean matches1 = \r
+ (expected1==null && cv1==null) || \r
+ (cv1!=null && cb1!=null && binding1!=null && expected1!=null &&\r
+ Bindings.equals(cb1, cv1, binding1, expected1));\r
+ \r
+ if ( matches1 ) result.add(item); \r
+ }\r
+ return result;\r
+ } catch (BindingConstructionException bce) {\r
+ throw new BindingException(bce);\r
+ }\r
+ }\r
+\r
+ public List<Bean> search(String fieldName1, Object expected1, String fieldName2, Object expected2) throws BindingException\r
+ {\r
+ try {\r
+ List<Bean> result = new ArrayList<Bean>();\r
+ Binding binding1 = getBinding(expected1);\r
+ Binding binding2 = getBinding(expected2);\r
+ for (Bean item : items.values()) {\r
+ Binding cb1 = item.getFieldBinding(fieldName1);\r
+ Object cv1 = item.getField(fieldName1);\r
+ boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));\r
+ \r
+ Binding cb2 = item.getFieldBinding(fieldName2);\r
+ Object cv2 = item.getField(fieldName2);\r
+ boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));\r
+\r
+ if ( matches1 && matches2 ) result.add(item); \r
+ }\r
+ return result;\r
+ } catch (BindingConstructionException bce) {\r
+ throw new BindingException(bce);\r
+ }\r
+ }\r
+\r
+ public List<Bean> search(\r
+ String fieldName1, Object expected1,\r
+ String fieldName2, Object expected2,\r
+ String fieldName3, Object expected3)\r
+ throws BindingException\r
+ {\r
+ try {\r
+ List<Bean> result = new ArrayList<Bean>();\r
+ Binding binding1 = getBinding(expected1);\r
+ Binding binding2 = getBinding(expected2);\r
+ Binding binding3 = getBinding(expected3);\r
+ for (Bean item : items.values()) {\r
+ Binding cb1 = item.getFieldBinding(fieldName1);\r
+ Object cv1 = item.getField(fieldName1);\r
+ boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));\r
+ \r
+ Binding cb2 = item.getFieldBinding(fieldName2);\r
+ Object cv2 = item.getField(fieldName2);\r
+ boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));\r
+\r
+ Binding cb3 = item.getFieldBinding(fieldName3);\r
+ Object cv3 = item.getField(fieldName3);\r
+ boolean matches3 = (expected3==null && cv3==null) || (Bindings.equals(cb3, cv3, binding3, expected3));\r
+\r
+ if ( matches1 && matches2 && matches3 ) result.add(item);\r
+ }\r
+ return result;\r
+ } catch (BindingConstructionException bce) {\r
+ throw new BindingException(bce);\r
+ }\r
+ }\r
+ \r
+ Binding getBinding(Object data) throws BindingConstructionException {\r
+ if ( data==null ) return null;\r
+ Class<?> clazz = data.getClass();\r
+ if ( Datatype.class.isAssignableFrom(clazz) ) return Bindings.getBinding( Datatype.class );\r
+ return Bindings.getBinding(clazz);\r
+ }\r
+\r
+ public List<Bean> search(\r
+ String fieldName1, Object expected1, \r
+ String fieldName2, Object expected2, \r
+ String fieldName3, Object expected3, \r
+ String fieldName4, Object expected4, \r
+ String fieldName5, Object expected5, \r
+ String fieldName6, Object expected6, \r
+ String fieldName7, Object expected7\r
+ ) throws BindingException\r
+ {\r
+ try {\r
+ List<Bean> result = new ArrayList<Bean>();\r
+ Binding binding1 = getBinding(expected1);\r
+ Binding binding2 = getBinding(expected2);\r
+ Binding binding3 = getBinding(expected3);\r
+ Binding binding4 = getBinding(expected4);\r
+ Binding binding5 = getBinding(expected5);\r
+ Binding binding6 = getBinding(expected6);\r
+ Binding binding7 = getBinding(expected7);\r
+ for (Bean item : items.values()) {\r
+ Binding cb1 = item.getFieldBinding(fieldName1);\r
+ Object cv1 = item.getField(fieldName1);\r
+ boolean matches1 = (expected1==null && cv1==null) || (Bindings.equals(cb1, cv1, binding1, expected1));\r
+ \r
+ Binding cb2 = item.getFieldBinding(fieldName2);\r
+ Object cv2 = item.getField(fieldName2);\r
+ boolean matches2 = (expected2==null && cv2==null) || (Bindings.equals(cb2, cv2, binding2, expected2));\r
+\r
+ Binding cb3 = item.getFieldBinding(fieldName3);\r
+ Object cv3 = item.getField(fieldName3);\r
+ boolean matches3 = (expected3==null && cv3==null) || (Bindings.equals(cb3, cv3, binding3, expected3));\r
+\r
+ Binding cb4 = item.getFieldBinding(fieldName4);\r
+ Object cv4 = item.getField(fieldName4);\r
+ boolean matches4 = (expected4==null && cv4==null) || (Bindings.equals(cb4, cv4, binding4, expected4));\r
+\r
+ Binding cb5 = item.getFieldBinding(fieldName5);\r
+ Object cv5 = item.getField(fieldName5);\r
+ boolean matches5 = (expected5==null && cv5==null) || (Bindings.equals(cb5, cv5, binding5, expected5));\r
+\r
+ Binding cb6 = item.getFieldBinding(fieldName6);\r
+ Object cv6 = item.getField(fieldName6);\r
+ boolean matches6 = (expected6==null && cv6==null) || (Bindings.equals(cb6, cv6, binding6, expected6));\r
+\r
+ Binding cb7 = item.getFieldBinding(fieldName7);\r
+ Object cv7 = item.getField(fieldName7);\r
+ boolean matches7 = (expected7==null && cv7==null) || (Bindings.equals(cb7, cv7, binding7, expected7));\r
+ \r
+// if (expected2.equals("/RL10F001/XB_02#BINARY_VALUE") && cv2.equals("/RL10F001/XB_02#BINARY_VALUE"));\r
+// System.out.println("debug here");\r
+\r
+ if ( matches1 && matches2 & matches3 & matches4 & matches5 & matches6 & matches7 ) result.add(item); \r
+ }\r
+ /*\r
+ if (result.isEmpty()) {\r
+ System.out.println("No match for "+expected2);\r
+ } else {\r
+ if (result.size()>1) System.out.println("many matches for "+expected2); else\r
+ System.out.println("match for "+expected2+" is "+result.iterator().next().getField(fieldName2));\r
+ }*/\r
+ return result;\r
+ } catch (BindingConstructionException bce) {\r
+ throw new BindingException(bce);\r
+ }\r
+ }\r
+ \r
+\r
+}\r