public static void copy(IMetaStore from, IMetaStore to, boolean overwrite) throws MetaStoreException { // get all of the namespace in the "from" metastore List<String> namespaces = from.getNamespaces(); for (String namespace : namespaces) { // create the sme namespace in the "to" metastore try { to.createNamespace(namespace); } catch (MetaStoreNamespaceExistsException e) { // already there } // get all of the element types defined in this namespace List<IMetaStoreElementType> elementTypes = from.getElementTypes(namespace); for (IMetaStoreElementType elementType : elementTypes) { // See if it's already there IMetaStoreElementType existingType = to.getElementTypeByName(namespace, elementType.getName()); if (existingType != null) { if (overwrite) { // we looked it up by name, but need to update it by id elementType.setId(existingType.getId()); to.updateElementType(namespace, elementType); } else { elementType = existingType; } } else { // create the elementType in the "to" metastore to.createElementType(namespace, elementType); } // get all of the elements defined for this type List<IMetaStoreElement> elements = from.getElements(namespace, elementType); for (IMetaStoreElement element : elements) { IMetaStoreElement existingElement = to.getElementByName(namespace, elementType, element.getName()); if (existingElement != null) { element.setId(existingElement.getId()); if (overwrite) { to.updateElement(namespace, elementType, existingElement.getId(), element); } } else { to.createElement(namespace, elementType, element); } } } } }
@Test public void testMyElement() throws Exception { IMetaStore metaStore = new MemoryMetaStore(); // List of named elements... // List<MyNameElement> nameList = new ArrayList<MyNameElement>(); for (int i = 0; i < NR_NAME; i++) { nameList.add(new MyNameElement("name" + i, "description" + i, "color" + i)); } List<MyFilenameElement> filenameList = new ArrayList<MyFilenameElement>(); for (int i = 0; i < NR_FILENAME; i++) { filenameList.add(new MyFilenameElement("filename" + i, "size" + i, "gender" + i)); } // Construct our test element... // MyElement me = new MyElement(NAME, ATTR, ANOTHER, PASSWORD, INT, LONG, BOOL, DATE); for (int i = 0; i < NR_ATTR; i++) { me.getSubAttributes().add(new MyElementAttr("key" + i, "value" + i, "desc" + i)); } me.setNameElement(nameList.get(NR_NAME - 1)); me.setFilenameElement(filenameList.get(NR_FILENAME - 1)); List<String> stringList = Arrays.asList("a", "b", "c", "d"); me.setStringList(stringList); MyOtherElement myOtherElement = new MyOtherElement("other", "other attribute"); me.setMyOtherElement(myOtherElement); MetaStoreFactory<MyOtherElement> otherFactory = new MetaStoreFactory<MyOtherElement>(MyOtherElement.class, metaStore, "custom"); MetaStoreFactory<MyElement> factory = new MetaStoreFactory<MyElement>(MyElement.class, metaStore, "custom"); // For loading, specify the name, filename lists or factory that we're referencing... // factory.addNameList(MyElement.LIST_KEY_MY_NAMES, nameList); factory.addFilenameList(MyElement.LIST_KEY_MY_FILENAMES, filenameList); factory.addNameFactory(MyElement.FACTORY_OTHER_ELEMENT, otherFactory); // Store the class in the meta store // factory.saveElement(me); // Load the class from the meta store // MyElement verify = factory.loadElement(NAME); // Verify list element details... // IMetaStoreElement element = metaStore.getElementByName("custom", factory.getElementType(), NAME); assertNotNull(element); // Verify the general idea // assertNotNull(verify); assertEquals(ATTR, verify.getMyAttribute()); assertEquals(ANOTHER, verify.getAnotherAttribute()); assertEquals(PASSWORD, verify.getPasswordAttribute()); assertEquals(INT, verify.getIntAttribute()); assertEquals(LONG, verify.getLongAttribute()); assertEquals(BOOL, verify.isBoolAttribute()); assertEquals(DATE, verify.getDateAttribute()); assertEquals(me.getSubAttributes().size(), verify.getSubAttributes().size()); assertEquals(me.getNameElement(), verify.getNameElement()); assertEquals(me.getFilenameElement(), verify.getFilenameElement()); // verify the details... // assertTrue(metaStore.namespaceExists("custom")); IMetaStoreElementType elementType = factory.getElementType(); assertNotNull(elementType); assertEquals("My element type", elementType.getName()); assertEquals("This is my element type", elementType.getDescription()); assertNotNull(element); IMetaStoreAttribute child = element.getChild("my_attribute"); assertNotNull(child); assertEquals(ATTR, MetaStoreUtil.getAttributeString(child)); child = element.getChild("passwordAttribute"); assertNotNull(child); assertNotSame( "Password needs to be encoded", PASSWORD, MetaStoreUtil.getAttributeString(child)); child = element.getChild("anotherAttribute"); assertNotNull(child); assertEquals(ANOTHER, MetaStoreUtil.getAttributeString(child)); // Verify the child attributes as well... // This also verifies that the attributes are in the right order. // The list can't be re-ordered after loading. // for (int i = 0; i < NR_ATTR; i++) { MyElementAttr attr = verify.getSubAttributes().get(i); assertEquals("key" + i, attr.getKey()); assertEquals("value" + i, attr.getValue()); assertEquals("desc" + i, attr.getDescription()); } // Verify the referenced MyOtherElement // MyOtherElement verifyOtherElement = verify.getMyOtherElement(); assertNotNull(verifyOtherElement); assertEquals(myOtherElement.getName(), verifyOtherElement.getName()); assertEquals(myOtherElement.getSomeAttribute(), verifyOtherElement.getSomeAttribute()); // verify that the String list is loaded... List<String> verifyList = verify.getStringList(); assertEquals(stringList.size(), verifyList.size()); for (int i = 0; i < stringList.size(); i++) { assertEquals(stringList.get(i), verifyList.get(i)); } List<String> names = factory.getElementNames(); assertEquals(1, names.size()); assertEquals(NAME, names.get(0)); List<MyElement> list = factory.getElements(); assertEquals(1, list.size()); assertEquals(NAME, list.get(0).getName()); factory.deleteElement(NAME); assertEquals(0, factory.getElementNames().size()); assertEquals(0, factory.getElements().size()); }