diff options
author | Carlos Sanchez <csanchez@maestrodev.com> | 2014-11-05 20:11:08 +0100 |
---|---|---|
committer | Carlos Sanchez <carlos@apache.org> | 2014-12-01 16:48:13 +0100 |
commit | e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa (patch) | |
tree | 6477dd5905f983cc3f4cd83580795ab8a7be9ef8 /archiva-modules/plugins/metadata-store-jcr | |
parent | d9473241c956359325aa10bdbff4e39be284c559 (diff) | |
download | archiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.tar.gz archiva-e4da1fa6f5a56bbf63bae1de7d1b7dd0ae0375aa.zip |
[MRM-1390] Add REST methods to search JCR store for generic metadata and properties
Added REST methods:
artifactsByProjectVersionMetadata/{key}/{value}
artifactsByMetadata/{key}/{value}
artifactsByProperty/{key}/{value}
searchArtifacts/{text}
searchArtifacts/{key}/{text}
In JCR implementation When searching into any property (key = nil) we can't do exact searchs
Diffstat (limited to 'archiva-modules/plugins/metadata-store-jcr')
2 files changed, 121 insertions, 1 deletions
diff --git a/archiva-modules/plugins/metadata-store-jcr/pom.xml b/archiva-modules/plugins/metadata-store-jcr/pom.xml index e139e8a4d..910298258 100644 --- a/archiva-modules/plugins/metadata-store-jcr/pom.xml +++ b/archiva-modules/plugins/metadata-store-jcr/pom.xml @@ -79,6 +79,16 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>generic-metadata-support</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>maven2-repository</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> diff --git a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java index fbfe37386..d4c79b603 100644 --- a/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java +++ b/archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java @@ -40,6 +40,8 @@ import org.apache.jackrabbit.commons.JcrUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableMap; + import javax.jcr.NamespaceRegistry; import javax.jcr.Node; import javax.jcr.NodeIterator; @@ -55,6 +57,9 @@ import javax.jcr.nodetype.NodeTypeManager; import javax.jcr.nodetype.NodeTypeTemplate; import javax.jcr.query.Query; import javax.jcr.query.QueryResult; +import javax.jcr.query.Row; +import javax.jcr.query.RowIterator; + import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; @@ -66,6 +71,7 @@ import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; /** @@ -711,6 +717,83 @@ public class JcrMetadataRepository return artifacts; } + private List<ArtifactMetadata> runJcrQuery( String repositoryId, String q, Map<String, String> bindings ) + throws MetadataRepositoryException + { + List<ArtifactMetadata> artifacts; + if ( repositoryId != null ) + { + q += " AND ISDESCENDANTNODE(artifact,'/" + getRepositoryContentPath( repositoryId ) + "')"; + } + + log.info( "Running JCR Query: {}", q ); + + try + { + Query query = getJcrSession().getWorkspace().getQueryManager().createQuery( q, Query.JCR_SQL2 ); + ValueFactory valueFactory = getJcrSession().getValueFactory(); + for ( Entry<String, String> entry : bindings.entrySet() ) + { + query.bindValue( entry.getKey(), valueFactory.createValue( entry.getValue() ) ); + } + long start = Calendar.getInstance().getTimeInMillis(); + QueryResult result = query.execute(); + long end = Calendar.getInstance().getTimeInMillis(); + log.info( "JCR Query ran in {} milliseconds: {}", end - start , q ); + + artifacts = new ArrayList<>(); + RowIterator rows = result.getRows(); + while ( rows.hasNext() ) + { + Row row = rows.nextRow(); + Node node = row.getNode( "artifact" ); + artifacts.add( getArtifactFromNode( repositoryId, node ) ); + } + } + catch ( RepositoryException e ) + { + throw new MetadataRepositoryException( e.getMessage(), e ); + } + return artifacts; + } + + @Override + public List<ArtifactMetadata> getArtifactsByProjectVersionMetadata( String key, String value, String repositoryId ) + throws MetadataRepositoryException + { + String q = + "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion INNER JOIN [" + ARTIFACT_NODE_TYPE + + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) INNER JOIN [" + FACET_NODE_TYPE + + "] AS facet ON ISCHILDNODE(facet, projectVersion) WHERE ([facet].[" + key + "] = $value)"; + + return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) ); + } + + + @Override + public List<ArtifactMetadata> getArtifactsByMetadata( String key, String value, String repositoryId ) + throws MetadataRepositoryException + { + String q = + "SELECT * FROM [" + ARTIFACT_NODE_TYPE + "] AS artifact INNER JOIN [" + FACET_NODE_TYPE + + "] AS facet ON ISCHILDNODE(facet, artifact) WHERE ([facet].[" + key + "] = $value)"; + + return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) ); + } + + + @Override + public List<ArtifactMetadata> getArtifactsByProperty( String key, String value, String repositoryId ) + throws MetadataRepositoryException + { + String q = + "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion INNER JOIN [" + ARTIFACT_NODE_TYPE + + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) WHERE ([projectVersion].[" + key + + "] = $value)"; + + return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", value ) ); + } + @Override public void removeRepository( String repositoryId ) @@ -1319,6 +1402,33 @@ public class JcrMetadataRepository } } + + @Override + public List<ArtifactMetadata> searchArtifacts( String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException + { + return searchArtifacts( null, text, repositoryId, exact ); + } + + @Override + public List<ArtifactMetadata> searchArtifacts( String key, String text, String repositoryId, boolean exact ) + throws MetadataRepositoryException + { + // we can't do exact search in any property (*), we need a key + boolean e = exact && key != null; + String theKey = key == null ? "*" : "[" + key + "]"; + String projectVersionCondition = + e ? "(projectVersion." + theKey + " = $value)" : "contains([projectVersion]." + theKey + ", $value)"; + String facetCondition = e ? "(facet." + theKey + " = $value)" : "contains([facet]." + theKey + ", $value)"; + String q = + "SELECT * FROM [" + PROJECT_VERSION_NODE_TYPE + "] AS projectVersion LEFT OUTER JOIN [" + + ARTIFACT_NODE_TYPE + "] AS artifact ON ISCHILDNODE(artifact, projectVersion) LEFT OUTER JOIN [" + + FACET_NODE_TYPE + "] AS facet ON ISCHILDNODE(facet, projectVersion) WHERE (" + + projectVersionCondition + " OR " + facetCondition + ")"; + + return runJcrQuery( repositoryId, q, ImmutableMap.of( "value", text ) ); + } + private ArtifactMetadata getArtifactFromNode( String repositoryId, Node artifactNode ) throws RepositoryException { @@ -1326,7 +1436,7 @@ public class JcrMetadataRepository ArtifactMetadata artifact = new ArtifactMetadata(); artifact.setId( id ); - artifact.setRepositoryId( repositoryId ); + artifact.setRepositoryId( repositoryId == null ? artifactNode.getAncestor(2).getName() : repositoryId ); Node projectVersionNode = artifactNode.getParent(); Node projectNode = projectVersionNode.getParent(); |