From c646496336273232e5470799313c2ccdb18cf751 Mon Sep 17 00:00:00 2001 From: Dan Florian Date: Mon, 11 Feb 2013 09:30:40 -0500 Subject: [PATCH] TEIIDDES-1584 Create Relationships Between VDB Schema Source Artifact And VDB Translator Artifact Setup a two direcitional relationship between a source and its translator and a translator and all the sources that reference it. --- .../artifact/teiid/SourceArtifact.java | 17 +++ .../artifact/teiid/TranslatorArtifact.java | 17 +++ .../repository/sramp/SrampRepositoryUtil.java | 2 +- .../repository/sramp/deriver/VdbDeriver.java | 102 ++++++++++++------ .../sramp/deriver/VdbDeriverTest.java | 8 ++ 5 files changed, 113 insertions(+), 33 deletions(-) diff --git a/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/SourceArtifact.java b/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/SourceArtifact.java index f23277397d..0de2f47bc3 100644 --- a/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/SourceArtifact.java +++ b/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/SourceArtifact.java @@ -31,6 +31,23 @@ public String getId() { }; + /** + * A relationship between a source artifact and its translator artifact. + */ + public static final RelationshipType TRANSLATOR_RELATIONSHIP = new RelationshipType() { + + /** + * {@inheritDoc} + * + * @see org.komodo.repository.artifact.Artifact.RelationshipType#getId() + */ + @Override + public String getId() { + return "SourceTranslator"; //$NON-NLS-1$ + } + + }; + /** * The artifact type for a schema data source artifact. */ diff --git a/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/TranslatorArtifact.java b/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/TranslatorArtifact.java index 5b19bfbc04..014673776b 100644 --- a/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/TranslatorArtifact.java +++ b/komodo-repository/src/main/java/org/komodo/repository/artifact/teiid/TranslatorArtifact.java @@ -14,6 +14,23 @@ */ public interface TranslatorArtifact extends Artifact { + /** + * A relationship between a translator artifact and the source artifacts that reference it. + */ + public static final RelationshipType SOURCES_RELATIONSHIP = new RelationshipType() { + + /** + * {@inheritDoc} + * + * @see org.komodo.repository.artifact.Artifact.RelationshipType#getId() + */ + @Override + public String getId() { + return "TranslatorSources"; //$NON-NLS-1$ + } + + }; + /** * The artifact type for a translator artifact. */ diff --git a/komodo-repository/src/main/java/org/komodo/repository/sramp/SrampRepositoryUtil.java b/komodo-repository/src/main/java/org/komodo/repository/sramp/SrampRepositoryUtil.java index 8b052e5fd8..a43d9de595 100644 --- a/komodo-repository/src/main/java/org/komodo/repository/sramp/SrampRepositoryUtil.java +++ b/komodo-repository/src/main/java/org/komodo/repository/sramp/SrampRepositoryUtil.java @@ -16,7 +16,7 @@ import org.s_ramp.xmlns._2010.s_ramp.BaseArtifactType; /** - * Utilities for use with derivers. + * Utilities for use with S-RAMP repositories and derivers. */ public class SrampRepositoryUtil implements SrampRepositoryConstants { diff --git a/komodo-repository/src/main/java/org/komodo/repository/sramp/deriver/VdbDeriver.java b/komodo-repository/src/main/java/org/komodo/repository/sramp/deriver/VdbDeriver.java index 9de563b656..9a1577d237 100644 --- a/komodo-repository/src/main/java/org/komodo/repository/sramp/deriver/VdbDeriver.java +++ b/komodo-repository/src/main/java/org/komodo/repository/sramp/deriver/VdbDeriver.java @@ -8,6 +8,7 @@ package org.komodo.repository.sramp.deriver; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.UUID; import javax.xml.namespace.QName; @@ -15,6 +16,7 @@ import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import org.komodo.common.i18n.I18n; +import org.komodo.common.util.CollectionUtil; import org.komodo.common.util.Precondition; import org.komodo.common.util.StringUtil; import org.komodo.repository.RepositoryI18n; @@ -78,6 +80,9 @@ private static BaseArtifactType create(final Artifact.Type artifactType) { return artifact; } + private final Collection sources = new ArrayList(); + private final Collection translators = new ArrayList(); + /** * {@inheritDoc} * @@ -88,7 +93,9 @@ protected void derive(final Collection derivedArtifacts, final BaseArtifactType artifact, final Element rootElement, final XPath xpath) throws IOException { - LOGGER.debug("derive:root element='{}'", rootElement.getLocalName()); //$NON-NLS-1$ + LOGGER.debug("deriver:root element='{}'", rootElement.getLocalName()); //$NON-NLS-1$ + this.sources.clear(); + this.translators.clear(); if (!(artifact instanceof ExtendedArtifactType) || !VdbArtifact.TYPE.getId().equals(((ExtendedArtifactType)artifact).getExtendedType())) { @@ -104,11 +111,36 @@ protected void derive(final Collection derivedArtifacts, } processVdb(derivedArtifacts, vdbArtifact, rootElement, xpath); + postProcess(derivedArtifacts, vdbArtifact); } catch (final Exception e) { throw new IOException(e); } } + private void postProcess(final Collection derivedArtifacts, + final ExtendedArtifactType vdbArtifact) { + // setup relationships between sources and translators + if (!CollectionUtil.isEmpty(this.sources) && !CollectionUtil.isEmpty(this.translators)) { + for (final BaseArtifactType sourceArtifact : this.sources) { + final String translatorName = SrampModelUtils.getCustomProperty(sourceArtifact, + Source.PropertyName.TRANSLATOR_NAME); + if (!StringUtil.isEmpty(translatorName)) { + for (final BaseArtifactType translatorArtifact : this.translators) { + if (translatorName.equals(translatorArtifact.getName())) { + LOGGER.debug("deriver:adding relationships between source '{}' and translator '{}'", //$NON-NLS-1$ + sourceArtifact.getName(), + translatorArtifact.getName()); + SrampRepositoryUtil.addRelationship(sourceArtifact, + translatorArtifact, + SourceArtifact.TRANSLATOR_RELATIONSHIP, + TranslatorArtifact.SOURCES_RELATIONSHIP); + } + } + } + } + } + } + private void processDataRoles(final Collection derivedArtifacts, final BaseArtifactType vdbArtifact, final Element vdb, @@ -119,7 +151,7 @@ private void processDataRoles(final Collection derivedArtifact XPathConstants.NODESET); if (dataPolicies.getLength() != 0) { - LOGGER.debug("processing '{}' data policies", dataPolicies.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' data policies", dataPolicies.getLength()); //$NON-NLS-1$ for (int dataPolicyIndex = 0, numDataPolicies = dataPolicies.getLength(); dataPolicyIndex < numDataPolicies; ++dataPolicyIndex) { final Element dataPolicy = (Element)dataPolicies.item(dataPolicyIndex); @@ -153,7 +185,7 @@ private void processDataRoles(final Collection derivedArtifact XPathConstants.NODESET); if (roleNames.getLength() != 0) { - LOGGER.debug("processing '{}' mapped role names for data policy '{}'", //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' mapped role names for data policy '{}'", //$NON-NLS-1$ roleNames.getLength(), dataPolicyArtifact.getName()); @@ -170,7 +202,7 @@ private void processDataRoles(final Collection derivedArtifact mappedNames.append(name); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("found mapped role name '{}' for data policy '{}'", dataPolicyArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:found mapped role name '{}' for data policy '{}'", dataPolicyArtifact.getName()); //$NON-NLS-1$ } } @@ -181,11 +213,13 @@ private void processDataRoles(final Collection derivedArtifact } if (LOGGER.isDebugEnabled()) { - LOGGER.debug("data policy name '{}'", dataPolicyArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("data policy description '{}'", dataPolicyArtifact.getDescription()); //$NON-NLS-1$ + LOGGER.debug("deriver:data policy name '{}'", dataPolicyArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:data policy description '{}'", dataPolicyArtifact.getDescription()); //$NON-NLS-1$ for (final Property prop : dataPolicyArtifact.getProperty()) { - LOGGER.debug("data policy property '{}' with value '{}'", prop.getPropertyName(), prop.getPropertyValue()); //$NON-NLS-1$ + LOGGER.debug("deriver:data policy property '{}' with value '{}'", //$NON-NLS-1$ + prop.getPropertyName(), + prop.getPropertyValue()); } } @@ -196,7 +230,7 @@ private void processDataRoles(final Collection derivedArtifact XPathConstants.NODESET); if (permissions.getLength() != 0) { - LOGGER.debug("processing '{}' data permissions for data policy '{}'", //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' data permissions for data policy '{}'", //$NON-NLS-1$ permissions.getLength(), dataPolicyArtifact.getName()); @@ -271,11 +305,11 @@ private void processDataRoles(final Collection derivedArtifact xpath); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("permission resource name '{}'", permissionArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:permission resource name '{}'", permissionArtifact.getName()); //$NON-NLS-1$ // properties for (final Property prop : permissionArtifact.getProperty()) { - LOGGER.debug("Source property '{}' with value '{}'", //$NON-NLS-1$ + LOGGER.debug("deriver:Source property '{}' with value '{}'", //$NON-NLS-1$ prop.getPropertyName(), prop.getPropertyValue()); } @@ -306,7 +340,7 @@ private void processEntries(final Collection derivedArtifacts, XPathConstants.NODESET); if (entries.getLength() != 0) { - LOGGER.debug("processing '{}' entries", entries.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' entries", entries.getLength()); //$NON-NLS-1$ for (int entryIndex = 0, numEntries = entries.getLength(); entryIndex < numEntries; ++entryIndex) { final Element entry = (Element)entries.item(entryIndex); @@ -322,8 +356,8 @@ private void processEntries(final Collection derivedArtifacts, setDescriptionFromElementValue(entry, Entry.ManifestId.DESCRIPTION, entryArtifact, xpath); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("entry path '{}'", entryArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("entry description '{}'", entryArtifact.getDescription()); //$NON-NLS-1$ + LOGGER.debug("deriver:entry path '{}'", entryArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:entry description '{}'", entryArtifact.getDescription()); //$NON-NLS-1$ } // properties @@ -344,7 +378,7 @@ private void processProperties(final BaseArtifactType artifact, XPathConstants.NODESET); if (props.getLength() != 0) { - LOGGER.debug("processing '{}' properties", props.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' properties", props.getLength()); //$NON-NLS-1$ for (int propIndex = 0, numProps = props.getLength(); propIndex < numProps; ++propIndex) { final Element prop = (Element)props.item(propIndex); @@ -355,7 +389,7 @@ private void processProperties(final BaseArtifactType artifact, if (LOGGER.isDebugEnabled()) { for (final Property prop : artifact.getProperty()) { - LOGGER.debug("artifact '{}' has property '{}' with value '{}'", //$NON-NLS-1$ + LOGGER.debug("deriver:artifact '{}' has property '{}' with value '{}'", //$NON-NLS-1$ new Object[] {artifact.getName(), prop.getPropertyName(), prop.getPropertyValue()}); } } @@ -372,7 +406,7 @@ private void processSchemas(final Collection derivedArtifacts, XPathConstants.NODESET); if (schemas.getLength() != 0) { - LOGGER.debug("processing '{}' schemas", schemas.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' schemas", schemas.getLength()); //$NON-NLS-1$ for (int schemaIndex = 0, numSchemas = schemas.getLength(); schemaIndex < numSchemas; ++schemaIndex) { final Element schema = (Element)schemas.item(schemaIndex); @@ -388,8 +422,8 @@ private void processSchemas(final Collection derivedArtifacts, setDescriptionFromElementValue(schema, Schema.ManifestId.DESCRIPTION, schemaArtifact, xpath); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("schema name '{}'", schemaArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("schema description '{}'", schemaArtifact.getDescription()); //$NON-NLS-1$ + LOGGER.debug("deriver:schema name '{}'", schemaArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:schema description '{}'", schemaArtifact.getDescription()); //$NON-NLS-1$ } { // visible @@ -430,12 +464,13 @@ private void processSchemas(final Collection derivedArtifacts, XPathConstants.NODESET); if (sources.getLength() != 0) { - LOGGER.debug("processing '{}' sources for schema '{}'", sources.getLength(), schemaArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' sources for schema '{}'", sources.getLength(), schemaArtifact.getName()); //$NON-NLS-1$ for (int sourceIndex = 0, numSources = sources.getLength(); sourceIndex < numSources; ++sourceIndex) { final Element source = (Element)sources.item(sourceIndex); final BaseArtifactType sourceArtifact = VdbDeriver.create(SourceArtifact.TYPE); derivedArtifacts.add(sourceArtifact); + this.sources.add(sourceArtifact); { // name final String name = source.getAttribute(Source.ManifestId.Attribute.NAME); @@ -453,11 +488,11 @@ private void processSchemas(final Collection derivedArtifacts, } if (LOGGER.isDebugEnabled()) { - LOGGER.debug("schema source name '{}'", sourceArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:schema source name '{}'", sourceArtifact.getName()); //$NON-NLS-1$ // properties for (final Property prop : sourceArtifact.getProperty()) { - LOGGER.debug("Source property '{}' with value '{}'", //$NON-NLS-1$ + LOGGER.debug("deriver:Source property '{}' with value '{}'", //$NON-NLS-1$ prop.getPropertyName(), prop.getPropertyValue()); } @@ -487,12 +522,13 @@ private void processTranslators(final Collection derivedArtifa XPathConstants.NODESET); if (translators.getLength() != 0) { - LOGGER.debug("processing '{}' translators", translators.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' translators", translators.getLength()); //$NON-NLS-1$ for (int translatorIndex = 0, numTranslators = translators.getLength(); translatorIndex < numTranslators; ++translatorIndex) { final Element translator = (Element)translators.item(translatorIndex); final BaseArtifactType translatorArtifact = VdbDeriver.create(TranslatorArtifact.TYPE); derivedArtifacts.add(translatorArtifact); + this.translators.add(translatorArtifact); { // name final String name = translator.getAttribute(Translator.ManifestId.Attribute.NAME); @@ -505,8 +541,8 @@ private void processTranslators(final Collection derivedArtifa } if (LOGGER.isDebugEnabled()) { - LOGGER.debug("translator name '{}'", translatorArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("translator description '{}'", translatorArtifact.getDescription()); //$NON-NLS-1$ + LOGGER.debug("deriver:translator name '{}'", translatorArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:translator description '{}'", translatorArtifact.getDescription()); //$NON-NLS-1$ } { // type @@ -539,9 +575,9 @@ private void processVdb(final Collection derivedArtifacts, setVersionFromAttribueValue(vdb, Vdb.ManifestId.Attribute.VERSION, vdbArtifact, xpath); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("VDB name '{}'", vdbArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("VDB description '{}'", vdbArtifact.getDescription()); //$NON-NLS-1$ - LOGGER.debug("VDB version '{}'", vdbArtifact.getVersion()); //$NON-NLS-1$ + LOGGER.debug("deriver:VDB name '{}'", vdbArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:VDB description '{}'", vdbArtifact.getDescription()); //$NON-NLS-1$ + LOGGER.debug("deriver:VDB version '{}'", vdbArtifact.getVersion()); //$NON-NLS-1$ } processProperties(vdbArtifact, vdb, xpath); @@ -564,7 +600,7 @@ private void processVdbImports(final Collection derivedArtifac XPathConstants.NODESET); if (vdbImports.getLength() != 0) { - LOGGER.debug("processing '{}' VDB imports", vdbImports.getLength()); //$NON-NLS-1$ + LOGGER.debug("deriver:processing '{}' VDB imports", vdbImports.getLength()); //$NON-NLS-1$ for (int vdbImportIndex = 0, numVdbImports = vdbImports.getLength(); vdbImportIndex < numVdbImports; ++vdbImportIndex) { final Element vdbImport = (Element)vdbImports.item(vdbImportIndex); @@ -592,12 +628,14 @@ private void processVdbImports(final Collection derivedArtifac } if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Import VDB name '{}'", vdbImportArtifact.getName()); //$NON-NLS-1$ - LOGGER.debug("Import VDB version '{}'", vdbImportArtifact.getVersion()); //$NON-NLS-1$ + LOGGER.debug("deriver:Import VDB name '{}'", vdbImportArtifact.getName()); //$NON-NLS-1$ + LOGGER.debug("deriver:Import VDB version '{}'", vdbImportArtifact.getVersion()); //$NON-NLS-1$ // properties for (final Property prop : vdbImportArtifact.getProperty()) { - LOGGER.debug("Import VDB property '{}' with value '{}'", prop.getPropertyName(), prop.getPropertyValue()); //$NON-NLS-1$ + LOGGER.debug("deriver:Import VDB property '{}' with value '{}'", //$NON-NLS-1$ + prop.getPropertyName(), + prop.getPropertyValue()); } } @@ -617,7 +655,7 @@ protected Object query(final XPath xpath, final Element context, final String query, final QName returnType) throws XPathExpressionException { - LOGGER.debug("executing query '{}'", query); //$NON-NLS-1$ + LOGGER.debug("deriver:executing query '{}'", query); //$NON-NLS-1$ return super.query(xpath, context, query, returnType); } diff --git a/komodo-repository/src/test/java/org/komodo/repository/sramp/deriver/VdbDeriverTest.java b/komodo-repository/src/test/java/org/komodo/repository/sramp/deriver/VdbDeriverTest.java index 51116c98ea..a356b99536 100644 --- a/komodo-repository/src/test/java/org/komodo/repository/sramp/deriver/VdbDeriverTest.java +++ b/komodo-repository/src/test/java/org/komodo/repository/sramp/deriver/VdbDeriverTest.java @@ -342,6 +342,9 @@ public void shouldDeriveTwitterVdbArtifacts() throws Exception { boolean foundViewModel = false; boolean foundDataSource = false; + BaseArtifactType sourceArtifact = null; + BaseArtifactType translatorArtifact = null; + for (final Artifact derivedArtifact : results) { assertSrampArtifact(derivedArtifact); @@ -350,6 +353,8 @@ public void shouldDeriveTwitterVdbArtifacts() throws Exception { if (!foundTranslator && isExtendedType(srampArtifact, TranslatorArtifact.TYPE)) { foundTranslator = true; + translatorArtifact = srampArtifact.getDelegate(); + assertThat(artifactName, is("rest")); assertPropertyValue(derivedArtifact, Translator.PropertyName.TYPE, "ws"); assertPropertyValue(derivedArtifact, "DefaultBinding", "HTTP"); @@ -390,6 +395,7 @@ public void shouldDeriveTwitterVdbArtifacts() throws Exception { } } else if (!foundDataSource && isExtendedType(srampArtifact, SourceArtifact.TYPE)) { foundDataSource = true; + sourceArtifact = srampArtifact.getDelegate(); assertThat(artifactName, is("twitter")); assertPropertyValue(derivedArtifact, Source.PropertyName.TRANSLATOR_NAME, "rest"); assertPropertyValue(derivedArtifact, Source.PropertyName.JNDI_NAME, "java:/twitterDS"); @@ -399,6 +405,8 @@ public void shouldDeriveTwitterVdbArtifacts() throws Exception { } assertThat((foundTranslator && foundPhysicalModel && foundViewModel && foundDataSource), is(true)); + assertRelationshipTargetUuid(sourceArtifact, SourceArtifact.TRANSLATOR_RELATIONSHIP, translatorArtifact.getUuid()); + assertRelationshipTargetUuid(translatorArtifact, TranslatorArtifact.SOURCES_RELATIONSHIP, sourceArtifact.getUuid()); } }