Ejemplo n.º 1
0
  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);
          }
        }
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Get a sorted list of element names for the specified element type in the given namespace.
   *
   * @param namespace
   * @param metaStore
   * @param elementType
   * @return
   * @throws MetaStoreException
   */
  public String[] getElementNames(
      String namespace, IMetaStore metaStore, IMetaStoreElementType elementType)
      throws MetaStoreException {
    List<String> names = new ArrayList<String>();

    List<IMetaStoreElement> elements = metaStore.getElements(namespace, elementType);
    for (IMetaStoreElement element : elements) {
      names.add(element.getName());
    }

    // Alphabetical sort
    //
    Collections.sort(names);

    return names.toArray(new String[names.size()]);
  }
  @Test
  public void testIDMigration() throws Exception {

    String namespace = "custom";
    String stepName = "Step Name";
    String elementName = "migration";
    String hostName = "Host Name";
    String fieldMappings = "Field Mappings";
    String sourceFieldName = "Source Field Name";
    String targetFieldName = "Target Field Name";
    String parameterName = "Parameter Name";

    IMetaStore metaStore = new MemoryMetaStore();

    MetaStoreFactory<MyMigrationElement> factory =
        new MetaStoreFactory<MyMigrationElement>(MyMigrationElement.class, metaStore, namespace);

    if (!metaStore.namespaceExists(namespace)) {
      metaStore.createNamespace(namespace);
    }

    MetaStoreElementType elementTypeAnnotation =
        MyMigrationElement.class.getAnnotation(MetaStoreElementType.class);

    // Make sure the element type exists...
    IMetaStoreElementType elementType =
        metaStore.getElementTypeByName(namespace, elementTypeAnnotation.name());
    if (elementType == null) {
      elementType = metaStore.newElementType(namespace);
      elementType.setName(elementTypeAnnotation.name());
      elementType.setDescription(elementTypeAnnotation.description());
      metaStore.createElementType(namespace, elementType);
    }

    // Create an element with the old keys we want to migrate
    IMetaStoreElement element = metaStore.newElement();
    element.setName(elementName);
    element.setElementType(elementType);

    element.addChild(metaStore.newAttribute("stepName", stepName));
    element.addChild(metaStore.newAttribute("hostname", hostName));
    element.addChild(metaStore.newAttribute("fieldMappings", fieldMappings));
    element.addChild(metaStore.newAttribute("sourceFieldName", sourceFieldName));
    element.addChild(metaStore.newAttribute("targetFieldName", targetFieldName));
    element.addChild(metaStore.newAttribute("parameterName", parameterName));

    metaStore.createElement(namespace, elementType, element);

    MyMigrationElement loadedElement = factory.loadElement(elementName);

    assertNotNull(loadedElement);
    assertEquals(loadedElement.getStepName(), stepName);
    assertEquals(loadedElement.getHostname(), hostName);
    assertEquals(loadedElement.getFieldMappings(), fieldMappings);
    assertEquals(loadedElement.getSourceFieldName(), sourceFieldName);
    assertEquals(loadedElement.getTargetFieldName(), targetFieldName);
    assertEquals(loadedElement.getParameterName(), parameterName);

    // Test the variation of the step name id
    element = metaStore.newElement();
    element.setName(elementName);
    element.setElementType(elementType);

    element.addChild(metaStore.newAttribute("stepname", stepName));

    metaStore.createElement(namespace, elementType, element);

    loadedElement = factory.loadElement(elementName);

    assertNotNull(loadedElement);
    assertEquals(loadedElement.getStepname(), stepName);
  }
  @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());
  }
Ejemplo n.º 5
0
 /**
  * Create the specified namespace if it doesn't exist.
  *
  * @param metaStore
  * @param namespace
  * @throws MetaStoreException
  */
 public static void verifyNamespaceCreated(IMetaStore metaStore, String namespace)
     throws MetaStoreException {
   if (!metaStore.namespaceExists(namespace)) {
     metaStore.createNamespace(namespace);
   }
 }