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.Keyspace;
26 import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
27 import com.netflix.astyanax.connectionpool.exceptions.NotFoundException;
28 import com.netflix.astyanax.entitystore.DefaultEntityManager;
29 import com.netflix.astyanax.entitystore.EntityManager;
30 import net.sf.beanlib.provider.replicator.BeanReplicator;
31 import org.apache.archiva.configuration.ArchivaConfiguration;
32 import org.apache.archiva.metadata.model.ArtifactMetadata;
33 import org.apache.archiva.metadata.model.FacetedMetadata;
34 import org.apache.archiva.metadata.model.MetadataFacet;
35 import org.apache.archiva.metadata.model.MetadataFacetFactory;
36 import org.apache.archiva.metadata.model.ProjectMetadata;
37 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
38 import org.apache.archiva.metadata.model.ProjectVersionReference;
39 import org.apache.archiva.metadata.repository.MetadataRepository;
40 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
41 import org.apache.archiva.metadata.repository.MetadataResolutionException;
42 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
43 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
44 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
45 import org.apache.archiva.metadata.repository.cassandra.model.Project;
46 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
47 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
48 import org.apache.commons.lang.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 import javax.persistence.PersistenceException;
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.Date;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.Iterator;
60 import java.util.List;
62 import java.util.Properties;
66 * @author Olivier Lamy
68 public class CassandraMetadataRepository
69 implements MetadataRepository
72 private Logger logger = LoggerFactory.getLogger( getClass() );
74 private ArchivaConfiguration configuration;
76 private final Map<String, MetadataFacetFactory> metadataFacetFactories;
78 private CassandraEntityManagerFactory cassandraEntityManagerFactory;
80 public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
81 ArchivaConfiguration configuration, CassandraEntityManagerFactory cassandraEntityManagerFactory )
83 this.metadataFacetFactories = metadataFacetFactories;
84 this.configuration = configuration;
85 this.cassandraEntityManagerFactory = cassandraEntityManagerFactory;
90 public EntityManager<Repository, String> getRepositoryEntityManager()
92 return this.cassandraEntityManagerFactory.getRepositoryEntityManager();
95 public EntityManager<Namespace, String> getNamespaceEntityManager()
97 return this.cassandraEntityManagerFactory.getNamespaceEntityManager();
100 public EntityManager<Project, String> getProjectEntityManager()
102 return this.cassandraEntityManagerFactory.getProjectEntityManager();
105 public EntityManager<ArtifactMetadataModel, String> getArtifactMetadataModelEntityManager()
107 return cassandraEntityManagerFactory.getArtifactMetadataModelEntityManager();
110 public EntityManager<MetadataFacetModel, String> getMetadataFacetModelEntityManager()
112 return this.cassandraEntityManagerFactory.getMetadataFacetModelEntityManager();
115 public EntityManager<ProjectVersionMetadataModel, String> getProjectVersionMetadataModelEntityManager()
117 return this.cassandraEntityManagerFactory.getProjectVersionMetadataModelEntityManager();
121 public void updateNamespace( String repositoryId, String namespaceId )
122 throws MetadataRepositoryException
124 updateOrAddNamespace( repositoryId, namespaceId );
128 public Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
129 throws MetadataRepositoryException
133 Repository repository = this.getRepositoryEntityManager().get( repositoryId );
135 if ( repository == null )
137 repository = new Repository( repositoryId );
139 Namespace namespace = new Namespace( namespaceId, repository );
140 this.getRepositoryEntityManager().put( repository );
142 this.getNamespaceEntityManager().put( namespace );
144 // FIXME add a Namespace id builder
145 Namespace namespace = getNamespaceEntityManager().get(
146 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build() );
147 if ( namespace == null )
149 namespace = new Namespace( namespaceId, repository );
150 getNamespaceEntityManager().put( namespace );
154 catch ( PersistenceException e )
156 throw new MetadataRepositoryException( e.getMessage(), e );
163 public void removeNamespace( String repositoryId, String namespaceId )
164 throws MetadataRepositoryException
168 Namespace namespace = getNamespaceEntityManager().get(
169 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build() );
170 if ( namespace != null )
172 getNamespaceEntityManager().remove( namespace );
175 catch ( PersistenceException e )
177 throw new MetadataRepositoryException( e.getMessage(), e );
183 public void removeRepository( final String repositoryId )
184 throws MetadataRepositoryException
188 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
190 // remove data related to the repository
191 this.getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
194 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
196 if ( artifactMetadataModel != null )
198 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId ) )
200 artifactMetadataModels.add( artifactMetadataModel );
207 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
209 final List<Namespace> namespaces = new ArrayList<Namespace>();
211 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
214 public Boolean apply( Namespace namespace )
216 if ( namespace != null )
218 if ( StringUtils.equals( namespace.getRepository().getId(), repositoryId ) )
220 namespaces.add( namespace );
227 getNamespaceEntityManager().remove( namespaces );
229 final List<Project> projects = new ArrayList<Project>();
230 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
233 public Boolean apply( Project project )
235 if ( project != null )
237 if ( StringUtils.equals( project.getNamespace().getRepository().getId(), repositoryId ) )
239 projects.add( project );
246 getProjectEntityManager().remove( projects );
248 // TODO cleanup or not
249 //final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>( );
250 //getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
252 final List<ProjectVersionMetadataModel> projectVersionMetadataModels =
253 new ArrayList<ProjectVersionMetadataModel>();
255 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
258 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
260 if ( projectVersionMetadataModel != null )
262 if ( StringUtils.equals( projectVersionMetadataModel.getNamespace().getRepository().getId(),
265 projectVersionMetadataModels.add( projectVersionMetadataModel );
272 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModels );
274 Repository repository = getRepositoryEntityManager().get( repositoryId );
275 if ( repository != null )
277 getRepositoryEntityManager().remove( repository );
281 catch ( PersistenceException e )
283 throw new MetadataRepositoryException( e.getMessage(), e );
288 public Collection<String> getRepositories()
289 throws MetadataRepositoryException
293 logger.debug( "getRepositories" );
295 List<Repository> repositories = getRepositoryEntityManager().getAll();
296 if ( repositories == null )
298 return Collections.emptyList();
300 List<String> repoIds = new ArrayList<String>( repositories.size() );
301 for ( Repository repository : repositories )
303 repoIds.add( repository.getName() );
305 logger.debug( "getRepositories found: {}", repoIds );
308 catch ( PersistenceException e )
310 throw new MetadataRepositoryException( e.getMessage(), e );
317 public Collection<String> getRootNamespaces( final String repoId )
318 throws MetadataResolutionException
322 final Set<String> namespaces = new HashSet<String>();
324 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
326 // @Nullable add dependency ?
328 public Boolean apply( Namespace namespace )
330 if ( namespace != null && namespace.getRepository() != null && StringUtils.equalsIgnoreCase( repoId,
331 namespace.getRepository().getId() ) )
333 String name = namespace.getName();
334 if ( StringUtils.isNotEmpty( name ) )
336 namespaces.add( StringUtils.substringBefore( name, "." ) );
345 catch ( PersistenceException e )
347 throw new MetadataResolutionException( e.getMessage(), e );
352 public Collection<String> getNamespaces( final String repoId, final String namespaceId )
353 throws MetadataResolutionException
357 final Set<String> namespaces = new HashSet<String>();
359 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
361 // @Nullable add dependency ?
363 public Boolean apply( Namespace namespace )
365 if ( namespace != null && namespace.getRepository() != null && StringUtils.equalsIgnoreCase( repoId,
366 namespace.getRepository().getId() ) )
368 String currentNamespace = namespace.getName();
369 // we only return childs
370 if ( StringUtils.startsWith( currentNamespace, namespaceId ) && (
371 StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
373 // store after namespaceId '.' but before next '.'
374 // call org namespace org.apache.maven.shared -> stored apache
376 String calledNamespace =
377 StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
378 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
380 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
382 namespaces.add( storedNamespace );
391 catch ( PersistenceException e )
393 throw new MetadataResolutionException( e.getMessage(), e );
398 public List<String> getNamespaces( final String repoId )
399 throws MetadataResolutionException
403 logger.debug( "getNamespaces for repository '{}'", repoId );
404 //TypedQuery<Repository> typedQuery =
405 // entityManager.createQuery( "select n from Namespace n where n.repository_id=:id", Namespace.class );
407 //List<Repository> namespaces = typedQuery.setParameter( "id", repoId ).getResultList();
409 Repository repository = getRepositoryEntityManager().get( repoId );
411 if ( repository == null )
413 return Collections.emptyList();
416 // FIXME find correct cql query
417 //String query = "select * from namespace where repository.id = '" + repoId + "';";
419 //List<Namespace> namespaces = getNamespaceEntityManager().find( query );
421 final Set<Namespace> namespaces = new HashSet<Namespace>();
423 getNamespaceEntityManager().visitAll( new Function<Namespace, Boolean>()
425 // @Nullable add dependency ?
427 public Boolean apply( Namespace namespace )
429 if ( namespace != null && namespace.getRepository() != null && StringUtils.equalsIgnoreCase( repoId,
430 namespace.getRepository().getId() ) )
432 namespaces.add( namespace );
438 repository.setNamespaces( new ArrayList<Namespace>( namespaces ) );
440 if ( repository == null || repository.getNamespaces().isEmpty() )
442 return Collections.emptyList();
444 List<String> namespaceIds = new ArrayList<String>( repository.getNamespaces().size() );
446 for ( Namespace n : repository.getNamespaces() )
448 namespaceIds.add( n.getName() );
451 logger.debug( "getNamespaces for repository '{}' found {}", repoId, namespaceIds.size() );
454 catch ( PersistenceException e )
456 throw new MetadataResolutionException( e.getMessage(), e );
462 public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
463 throws MetadataRepositoryException
466 // project exists ? if yes return
467 String projectKey = new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace(
468 new Namespace( projectMetadata.getNamespace(), new Repository( repositoryId ) ) ).build();
470 Project project = getProjectEntityManager().get( projectKey );
471 if ( project != null )
476 String namespaceKey = new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace(
477 projectMetadata.getNamespace() ).build();
478 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
479 if ( namespace == null )
481 namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
484 project = new Project( projectKey, projectMetadata.getId(), namespace );
488 getProjectEntityManager().put( project );
490 catch ( PersistenceException e )
492 throw new MetadataRepositoryException( e.getMessage(), e );
498 public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
499 throws MetadataRepositoryException
502 // cleanup ArtifactMetadataModel
503 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
505 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
508 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
510 if ( artifactMetadataModel != null )
512 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
513 && StringUtils.equals( artifactMetadataModel.getNamespace(), namespaceId )
514 && StringUtils.equals( artifactMetadataModel.getProject(), projectId ) )
516 artifactMetadataModels.add( artifactMetadataModel );
523 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
525 Namespace namespace = new Namespace( namespaceId, new Repository( repositoryId ) );
527 final List<ProjectVersionMetadataModel> projectVersionMetadataModels =
528 new ArrayList<ProjectVersionMetadataModel>();
530 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
533 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
535 if ( projectVersionMetadataModel != null )
537 if ( StringUtils.equals( repositoryId,
538 projectVersionMetadataModel.getNamespace().getRepository().getName() )
539 && StringUtils.equals( namespaceId, projectVersionMetadataModel.getNamespace().getName() )
540 && StringUtils.equals( projectId, projectVersionMetadataModel.getProjectId() ) )
542 projectVersionMetadataModels.add( projectVersionMetadataModel );
549 if ( !projectVersionMetadataModels.isEmpty() )
551 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModels );
554 String key = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
556 Project project = getProjectEntityManager().get( key );
557 if ( project == null )
559 logger.debug( "removeProject notfound" );
562 logger.debug( "removeProject {}", project );
564 getProjectEntityManager().remove( project );
568 public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
569 throws MetadataResolutionException
571 final Set<String> versions = new HashSet<String>();
572 getProjectVersionMetadataModelEntityManager().visitAll( new Function<ProjectVersionMetadataModel, Boolean>()
575 public Boolean apply( ProjectVersionMetadataModel projectVersionMetadataModel )
577 if ( projectVersionMetadataModel != null )
579 if ( StringUtils.equals( repoId,
580 projectVersionMetadataModel.getNamespace().getRepository().getName() )
581 && StringUtils.startsWith( projectVersionMetadataModel.getNamespace().getName(), namespace )
582 && StringUtils.equals( projectId, projectVersionMetadataModel.getProjectId() ) )
584 versions.add( projectVersionMetadataModel.getId() );
590 // FIXME use cql query
591 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
594 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
596 if ( artifactMetadataModel != null )
598 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
599 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
600 artifactMetadataModel.getProject() ) )
602 versions.add( artifactMetadataModel.getProjectVersion() );
613 public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
614 ArtifactMetadata artifactMeta )
615 throws MetadataRepositoryException
617 String namespaceKey =
618 new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespaceId ).build();
619 // create the namespace if not exists
620 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
621 if ( namespace == null )
623 namespace = updateOrAddNamespace( repositoryId, namespaceId );
626 // create the project if not exist
627 String projectKey = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
629 Project project = getProjectEntityManager().get( projectKey );
630 if ( project == null )
632 project = new Project( projectKey, projectId, namespace );
635 getProjectEntityManager().put( project );
637 catch ( PersistenceException e )
639 throw new MetadataRepositoryException( e.getMessage(), e );
643 String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
644 artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
646 ArtifactMetadataModel artifactMetadataModel = getArtifactMetadataModelEntityManager().get( key );
647 if ( artifactMetadataModel == null )
649 artifactMetadataModel = new ArtifactMetadataModel( key, artifactMeta.getId(), repositoryId, namespaceId,
650 artifactMeta.getProject(), projectVersion,
651 artifactMeta.getVersion(),
652 artifactMeta.getFileLastModified(),
653 artifactMeta.getSize(), artifactMeta.getMd5(),
654 artifactMeta.getSha1(), artifactMeta.getWhenGathered() );
659 artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified().getTime() );
660 artifactMetadataModel.setWhenGathered( artifactMeta.getWhenGathered().getTime() );
661 artifactMetadataModel.setSize( artifactMeta.getSize() );
662 artifactMetadataModel.setMd5( artifactMeta.getMd5() );
663 artifactMetadataModel.setSha1( artifactMeta.getSha1() );
664 artifactMetadataModel.setVersion( artifactMeta.getVersion() );
669 getArtifactMetadataModelEntityManager().put( artifactMetadataModel );
671 catch ( PersistenceException e )
673 throw new MetadataRepositoryException( e.getMessage(), e );
676 key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
677 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
679 ProjectVersionMetadataModel projectVersionMetadataModel = getProjectVersionMetadataModelEntityManager().get( key );
681 if ( projectVersionMetadataModel == null )
683 projectVersionMetadataModel = new ProjectVersionMetadataModel();
684 projectVersionMetadataModel.setRowId( key );
685 projectVersionMetadataModel.setProjectId( projectId );
686 projectVersionMetadataModel.setId( projectVersion );
687 projectVersionMetadataModel.setNamespace( namespace );
689 getProjectVersionMetadataModelEntityManager().put( projectVersionMetadataModel );
694 updateFacets( artifactMeta, artifactMetadataModel );
699 public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
700 final String projectVersion )
701 throws MetadataResolutionException
703 final Set<String> versions = new HashSet<String>();
704 // FIXME use cql query
705 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
708 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
710 if ( artifactMetadataModel != null )
712 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
713 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
714 artifactMetadataModel.getProject() )
715 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
717 versions.add( artifactMetadataModel.getVersion() );
728 * iterate over available facets to remove/add from the artifactMetadata
730 * @param facetedMetadata
731 * @param artifactMetadataModel only use for the key
733 private void updateFacets( final FacetedMetadata facetedMetadata,
734 final ArtifactMetadataModel artifactMetadataModel )
737 for ( final String facetId : metadataFacetFactories.keySet() )
739 MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
740 if ( metadataFacet == null )
746 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
748 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
751 public Boolean apply( MetadataFacetModel metadataFacetModel )
753 ArtifactMetadataModel tmp = metadataFacetModel.getArtifactMetadataModel();
754 if ( StringUtils.equals( metadataFacetModel.getFacetId(), facetId ) && StringUtils.equals(
755 tmp.getRepositoryId(), artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
756 tmp.getNamespace(), artifactMetadataModel.getNamespace() ) && StringUtils.equals(
757 tmp.getProject(), artifactMetadataModel.getProject() ) )
759 metadataFacetModels.add( metadataFacetModel );
765 getMetadataFacetModelEntityManager().remove( metadataFacetModels );
767 Map<String, String> properties = metadataFacet.toProperties();
769 final List<MetadataFacetModel> metadataFacetModelsToAdd =
770 new ArrayList<MetadataFacetModel>( properties.size() );
772 for ( Map.Entry<String, String> entry : properties.entrySet() )
774 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
775 artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
776 MetadataFacetModel metadataFacetModel =
777 new MetadataFacetModel( key, artifactMetadataModel, facetId, entry.getKey(), entry.getValue(),
778 metadataFacet.getName() );
779 metadataFacetModelsToAdd.add( metadataFacetModel );
782 getMetadataFacetModelEntityManager().put( metadataFacetModelsToAdd );
788 public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
789 ProjectVersionMetadata versionMetadata )
790 throws MetadataRepositoryException
792 String namespaceKey =
793 new Namespace.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespaceId ).build();
794 Namespace namespace = getNamespaceEntityManager().get( namespaceKey );
795 if ( namespace == null )
797 namespace = updateOrAddNamespace( repositoryId, namespaceId );
800 String key = new Project.KeyBuilder().withNamespace( namespace ).withProjectId( projectId ).build();
802 Project project = getProjectEntityManager().get( key );
803 if ( project == null )
805 project = new Project( key, projectId, namespace );
806 getProjectEntityManager().put( project );
809 // we don't test of repository and namespace really exist !
810 key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
811 namespaceId ).withProjectId( projectId ).withId( versionMetadata.getId() ).build();
813 ProjectVersionMetadataModel projectVersionMetadataModel = getProjectVersionMetadataModelEntityManager().get( key );
815 if ( projectVersionMetadataModel == null )
817 projectVersionMetadataModel =
818 new BeanReplicator().replicateBean( versionMetadata, ProjectVersionMetadataModel.class );
819 projectVersionMetadataModel.setRowId( key );
821 projectVersionMetadataModel.setProjectId( projectId );
822 projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
823 projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
824 projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
825 projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
826 projectVersionMetadataModel.setScm( versionMetadata.getScm() );
828 projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
829 projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
830 projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
835 getProjectVersionMetadataModelEntityManager().put( projectVersionMetadataModel );
837 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
838 artifactMetadataModel.setArtifactMetadataModelId(
839 new ArtifactMetadataModel.KeyBuilder().withId( versionMetadata.getId() ).withRepositoryId(
840 repositoryId ).withNamespace( namespaceId ).withProjectVersion(
841 versionMetadata.getVersion() ).build() );
842 artifactMetadataModel.setRepositoryId( repositoryId );
843 artifactMetadataModel.setNamespace( namespaceId );
844 artifactMetadataModel.setProject( projectId );
845 artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
846 artifactMetadataModel.setVersion( versionMetadata.getVersion() );
848 updateFacets( versionMetadata, artifactMetadataModel );
850 catch ( PersistenceException e )
852 throw new MetadataRepositoryException( e.getMessage(), e );
857 private static class BooleanHolder
859 private boolean value = false;
863 public List<String> getMetadataFacets( final String repositoryId, final String facetId )
864 throws MetadataRepositoryException
866 // FIXME use cql query !!
867 final List<String> facets = new ArrayList<String>();
868 this.getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
871 public Boolean apply( MetadataFacetModel metadataFacetModel )
873 if ( metadataFacetModel != null )
875 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
876 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
879 facets.add( metadataFacetModel.getName() );
891 public boolean hasMetadataFacet( String repositoryId, String facetId )
892 throws MetadataRepositoryException
894 return !getMetadataFacets( repositoryId, facetId ).isEmpty();
898 public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
899 throws MetadataRepositoryException
901 // FIXME use cql query !!
902 final List<MetadataFacetModel> facets = new ArrayList<MetadataFacetModel>();
903 this.getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
906 public Boolean apply( MetadataFacetModel metadataFacetModel )
908 if ( metadataFacetModel != null )
910 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
911 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
912 facetId ) && StringUtils.equals(
913 metadataFacetModel.getName(), name ) )
915 facets.add( metadataFacetModel );
922 if ( facets.isEmpty() )
927 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
928 if ( metadataFacetFactory == null )
932 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
933 Map<String, String> map = new HashMap<String, String>( facets.size() );
934 for ( MetadataFacetModel metadataFacetModel : facets )
936 map.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
938 metadataFacet.fromProperties( map );
939 return metadataFacet;
943 public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
944 throws MetadataRepositoryException
947 if ( metadataFacet == null )
952 if ( metadataFacet.toProperties().isEmpty() )
954 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
955 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
956 MetadataFacetModel metadataFacetModel = getMetadataFacetModelEntityManager().get( key );
957 if ( metadataFacetModel == null )
959 metadataFacetModel = new MetadataFacetModel();
961 // we need to store the repositoryId
962 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
963 artifactMetadataModel.setRepositoryId( repositoryId );
964 metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
965 metadataFacetModel.setId( key );
966 metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
967 metadataFacetModel.setName( metadataFacet.getName() );
971 getMetadataFacetModelEntityManager().put( metadataFacetModel );
973 catch ( PersistenceException e )
975 throw new MetadataRepositoryException( e.getMessage(), e );
980 for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
983 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
984 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
986 MetadataFacetModel metadataFacetModel = getMetadataFacetModelEntityManager().get( key );
987 if ( metadataFacetModel == null )
989 metadataFacetModel = new MetadataFacetModel();
990 // we need to store the repositoryId
991 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
992 artifactMetadataModel.setRepositoryId( repositoryId );
993 metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
994 metadataFacetModel.setId( key );
995 metadataFacetModel.setKey( entry.getKey() );
996 metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
997 metadataFacetModel.setName( metadataFacet.getName() );
999 metadataFacetModel.setValue( entry.getValue() );
1002 getMetadataFacetModelEntityManager().put( metadataFacetModel );
1004 catch ( PersistenceException e )
1006 throw new MetadataRepositoryException( e.getMessage(), e );
1014 public void removeMetadataFacets( final String repositoryId, final String facetId )
1015 throws MetadataRepositoryException
1017 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}'", repositoryId, facetId );
1018 final List<MetadataFacetModel> toRemove = new ArrayList<MetadataFacetModel>();
1021 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1024 public Boolean apply( MetadataFacetModel metadataFacetModel )
1026 if ( metadataFacetModel != null )
1028 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
1029 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
1032 toRemove.add( metadataFacetModel );
1035 return Boolean.TRUE;
1038 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}', toRemove: {}", repositoryId, facetId,
1040 getMetadataFacetModelEntityManager().remove( toRemove );
1044 public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1045 throws MetadataRepositoryException
1047 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}'", repositoryId, facetId );
1048 final List<MetadataFacetModel> toRemove = new ArrayList<MetadataFacetModel>();
1051 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1054 public Boolean apply( MetadataFacetModel metadataFacetModel )
1056 if ( metadataFacetModel != null )
1058 if ( StringUtils.equals( metadataFacetModel.getArtifactMetadataModel().getRepositoryId(),
1059 repositoryId ) && StringUtils.equals( metadataFacetModel.getFacetId(),
1060 facetId ) && StringUtils.equals(
1061 metadataFacetModel.getName(), name ) )
1063 toRemove.add( metadataFacetModel );
1066 return Boolean.TRUE;
1069 logger.debug( "removeMetadataFacets repositoryId: '{}', facetId: '{}', toRemove: {}", repositoryId, facetId,
1071 getMetadataFacetModelEntityManager().remove( toRemove );
1075 public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1076 final Date endTime )
1077 throws MetadataRepositoryException
1080 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1083 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1086 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1088 if ( artifactMetadataModel != null )
1090 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
1091 && artifactMetadataModel.getNamespace() != null &&
1092 artifactMetadataModel.getProject() != null && artifactMetadataModel.getId() != null )
1095 Date when = artifactMetadataModel.getWhenGathered();
1096 if ( ( startTime != null ? when.getTime() >= startTime.getTime() : true ) && ( endTime != null ?
1097 when.getTime() <= endTime.getTime() : true ) )
1099 logger.debug( "getArtifactsByDateRange visitAll found: {}", artifactMetadataModel );
1100 artifactMetadataModels.add( artifactMetadataModel );
1104 return Boolean.TRUE;
1107 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1109 for ( ArtifactMetadataModel model : artifactMetadataModels )
1111 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1112 populateFacets( artifactMetadata );
1113 artifactMetadatas.add( artifactMetadata );
1118 logger.debug( "getArtifactsByDateRange repositoryId: {}, startTime: {}, endTime: {}, artifactMetadatas: {}",
1119 repositoryId, startTime, endTime, artifactMetadatas );
1121 return artifactMetadatas;
1124 protected void populateFacets( final ArtifactMetadata artifactMetadata )
1126 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1128 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1131 public Boolean apply( MetadataFacetModel metadataFacetModel )
1133 if ( metadataFacetModel != null )
1135 ArtifactMetadataModel artifactMetadataModel = metadataFacetModel.getArtifactMetadataModel();
1136 if ( artifactMetadataModel != null )
1138 if ( StringUtils.equals( artifactMetadata.getRepositoryId(),
1139 artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1140 artifactMetadata.getNamespace(), artifactMetadataModel.getNamespace() )
1141 && StringUtils.equals( artifactMetadata.getRepositoryId(),
1142 artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1143 artifactMetadata.getProject(), artifactMetadataModel.getProject() ) && StringUtils.equals(
1144 artifactMetadata.getId(), artifactMetadataModel.getId() ) )
1146 metadataFacetModels.add( metadataFacetModel );
1150 return Boolean.TRUE;
1153 Map<String, Map<String, String>> facetValuesPerFacet = new HashMap<String, Map<String, String>>();
1155 for ( MetadataFacetModel model : metadataFacetModels )
1157 Map<String, String> values = facetValuesPerFacet.get( model.getName() );
1158 if ( values == null )
1160 values = new HashMap<String, String>();
1162 values.put( model.getKey(), model.getValue() );
1163 facetValuesPerFacet.put( model.getName(), values );
1166 for ( Map.Entry<String, Map<String, String>> entry : facetValuesPerFacet.entrySet() )
1168 MetadataFacetFactory factory = metadataFacetFactories.get( entry.getKey() );
1169 if ( factory == null )
1173 MetadataFacet metadataFacet =
1174 factory.createMetadataFacet( artifactMetadata.getRepositoryId(), entry.getKey() );
1175 metadataFacet.fromProperties( entry.getValue() );
1176 artifactMetadata.addFacet( metadataFacet );
1181 public List<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1182 throws MetadataRepositoryException
1184 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1186 if ( logger.isDebugEnabled() )
1188 logger.debug( "all ArtifactMetadataModel: {}", getArtifactMetadataModelEntityManager().getAll() );
1192 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1195 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1197 if ( artifactMetadataModel != null )
1199 if ( StringUtils.equals( artifactMetadataModel.getRepositoryId(), repositoryId )
1200 && artifactMetadataModel.getNamespace() != null &&
1201 artifactMetadataModel.getProject() != null && artifactMetadataModel.getId() != null )
1204 if ( StringUtils.equals( checksum, artifactMetadataModel.getMd5() ) || StringUtils.equals(
1205 checksum, artifactMetadataModel.getSha1() ) )
1207 artifactMetadataModels.add( artifactMetadataModel );
1211 return Boolean.TRUE;
1214 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1216 for ( ArtifactMetadataModel model : artifactMetadataModels )
1218 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1219 populateFacets( artifactMetadata );
1220 artifactMetadatas.add( artifactMetadata );
1223 logger.debug( "getArtifactsByChecksum repositoryId: {}, checksum: {}, artifactMetadatas: {}", repositoryId,
1224 checksum, artifactMetadatas );
1226 return artifactMetadatas;
1230 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1231 final String version, final String id )
1232 throws MetadataRepositoryException
1234 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1235 repositoryId, namespace, project, version, id );
1237 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1238 id ).withProjectVersion( version ).withProject( project ).build();
1240 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1241 artifactMetadataModel.setArtifactMetadataModelId( key );
1243 getArtifactMetadataModelEntityManager().remove( artifactMetadataModel );
1246 new ProjectVersionMetadataModel.KeyBuilder().withId( version ).withRepository( repositoryId ).withNamespace(
1247 namespace ).withProjectId( project ).build();
1249 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1250 projectVersionMetadataModel.setRowId( key );
1252 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModel );
1256 public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1257 throws MetadataRepositoryException
1259 logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1260 artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1261 artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1263 new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1264 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1265 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1267 ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1268 artifactMetadataModel.setArtifactMetadataModelId( key );
1270 getArtifactMetadataModelEntityManager().remove( artifactMetadataModel );
1274 public void removeArtifact( final String repositoryId, final String namespace, final String project,
1275 final String version, final MetadataFacet metadataFacet )
1276 throws MetadataRepositoryException
1278 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1279 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1282 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1284 if ( artifactMetadataModel != null )
1286 if ( StringUtils.equals( repositoryId, artifactMetadataModel.getRepositoryId() )
1287 && StringUtils.equals( namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals(
1288 project, artifactMetadataModel.getProject() ) && StringUtils.equals( project,
1289 artifactMetadataModel.getVersion() ) )
1291 artifactMetadataModels.add( artifactMetadataModel );
1294 return Boolean.TRUE;
1297 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
1299 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1302 public Boolean apply( MetadataFacetModel metadataFacetModel )
1304 if ( metadataFacetModel != null )
1306 ArtifactMetadataModel artifactMetadataModel = metadataFacetModel.getArtifactMetadataModel();
1307 if ( artifactMetadataModel != null )
1309 if ( StringUtils.equals( repositoryId, artifactMetadataModel.getRepositoryId() )
1310 && StringUtils.equals( namespace, artifactMetadataModel.getNamespace() )
1311 && StringUtils.equals( project, artifactMetadataModel.getProject() ) && StringUtils.equals(
1312 version, artifactMetadataModel.getVersion() ) )
1314 if ( StringUtils.equals( metadataFacetModel.getFacetId(), metadataFacet.getFacetId() )
1315 && StringUtils.equals( metadataFacetModel.getName(), metadataFacet.getName() ) )
1317 metadataFacetModels.add( metadataFacetModel );
1322 return Boolean.TRUE;
1325 getMetadataFacetModelEntityManager().remove( metadataFacetModels );
1331 public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1332 throws MetadataRepositoryException
1334 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1335 // FIXME use cql query !
1336 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1339 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1341 if ( artifactMetadataModel != null )
1343 if ( StringUtils.equals( repositoryId, artifactMetadataModel.getRepositoryId() ) )
1345 artifactMetadataModels.add( artifactMetadataModel );
1349 return Boolean.TRUE;
1353 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1355 for ( ArtifactMetadataModel model : artifactMetadataModels )
1357 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1358 populateFacets( artifactMetadata );
1359 artifactMetadatas.add( artifactMetadata );
1362 return artifactMetadatas;
1366 public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
1367 throws MetadataResolutionException
1369 //basically just checking it exists
1370 // FIXME use cql query
1372 final BooleanHolder booleanHolder = new BooleanHolder();
1374 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
1377 public Boolean apply( Project project )
1379 if ( project != null )
1381 if ( StringUtils.equals( repoId, project.getNamespace().getRepository().getName() )
1382 && StringUtils.equals( namespace, project.getNamespace().getName() ) && StringUtils.equals( id,
1383 project.getProjectId() ) )
1385 booleanHolder.value = true;
1388 return Boolean.TRUE;
1392 if ( !booleanHolder.value )
1397 ProjectMetadata projectMetadata = new ProjectMetadata();
1398 projectMetadata.setId( id );
1399 projectMetadata.setNamespace( namespace );
1401 logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
1404 return projectMetadata;
1408 public ProjectVersionMetadata getProjectVersion( final String repoId, final String namespace,
1409 final String projectId, final String projectVersion )
1410 throws MetadataResolutionException
1412 String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
1413 namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1415 ProjectVersionMetadataModel projectVersionMetadataModel = getProjectVersionMetadataModelEntityManager().get( key );
1417 if ( projectVersionMetadataModel == null )
1420 "getProjectVersion repoId: '{}', namespace: '{}', projectId: '{}', projectVersion: {} -> not found",
1421 repoId, namespace, projectId, projectVersion );
1425 ProjectVersionMetadata projectVersionMetadata =
1426 new BeanReplicator().replicateBean( projectVersionMetadataModel, ProjectVersionMetadata.class );
1428 logger.debug( "getProjectVersion repoId: '{}', namespace: '{}', projectId: '{}', projectVersion: {} -> {}",
1429 repoId, namespace, projectId, projectVersion, projectVersionMetadata );
1431 projectVersionMetadata.setCiManagement( projectVersionMetadataModel.getCiManagement() );
1432 projectVersionMetadata.setIssueManagement( projectVersionMetadataModel.getIssueManagement() );
1433 projectVersionMetadata.setOrganization( projectVersionMetadataModel.getOrganization() );
1434 projectVersionMetadata.setScm( projectVersionMetadataModel.getScm() );
1436 // FIXME complete collections !!
1439 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1440 // FIXME use cql query
1441 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1444 public Boolean apply( MetadataFacetModel metadataFacetModel )
1446 if ( metadataFacetModel != null )
1448 if ( StringUtils.equals( repoId, metadataFacetModel.getArtifactMetadataModel().getRepositoryId() )
1449 && StringUtils.equals( namespace, metadataFacetModel.getArtifactMetadataModel().getNamespace() )
1450 && StringUtils.equals( projectId, metadataFacetModel.getArtifactMetadataModel().getProject() )
1451 && StringUtils.equals( projectVersion,
1452 metadataFacetModel.getArtifactMetadataModel().getProjectVersion() ) )
1454 metadataFacetModels.add( metadataFacetModel );
1457 return Boolean.TRUE;
1460 Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<String, Map<String, String>>();
1461 for ( MetadataFacetModel metadataFacetModel : metadataFacetModels )
1464 Map<String, String> metaValues = metadataFacetsPerFacetIds.get( metadataFacetModel.getFacetId() );
1465 if ( metaValues == null )
1467 metaValues = new HashMap<String, String>();
1468 metadataFacetsPerFacetIds.put( metadataFacetModel.getFacetId(), metaValues );
1470 metaValues.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1474 if ( !metadataFacetsPerFacetIds.isEmpty() )
1476 for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1478 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1479 if ( metadataFacetFactory != null )
1481 MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repoId, entry.getKey() );
1482 metadataFacet.fromProperties( entry.getValue() );
1483 projectVersionMetadata.addFacet( metadataFacet );
1488 return projectVersionMetadata;
1493 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1494 String projectVersion )
1495 throws MetadataResolutionException
1497 // FIXME implement this
1498 return Collections.emptyList();
1502 public Collection<String> getProjects( final String repoId, final String namespace )
1503 throws MetadataResolutionException
1505 final Set<String> projects = new HashSet<String>();
1507 // FIXME use cql query
1508 getProjectEntityManager().visitAll( new Function<Project, Boolean>()
1511 public Boolean apply( Project project )
1513 if ( project != null )
1515 if ( StringUtils.equals( repoId, project.getNamespace().getRepository().getName() )
1516 && StringUtils.startsWith( project.getNamespace().getName(), namespace ) )
1518 projects.add( project.getProjectId() );
1521 return Boolean.TRUE;
1526 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1529 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1531 if ( artifactMetadataModel != null )
1533 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1534 namespace, artifactMetadataModel.getNamespace() ) )
1536 projects.add( artifactMetadataModel.getProject() );
1539 return Boolean.TRUE;
1548 public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1549 final String projectVersion )
1550 throws MetadataRepositoryException
1553 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1555 // FIXME use cql query
1557 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1560 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1562 if ( artifactMetadataModel != null )
1564 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1565 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
1566 artifactMetadataModel.getProject() )
1567 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
1569 artifactMetadataModels.add( artifactMetadataModel );
1572 return Boolean.TRUE;
1576 logger.debug( "removeProjectVersions:{}", artifactMetadataModels );
1577 if ( artifactMetadataModels.isEmpty() )
1582 getArtifactMetadataModelEntityManager().remove( artifactMetadataModels );
1584 String key = new ProjectVersionMetadataModel.KeyBuilder().withProjectId( projectId ).withId(
1585 projectVersion ).withRepository( repoId ).withNamespace( namespace ).build();
1587 ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1588 projectVersionMetadataModel.setRowId( key );
1590 getProjectVersionMetadataModelEntityManager().remove( projectVersionMetadataModel );
1594 public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
1595 final String projectId, final String projectVersion )
1596 throws MetadataResolutionException
1598 final List<ArtifactMetadataModel> artifactMetadataModels = new ArrayList<ArtifactMetadataModel>();
1599 // FIXME use cql query !
1600 getArtifactMetadataModelEntityManager().visitAll( new Function<ArtifactMetadataModel, Boolean>()
1603 public Boolean apply( ArtifactMetadataModel artifactMetadataModel )
1605 if ( artifactMetadataModel != null )
1607 if ( StringUtils.equals( repoId, artifactMetadataModel.getRepositoryId() ) && StringUtils.equals(
1608 namespace, artifactMetadataModel.getNamespace() ) && StringUtils.equals( projectId,
1609 artifactMetadataModel.getProject() )
1610 && StringUtils.equals( projectVersion, artifactMetadataModel.getProjectVersion() ) )
1612 artifactMetadataModels.add( artifactMetadataModel );
1616 return Boolean.TRUE;
1620 List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( artifactMetadataModels.size() );
1622 for ( ArtifactMetadataModel model : artifactMetadataModels )
1624 ArtifactMetadata artifactMetadata = new BeanReplicator().replicateBean( model, ArtifactMetadata.class );
1625 populateFacets( artifactMetadata );
1626 artifactMetadatas.add( artifactMetadata );
1630 final List<MetadataFacetModel> metadataFacetModels = new ArrayList<MetadataFacetModel>();
1631 getMetadataFacetModelEntityManager().visitAll( new Function<MetadataFacetModel, Boolean>()
1634 public Boolean apply( MetadataFacetModel metadataFacetModel )
1636 if ( metadataFacetModel != null )
1638 if ( StringUtils.equals( repoId, metadataFacetModel.getArtifactMetadataModel().getRepositoryId() )
1639 && StringUtils.equals( namespace, metadataFacetModel.getArtifactMetadataModel().getNamespace() )
1640 && StringUtils.equals( projectId, metadataFacetModel.getArtifactMetadataModel().getProject() )
1641 && StringUtils.equals( projectVersion,
1642 metadataFacetModel.getArtifactMetadataModel().getProjectVersion() ) )
1644 metadataFacetModels.add( metadataFacetModel );
1648 return Boolean.TRUE;
1652 // rebuild MetadataFacet for artifacts
1654 for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
1656 Iterable<MetadataFacetModel> metadataFacetModelIterable =
1657 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
1660 public boolean apply( MetadataFacetModel metadataFacetModel )
1662 if ( metadataFacetModel != null )
1664 return StringUtils.equals( artifactMetadata.getVersion(),
1665 metadataFacetModel.getArtifactMetadataModel().getVersion() );
1670 Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
1671 Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId =
1672 new HashMap<String, List<MetadataFacetModel>>();
1673 while ( iterator.hasNext() )
1675 MetadataFacetModel metadataFacetModel = iterator.next();
1676 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
1677 if ( values == null )
1679 values = new ArrayList<MetadataFacetModel>();
1680 metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
1682 values.add( metadataFacetModel );
1686 for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
1688 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1689 if ( metadataFacetFactory != null )
1691 List<MetadataFacetModel> facetModels = entry.getValue();
1692 if ( !facetModels.isEmpty() )
1694 MetadataFacet metadataFacet =
1695 metadataFacetFactory.createMetadataFacet( repoId, facetModels.get( 0 ).getName() );
1696 Map<String, String> props = new HashMap<String, String>( facetModels.size() );
1697 for ( MetadataFacetModel metadataFacetModel : facetModels )
1699 props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1701 metadataFacet.fromProperties( props );
1702 artifactMetadata.addFacet( metadataFacet );
1710 return artifactMetadatas;
1716 logger.trace( "save" );
1721 throws MetadataRepositoryException
1723 logger.trace( "close" );
1727 public void revert()
1729 logger.warn( "CassandraMetadataRepository cannot revert" );
1733 public boolean canObtainAccess( Class<?> aClass )
1739 public <T> T obtainAccess( Class<T> aClass )
1740 throws MetadataRepositoryException
1742 throw new IllegalArgumentException(
1743 "Access using " + aClass + " is not supported on the cassandra metadata storage" );