1 package org.apache.archiva.metadata.repository.cassandra;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.google.common.base.Function;
23 import com.google.common.base.Predicate;
24 import com.google.common.collect.Iterables;
25 import com.netflix.astyanax.entitystore.EntityManager;
26 import net.sf.beanlib.provider.replicator.BeanReplicator;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.FacetedMetadata;
30 import org.apache.archiva.metadata.model.MetadataFacet;
31 import org.apache.archiva.metadata.model.MetadataFacetFactory;
32 import org.apache.archiva.metadata.model.ProjectMetadata;
33 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
34 import org.apache.archiva.metadata.model.ProjectVersionReference;
35 import org.apache.archiva.metadata.repository.MetadataRepository;
36 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
37 import org.apache.archiva.metadata.repository.MetadataResolutionException;
38 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
39 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
40 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
41 import org.apache.archiva.metadata.repository.cassandra.model.Project;
42 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
43 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import javax.persistence.PersistenceException;
49 import java.util.ArrayList;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.HashSet;
55 import java.util.Iterator;
56 import java.util.List;
61 * @author Olivier Lamy
64 public class CassandraMetadataRepository
65 implements MetadataRepository
68 private Logger logger = LoggerFactory.getLogger( getClass() );
70 private ArchivaConfiguration configuration;
72 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
74 private CassandraEntityManagerFactory cassandraEntityManagerFactory;
76 public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
77 ArchivaConfiguration configuration,
78 CassandraEntityManagerFactory cassandraEntityManagerFactory )
80 this.metadataFacetFactories = metadataFacetFactories;
81 this.configuration = configuration;
82 this.cassandraEntityManagerFactory = cassandraEntityManagerFactory;
86 public EntityManager<Repository, String> getRepositoryEntityManager()
88 return this.cassandraEntityManagerFactory.getRepositoryEntityManager();
91 public EntityManager<Namespace, String> getNamespaceEntityManager()
93 return this.cassandraEntityManagerFactory.getNamespaceEntityManager();
96 public EntityManager<Project, String> getProjectEntityManager()
98 return this.cassandraEntityManagerFactory.getProjectEntityManager();
101 public EntityManager<ArtifactMetadataModel, String> getArtifactMetadataModelEntityManager()
103 return cassandraEntityManagerFactory.getArtifactMetadataModelEntityManager();
106 public EntityManager<MetadataFacetModel, String> getMetadataFacetModelEntityManager()
108 return this.cassandraEntityManagerFactory.getMetadataFacetModelEntityManager();
111 public EntityManager<ProjectVersionMetadataModel, String> getProjectVersionMetadataModelEntityManager()
113 return this.cassandraEntityManagerFactory.getProjectVersionMetadataModelEntityManager();
117 public void updateNamespace( String repositoryId, String namespaceId )
118 throws MetadataRepositoryException
120 updateOrAddNamespace( repositoryId, namespaceId );
124 public Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
125 throws MetadataRepositoryException
129 Repository repository = this.getRepositoryEntityManager().get( repositoryId );
131 if ( repository == null )
133 repository = new Repository( repositoryId );
135 Namespace namespace = new Namespace( namespaceId, repository );
136 this.getRepositoryEntityManager().put( repository );
138 this.getNamespaceEntityManager().put( namespace );
140 // FIXME add a Namespace id builder
141 Namespace namespace = getNamespaceEntityManager().get(
142 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build() );
143 if ( namespace == null )
145 namespace = new Namespace( namespaceId, repository );
146 getNamespaceEntityManager().put( namespace );
150 catch ( PersistenceException e )
152 throw new MetadataRepositoryException( e.getMessage(), e );
159 public void removeNamespace( String repositoryId, String namespaceId )
160 throws MetadataRepositoryException
164 Namespace namespace = getNamespaceEntityManager().get(
165 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build() );
166 if ( namespace != null )
168 getNamespaceEntityManager().remove( namespace );
171 catch ( PersistenceException e )
173 throw new MetadataRepositoryException( e.getMessage(), e );
179 public void removeRepository( final String repositoryId )
180 throws MetadataRepositoryException
184 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
186 // remove data related to the repository
187 this.getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
190 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
192 if ( artifactMetadataModel != null )
194 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId ) )
196 artifactMetadataModels.add( artifactMetadataModel );
203 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
205 final List<Namespace> namespaces = new ArrayList<Namespace>();
207 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
210 public Boolean apply( Namespace namespace )
212 if ( namespace != null )
214 if ( StringUtils.equals( namespace.getRepository().getId(), repositoryId ) )
216 namespaces.add( namespace );
223 getNamespaceEntityManager().remove( namespaces );
225 final List<Project> projects = new ArrayList<Project>();
226 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
229 public Boolean apply( Project project )
231 if ( project != null )
233 if ( StringUtils.equals( project.getNamespace().getRepository().getId(), repositoryId ) )
235 projects.add( project );
242 getProjectEntityManager().remove( projects );
244 // TODO cleanup or not
245 //final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>( );
246 //getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
248 final List<ProjectVersionMetadataModel> projectVersionMetadataModels =
249 new ArrayList<ProjectVersionMetadataModel>();
251 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
254 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
256 if ( projectVersionMetadataModel != null )
258 if ( StringUtils.equals( projectVersionMetadataModel.getNamespace().getRepository().getId(),
261 projectVersionMetadataModels.add( projectVersionMetadataModel );
268 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModels );
270 Repository repository = getRepositoryEntityManager().get( repositoryId );
271 if ( repository != null )
273 getRepositoryEntityManager().remove( repository );
277 catch ( PersistenceException e )
279 throw new MetadataRepositoryException( e.getMessage(), e );
284 public Collection<String> getRepositories()
285 throws MetadataRepositoryException
289 logger.debug( "getRepositories" );
291 List<Repository> repositories = getRepositoryEntityManager().getAll();
292 if ( repositories == null )
294 return Collections.emptyList();
296 List<String> repoIds = new ArrayList<String>( repositories.size() );
297 for ( Repository repository : repositories )
299 repoIds.add( repository.getName() );
301 logger.debug( "getRepositories found: {}", repoIds );
304 catch ( PersistenceException e )
306 throw new MetadataRepositoryException( e.getMessage(), e );
313 public Collection<String> getRootNamespaces( final String repoId )
314 throws MetadataResolutionException
319 //final List<Namespace> namespaceList =
320 // getNamespaceEntityManager().find( "SELECT name FROM namespace WHERE repository.id='"+ repoId + "'" );
322 //final Set<String> namespaces = new HashSet<String>( namespaceList.size() );
324 final Set<String> namespaces = new HashSet<String>( );
327 for ( Namespace namespace : namespaceList )
329 String name = namespace.getName();
330 if ( StringUtils.isNotEmpty( name ) )
332 namespaces.add( StringUtils.substringBefore( name, "." ) );
338 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
340 // @Nullable add dependency ?
342 public Boolean apply( Namespace namespace )
344 if ( namespace != null && namespace.getRepository() != null
345 && StringUtils.equalsIgnoreCase( repoId, namespace.getRepository().getId() ) )
347 String name = namespace.getName();
348 if ( StringUtils.isNotEmpty( name ) )
350 namespaces.add( StringUtils.substringBefore( name, "." ) );
359 catch ( PersistenceException e )
361 throw new MetadataResolutionException( e.getMessage(), e );
366 public Collection<String> getNamespaces( final String repoId, final String namespaceId )
367 throws MetadataResolutionException
371 final Set<String> namespaces = new HashSet<String>();
373 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
375 // @Nullable add dependency ?
377 public Boolean apply( Namespace namespace )
379 if ( namespace != null && namespace.getRepository() != null && StringUtils.equalsIgnoreCase( repoId,
380 namespace.getRepository().getId() ) )
382 String currentNamespace = namespace.getName();
383 // we only return childs
384 if ( StringUtils.startsWith( currentNamespace, namespaceId ) && (
385 StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
387 // store after namespaceId '.' but before next '.'
388 // call org namespace org.apache.maven.shared -> stored apache
390 String calledNamespace =
391 StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
392 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
394 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
396 namespaces.add( storedNamespace );
405 catch ( PersistenceException e )
407 throw new MetadataResolutionException( e.getMessage(), e );
412 public List<String> getNamespaces( final String repoId )
413 throws MetadataResolutionException
417 logger.debug( "getNamespaces for repository '{}'", repoId );
418 //TypedQuery<Repository> typedQuery =
419 // entityManager.createQuery( "select n from Namespace n where n.repository_id=:id", Namespace.class );
421 //List<Repository> namespaces = typedQuery.setParameter( "id", repoId ).getResultList();
423 Repository repository = getRepositoryEntityManager().get( repoId );
425 if ( repository == null )
427 return Collections.emptyList();
430 // FIXME find correct cql query
431 //String query = "select * from namespace where repository.id = '" + repoId + "';";
433 //List<Namespace> namespaces = getNamespaceEntityManager().find( query );
435 //final Set<Namespace> namespaces = new HashSet<Namespace>();
436 final Set<String> namespaces = new HashSet<String>();
438 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
440 // @Nullable add dependency ?
442 public Boolean apply( Namespace namespace )
444 if ( namespace != null && namespace.getRepository() != null && StringUtils.equalsIgnoreCase( repoId,
445 namespace.getRepository().getId() ) )
447 namespaces.add( namespace.getId() );
456 repository.setNamespaces( new ArrayList<Namespace>( namespaces ) );
457 if ( repository == null || repository.getNamespaces().isEmpty() )
459 return Collections.emptyList();
462 List<String> namespaceIds = new ArrayList<String>( repository.getNamespaces().size() );
464 for ( Namespace n : repository.getNamespaces() )
466 namespaceIds.add( n.getName() );
469 logger.debug( "getNamespaces for repository '{}' found {}", repoId, namespaceIds.size() );
473 return new ArrayList<String>( namespaces );
475 catch ( PersistenceException e )
477 throw new MetadataResolutionException( e.getMessage(), e );
483 public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
484 throws MetadataRepositoryException
487 // project exists ? if yes return
488 String projectKey = new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace(
489 new Namespace( projectMetadata.getNamespace(), new Repository( repositoryId ) ) ).build();
491 Project project = getProjectEntityManager().get( projectKey );
492 if ( project != null )
497 String namespaceKey = new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace(
498 projectMetadata.getNamespace() ).build();
499 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
500 if ( namespace == null )
502 namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
505 project = new Project( projectKey, projectMetadata.getId(), namespace );
509 getProjectEntityManager().put( project );
511 catch ( PersistenceException e )
513 throw new MetadataRepositoryException( e.getMessage(), e );
519 public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
520 throws MetadataRepositoryException
523 // cleanup ArtifactMetadataModel
524 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
526 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
529 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
531 if ( artifactMetadataModel != null )
533 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
534 && StringUtils.equals( artifactMetadataModel.getNamespace(), namespaceId )
535 && StringUtils.equals( artifactMetadataModel.getProject(), projectId ) )
537 artifactMetadataModels.add( artifactMetadataModel );
544 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
546 Namespace namespace = new Namespace( namespaceId, new Repository( repositoryId ) );
548 final List<ProjectVersionMetadataModel> projectVersionMetadataModels =
549 new ArrayList<ProjectVersionMetadataModel>();
551 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
554 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
556 if ( projectVersionMetadataModel != null )
558 if ( StringUtils.equals( repositoryId,
559 projectVersionMetadataModel.getNamespace().getRepository().getName() )
560 && StringUtils.equals( namespaceId, projectVersionMetadataModel.getNamespace().getName() )
561 && StringUtils.equals( projectId, projectVersionMetadataModel.getProjectId() ) )
563 projectVersionMetadataModels.add( projectVersionMetadataModel );
570 if ( !projectVersionMetadataModels.isEmpty() )
572 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModels );
575 String key = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
577 Project project = getProjectEntityManager().get( key );
578 if ( project == null )
580 logger.debug( "removeProject notfound" );
583 logger.debug( "removeProject {}", project );
585 getProjectEntityManager().remove( project );
589 public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
590 throws MetadataResolutionException
592 final Set<String> versions = new HashSet<String>();
593 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
596 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
598 if ( projectVersionMetadataModel != null )
600 if ( StringUtils.equals( repoId,
601 projectVersionMetadataModel.getNamespace().getRepository().getName() )
602 && StringUtils.startsWith( projectVersionMetadataModel.getNamespace().getName(), namespace )
603 && StringUtils.equals( projectId, projectVersionMetadataModel.getProjectId() ) )
605 versions.add( projectVersionMetadataModel.getId() );
611 // FIXME use cql query
612 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
615 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
617 if ( artifactMetadataModel != null )
619 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
620 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
621 artifactMetadataModel.getProject() ) )
623 versions.add( artifactMetadataModel.getProjectVersion() );
634 public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
635 ArtifactMetadata artifactMeta )
636 throws MetadataRepositoryException
638 String namespaceKey =
639 new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespaceId ).build();
640 // create the namespace if not exists
641 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
642 if ( namespace == null )
644 namespace = updateOrAddNamespace( repositoryId, namespaceId );
647 // create the project if not exist
648 String projectKey = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
650 Project project = getProjectEntityManager().get( projectKey );
651 if ( project == null )
653 project = new Project( projectKey, projectId, namespace );
656 getProjectEntityManager().put( project );
658 catch ( PersistenceException e )
660 throw new MetadataRepositoryException( e.getMessage(), e );
664 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
665 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
667 ArtifactMetadataModel artifactMetadataModel = getArtifactMetadataModelEntityManager().get( key );
668 if ( artifactMetadataModel == null )
670 artifactMetadataModel = new ArtifactMetadataModel( key, artifactMeta.getId(), repositoryId, namespaceId,
671 artifactMeta.getProject(), projectVersion,
672 artifactMeta.getVersion(),
673 artifactMeta.getFileLastModified(),
674 artifactMeta.getSize(), artifactMeta.getMd5(),
675 artifactMeta.getSha1(), artifactMeta.getWhenGathered() );
680 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified().getTime() );
681 artifactMetadataModel.setWhenGathered( artifactMeta.getWhenGathered().getTime() );
682 artifactMetadataModel.setSize( artifactMeta.getSize() );
683 artifactMetadataModel.setMd5( artifactMeta.getMd5() );
684 artifactMetadataModel.setSha1( artifactMeta.getSha1() );
685 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
690 getArtifactMetadataModelEntityManager().put( artifactMetadataModel );
692 catch ( PersistenceException e )
694 throw new MetadataRepositoryException( e.getMessage(), e );
697 key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
698 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
700 ProjectVersionMetadataModel projectVersionMetadataModel =
701 getProjectVersionMetadataModelEntityManager().get( key );
703 if ( projectVersionMetadataModel == null )
705 projectVersionMetadataModel = new ProjectVersionMetadataModel();
706 projectVersionMetadataModel.setRowId( key );
707 projectVersionMetadataModel.setProjectId( projectId );
708 projectVersionMetadataModel.setId( projectVersion );
709 projectVersionMetadataModel.setNamespace( namespace );
711 getProjectVersionMetadataModelEntityManager().put( projectVersionMetadataModel );
716 updateFacets( artifactMeta, artifactMetadataModel );
721 public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
722 final String projectVersion )
723 throws MetadataResolutionException
725 final Set<String> versions = new HashSet<String>();
726 // FIXME use cql query
727 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
730 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
732 if ( artifactMetadataModel != null )
734 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
735 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
736 artifactMetadataModel.getProject() )
737 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
739 versions.add( artifactMetadataModel.getVersion() );
750 * iterate over available facets to remove/add from the artifactMetadata
752 * @param facetedMetadata
753 * @param artifactMetadataModel only use for the key
755 private void updateFacets( final FacetedMetadata facetedMetadata,
756 final ArtifactMetadataModel artifactMetadataModel )
759 for ( final String facetId : metadataFacetFactories.keySet() )
761 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
762 if ( metadataFacet == null )
768 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
770 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
773 public Boolean apply( MetadataFacetModel metadataFacetModel )
775 ArtifactMetadataModel tmp = metadataFacetModel.getArtifactMetadataModel();
776 if ( StringUtils.equals( metadataFacetModel.getFacetId(), facetId ) && StringUtils.equals(
777 tmp.getRepositoryId(), artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
778 tmp.getNamespace(), artifactMetadataModel.getNamespace() ) && StringUtils.equals(
779 tmp.getProject(), artifactMetadataModel.getProject() ) )
781 metadataFacetModels.add( metadataFacetModel );
787 getMetadataFacetModelEntityManager().remove( metadataFacetModels );
789 Map<String, String> properties = metadataFacet.toProperties();
791 final List<MetadataFacetModel> metadataFacetModelsToAdd =
792 new ArrayList<MetadataFacetModel>( properties.size() );
794 for ( Map.Entry<String, String> entry : properties.entrySet() )
796 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
797 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
798 MetadataFacetModel metadataFacetModel =
799 new MetadataFacetModel( key, artifactMetadataModel, facetId, entry.getKey(), entry.getValue(),
800 metadataFacet.getName() );
801 metadataFacetModelsToAdd.add( metadataFacetModel );
804 getMetadataFacetModelEntityManager().put( metadataFacetModelsToAdd );
810 public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
811 ProjectVersionMetadata versionMetadata )
812 throws MetadataRepositoryException
814 String namespaceKey =
815 new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespaceId ).build();
816 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
817 if ( namespace == null )
819 namespace = updateOrAddNamespace( repositoryId, namespaceId );
822 String key = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
824 Project project = getProjectEntityManager().get( key );
825 if ( project == null )
827 project = new Project( key, projectId, namespace );
828 getProjectEntityManager().put( project );
831 // we don't test of repository and namespace really exist !
832 key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
833 namespaceId ).withProjectId( projectId ).withId( versionMetadata.getId() ).build();
835 ProjectVersionMetadataModel projectVersionMetadataModel =
836 getProjectVersionMetadataModelEntityManager().get( key );
838 if ( projectVersionMetadataModel == null )
840 projectVersionMetadataModel =
841 new BeanReplicator().replicateBean( versionMetadata, ProjectVersionMetadataModel.class );
842 projectVersionMetadataModel.setRowId( key );
844 projectVersionMetadataModel.setProjectId( projectId );
845 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
846 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
847 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
848 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
849 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
851 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
852 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
853 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
857 getProjectVersionMetadataModelEntityManager().put( projectVersionMetadataModel );
859 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
860 artifactMetadataModel.setArtifactMetadataModelId(
861 new ArtifactMetadataModel.KeyBuilder().withId( versionMetadata.getId() ).withRepositoryId(
862 repositoryId ).withNamespace( namespaceId ).withProjectVersion(
863 versionMetadata.getVersion() ).withProject( projectId ).build() );
864 artifactMetadataModel.setRepositoryId( repositoryId );
865 artifactMetadataModel.setNamespace( namespaceId );
866 artifactMetadataModel.setProject( projectId );
867 artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
868 artifactMetadataModel.setVersion( versionMetadata.getVersion() );
870 updateFacets( versionMetadata, artifactMetadataModel );
872 catch ( PersistenceException e )
874 throw new MetadataRepositoryException( e.getMessage(), e );
879 private static class BooleanHolder
881 private boolean value = false;
885 public List<String> getMetadataFacets( final String repositoryId, final String facetId )
886 throws MetadataRepositoryException
888 // FIXME use cql query !!
889 final List<String> facets = new ArrayList<String>();
890 this.getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
893 public Boolean apply( MetadataFacetModel metadataFacetModel )
895 if ( metadataFacetModel != null )
897 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
898 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
901 facets.add( metadataFacetModel.getName() );
913 public boolean hasMetadataFacet( String repositoryId, String facetId )
914 throws MetadataRepositoryException
916 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
920 public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
921 throws MetadataRepositoryException
923 // FIXME use cql query !!
924 final List<MetadataFacetModel> facets = new ArrayList<MetadataFacetModel>();
925 this.getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
928 public Boolean apply( MetadataFacetModel metadataFacetModel )
930 if ( metadataFacetModel != null )
932 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
933 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
934 facetId ) && StringUtils.equals(
935 metadataFacetModel.getName(), name ) )
937 facets.add( metadataFacetModel );
944 if ( facets.isEmpty() )
949 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
950 if ( metadataFacetFactory == null )
954 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
955 Map<String, String> map = new HashMap<String, String>( facets.size() );
956 for ( MetadataFacetModel metadataFacetModel : facets )
958 map.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
960 metadataFacet.fromProperties( map );
961 return metadataFacet;
965 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
966 throws MetadataRepositoryException
969 if ( metadataFacet == null )
974 if ( metadataFacet.toProperties().isEmpty() )
976 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
977 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
978 MetadataFacetModel metadataFacetModel = getMetadataFacetModelEntityManager().get( key );
979 if ( metadataFacetModel == null )
981 metadataFacetModel = new MetadataFacetModel();
983 // we need to store the repositoryId
984 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
985 artifactMetadataModel.setRepositoryId( repositoryId );
986 metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
987 metadataFacetModel.setId( key );
988 metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
989 metadataFacetModel.setName( metadataFacet.getName() );
993 getMetadataFacetModelEntityManager().put( metadataFacetModel );
995 catch ( PersistenceException e )
997 throw new MetadataRepositoryException( e.getMessage(), e );
1002 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1005 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1006 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1008 MetadataFacetModel metadataFacetModel = getMetadataFacetModelEntityManager().get( key );
1009 if ( metadataFacetModel == null )
1011 metadataFacetModel = new MetadataFacetModel();
1012 // we need to store the repositoryId
1013 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1014 artifactMetadataModel.setRepositoryId( repositoryId );
1015 metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
1016 metadataFacetModel.setId( key );
1017 metadataFacetModel.setKey( entry.getKey() );
1018 metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
1019 metadataFacetModel.setName( metadataFacet.getName() );
1021 metadataFacetModel.setValue( entry.getValue() );
1024 getMetadataFacetModelEntityManager().put( metadataFacetModel );
1026 catch ( PersistenceException e )
1028 throw new MetadataRepositoryException( e.getMessage(), e );
1036 public void removeMetadataFacets( final String repositoryId, final String facetId )
1037 throws MetadataRepositoryException
1039 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}'", repositoryId, facetId );
1040 final List<MetadataFacetModel> toRemove = new ArrayList<MetadataFacetModel>();
1043 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1046 public Boolean apply( MetadataFacetModel metadataFacetModel )
1048 if ( metadataFacetModel != null )
1050 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
1051 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
1054 toRemove.add( metadataFacetModel );
1057 return Boolean.TRUE;
1060 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}', toRemove: {}", repositoryId, facetId,
1062 getMetadataFacetModelEntityManager().remove( toRemove );
1066 public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1067 throws MetadataRepositoryException
1069 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}'", repositoryId, facetId );
1070 final List<MetadataFacetModel> toRemove = new ArrayList<MetadataFacetModel>();
1073 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1076 public Boolean apply( MetadataFacetModel metadataFacetModel )
1078 if ( metadataFacetModel != null )
1080 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
1081 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
1082 facetId ) && StringUtils.equals(
1083 metadataFacetModel.getName(), name ) )
1085 toRemove.add( metadataFacetModel );
1088 return Boolean.TRUE;
1091 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}', toRemove: {}", repositoryId, facetId,
1093 getMetadataFacetModelEntityManager().remove( toRemove );
1097 public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1098 final Date endTime )
1099 throws MetadataRepositoryException
1102 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1105 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1108 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1110 if ( artifactMetadataModel != null )
1112 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
1113 && artifactMetadataModel.getNamespace() != null &&
1114 artifactMetadataModel.getProject() != null && artifactMetadataModel.getId() != null )
1117 Date when = artifactMetadataModel.getWhenGathered();
1118 if ( ( startTime != null ? when.getTime() >= startTime.getTime() : true ) && ( endTime != null ?
1119 when.getTime() <= endTime.getTime() : true ) )
1121 logger.debug( "getArtifactsByDateRange visitAll found: {}", artifactMetadataModel );
1122 artifactMetadataModels.add( artifactMetadataModel );
1126 return Boolean.TRUE;
1129 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1131 for ( ArtifactMetadataModel model : artifactMetadataModels )
1133 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1134 populateFacets( artifactMetadata );
1135 artifactMetadatas.add( artifactMetadata );
1140 logger.debug( "getArtifactsByDateRange repositoryId: {}, startTime: {}, endTime: {}, artifactMetadatas: {}",
1141 repositoryId, startTime, endTime, artifactMetadatas );
1143 return artifactMetadatas;
1146 protected void populateFacets( final ArtifactMetadata artifactMetadata )
1148 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1150 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1153 public Boolean apply( MetadataFacetModel metadataFacetModel )
1155 if ( metadataFacetModel != null )
1157 ArtifactMetadataModel artifactMetadataModel = metadataFacetModel.getArtifactMetadataModel();
1158 if ( artifactMetadataModel != null )
1160 if ( StringUtils.equals( artifactMetadata.getRepositoryId(),
1161 artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1162 artifactMetadata.getNamespace(), artifactMetadataModel.getNamespace() )
1163 && StringUtils.equals( artifactMetadata.getRepositoryId(),
1164 artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1165 artifactMetadata.getProject(), artifactMetadataModel.getProject() ) && StringUtils.equals(
1166 artifactMetadata.getId(), artifactMetadataModel.getId() ) )
1168 metadataFacetModels.add( metadataFacetModel );
1172 return Boolean.TRUE;
1175 Map<String, Map<String, String>> facetValuesPerFacet = new HashMap<String, Map<String, String>>();
1177 for ( MetadataFacetModel model : metadataFacetModels )
1179 Map<String, String> values = facetValuesPerFacet.get( model.getName() );
1180 if ( values == null )
1182 values = new HashMap<String, String>();
1184 values.put( model.getKey(), model.getValue() );
1185 facetValuesPerFacet.put( model.getName(), values );
1188 for ( Map.Entry<String, Map<String, String>> entry : facetValuesPerFacet.entrySet() )
1190 MetadataFacetFactory factory = metadataFacetFactories.get( entry.getKey() );
1191 if ( factory == null )
1195 MetadataFacet metadataFacet =
1196 factory.createMetadataFacet( artifactMetadata.getRepositoryId(), entry.getKey() );
1197 metadataFacet.fromProperties( entry.getValue() );
1198 artifactMetadata.addFacet( metadataFacet );
1203 public List<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1204 throws MetadataRepositoryException
1206 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1208 if ( logger.isDebugEnabled() )
1210 logger.debug( "all ArtifactMetadataModel: {}", getArtifactMetadataModelEntityManager().getAll() );
1214 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1217 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1219 if ( artifactMetadataModel != null )
1221 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
1222 && artifactMetadataModel.getNamespace() != null &&
1223 artifactMetadataModel.getProject() != null && artifactMetadataModel.getId() != null )
1226 if ( StringUtils.equals( checksum, artifactMetadataModel.getMd5() ) || StringUtils.equals(
1227 checksum, artifactMetadataModel.getSha1() ) )
1229 artifactMetadataModels.add( artifactMetadataModel );
1233 return Boolean.TRUE;
1236 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1238 for ( ArtifactMetadataModel model : artifactMetadataModels )
1240 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1241 populateFacets( artifactMetadata );
1242 artifactMetadatas.add( artifactMetadata );
1245 logger.debug( "getArtifactsByChecksum repositoryId: {}, checksum: {}, artifactMetadatas: {}", repositoryId,
1246 checksum, artifactMetadatas );
1248 return artifactMetadatas;
1252 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1253 final String version, final String id )
1254 throws MetadataRepositoryException
1256 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1257 repositoryId, namespace, project, version, id );
1259 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1260 id ).withProjectVersion( version ).withProject( project ).build();
1262 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1263 artifactMetadataModel.setArtifactMetadataModelId( key );
1265 getArtifactMetadataModelEntityManager().remove( artifactMetadataModel );
1268 new ProjectVersionMetadataModel.KeyBuilder().withId( version ).withRepository( repositoryId ).withNamespace(
1269 namespace ).withProjectId( project ).build();
1271 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1272 projectVersionMetadataModel.setRowId( key );
1274 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModel );
1278 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1279 throws MetadataRepositoryException
1281 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1282 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1283 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1285 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1286 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1287 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1289 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1290 artifactMetadataModel.setArtifactMetadataModelId( key );
1292 getArtifactMetadataModelEntityManager().remove( artifactMetadataModel );
1296 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1297 final String version, final MetadataFacet metadataFacet )
1298 throws MetadataRepositoryException
1300 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1301 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1304 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1306 if ( artifactMetadataModel != null )
1308 if ( StringUtils.equals( repositoryId, artifactMetadataModel.getRepositoryId() )
1309 && StringUtils.equals( namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals(
1310 project, artifactMetadataModel.getProject() ) && StringUtils.equals( project,
1311 artifactMetadataModel.getVersion() ) )
1313 artifactMetadataModels.add( artifactMetadataModel );
1316 return Boolean.TRUE;
1319 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
1325 public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1326 throws MetadataRepositoryException
1328 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1329 // FIXME use cql query !
1330 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1333 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1335 if ( artifactMetadataModel != null )
1337 if ( StringUtils.equals( repositoryId, artifactMetadataModel.getRepositoryId() ) )
1339 artifactMetadataModels.add( artifactMetadataModel );
1343 return Boolean.TRUE;
1347 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1349 for ( ArtifactMetadataModel model : artifactMetadataModels )
1351 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1352 populateFacets( artifactMetadata );
1353 artifactMetadatas.add( artifactMetadata );
1356 return artifactMetadatas;
1360 public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
1361 throws MetadataResolutionException
1363 //basically just checking it exists
1364 // FIXME use cql query
1366 final BooleanHolder booleanHolder = new BooleanHolder();
1368 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
1371 public Boolean apply( Project project )
1373 if ( project != null )
1375 if ( StringUtils.equals( repoId, project.getNamespace().getRepository().getName() )
1376 && StringUtils.equals( namespace, project.getNamespace().getName() ) && StringUtils.equals( id,
1377 project.getProjectId() ) )
1379 booleanHolder.value = true;
1382 return Boolean.TRUE;
1386 if ( !booleanHolder.value )
1391 ProjectMetadata projectMetadata = new ProjectMetadata();
1392 projectMetadata.setId( id );
1393 projectMetadata.setNamespace( namespace );
1395 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
1398 return projectMetadata;
1402 public ProjectVersionMetadata getProjectVersion( final String repoId, final String namespace,
1403 final String projectId, final String projectVersion )
1404 throws MetadataResolutionException
1406 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
1407 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1409 ProjectVersionMetadataModel projectVersionMetadataModel =
1410 getProjectVersionMetadataModelEntityManager().get( key );
1412 if ( projectVersionMetadataModel == null )
1415 "getProjectVersion repoId: '{}', namespace: '{}', projectId: '{}', projectVersion: {} -> not found",
1416 repoId, namespace, projectId, projectVersion );
1420 ProjectVersionMetadata projectVersionMetadata =
1421 new BeanReplicator().replicateBean( projectVersionMetadataModel, ProjectVersionMetadata.class );
1423 logger.debug( "getProjectVersion repoId: '{}', namespace: '{}', projectId: '{}', projectVersion: {} -> {}",
1424 repoId, namespace, projectId, projectVersion, projectVersionMetadata );
1426 projectVersionMetadata.setCiManagement( projectVersionMetadataModel.getCiManagement() );
1427 projectVersionMetadata.setIssueManagement( projectVersionMetadataModel.getIssueManagement() );
1428 projectVersionMetadata.setOrganization( projectVersionMetadataModel.getOrganization() );
1429 projectVersionMetadata.setScm( projectVersionMetadataModel.getScm() );
1431 // FIXME complete collections !!
1434 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1435 // FIXME use cql query
1436 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1439 public Boolean apply( MetadataFacetModel metadataFacetModel )
1441 if ( metadataFacetModel != null )
1443 if ( StringUtils.equals( repoId, metadataFacetModel.getArtifactMetadataModel().getRepositoryId() )
1444 && StringUtils.equals( namespace, metadataFacetModel.getArtifactMetadataModel().getNamespace() )
1445 && StringUtils.equals( projectId, metadataFacetModel.getArtifactMetadataModel().getProject() )
1446 && StringUtils.equals( projectVersion,
1447 metadataFacetModel.getArtifactMetadataModel().getProjectVersion() ) )
1449 metadataFacetModels.add( metadataFacetModel );
1452 return Boolean.TRUE;
1455 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<String, Map<String, String>>();
1456 for ( MetadataFacetModel metadataFacetModel : metadataFacetModels )
1459 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( metadataFacetModel.getFacetId() );
1460 if ( metaValues == null )
1462 metaValues = new HashMap<String, String>();
1463 metadataFacetsPerFacetIds.put( metadataFacetModel.getFacetId(), metaValues );
1465 metaValues.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1469 if ( !metadataFacetsPerFacetIds.isEmpty() )
1471 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1473 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1474 if ( metadataFacetFactory != null )
1476 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repoId, entry.getKey() );
1477 metadataFacet.fromProperties( entry.getValue() );
1478 projectVersionMetadata.addFacet( metadataFacet );
1483 return projectVersionMetadata;
1488 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1489 String projectVersion )
1490 throws MetadataResolutionException
1492 // FIXME implement this
1493 return Collections.emptyList();
1497 public Collection<String> getProjects( final String repoId, final String namespace )
1498 throws MetadataResolutionException
1500 final Set<String> projects = new HashSet<String>();
1502 // FIXME use cql query
1503 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
1506 public Boolean apply( Project project )
1508 if ( project != null )
1510 if ( StringUtils.equals( repoId, project.getNamespace().getRepository().getName() )
1511 && StringUtils.startsWith( project.getNamespace().getName(), namespace ) )
1513 projects.add( project.getProjectId() );
1516 return Boolean.TRUE;
1525 public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1526 final String projectVersion )
1527 throws MetadataRepositoryException
1530 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1532 // FIXME use cql query
1534 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1537 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1539 if ( artifactMetadataModel != null )
1541 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1542 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
1543 artifactMetadataModel.getProject() )
1544 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
1546 artifactMetadataModels.add( artifactMetadataModel );
1549 return Boolean.TRUE;
1553 logger.debug( "removeProjectVersions:{}", artifactMetadataModels );
1554 if ( artifactMetadataModels.isEmpty() )
1559 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
1561 String key = new ProjectVersionMetadataModel.KeyBuilder().withProjectId( projectId ).withId(
1562 projectVersion ).withRepository( repoId ).withNamespace( namespace ).build();
1564 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1565 projectVersionMetadataModel.setRowId( key );
1567 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModel );
1571 public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
1572 final String projectId, final String projectVersion )
1573 throws MetadataResolutionException
1575 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1576 // FIXME use cql query !
1577 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1580 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1582 if ( artifactMetadataModel != null )
1584 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1585 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
1586 artifactMetadataModel.getProject() )
1587 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
1589 artifactMetadataModels.add( artifactMetadataModel );
1593 return Boolean.TRUE;
1597 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1599 for ( ArtifactMetadataModel model : artifactMetadataModels )
1601 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1602 populateFacets( artifactMetadata );
1603 artifactMetadatas.add( artifactMetadata );
1607 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1608 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1611 public Boolean apply( MetadataFacetModel metadataFacetModel )
1613 if ( metadataFacetModel != null )
1615 if ( StringUtils.equals( repoId, metadataFacetModel.getArtifactMetadataModel().getRepositoryId() )
1616 && StringUtils.equals( namespace, metadataFacetModel.getArtifactMetadataModel().getNamespace() )
1617 && StringUtils.equals( projectId, metadataFacetModel.getArtifactMetadataModel().getProject() )
1618 && StringUtils.equals( projectVersion,
1619 metadataFacetModel.getArtifactMetadataModel().getProjectVersion() ) )
1621 metadataFacetModels.add( metadataFacetModel );
1625 return Boolean.TRUE;
1629 // rebuild MetadataFacet for artifacts
1631 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
1633 Iterable<MetadataFacetModel> metadataFacetModelIterable =
1634 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
1637 public boolean apply( MetadataFacetModel metadataFacetModel )
1639 if ( metadataFacetModel != null )
1641 return StringUtils.equals( artifactMetadata.getVersion(),
1642 metadataFacetModel.getArtifactMetadataModel().getVersion() );
1647 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
1648 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId =
1649 new HashMap<String, List<MetadataFacetModel>>();
1650 while ( iterator.hasNext() )
1652 MetadataFacetModel metadataFacetModel = iterator.next();
1653 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
1654 if ( values == null )
1656 values = new ArrayList<MetadataFacetModel>();
1657 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
1659 values.add( metadataFacetModel );
1663 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
1665 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1666 if ( metadataFacetFactory != null )
1668 List<MetadataFacetModel> facetModels = entry.getValue();
1669 if ( !facetModels.isEmpty() )
1671 MetadataFacet metadataFacet =
1672 metadataFacetFactory.createMetadataFacet( repoId, facetModels.get( 0 ).getName() );
1673 Map<String, String> props = new HashMap<String, String>( facetModels.size() );
1674 for ( MetadataFacetModel metadataFacetModel : facetModels )
1676 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1678 metadataFacet.fromProperties( props );
1679 artifactMetadata.addFacet( metadataFacet );
1687 return artifactMetadatas;
1693 logger.trace( "save" );
1698 throws MetadataRepositoryException
1700 logger.trace( "close" );
1704 public void revert()
1706 logger.warn( "CassandraMetadataRepository cannot revert" );
1710 public boolean canObtainAccess( Class<?> aClass )
1716 public <T> T obtainAccess( Class<T> aClass )
1717 throws MetadataRepositoryException
1719 throw new IllegalArgumentException(
1720 "Access using " + aClass + " is not supported on the cassandra metadata storage" );