/**
   * Used to save a model entry
   *
   * @param entry
   * @throws Exception
   */
  ModelElement(final VdbModelEntry entry) throws Exception {
    super(entry);
    final String lastSeg = entry.getPath().lastSegment();
    final int ndx = lastSeg.lastIndexOf('.');
    name = (ndx < 0 ? lastSeg : lastSeg.substring(0, ndx));
    type = entry.getType();
    visible = entry.isVisible();
    description = entry.getDescription();

    for (VdbSource source : entry.getSourceInfo().getSources()) {
      getSources().add(new SourceElement(source));
    }
    for (final Problem problem : entry.getProblems())
      getProblems().add(new ProblemElement(problem));
    final List<PropertyElement> props = getProperties();
    String modelUuid = entry.getModelUuid();
    if (modelUuid != null) {
      props.add(new PropertyElement(MODEL_UUID, entry.getModelUuid()));
    }
    String modelClass = entry.getModelClass();
    if (modelClass != null) {
      props.add(new PropertyElement(MODEL_CLASS, entry.getModelClass()));
    }
    props.add(new PropertyElement(BUILT_IN, Boolean.toString(entry.isBuiltIn())));
    if (entry.getSourceInfo().isMultiSource()) {
      props.add(new PropertyElement(MULTI_SOURCE, Boolean.toString(true)));
      if (entry.getSourceInfo().isAddColumn()) {
        props.add(new PropertyElement(MULTI_SOURCE_ADD_COLUMN, Boolean.toString(true)));
      }
      String alias = entry.getSourceInfo().getColumnAlias();
      if (alias != null && alias.length() > 0) {
        props.add(new PropertyElement(MULTI_SOURCE_COLUMN_ALIAS, alias));
      }
    }

    for (Object key : entry.getProperties().keySet()) {
      props.add(new PropertyElement((String) key, (String) entry.getProperties().get(key)));
    }
    for (final VdbEntry importedEntry : entry.getImports())
      props.add(new PropertyElement(IMPORTS, importedEntry.getName().toString()));
    for (final String importedVdbName : entry.getImportVdbNames())
      props.add(new PropertyElement(IMPORT_VDB_REFERENCE, importedVdbName));

    if (entry.getSchemaText() != null) {
      getMetadata().add(new MetadataElement(entry.getSchemaText(), entry.getType()));
    }
  }
  @Test
  public void convertDynamicVdbToXmiVdb() throws Exception {
    DynamicVdb dynVdb = VdbTestUtils.mockPortfolioDynamicVdb(modelWorkspaceMock);
    assertNotNull(dynVdb);

    IFile dynVdbSrcFile = dynVdb.getSourceFile();
    IProject parent = dynVdbSrcFile.getProject();
    assertNotNull(parent);

    File destFile = File.createTempFile(dynVdb.getName(), ITeiidVdb.VDB_DOT_EXTENSION);
    MockFileBuilder destination = new MockFileBuilder(destFile);

    XmiVdb xmiVdb = dynVdb.convert(XmiVdb.class, destination.getResourceFile(), new Properties());

    assertEquals(dynVdb.getName(), xmiVdb.getName());
    assertEquals(dynVdb.getDescription(), xmiVdb.getDescription());

    for (Map.Entry<Object, Object> entry : dynVdb.getProperties().entrySet()) {
      System.out.println(
          "VDB Property:  "
              + entry.getValue()
              + " == "
              + xmiVdb.getProperties().getProperty(entry.getKey().toString()));
      assertEquals(entry.getValue(), xmiVdb.getProperties().getProperty(entry.getKey().toString()));
    }

    assertEquals(destination.getResourceFile(), xmiVdb.getSourceFile());
    assertEquals(dynVdb.getVersion(), xmiVdb.getVersion());

    assertEquals(dynVdb.getConnectionType(), xmiVdb.getConnectionType());
    assertEquals(dynVdb.isPreview(), xmiVdb.isPreview());
    assertEquals(dynVdb.getQueryTimeout(), xmiVdb.getQueryTimeout());

    assertEquals(dynVdb.getAllowedLanguages().size(), xmiVdb.getAllowedLanguages().size());
    List<String> dynLanguageValues =
        Arrays.asList(xmiVdb.getAllowedLanguages().getAllowedLanguageValues());
    for (String language : dynVdb.getAllowedLanguages().getAllowedLanguageValues()) {
      assertTrue(dynLanguageValues.contains(language));
    }

    assertEquals(dynVdb.getSecurityDomain(), xmiVdb.getSecurityDomain());
    assertEquals(dynVdb.getGssPattern(), xmiVdb.getGssPattern());
    assertEquals(dynVdb.getPasswordPattern(), xmiVdb.getPasswordPattern());
    assertEquals(dynVdb.getAuthenticationType(), xmiVdb.getAuthenticationType());
    assertEquals(dynVdb.getValidationDateTime(), xmiVdb.getValidationDateTime());
    assertEquals(dynVdb.isAutoGenerateRESTWar(), xmiVdb.isAutoGenerateRESTWar());

    assertEquals(dynVdb.getImports().size(), xmiVdb.getImports().size());
    for (VdbImportVdbEntry entry : dynVdb.getImports()) {
      assertTrue(xmiVdb.getImports().contains(entry));
    }

    assertEquals(dynVdb.getTranslators().size(), xmiVdb.getTranslators().size());
    for (TranslatorOverride translator : dynVdb.getTranslators()) {
      assertTrue(xmiVdb.getTranslators().contains(translator));
    }

    assertEquals(dynVdb.getDataRoles().size(), xmiVdb.getDataRoles().size());
    for (DataRole role : dynVdb.getDataRoles()) {
      assertTrue(xmiVdb.getDataRoles().contains(role));
    }

    assertEquals(dynVdb.getDynamicModels().size(), xmiVdb.getModelEntries().size());
    for (DynamicModel dynModel : dynVdb.getDynamicModels()) {

      VdbModelEntry modelEntry = null;
      Collection<VdbModelEntry> entries = xmiVdb.getModelEntries();
      for (VdbModelEntry entry : entries) {
        if (dynModel.getName().equals(entry.getName())) {
          modelEntry = entry;
          break;
        }
      }
      assertNotNull(modelEntry);

      assertEquals(dynModel.getDescription(), modelEntry.getDescription());

      for (Map.Entry<Object, Object> prop : dynModel.getProperties().entrySet()) {
        assertEquals(
            prop.getValue(), modelEntry.getProperties().getProperty(prop.getKey().toString()));
      }

      VdbSourceInfo sourceInfo = modelEntry.getSourceInfo();

      assertEquals(dynModel.getModelType().toString(), modelEntry.getType());
      assertEquals(dynModel.isMultiSource(), sourceInfo.isMultiSource());
      assertEquals(dynModel.doAddColumn(), sourceInfo.isAddColumn());
      assertEquals(dynModel.getColumnAlias(), sourceInfo.getColumnAlias());

      assertEquals(dynModel.getSources().length, sourceInfo.getSources().size());
      List<VdbSource> entrySources = new ArrayList<VdbSource>(sourceInfo.getSources());
      for (VdbSource source : dynModel.getSources()) {
        assertTrue(entrySources.contains(source));
      }
    }
  }