]> source.dussan.org Git - archiva.git/blob
d7ba7b1174965faf7a90f88b5f3f4c707280492d
[archiva.git] /
1 package org.apache.archiva.metadata.repository.cassandra;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import com.google.common.base.Predicate;
23 import com.google.common.collect.Iterables;
24 import me.prettyprint.cassandra.serializers.LongSerializer;
25 import me.prettyprint.cassandra.serializers.StringSerializer;
26 import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
27 import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
28 import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
29 import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
30 import me.prettyprint.hector.api.Keyspace;
31 import me.prettyprint.hector.api.beans.ColumnSlice;
32 import me.prettyprint.hector.api.beans.OrderedRows;
33 import me.prettyprint.hector.api.beans.Row;
34 import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
35 import me.prettyprint.hector.api.factory.HFactory;
36 import me.prettyprint.hector.api.mutation.MutationResult;
37 import me.prettyprint.hector.api.mutation.Mutator;
38 import me.prettyprint.hector.api.query.QueryResult;
39 import me.prettyprint.hector.api.query.RangeSlicesQuery;
40 import org.apache.archiva.configuration.ArchivaConfiguration;
41 import org.apache.archiva.metadata.model.ArtifactMetadata;
42 import org.apache.archiva.metadata.model.CiManagement;
43 import org.apache.archiva.metadata.model.FacetedMetadata;
44 import org.apache.archiva.metadata.model.IssueManagement;
45 import org.apache.archiva.metadata.model.MetadataFacet;
46 import org.apache.archiva.metadata.model.MetadataFacetFactory;
47 import org.apache.archiva.metadata.model.Organization;
48 import org.apache.archiva.metadata.model.ProjectMetadata;
49 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
50 import org.apache.archiva.metadata.model.ProjectVersionReference;
51 import org.apache.archiva.metadata.model.Scm;
52 import org.apache.archiva.metadata.repository.MetadataRepository;
53 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
54 import org.apache.archiva.metadata.repository.MetadataResolutionException;
55 import org.apache.archiva.metadata.repository.cassandra.model.ArtifactMetadataModel;
56 import org.apache.archiva.metadata.repository.cassandra.model.MetadataFacetModel;
57 import org.apache.archiva.metadata.repository.cassandra.model.Namespace;
58 import org.apache.archiva.metadata.repository.cassandra.model.Project;
59 import org.apache.archiva.metadata.repository.cassandra.model.ProjectVersionMetadataModel;
60 import org.apache.archiva.metadata.repository.cassandra.model.Repository;
61 import org.apache.commons.lang.StringUtils;
62 import org.modelmapper.ModelMapper;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 import javax.persistence.PersistenceException;
67 import java.util.ArrayList;
68 import java.util.Collection;
69 import java.util.Collections;
70 import java.util.Date;
71 import java.util.HashMap;
72 import java.util.HashSet;
73 import java.util.Iterator;
74 import java.util.List;
75 import java.util.Map;
76 import java.util.Set;
77
78 import static org.apache.archiva.metadata.repository.cassandra.CassandraUtils.*;
79
80 /**
81  * @author Olivier Lamy
82  * @since 2.0.0
83  */
84 public class CassandraMetadataRepository
85     implements MetadataRepository
86 {
87
88     private Logger logger = LoggerFactory.getLogger( getClass() );
89
90     private ArchivaConfiguration configuration;
91
92     private final Map<String, MetadataFacetFactory> metadataFacetFactories;
93
94     private final CassandraArchivaManager cassandraArchivaManager;
95
96     private final ColumnFamilyTemplate<String, String> projectVersionMetadataModelTemplate;
97
98     private final ColumnFamilyTemplate<String, String> projectTemplate;
99
100     private final ColumnFamilyTemplate<String, String> artifactMetadataTemplate;
101
102     private final ColumnFamilyTemplate<String, String> metadataFacetTemplate;
103
104     public CassandraMetadataRepository( Map<String, MetadataFacetFactory> metadataFacetFactories,
105                                         ArchivaConfiguration configuration,
106                                         CassandraArchivaManager cassandraArchivaManager )
107     {
108         this.metadataFacetFactories = metadataFacetFactories;
109         this.configuration = configuration;
110         this.cassandraArchivaManager = cassandraArchivaManager;
111
112         this.projectVersionMetadataModelTemplate =
113             new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
114                                                             cassandraArchivaManager.getProjectVersionMetadataModelFamilyName(),
115                                                             StringSerializer.get(), //
116                                                             StringSerializer.get() );
117
118         this.projectTemplate = new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
119                                                                                cassandraArchivaManager.getProjectFamilyName(),
120                                                                                //
121                                                                                StringSerializer.get(), //
122                                                                                StringSerializer.get() );
123
124         this.artifactMetadataTemplate =
125             new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
126                                                             cassandraArchivaManager.getArtifactMetadataModelFamilyName(),
127                                                             //
128                                                             StringSerializer.get(), //
129                                                             StringSerializer.get() );
130
131         this.metadataFacetTemplate =
132             new ThriftColumnFamilyTemplate<String, String>( cassandraArchivaManager.getKeyspace(), //
133                                                             cassandraArchivaManager.getMetadataFacetModelFamilyName(),
134                                                             //
135                                                             StringSerializer.get(), //
136                                                             StringSerializer.get() );
137     }
138
139
140     /**
141      * if the repository doesn't exist it will be created
142      *
143      * @param repositoryId
144      * @return
145      */
146     public Repository getOrCreateRepository( String repositoryId )
147         throws MetadataRepositoryException
148     {
149         String cf = cassandraArchivaManager.getRepositoryFamilyName();
150         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
151         QueryResult<OrderedRows<String, String, String>> result = HFactory //
152             .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
153                                      StringSerializer.get() ) //
154             .setColumnFamily( cf ) //
155             .setColumnNames( "repositoryName" ) //
156             .addEqualsExpression( "repositoryName", repositoryId ) //
157             .execute();
158
159         if ( result.get().getCount() < 1 )
160         {
161             // we need to create the repository
162             Repository repository = new Repository( repositoryId );
163
164             try
165             {
166                 MutationResult mutationResult = HFactory.createMutator( keyspace, StringSerializer.get() ) //
167                     .addInsertion( repositoryId, cf,
168                                    CassandraUtils.column( "repositoryName", repository.getName() ) ) //
169                     .execute();
170                 return repository;
171             }
172             catch ( HInvalidRequestException e )
173             {
174                 logger.error( e.getMessage(), e );
175                 throw new MetadataRepositoryException( e.getMessage(), e );
176             }
177
178         }
179
180         return new Repository(
181             result.get().getList().get( 0 ).getColumnSlice().getColumnByName( "repositoryName" ).getValue() );
182     }
183
184
185     protected Repository getRepository( String repositoryId )
186         throws MetadataRepositoryException
187     {
188         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
189         QueryResult<OrderedRows<String, String, String>> result = HFactory //
190             .createRangeSlicesQuery( keyspace, StringSerializer.get(), StringSerializer.get(),
191                                      StringSerializer.get() ) //
192             .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
193             .setColumnNames( "repositoryName" ) //
194             .addEqualsExpression( "repositoryName", repositoryId ) //
195             .execute();
196         return ( result.get().getCount() > 0 ) ? new Repository( repositoryId ) : null;
197     }
198
199     @Override
200     public void updateNamespace( String repositoryId, String namespaceId )
201         throws MetadataRepositoryException
202     {
203         updateOrAddNamespace( repositoryId, namespaceId );
204     }
205
206     public Namespace updateOrAddNamespace( String repositoryId, String namespaceId )
207         throws MetadataRepositoryException
208     {
209         try
210         {
211             Repository repository = getOrCreateRepository( repositoryId );
212
213             Keyspace keyspace = cassandraArchivaManager.getKeyspace();
214
215             String key =
216                 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
217
218             Namespace namespace = getNamespace( repositoryId, namespaceId );
219             if ( namespace == null )
220             {
221                 String cf = cassandraArchivaManager.getNamespaceFamilyName();
222                 namespace = new Namespace( namespaceId, repository );
223                 HFactory.createMutator( keyspace, StringSerializer.get() )
224                     //  values
225                     .addInsertion( key, cf, CassandraUtils.column( "name", namespace.getName() ) ) //
226                     .addInsertion( key, cf, CassandraUtils.column( "repositoryName", repository.getName() ) ) //
227                     .execute();
228             }
229
230             return namespace;
231         }
232         catch ( HInvalidRequestException e )
233         {
234             logger.error( e.getMessage(), e );
235             throw new MetadataRepositoryException( e.getMessage(), e );
236         }
237     }
238
239     protected Namespace getNamespace( String repositoryId, String namespaceId )
240     {
241         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
242         QueryResult<OrderedRows<String, String, String>> result = HFactory //
243             .createRangeSlicesQuery( keyspace, //
244                                      StringSerializer.get(), //
245                                      StringSerializer.get(), //
246                                      StringSerializer.get() ) //
247             .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
248             .setColumnNames( "repositoryName", "name" ) //
249             .addEqualsExpression( "repositoryName", repositoryId ) //
250             .addEqualsExpression( "name", namespaceId ) //
251             .execute();
252         if ( result.get().getCount() > 0 )
253         {
254             ColumnSlice<String, String> columnSlice = result.get().getList().get( 0 ).getColumnSlice();
255             return new Namespace( getStringValue( columnSlice, "name" ), //
256                                   new Repository( getStringValue( columnSlice, "repositoryName" ) ) );
257
258         }
259         return null;
260     }
261
262
263     @Override
264     public void removeNamespace( String repositoryId, String namespaceId )
265         throws MetadataRepositoryException
266     {
267         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
268         try
269         {
270             String key =
271                 new Namespace.KeyBuilder().withNamespace( namespaceId ).withRepositoryId( repositoryId ).build();
272
273             HFactory.createMutator( cassandraArchivaManager.getKeyspace(), new StringSerializer() ) //
274                 .addDeletion( key, cassandraArchivaManager.getNamespaceFamilyName() ) //
275                 .execute();
276
277             QueryResult<OrderedRows<String, String, String>> result = HFactory //
278                 .createRangeSlicesQuery( keyspace, //
279                                          StringSerializer.get(), //
280                                          StringSerializer.get(), //
281                                          StringSerializer.get() ) //
282                 .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
283                 .setColumnNames( "repositoryName" ) //
284                 .addEqualsExpression( "repositoryName", repositoryId ) //
285                 .addEqualsExpression( "namespaceId", namespaceId ) //
286                 .execute();
287
288             for ( Row<String, String, String> row : result.get() )
289             {
290                 this.projectTemplate.deleteRow( row.getKey() );
291             }
292
293             result = HFactory //
294                 .createRangeSlicesQuery( keyspace, //
295                                          StringSerializer.get(), //
296                                          StringSerializer.get(), //
297                                          StringSerializer.get() ) //
298                 .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
299                 .setColumnNames( "repositoryName" ) //
300                 .addEqualsExpression( "repositoryName", repositoryId ) //
301                 .addEqualsExpression( "namespaceId", namespaceId ) //
302                 .execute();
303
304             for ( Row<String, String, String> row : result.get() )
305             {
306                 this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
307             }
308
309             result = HFactory //
310                 .createRangeSlicesQuery( keyspace, //
311                                          StringSerializer.get(), //
312                                          StringSerializer.get(), //
313                                          StringSerializer.get() ) //
314                 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
315                 .setColumnNames( "repositoryName" ) //
316                 .addEqualsExpression( "repositoryName", repositoryId ) //
317                 .addEqualsExpression( "namespaceId", namespaceId ) //
318                 .execute();
319
320             for ( Row<String, String, String> row : result.get() )
321             {
322                 this.artifactMetadataTemplate.deleteRow( row.getKey() );
323             }
324
325             result = HFactory //
326                 .createRangeSlicesQuery( keyspace, //
327                                          StringSerializer.get(), //
328                                          StringSerializer.get(), //
329                                          StringSerializer.get() ) //
330                 .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
331                 .setColumnNames( "repositoryName" ) //
332                 .addEqualsExpression( "repositoryName", repositoryId ) //
333                 .addEqualsExpression( "namespaceId", namespaceId ) //
334                 .execute();
335
336             for ( Row<String, String, String> row : result.get() )
337             {
338                 this.metadataFacetTemplate.deleteRow( row.getKey() );
339             }
340
341         }
342         catch ( HInvalidRequestException e )
343         {
344             logger.error( e.getMessage(), e );
345             throw new MetadataRepositoryException( e.getMessage(), e );
346         }
347     }
348
349
350     @Override
351     public void removeRepository( final String repositoryId )
352         throws MetadataRepositoryException
353     {
354
355         StringSerializer ss = StringSerializer.get();
356
357         // TODO use cql queries to delete all
358         List<String> namespacesKey = new ArrayList<String>();
359
360         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
361         QueryResult<OrderedRows<String, String, String>> result = HFactory //
362             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
363             .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
364             .setColumnNames( "repositoryName" ) //
365             .addEqualsExpression( "repositoryName", repositoryId ) //
366             .execute();
367
368         for ( Row<String, String, String> row : result.get().getList() )
369         {
370             namespacesKey.add( row.getKey() );
371         }
372
373         HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
374             .addDeletion( namespacesKey, cassandraArchivaManager.getNamespaceFamilyName() ) //
375             .execute();
376
377         //delete repositoryId
378         HFactory.createMutator( cassandraArchivaManager.getKeyspace(), ss ) //
379             .addDeletion( repositoryId, cassandraArchivaManager.getRepositoryFamilyName() ) //
380             .execute();
381
382         result = HFactory //
383             .createRangeSlicesQuery( keyspace, //
384                                      StringSerializer.get(), //
385                                      StringSerializer.get(), //
386                                      StringSerializer.get() ) //
387             .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
388             .setColumnNames( "repositoryName" ) //
389             .addEqualsExpression( "repositoryName", repositoryId ) //
390             .execute();
391
392         for ( Row<String, String, String> row : result.get() )
393         {
394             this.projectTemplate.deleteRow( row.getKey() );
395         }
396
397         result = HFactory //
398             .createRangeSlicesQuery( keyspace, //
399                                      StringSerializer.get(), //
400                                      StringSerializer.get(), //
401                                      StringSerializer.get() ) //
402             .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
403             .setColumnNames( "repositoryName" ) //
404             .addEqualsExpression( "repositoryName", repositoryId ) //
405             .execute();
406
407         for ( Row<String, String, String> row : result.get() )
408         {
409             this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
410         }
411
412         result = HFactory //
413             .createRangeSlicesQuery( keyspace, //
414                                      StringSerializer.get(), //
415                                      StringSerializer.get(), //
416                                      StringSerializer.get() ) //
417             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
418             .setColumnNames( "repositoryName" ) //
419             .addEqualsExpression( "repositoryName", repositoryId ) //
420             .execute();
421
422         for ( Row<String, String, String> row : result.get() )
423         {
424             this.artifactMetadataTemplate.deleteRow( row.getKey() );
425         }
426
427         result = HFactory //
428             .createRangeSlicesQuery( keyspace, //
429                                      StringSerializer.get(), //
430                                      StringSerializer.get(), //
431                                      StringSerializer.get() ) //
432             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
433             .setColumnNames( "repositoryName" ) //
434             .addEqualsExpression( "repositoryName", repositoryId ) //
435             .execute();
436
437         for ( Row<String, String, String> row : result.get() )
438         {
439             this.metadataFacetTemplate.deleteRow( row.getKey() );
440         }
441
442
443     }
444
445     @Override
446     public Collection<String> getRepositories()
447         throws MetadataRepositoryException
448     {
449         try
450         {
451             logger.debug( "getRepositories" );
452
453             final QueryResult<OrderedRows<String, String, String>> cResult = //
454                 HFactory.createRangeSlicesQuery( cassandraArchivaManager.getKeyspace(), //
455                                                  StringSerializer.get(), //
456                                                  StringSerializer.get(), //
457                                                  StringSerializer.get() ) //
458                     .setColumnFamily( cassandraArchivaManager.getRepositoryFamilyName() ) //
459                     .setColumnNames( "repositoryName" ) //
460                     .setRange( null, null, false, Integer.MAX_VALUE ) //
461                     .execute();
462
463             List<String> repoIds = new ArrayList<String>( cResult.get().getCount() );
464
465             for ( Row<String, String, String> row : cResult.get() )
466             {
467                 repoIds.add( getStringValue( row.getColumnSlice(), "repositoryName" ) );
468             }
469
470             return repoIds;
471         }
472         catch ( PersistenceException e )
473         {
474             throw new MetadataRepositoryException( e.getMessage(), e );
475         }
476
477     }
478
479
480     @Override
481     public Collection<String> getRootNamespaces( final String repoId )
482         throws MetadataResolutionException
483     {
484         StringSerializer ss = StringSerializer.get();
485         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
486         QueryResult<OrderedRows<String, String, String>> result = HFactory //
487             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
488             .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
489             .setColumnNames( "name" ) //
490             .addEqualsExpression( "repositoryName", repoId ) //
491             .execute();
492
493         Set<String> namespaces = new HashSet<String>( result.get().getCount() );
494
495         for ( Row<String, String, String> row : result.get() )
496         {
497             namespaces.add( StringUtils.substringBefore( getStringValue( row.getColumnSlice(), "name" ), "." ) );
498         }
499
500         return namespaces;
501     }
502
503
504     @Override
505     public Collection<String> getNamespaces( final String repoId, final String namespaceId )
506         throws MetadataResolutionException
507     {
508         StringSerializer ss = StringSerializer.get();
509         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
510         QueryResult<OrderedRows<String, String, String>> result = HFactory //
511             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
512             .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
513             .setColumnNames( "name" ) //
514             .addEqualsExpression( "repositoryName", repoId ) //
515             .execute();
516
517         List<String> namespaces = new ArrayList<String>( result.get().getCount() );
518
519         for ( Row<String, String, String> row : result.get() )
520         {
521             String currentNamespace = getStringValue( row.getColumnSlice(), "name" );
522             if ( StringUtils.startsWith( currentNamespace, namespaceId ) //
523                 && ( StringUtils.length( currentNamespace ) > StringUtils.length( namespaceId ) ) )
524             {
525                 // store after namespaceId '.' but before next '.'
526                 // call org namespace org.apache.maven.shared -> stored apache
527
528                 String calledNamespace = StringUtils.endsWith( namespaceId, "." ) ? namespaceId : namespaceId + ".";
529                 String storedNamespace = StringUtils.substringAfter( currentNamespace, calledNamespace );
530
531                 storedNamespace = StringUtils.substringBefore( storedNamespace, "." );
532
533                 namespaces.add( storedNamespace );
534             }
535         }
536
537         return namespaces;
538
539     }
540
541
542     public List<String> getNamespaces( final String repoId )
543         throws MetadataResolutionException
544     {
545         StringSerializer ss = StringSerializer.get();
546         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
547         QueryResult<OrderedRows<String, String, String>> result = HFactory //
548             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
549             .setColumnFamily( cassandraArchivaManager.getNamespaceFamilyName() ) //
550             .setColumnNames( "name" ) //
551             .addEqualsExpression( "repositoryName", repoId ) //
552             .execute();
553
554         List<String> namespaces = new ArrayList<String>( result.get().getCount() );
555
556         for ( Row<String, String, String> row : result.get() )
557         {
558             namespaces.add( getStringValue( row.getColumnSlice(), "name" ) );
559         }
560
561         return namespaces;
562     }
563
564
565     @Override
566     public void updateProject( String repositoryId, ProjectMetadata projectMetadata )
567         throws MetadataRepositoryException
568     {
569         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
570         StringSerializer ss = StringSerializer.get();
571
572         QueryResult<OrderedRows<String, String, String>> result = HFactory //
573             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
574             .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
575             .setColumnNames( "projectId" ) //
576             .addEqualsExpression( "repositoryName", repositoryId ) //
577             .addEqualsExpression( "namespaceId", projectMetadata.getNamespace() ) //
578             .addEqualsExpression( "projectId", projectMetadata.getId() ) //
579             .execute();
580
581         // project exists ? if yes return nothing to update here
582         if ( result.get().getCount() > 0 )
583         {
584             return;
585         }
586         else
587         {
588             Namespace namespace = updateOrAddNamespace( repositoryId, projectMetadata.getNamespace() );
589
590             String key =
591                 new Project.KeyBuilder().withProjectId( projectMetadata.getId() ).withNamespace( namespace ).build();
592
593             String cf = cassandraArchivaManager.getProjectFamilyName();
594             projectTemplate.createMutator()
595                 //  values
596                 .addInsertion( key, cf, CassandraUtils.column( "projectId", projectMetadata.getId() ) ) //
597                 .addInsertion( key, cf, CassandraUtils.column( "repositoryName", repositoryId ) ) //
598                 .addInsertion( key, cf, CassandraUtils.column( "namespaceId", projectMetadata.getNamespace() ) )//
599                 .execute();
600         }
601     }
602
603     @Override
604     public Collection<String> getProjects( final String repoId, final String namespace )
605         throws MetadataResolutionException
606     {
607
608         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
609         StringSerializer ss = StringSerializer.get();
610         QueryResult<OrderedRows<String, String, String>> result = HFactory //
611             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
612             .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
613             .setColumnNames( "projectId" ) //
614             .addEqualsExpression( "repositoryName", repoId ) //
615             .addEqualsExpression( "namespaceId", namespace ) //
616             .execute();
617
618         final Set<String> projects = new HashSet<String>( result.get().getCount() );
619
620         for ( Row<String, String, String> row : result.get() )
621         {
622             projects.add( getStringValue( row.getColumnSlice(), "projectId" ) );
623         }
624
625         return projects;
626     }
627
628     @Override
629     public void removeProject( final String repositoryId, final String namespaceId, final String projectId )
630         throws MetadataRepositoryException
631     {
632         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
633         StringSerializer ss = StringSerializer.get();
634         String key = new Project.KeyBuilder() //
635             .withProjectId( projectId ) //
636             .withNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) ) //
637             .build();
638
639         this.projectTemplate.deleteRow( key );
640
641         QueryResult<OrderedRows<String, String, String>> result = HFactory //
642             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
643             .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
644             .setColumnNames( "id" ) //
645             .addEqualsExpression( "repositoryName", repositoryId ) //
646             .addEqualsExpression( "namespaceId", namespaceId ) //
647             .addEqualsExpression( "projectId", projectId ) //
648             .execute();
649
650         for ( Row<String, String, String> row : result.get() )
651         {
652             this.projectVersionMetadataModelTemplate.deleteRow( row.getKey() );
653         }
654
655         result = HFactory //
656             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
657             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
658             .setColumnNames( "projectId" ) //
659             .addEqualsExpression( "repositoryName", repositoryId ) //
660             .addEqualsExpression( "namespaceId", namespaceId ) //
661             .addEqualsExpression( "projectId", projectId ) //
662             .execute();
663
664         for ( Row<String, String, String> row : result.get() )
665         {
666             this.artifactMetadataTemplate.deleteRow( row.getKey() );
667         }
668     }
669
670     @Override
671     public Collection<String> getProjectVersions( final String repoId, final String namespace, final String projectId )
672         throws MetadataResolutionException
673     {
674
675         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
676         StringSerializer ss = StringSerializer.get();
677         QueryResult<OrderedRows<String, String, String>> result = HFactory //
678             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
679             .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
680             .setColumnNames( "projectVersion" ) //
681             .addEqualsExpression( "repositoryName", repoId ) //
682             .addEqualsExpression( "namespaceId", namespace ) //
683             .addEqualsExpression( "projectId", projectId ) //
684             .execute();
685
686         int count = result.get().getCount();
687
688         if ( count < 1 )
689         {
690             return Collections.emptyList();
691         }
692
693         Set<String> versions = new HashSet<String>( count );
694
695         for ( Row<String, String, String> orderedRows : result.get() )
696         {
697             versions.add( getStringValue( orderedRows.getColumnSlice(), "projectVersion" ) );
698         }
699
700         return versions;
701
702     }
703
704     @Override
705     public ProjectMetadata getProject( final String repoId, final String namespace, final String id )
706         throws MetadataResolutionException
707     {
708
709         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
710         StringSerializer ss = StringSerializer.get();
711         QueryResult<OrderedRows<String, String, String>> result = HFactory //
712             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
713             .setColumnFamily( cassandraArchivaManager.getProjectFamilyName() ) //
714             .setColumnNames( "projectId" ) //
715             .addEqualsExpression( "repositoryName", repoId ) //
716             .addEqualsExpression( "namespaceId", namespace ) //
717             .addEqualsExpression( "projectId", id ) //
718             .execute();
719
720         int count = result.get().getCount();
721
722         if ( count < 1 )
723         {
724             return null;
725         }
726
727         ProjectMetadata projectMetadata = new ProjectMetadata();
728         projectMetadata.setId( id );
729         projectMetadata.setNamespace( namespace );
730
731         logger.debug( "getProject repoId: {}, namespace: {}, projectId: {} -> {}", repoId, namespace, id,
732                       projectMetadata );
733
734         return projectMetadata;
735     }
736
737     protected ProjectVersionMetadataModel mapProjectVersionMetadataModel( ColumnSlice<String, String> columnSlice )
738     {
739         ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
740         projectVersionMetadataModel.setId( getStringValue( columnSlice, "id" ) );
741         projectVersionMetadataModel.setDescription( getStringValue( columnSlice, "description" ) );
742         projectVersionMetadataModel.setName( getStringValue( columnSlice, "name" ) );
743         projectVersionMetadataModel.setNamespace( new Namespace( getStringValue( columnSlice, "namespaceId" ), //
744                                                                  new Repository(
745                                                                      getStringValue( columnSlice, "repositoryName" ) )
746                                                   )
747         );
748         projectVersionMetadataModel.setIncomplete(
749             Boolean.parseBoolean( getStringValue( columnSlice, "incomplete" ) ) );
750         projectVersionMetadataModel.setProjectId( getStringValue( columnSlice, "projectId" ) );
751         projectVersionMetadataModel.setUrl( getStringValue( columnSlice, "url" ) );
752         return projectVersionMetadataModel;
753     }
754
755
756     @Override
757     public void updateProjectVersion( String repositoryId, String namespaceId, String projectId,
758                                       ProjectVersionMetadata versionMetadata )
759         throws MetadataRepositoryException
760     {
761         try
762         {
763             Namespace namespace = getNamespace( repositoryId, namespaceId );
764
765             if ( namespace == null )
766             {
767                 updateOrAddNamespace( repositoryId, namespaceId );
768             }
769
770             if ( getProject( repositoryId, namespaceId, projectId ) == null )
771             {
772                 ProjectMetadata projectMetadata = new ProjectMetadata();
773                 projectMetadata.setNamespace( namespaceId );
774                 projectMetadata.setId( projectId );
775                 updateProject( repositoryId, projectMetadata );
776             }
777
778         }
779         catch ( MetadataResolutionException e )
780         {
781             throw new MetadataRepositoryException( e.getMessage(), e );
782         }
783
784         StringSerializer ss = StringSerializer.get();
785
786         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
787         QueryResult<OrderedRows<String, String, String>> result = HFactory //
788             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
789             .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
790             .setColumnNames( "projectVersion" ) //
791             .addEqualsExpression( "repositoryName", repositoryId ) //
792             .addEqualsExpression( "namespaceId", namespaceId ) //
793             .addEqualsExpression( "projectId", projectId ) //
794             .addEqualsExpression( "projectVersion", versionMetadata.getId() ) //
795             .execute();
796
797         ProjectVersionMetadataModel projectVersionMetadataModel = null;
798         boolean creation = true;
799         if ( result.get().getCount() > 0 )
800         {
801             projectVersionMetadataModel =
802                 mapProjectVersionMetadataModel( result.get().getList().get( 0 ).getColumnSlice() );
803             creation = false;
804         }
805         else
806         {
807             projectVersionMetadataModel = getModelMapper().map( versionMetadata, ProjectVersionMetadataModel.class );
808         }
809
810         projectVersionMetadataModel.setProjectId( projectId );
811         projectVersionMetadataModel.setNamespace( new Namespace( namespaceId, new Repository( repositoryId ) ) );
812
813         projectVersionMetadataModel.setCiManagement( versionMetadata.getCiManagement() );
814         projectVersionMetadataModel.setIssueManagement( versionMetadata.getIssueManagement() );
815         projectVersionMetadataModel.setOrganization( versionMetadata.getOrganization() );
816         projectVersionMetadataModel.setScm( versionMetadata.getScm() );
817
818         projectVersionMetadataModel.setMailingLists( versionMetadata.getMailingLists() );
819         projectVersionMetadataModel.setDependencies( versionMetadata.getDependencies() );
820         projectVersionMetadataModel.setLicenses( versionMetadata.getLicenses() );
821
822         // we don't test of repository and namespace really exist !
823         String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
824             namespaceId ).withProjectId( projectId ).withId( versionMetadata.getId() ).build();
825
826         // FIXME nested objects to store!!!
827         if ( creation )
828         {
829             String cf = cassandraArchivaManager.getProjectVersionMetadataModelFamilyName();
830             Mutator<String> mutator = projectVersionMetadataModelTemplate.createMutator()
831                 //  values
832                 .addInsertion( key, cf, column( "projectId", projectId ) ) //
833                 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
834                 .addInsertion( key, cf, column( "namespaceId", namespaceId ) )//
835                 .addInsertion( key, cf, column( "projectVersion", versionMetadata.getVersion() ) ); //
836
837             addInsertion( mutator, key, cf, "description", versionMetadata.getDescription() );
838
839             addInsertion( mutator, key, cf, "name", versionMetadata.getName() );
840
841             addInsertion( mutator, key, cf, "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
842
843             addInsertion( mutator, key, cf, "url", versionMetadata.getUrl() );
844             {
845                 CiManagement ci = versionMetadata.getCiManagement();
846                 if ( ci != null )
847                 {
848                     addInsertion( mutator, key, cf, "ciManagement.system", ci.getSystem() );
849                     addInsertion( mutator, key, cf, "ciManagement.url", ci.getUrl() );
850                 }
851             }
852
853             {
854                 IssueManagement issueManagement = versionMetadata.getIssueManagement();
855
856                 if ( issueManagement != null )
857                 {
858                     addInsertion( mutator, key, cf, "issueManagement.system", issueManagement.getSystem() );
859                     addInsertion( mutator, key, cf, "issueManagement.url", issueManagement.getUrl() );
860                 }
861             }
862
863             {
864                 Organization organization = versionMetadata.getOrganization();
865                 if ( organization != null )
866                 {
867                     addInsertion( mutator, key, cf, "organization.name", organization.getName() );
868                     addInsertion( mutator, key, cf, "organization.url", organization.getUrl() );
869                 }
870             }
871
872             {
873                 Scm scm = versionMetadata.getScm();
874                 if ( scm != null )
875                 {
876                     addInsertion( mutator, key, cf, "scm.url", scm.getUrl() );
877                     addInsertion( mutator, key, cf, "scm.connection", scm.getConnection() );
878                     addInsertion( mutator, key, cf, "scm.developerConnection", scm.getDeveloperConnection() );
879                 }
880             }
881
882             MutationResult mutationResult = mutator.execute();
883         }
884         else
885         {
886             ColumnFamilyUpdater<String, String> updater = projectVersionMetadataModelTemplate.createUpdater( key );
887             updater.setString( "projectId", projectId );
888             updater.setString( "repositoryName", repositoryId );
889             updater.setString( "namespaceId", namespaceId );
890             updater.setString( "projectVersion", versionMetadata.getVersion() );
891             if ( StringUtils.isNotEmpty( versionMetadata.getDescription() ) )
892             {
893                 updater.setString( "description", versionMetadata.getDescription() );
894             }
895             if ( StringUtils.isNotEmpty( versionMetadata.getName() ) )
896             {
897                 updater.setString( "name", versionMetadata.getName() );
898             }
899             updater.setString( "incomplete", Boolean.toString( versionMetadata.isIncomplete() ) );
900             if ( StringUtils.isNotEmpty( versionMetadata.getUrl() ) )
901             {
902                 updater.setString( "url", versionMetadata.getUrl() );
903             }
904
905             {
906                 CiManagement ci = versionMetadata.getCiManagement();
907                 if ( ci != null )
908                 {
909                     updater.setString( "ciManagement.system", ci.getSystem() );
910                     updater.setString( "ciManagement.url", ci.getUrl() );
911                 }
912             }
913             {
914                 IssueManagement issueManagement = versionMetadata.getIssueManagement();
915                 if ( issueManagement != null )
916                 {
917                     updater.setString( "issueManagement.system", issueManagement.getSystem() );
918                     updater.setString( "issueManagement.url", issueManagement.getUrl() );
919                 }
920             }
921             {
922                 Organization organization = versionMetadata.getOrganization();
923                 if ( organization != null )
924                 {
925                     updater.setString( "organization.name", organization.getName() );
926                     updater.setString( "organization.url", organization.getUrl() );
927                 }
928             }
929             {
930                 Scm scm = versionMetadata.getScm();
931                 if ( scm != null )
932                 {
933                     updater.setString( "scm.url", scm.getUrl() );
934                     updater.setString( "scm.connection", scm.getConnection() );
935                     updater.setString( "scm.developerConnection", scm.getDeveloperConnection() );
936                 }
937             }
938
939             projectVersionMetadataModelTemplate.update( updater );
940
941         }
942
943         ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
944         artifactMetadataModel.setRepositoryId( repositoryId );
945         artifactMetadataModel.setNamespace( namespaceId );
946         artifactMetadataModel.setProject( projectId );
947         artifactMetadataModel.setProjectVersion( versionMetadata.getVersion() );
948         artifactMetadataModel.setVersion( versionMetadata.getVersion() );
949         updateFacets( versionMetadata, artifactMetadataModel );
950
951     }
952
953
954     @Override
955     public ProjectVersionMetadata getProjectVersion( final String repoId, final String namespace,
956                                                      final String projectId, final String projectVersion )
957         throws MetadataResolutionException
958     {
959         String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
960             namespace ).withProjectId( projectId ).withId( projectVersion ).build();
961
962         ColumnFamilyResult<String, String> columnFamilyResult =
963             this.projectVersionMetadataModelTemplate.queryColumns( key );
964         if ( !columnFamilyResult.hasResults() )
965         {
966             return null;
967         }
968
969         ProjectVersionMetadata projectVersionMetadata = new ProjectVersionMetadata();
970         projectVersionMetadata.setId( columnFamilyResult.getString( "projectVersion" ) );
971         projectVersionMetadata.setDescription( columnFamilyResult.getString( "description" ) );
972         projectVersionMetadata.setName( columnFamilyResult.getString( "name" ) );
973
974         projectVersionMetadata.setIncomplete( Boolean.parseBoolean( columnFamilyResult.getString( "incomplete" ) ) );
975
976         projectVersionMetadata.setUrl( columnFamilyResult.getString( "url" ) );
977         {
978             String ciUrl = columnFamilyResult.getString( "ciManagement.url" );
979             String ciSystem = columnFamilyResult.getString( "ciManagement.system" );
980
981             if ( StringUtils.isNotEmpty( ciSystem ) || StringUtils.isNotEmpty( ciUrl ) )
982             {
983                 projectVersionMetadata.setCiManagement( new CiManagement( ciSystem, ciUrl ) );
984             }
985         }
986         {
987             String issueUrl = columnFamilyResult.getString( "issueManagement.url" );
988             String issueSystem = columnFamilyResult.getString( "issueManagement.system" );
989             if ( StringUtils.isNotEmpty( issueSystem ) || StringUtils.isNotEmpty( issueUrl ) )
990             {
991                 projectVersionMetadata.setIssueManagement( new IssueManagement( issueSystem, issueUrl ) );
992             }
993         }
994         {
995             String organizationUrl = columnFamilyResult.getString( "organization.url" );
996             String organizationName = columnFamilyResult.getString( "organization.name" );
997             if ( StringUtils.isNotEmpty( organizationUrl ) || StringUtils.isNotEmpty( organizationName ) )
998             {
999                 projectVersionMetadata.setOrganization( new Organization( organizationName, organizationUrl ) );
1000             }
1001         }
1002         {
1003             String devConn = columnFamilyResult.getString( "scm.developerConnection" );
1004             String conn = columnFamilyResult.getString( "scm.connection" );
1005             String url = columnFamilyResult.getString( "scm.url" );
1006             if ( StringUtils.isNotEmpty( devConn ) || StringUtils.isNotEmpty( conn ) || StringUtils.isNotEmpty( url ) )
1007             {
1008                 projectVersionMetadata.setScm( new Scm( conn, devConn, url ) );
1009             }
1010         }
1011         // FIXME complete collections !!
1012         // facets
1013
1014         StringSerializer ss = StringSerializer.get();
1015
1016         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1017         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1018             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1019             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1020             .setColumnNames( "facetId", "key", "value", "name" ) //
1021             .addEqualsExpression( "repositoryName", repoId ) //
1022             .addEqualsExpression( "namespaceId", namespace ) //
1023             .addEqualsExpression( "projectId", projectId ) //
1024             .addEqualsExpression( "projectVersion", projectVersion ) //
1025             .execute();
1026
1027         Map<String, Map<String, String>> metadataFacetsPerFacetIds = new HashMap<String, Map<String, String>>();
1028
1029         for ( Row<String, String, String> row : result.get() )
1030         {
1031             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1032             String facetId = getStringValue( columnSlice, "facetId" );
1033             Map<String, String> metaValues = metadataFacetsPerFacetIds.get( facetId );
1034             if ( metaValues == null )
1035             {
1036                 metaValues = new HashMap<String, String>();
1037                 metadataFacetsPerFacetIds.put( facetId, metaValues );
1038             }
1039             metaValues.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1040         }
1041
1042         if ( !metadataFacetsPerFacetIds.isEmpty() )
1043         {
1044             for ( Map.Entry<String, Map<String, String>> entry : metadataFacetsPerFacetIds.entrySet() )
1045             {
1046                 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1047                 if ( metadataFacetFactory != null )
1048                 {
1049                     MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repoId, entry.getKey() );
1050                     metadataFacet.fromProperties( entry.getValue() );
1051                     projectVersionMetadata.addFacet( metadataFacet );
1052                 }
1053             }
1054         }
1055
1056         return projectVersionMetadata;
1057     }
1058
1059
1060     @Override
1061     public void updateArtifact( String repositoryId, String namespaceId, String projectId, String projectVersion,
1062                                 ArtifactMetadata artifactMeta )
1063         throws MetadataRepositoryException
1064     {
1065
1066         Namespace namespace = getNamespace( repositoryId, namespaceId );
1067         if ( namespace == null )
1068         {
1069             namespace = updateOrAddNamespace( repositoryId, namespaceId );
1070         }
1071
1072         ProjectMetadata projectMetadata = new ProjectMetadata();
1073         projectMetadata.setId( projectId );
1074         projectMetadata.setNamespace( namespaceId );
1075         updateProject( repositoryId, projectMetadata );
1076
1077         String key = new ArtifactMetadataModel.KeyBuilder().withNamespace( namespace ).withProject( projectId ).withId(
1078             artifactMeta.getId() ).withProjectVersion( projectVersion ).build();
1079
1080         // exists?
1081
1082         boolean exists = this.artifactMetadataTemplate.isColumnsExist( key );
1083
1084         if ( exists )
1085         {
1086             // updater
1087             ColumnFamilyUpdater<String, String> updater = this.artifactMetadataTemplate.createUpdater( key );
1088             updater.setLong( "fileLastModified", artifactMeta.getFileLastModified().getTime() );
1089             updater.setLong( "whenGathered", artifactMeta.getWhenGathered().getTime() );
1090             updater.setLong( "size", artifactMeta.getSize() );
1091             updater.setString( "md5", artifactMeta.getMd5() );
1092             updater.setString( "sha1", artifactMeta.getSha1() );
1093             updater.setString( "version", artifactMeta.getVersion() );
1094             this.artifactMetadataTemplate.update( updater );
1095         }
1096         else
1097         {
1098             String cf = this.cassandraArchivaManager.getArtifactMetadataModelFamilyName();
1099             // create
1100             this.artifactMetadataTemplate.createMutator() //
1101                 .addInsertion( key, cf, column( "id", artifactMeta.getId() ) )//
1102                 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1103                 .addInsertion( key, cf, column( "namespaceId", namespaceId ) ) //
1104                 .addInsertion( key, cf, column( "project", artifactMeta.getProject() ) ) //
1105                 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1106                 .addInsertion( key, cf, column( "version", artifactMeta.getVersion() ) ) //
1107                 .addInsertion( key, cf, column( "fileLastModified", artifactMeta.getFileLastModified().getTime() ) ) //
1108                 .addInsertion( key, cf, column( "size", artifactMeta.getSize() ) ) //
1109                 .addInsertion( key, cf, column( "md5", artifactMeta.getMd5() ) ) //
1110                 .addInsertion( key, cf, column( "sha1", artifactMeta.getSha1() ) ) //
1111                 .addInsertion( key, cf, column( "whenGathered", artifactMeta.getWhenGathered().getTime() ) )//
1112                 .execute();
1113         }
1114
1115         key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repositoryId ).withNamespace(
1116             namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1117
1118         exists = this.projectVersionMetadataModelTemplate.isColumnsExist( key );
1119
1120         if ( !exists )
1121         {
1122             ProjectVersionMetadataModel projectVersionMetadataModel = new ProjectVersionMetadataModel();
1123             projectVersionMetadataModel.setProjectId( projectId );
1124             projectVersionMetadataModel.setId( projectVersion );
1125             projectVersionMetadataModel.setNamespace( namespace );
1126
1127             String cf = this.cassandraArchivaManager.getProjectVersionMetadataModelFamilyName();
1128
1129             projectVersionMetadataModelTemplate.createMutator() //
1130                 .addInsertion( key, cf, column( "namespaceId", namespace.getName() ) ) //
1131                 .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1132                 .addInsertion( key, cf, column( "projectVersion", projectVersion ) ) //
1133                 .addInsertion( key, cf, column( "projectId", projectId ) ) //
1134                 .execute();
1135
1136         }
1137
1138         ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1139
1140         artifactMetadataModel.setRepositoryId( repositoryId );
1141         artifactMetadataModel.setNamespace( namespaceId );
1142         artifactMetadataModel.setProject( projectId );
1143         artifactMetadataModel.setProjectVersion( projectVersion );
1144         artifactMetadataModel.setVersion( artifactMeta.getVersion() );
1145         artifactMetadataModel.setFileLastModified( artifactMeta.getFileLastModified() == null
1146                                                        ? new Date().getTime()
1147                                                        : artifactMeta.getFileLastModified().getTime() );
1148
1149         // now facets
1150         updateFacets( artifactMeta, artifactMetadataModel );
1151
1152     }
1153
1154     @Override
1155     public Collection<String> getArtifactVersions( final String repoId, final String namespace, final String projectId,
1156                                                    final String projectVersion )
1157         throws MetadataResolutionException
1158     {
1159         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1160         StringSerializer ss = StringSerializer.get();
1161         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1162             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1163             .setColumnFamily( cassandraArchivaManager.getProjectVersionMetadataModelFamilyName() ) //
1164             .setColumnNames( "projectVersion" ) //
1165             .addEqualsExpression( "repositoryName", repoId ) //
1166             .addEqualsExpression( "namespaceId", namespace ) //
1167             .addEqualsExpression( "projectId", projectId ) //
1168             .addEqualsExpression( "projectVersion", projectVersion ) //
1169             .execute();
1170
1171         final Set<String> versions = new HashSet<String>();
1172
1173         for ( Row<String, String, String> row : result.get() )
1174         {
1175             versions.add( getStringValue( row.getColumnSlice(), "projectVersion" ) );
1176         }
1177
1178         return versions;
1179
1180     }
1181
1182     /**
1183      * iterate over available facets to remove/add from the artifactMetadata
1184      *
1185      * @param facetedMetadata
1186      * @param artifactMetadataModel only use for the key
1187      */
1188     private void updateFacets( final FacetedMetadata facetedMetadata,
1189                                final ArtifactMetadataModel artifactMetadataModel )
1190     {
1191         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1192         StringSerializer ss = StringSerializer.get();
1193         String cf = cassandraArchivaManager.getMetadataFacetModelFamilyName();
1194
1195         for ( final String facetId : metadataFacetFactories.keySet() )
1196         {
1197             MetadataFacet metadataFacet = facetedMetadata.getFacet( facetId );
1198             if ( metadataFacet == null )
1199             {
1200                 continue;
1201             }
1202             // clean first
1203
1204             QueryResult<OrderedRows<String, String, String>> result =
1205                 HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1206                     .setColumnFamily( cf ) //
1207                     .setColumnNames( "repositoryName" ) //
1208                     .addEqualsExpression( "repositoryName", artifactMetadataModel.getRepositoryId() ) //
1209                     .addEqualsExpression( "namespaceId", artifactMetadataModel.getNamespace() ) //
1210                     .addEqualsExpression( "projectId", artifactMetadataModel.getProject() ) //
1211                     .addEqualsExpression( "projectVersion",
1212                                           artifactMetadataModel.getProjectVersion() ).addEqualsExpression( "facetId",
1213                                                                                                            facetId ) //
1214                     .execute();
1215
1216             for ( Row<String, String, String> row : result.get().getList() )
1217             {
1218                 this.metadataFacetTemplate.deleteRow( row.getKey() );
1219             }
1220
1221             Map<String, String> properties = metadataFacet.toProperties();
1222
1223             for ( Map.Entry<String, String> entry : properties.entrySet() )
1224             {
1225                 String key = new MetadataFacetModel.KeyBuilder().withKey( entry.getKey() ).withArtifactMetadataModel(
1226                     artifactMetadataModel ).withFacetId( facetId ).withName( metadataFacet.getName() ).build();
1227                 metadataFacetTemplate.createMutator() //
1228                     .addInsertion( key, cf, column( "repositoryName", artifactMetadataModel.getRepositoryId() ) ) //
1229                     .addInsertion( key, cf, column( "namespaceId", artifactMetadataModel.getNamespace() ) ) //
1230                     .addInsertion( key, cf, column( "projectId", artifactMetadataModel.getProject() ) ) //
1231                     .addInsertion( key, cf, column( "projectVersion", artifactMetadataModel.getProjectVersion() ) ) //
1232                     .addInsertion( key, cf, column( "facetId", facetId ) ) //
1233                     .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1234                     .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1235                     .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1236                     .execute();
1237             }
1238         }
1239     }
1240
1241
1242     @Override
1243     public List<String> getMetadataFacets( final String repositoryId, final String facetId )
1244         throws MetadataRepositoryException
1245     {
1246
1247         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1248         StringSerializer ss = StringSerializer.get();
1249         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1250             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1251             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1252             .setColumnNames( "name" ) //
1253             .addEqualsExpression( "repositoryName", repositoryId ) //
1254             .addEqualsExpression( "facetId", facetId ) //
1255             .execute();
1256
1257         final List<String> facets = new ArrayList<String>();
1258
1259         for ( Row<String, String, String> row : result.get() )
1260         {
1261             facets.add( getStringValue( row.getColumnSlice(), "name" ) );
1262         }
1263         return facets;
1264     }
1265
1266     @Override
1267     public boolean hasMetadataFacet( String repositoryId, String facetId )
1268         throws MetadataRepositoryException
1269     {
1270         return !getMetadataFacets( repositoryId, facetId ).isEmpty();
1271     }
1272
1273     @Override
1274     public MetadataFacet getMetadataFacet( final String repositoryId, final String facetId, final String name )
1275         throws MetadataRepositoryException
1276     {
1277
1278         MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( facetId );
1279         if ( metadataFacetFactory == null )
1280         {
1281             return null;
1282         }
1283
1284         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1285         StringSerializer ss = StringSerializer.get();
1286         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1287             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1288             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1289             .setColumnNames( "key", "value" ) //
1290             .addEqualsExpression( "repositoryName", repositoryId ) //
1291             .addEqualsExpression( "facetId", facetId ) //
1292             .addEqualsExpression( "name", name ) //
1293             .execute();
1294
1295         MetadataFacet metadataFacet = metadataFacetFactory.createMetadataFacet( repositoryId, name );
1296         Map<String, String> map = new HashMap<String, String>( result.get().getCount() );
1297         for ( Row<String, String, String> row : result.get() )
1298         {
1299             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1300             map.put( getStringValue( columnSlice, "key" ), getStringValue( columnSlice, "value" ) );
1301         }
1302         metadataFacet.fromProperties( map );
1303         return metadataFacet;
1304     }
1305
1306     @Override
1307     public void addMetadataFacet( String repositoryId, MetadataFacet metadataFacet )
1308         throws MetadataRepositoryException
1309     {
1310
1311         if ( metadataFacet == null )
1312         {
1313             return;
1314         }
1315
1316         if ( metadataFacet.toProperties().isEmpty() )
1317         {
1318             String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1319                 metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).build();
1320
1321             boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1322
1323             if ( exists )
1324             {
1325                 ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1326                 updater.setString( "facetId", metadataFacet.getFacetId() );
1327                 updater.setString( "name", metadataFacet.getName() );
1328                 this.metadataFacetTemplate.update( updater );
1329             }
1330             else
1331             {
1332                 String cf = this.cassandraArchivaManager.getMetadataFacetModelFamilyName();
1333                 this.metadataFacetTemplate.createMutator() //
1334                     .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1335                     .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1336                     .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1337                     .execute();
1338             }
1339
1340         }
1341         else
1342         {
1343             for ( Map.Entry<String, String> entry : metadataFacet.toProperties().entrySet() )
1344             {
1345
1346                 String key = new MetadataFacetModel.KeyBuilder().withRepositoryId( repositoryId ).withFacetId(
1347                     metadataFacet.getFacetId() ).withName( metadataFacet.getName() ).withKey( entry.getKey() ).build();
1348
1349                 boolean exists = this.metadataFacetTemplate.isColumnsExist( key );
1350                 if ( !exists )
1351                 {
1352                     //metadataFacetModel = new MetadataFacetModel();
1353                     // we need to store the repositoryId
1354                     //ArtifactMetadataModel artifactMetadataModel = new ArtifactMetadataModel();
1355                     //artifactMetadataModel.setRepositoryId( repositoryId );
1356                     //metadataFacetModel.setArtifactMetadataModel( artifactMetadataModel );
1357                     //metadataFacetModel.setId( key );
1358                     //metadataFacetModel.setKey( entry.getKey() );
1359                     //metadataFacetModel.setFacetId( metadataFacet.getFacetId() );
1360                     //metadataFacetModel.setName( metadataFacet.getName() );
1361
1362                     String cf = this.cassandraArchivaManager.getMetadataFacetModelFamilyName();
1363                     this.metadataFacetTemplate.createMutator() //
1364                         .addInsertion( key, cf, column( "repositoryName", repositoryId ) ) //
1365                         .addInsertion( key, cf, column( "facetId", metadataFacet.getFacetId() ) ) //
1366                         .addInsertion( key, cf, column( "name", metadataFacet.getName() ) ) //
1367                         .addInsertion( key, cf, column( "key", entry.getKey() ) ) //
1368                         .addInsertion( key, cf, column( "value", entry.getValue() ) ) //
1369                         .execute();
1370
1371                 }
1372                 else
1373                 {
1374                     ColumnFamilyUpdater<String, String> updater = this.metadataFacetTemplate.createUpdater( key );
1375                     updater.setString( "value", entry.getValue() );
1376                     this.metadataFacetTemplate.update( updater );
1377                 }
1378             }
1379         }
1380     }
1381
1382     @Override
1383     public void removeMetadataFacets( final String repositoryId, final String facetId )
1384         throws MetadataRepositoryException
1385     {
1386         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1387         StringSerializer ss = StringSerializer.get();
1388         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1389             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1390             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1391             .setColumnNames( "key", "value" ) //
1392             .addEqualsExpression( "repositoryName", repositoryId ) //
1393             .addEqualsExpression( "facetId", facetId ) //
1394             .execute();
1395
1396         for ( Row<String, String, String> row : result.get() )
1397         {
1398             this.metadataFacetTemplate.deleteRow( row.getKey() );
1399         }
1400
1401     }
1402
1403     @Override
1404     public void removeMetadataFacet( final String repositoryId, final String facetId, final String name )
1405         throws MetadataRepositoryException
1406     {
1407         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1408         StringSerializer ss = StringSerializer.get();
1409         QueryResult<OrderedRows<String, String, String>> result = HFactory //
1410             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1411             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1412             .setColumnNames( "key", "value" ) //
1413             .addEqualsExpression( "repositoryName", repositoryId ) //
1414             .addEqualsExpression( "facetId", facetId ) //
1415             .addEqualsExpression( "name", name ) //
1416             .execute();
1417
1418         for ( Row<String, String, String> row : result.get() )
1419         {
1420             this.metadataFacetTemplate.deleteRow( row.getKey() );
1421         }
1422     }
1423
1424     @Override
1425     public List<ArtifactMetadata> getArtifactsByDateRange( final String repositoryId, final Date startTime,
1426                                                            final Date endTime )
1427         throws MetadataRepositoryException
1428     {
1429
1430         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1431         StringSerializer ss = StringSerializer.get();
1432         LongSerializer ls = LongSerializer.get();
1433         RangeSlicesQuery<String, String, Long> query = HFactory //
1434             .createRangeSlicesQuery( keyspace, ss, ss, ls ) //
1435             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1436             .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1437                              "repositoryName", "version", "whenGathered", "sha1" ); //
1438
1439         if ( startTime != null )
1440         {
1441             query = query.addGteExpression( "whenGathered", startTime.getTime() );
1442         }
1443         if ( endTime != null )
1444         {
1445             query = query.addLteExpression( "whenGathered", endTime.getTime() );
1446         }
1447         QueryResult<OrderedRows<String, String, Long>> result = query.execute();
1448
1449         List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1450
1451         for ( Row<String, String, Long> row : result.get() )
1452         {
1453             ColumnSlice<String, Long> columnSlice = row.getColumnSlice();
1454             String repositoryName = getAsStringValue( columnSlice, "repositoryName" );
1455             if ( StringUtils.equals( repositoryName, repositoryId ) )
1456             {
1457
1458                 artifactMetadatas.add( mapArtifactMetadataLongColumnSlice( columnSlice ) );
1459             }
1460         }
1461
1462         return artifactMetadatas;
1463     }
1464
1465
1466     protected ArtifactMetadata mapArtifactMetadataLongColumnSlice( ColumnSlice<String, Long> columnSlice )
1467     {
1468         ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1469         artifactMetadata.setNamespace( getAsStringValue( columnSlice, "namespaceId" ) );
1470         artifactMetadata.setSize( getLongValue( columnSlice, "size" ) );
1471         artifactMetadata.setId( getAsStringValue( columnSlice, "id" ) );
1472         artifactMetadata.setFileLastModified( getLongValue( columnSlice, "fileLastModified" ) );
1473         artifactMetadata.setMd5( getAsStringValue( columnSlice, "md5" ) );
1474         artifactMetadata.setProject( getAsStringValue( columnSlice, "project" ) );
1475         artifactMetadata.setProjectVersion( getAsStringValue( columnSlice, "projectVersion" ) );
1476         artifactMetadata.setRepositoryId( getAsStringValue( columnSlice, "repositoryName" ) );
1477         artifactMetadata.setSha1( getAsStringValue( columnSlice, "sha1" ) );
1478         artifactMetadata.setVersion( getAsStringValue( columnSlice, "version" ) );
1479         Long whenGathered = getLongValue( columnSlice, "whenGathered" );
1480         if ( whenGathered != null )
1481         {
1482             artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1483         }
1484         return artifactMetadata;
1485     }
1486
1487     protected ArtifactMetadata mapArtifactMetadataStringColumnSlice( ColumnSlice<String, String> columnSlice )
1488     {
1489         ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1490         artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
1491         artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
1492         artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
1493         artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
1494         artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
1495         artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
1496         artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1497         artifactMetadata.setRepositoryId( getStringValue( columnSlice, "repositoryName" ) );
1498         artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
1499         artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
1500         Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
1501         if ( whenGathered != null )
1502         {
1503             artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1504         }
1505         return artifactMetadata;
1506     }
1507
1508     @Override
1509     public Collection<ArtifactMetadata> getArtifactsByChecksum( final String repositoryId, final String checksum )
1510         throws MetadataRepositoryException
1511     {
1512         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1513         StringSerializer ss = StringSerializer.get();
1514
1515         // cql cannot run or in queries so running twice the query
1516         Map<String, ArtifactMetadata> artifactMetadataMap = new HashMap<String, ArtifactMetadata>();
1517
1518         RangeSlicesQuery<String, String, String> query = HFactory //
1519             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1520             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1521             .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1522                              "repositoryName", "version", "whenGathered", "sha1" ); //
1523
1524         query = query.addEqualsExpression( "sha1", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1525
1526         QueryResult<OrderedRows<String, String, String>> result = query.execute();
1527
1528         for ( Row<String, String, String> row : result.get() )
1529         {
1530             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1531
1532             artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1533
1534         }
1535
1536         query = HFactory //
1537             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1538             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1539             .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1540                              "repositoryName", "version", "whenGathered", "sha1" ); //
1541
1542         query = query.addEqualsExpression( "md5", checksum ).addEqualsExpression( "repositoryName", repositoryId );
1543
1544         result = query.execute();
1545
1546         for ( Row<String, String, String> row : result.get() )
1547         {
1548             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1549
1550             artifactMetadataMap.put( row.getKey(), mapArtifactMetadataStringColumnSlice( columnSlice ) );
1551
1552         }
1553
1554         return artifactMetadataMap.values();
1555     }
1556
1557
1558     @Override
1559     public void removeArtifact( final String repositoryId, final String namespace, final String project,
1560                                 final String version, final String id )
1561         throws MetadataRepositoryException
1562     {
1563         logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1564                       repositoryId, namespace, project, version, id );
1565         String key =
1566             new ArtifactMetadataModel.KeyBuilder().withRepositoryId( repositoryId ).withNamespace( namespace ).withId(
1567                 id ).withProjectVersion( version ).withProject( project ).build();
1568
1569         this.artifactMetadataTemplate.deleteRow( key );
1570
1571         key =
1572             new ProjectVersionMetadataModel.KeyBuilder().withId( version ).withRepository( repositoryId ).withNamespace(
1573                 namespace ).withProjectId( project ).build();
1574
1575         this.projectVersionMetadataModelTemplate.deleteRow( key );
1576
1577
1578     }
1579
1580     @Override
1581     public void removeArtifact( ArtifactMetadata artifactMetadata, String baseVersion )
1582         throws MetadataRepositoryException
1583     {
1584         logger.debug( "removeArtifact repositoryId: '{}', namespace: '{}', project: '{}', version: '{}', id: '{}'",
1585                       artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(),
1586                       artifactMetadata.getProject(), baseVersion, artifactMetadata.getId() );
1587         String key =
1588             new ArtifactMetadataModel.KeyBuilder().withRepositoryId( artifactMetadata.getRepositoryId() ).withNamespace(
1589                 artifactMetadata.getNamespace() ).withId( artifactMetadata.getId() ).withProjectVersion(
1590                 baseVersion ).withProject( artifactMetadata.getProject() ).build();
1591
1592         this.artifactMetadataTemplate.deleteRow( key );
1593
1594     }
1595
1596     @Override
1597     public void removeArtifact( final String repositoryId, final String namespace, final String project,
1598                                 final String version, final MetadataFacet metadataFacet )
1599         throws MetadataRepositoryException
1600     {
1601
1602         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1603
1604         StringSerializer ss = StringSerializer.get();
1605
1606         RangeSlicesQuery<String, String, String> query = HFactory //
1607             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1608             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1609             .setColumnNames( "namespaceId" ); //
1610
1611         query = query.addEqualsExpression( "repositoryName", repositoryId ) //
1612             .addEqualsExpression( "namespaceId", namespace ) //
1613             .addEqualsExpression( "project", project ) //
1614             .addEqualsExpression( "version", version );
1615
1616         QueryResult<OrderedRows<String, String, String>> result = query.execute();
1617
1618         for ( Row<String, String, String> row : result.get() )
1619         {
1620             this.artifactMetadataTemplate.deleteRow( row.getKey() );
1621         }
1622
1623
1624     }
1625
1626
1627     @Override
1628     public List<ArtifactMetadata> getArtifacts( final String repositoryId )
1629         throws MetadataRepositoryException
1630     {
1631         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1632         StringSerializer ss = StringSerializer.get();
1633
1634         RangeSlicesQuery<String, String, String> query = HFactory //
1635             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1636             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1637             .setColumnNames( "namespaceId", "size", "id", "fileLastModified", "md5", "project", "projectVersion",
1638                              "repositoryName", "version", "whenGathered", "sha1" ); //
1639
1640         query = query.addEqualsExpression( "repositoryName", repositoryId );
1641
1642         QueryResult<OrderedRows<String, String, String>> result = query.execute();
1643
1644         List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1645
1646         for ( Row<String, String, String> row : result.get() )
1647         {
1648             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1649
1650             artifactMetadatas.add( mapArtifactMetadataStringColumnSlice( columnSlice ) );
1651
1652         }
1653
1654         return artifactMetadatas;
1655     }
1656
1657
1658     @Override
1659     public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
1660                                                                      String projectVersion )
1661         throws MetadataResolutionException
1662     {
1663         // FIXME implement this
1664         return Collections.emptyList();
1665     }
1666
1667     @Override
1668     public void removeProjectVersion( final String repoId, final String namespace, final String projectId,
1669                                       final String projectVersion )
1670         throws MetadataRepositoryException
1671     {
1672
1673         String key = new ProjectVersionMetadataModel.KeyBuilder().withRepository( repoId ).withNamespace(
1674             namespace ).withProjectId( projectId ).withId( projectVersion ).build();
1675
1676         this.projectVersionMetadataModelTemplate.deleteRow( key );
1677
1678         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1679
1680         StringSerializer ss = StringSerializer.get();
1681
1682         RangeSlicesQuery<String, String, String> query = HFactory //
1683             .createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1684             .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1685             .setColumnNames( "namespaceId" ); //
1686
1687         query = query.addEqualsExpression( "repositoryName", repoId ) //
1688             .addEqualsExpression( "namespaceId", namespace ) //
1689             .addEqualsExpression( "project", projectId ) //
1690             .addEqualsExpression( "projectVersion", projectVersion );
1691
1692         QueryResult<OrderedRows<String, String, String>> result = query.execute();
1693
1694         for ( Row<String, String, String> row : result.get() )
1695         {
1696             this.artifactMetadataTemplate.deleteRow( row.getKey() );
1697         }
1698
1699     }
1700
1701     @Override
1702     public Collection<ArtifactMetadata> getArtifacts( final String repoId, final String namespace,
1703                                                       final String projectId, final String projectVersion )
1704         throws MetadataResolutionException
1705     {
1706
1707         Keyspace keyspace = cassandraArchivaManager.getKeyspace();
1708
1709         StringSerializer ss = StringSerializer.get();
1710
1711         QueryResult<OrderedRows<String, String, String>> result =
1712             HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1713                 .setColumnFamily( cassandraArchivaManager.getArtifactMetadataModelFamilyName() ) //
1714                 .setColumnNames( "id", "repositoryName", "namespaceId", "project", "projectVersion", "version",
1715                                  "fileLastModified", "size", "md5", "sha1", "whenGathered" )//
1716                 .setRowCount( Integer.MAX_VALUE ) //
1717                 .addEqualsExpression( "repositoryName", repoId ) //
1718                 .addEqualsExpression( "namespaceId", namespace ) //
1719                 .addEqualsExpression( "project", projectId ) //
1720                 .addEqualsExpression( "projectVersion", projectVersion ) //
1721                 .execute();
1722
1723         if ( result.get() == null || result.get().getCount() < 1 )
1724         {
1725             return Collections.emptyList();
1726         }
1727
1728         List<ArtifactMetadata> artifactMetadatas = new ArrayList<ArtifactMetadata>( result.get().getCount() );
1729
1730         for ( Row<String, String, String> row : result.get() )
1731         {
1732             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1733             ArtifactMetadata artifactMetadata = new ArtifactMetadata();
1734             artifactMetadata.setNamespace( getStringValue( columnSlice, "namespaceId" ) );
1735             artifactMetadata.setSize( getAsLongValue( columnSlice, "size" ) );
1736             artifactMetadata.setId( getStringValue( columnSlice, "id" ) );
1737             artifactMetadata.setFileLastModified( getAsLongValue( columnSlice, "fileLastModified" ) );
1738             artifactMetadata.setMd5( getStringValue( columnSlice, "md5" ) );
1739             artifactMetadata.setProject( getStringValue( columnSlice, "project" ) );
1740             artifactMetadata.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1741             artifactMetadata.setRepositoryId( repoId );
1742             artifactMetadata.setSha1( getStringValue( columnSlice, "sha1" ) );
1743             artifactMetadata.setVersion( getStringValue( columnSlice, "version" ) );
1744             Long whenGathered = getAsLongValue( columnSlice, "whenGathered" );
1745             if ( whenGathered != null )
1746             {
1747                 artifactMetadata.setWhenGathered( new Date( whenGathered ) );
1748             }
1749             artifactMetadatas.add( artifactMetadata );
1750         }
1751
1752         result = HFactory.createRangeSlicesQuery( keyspace, ss, ss, ss ) //
1753             .setColumnFamily( cassandraArchivaManager.getMetadataFacetModelFamilyName() ) //
1754             .setColumnNames( "facetId", "name", "value", "key", "projectVersion" ) //
1755             .setRowCount( Integer.MAX_VALUE ) //
1756             .addEqualsExpression( "repositoryName", repoId ) //
1757             .addEqualsExpression( "namespaceId", namespace ) //
1758             .addEqualsExpression( "projectId", projectId ) //
1759             .addEqualsExpression( "projectVersion", projectVersion ) //
1760             .execute();
1761
1762         if ( result.get() == null || result.get().getCount() < 1 )
1763         {
1764             return artifactMetadatas;
1765         }
1766
1767         final List<MetadataFacetModel> metadataFacetModels =
1768             new ArrayList<MetadataFacetModel>( result.get().getCount() );
1769
1770         for ( Row<String, String, String> row : result.get() )
1771         {
1772             ColumnSlice<String, String> columnSlice = row.getColumnSlice();
1773             MetadataFacetModel metadataFacetModel = new MetadataFacetModel();
1774             metadataFacetModel.setFacetId( getStringValue( columnSlice, "facetId" ) );
1775             metadataFacetModel.setName( getStringValue( columnSlice, "name" ) );
1776             metadataFacetModel.setValue( getStringValue( columnSlice, "value" ) );
1777             metadataFacetModel.setKey( getStringValue( columnSlice, "key" ) );
1778             metadataFacetModel.setProjectVersion( getStringValue( columnSlice, "projectVersion" ) );
1779             metadataFacetModels.add( metadataFacetModel );
1780         }
1781
1782         // rebuild MetadataFacet for artifacts
1783
1784         for ( final ArtifactMetadata artifactMetadata : artifactMetadatas )
1785         {
1786             Iterable<MetadataFacetModel> metadataFacetModelIterable =
1787                 Iterables.filter( metadataFacetModels, new Predicate<MetadataFacetModel>()
1788                 {
1789                     @Override
1790                     public boolean apply( MetadataFacetModel metadataFacetModel )
1791                     {
1792                         if ( metadataFacetModel != null )
1793                         {
1794                             return StringUtils.equals( artifactMetadata.getVersion(),
1795                                                        metadataFacetModel.getProjectVersion() );
1796                         }
1797                         return false;
1798                     }
1799                 } );
1800             Iterator<MetadataFacetModel> iterator = metadataFacetModelIterable.iterator();
1801             Map<String, List<MetadataFacetModel>> metadataFacetValuesPerFacetId =
1802                 new HashMap<String, List<MetadataFacetModel>>();
1803             while ( iterator.hasNext() )
1804             {
1805                 MetadataFacetModel metadataFacetModel = iterator.next();
1806                 List<MetadataFacetModel> values = metadataFacetValuesPerFacetId.get( metadataFacetModel.getName() );
1807                 if ( values == null )
1808                 {
1809                     values = new ArrayList<MetadataFacetModel>();
1810                     metadataFacetValuesPerFacetId.put( metadataFacetModel.getFacetId(), values );
1811                 }
1812                 values.add( metadataFacetModel );
1813
1814             }
1815
1816             for ( Map.Entry<String, List<MetadataFacetModel>> entry : metadataFacetValuesPerFacetId.entrySet() )
1817             {
1818                 MetadataFacetFactory metadataFacetFactory = metadataFacetFactories.get( entry.getKey() );
1819                 if ( metadataFacetFactory != null )
1820                 {
1821                     List<MetadataFacetModel> facetModels = entry.getValue();
1822                     if ( !facetModels.isEmpty() )
1823                     {
1824                         MetadataFacet metadataFacet =
1825                             metadataFacetFactory.createMetadataFacet( repoId, facetModels.get( 0 ).getName() );
1826                         Map<String, String> props = new HashMap<String, String>( facetModels.size() );
1827                         for ( MetadataFacetModel metadataFacetModel : facetModels )
1828                         {
1829                             props.put( metadataFacetModel.getKey(), metadataFacetModel.getValue() );
1830                         }
1831                         metadataFacet.fromProperties( props );
1832                         artifactMetadata.addFacet( metadataFacet );
1833                     }
1834                 }
1835             }
1836
1837
1838         }
1839
1840         return artifactMetadatas;
1841     }
1842
1843     @Override
1844     public void save()
1845     {
1846         logger.trace( "save" );
1847     }
1848
1849     @Override
1850     public void close()
1851         throws MetadataRepositoryException
1852     {
1853         logger.trace( "close" );
1854     }
1855
1856     @Override
1857     public void revert()
1858     {
1859         logger.warn( "CassandraMetadataRepository cannot revert" );
1860     }
1861
1862     @Override
1863     public boolean canObtainAccess( Class<?> aClass )
1864     {
1865         return false;
1866     }
1867
1868     @Override
1869     public <T> T obtainAccess( Class<T> aClass )
1870         throws MetadataRepositoryException
1871     {
1872         throw new IllegalArgumentException(
1873             "Access using " + aClass + " is not supported on the cassandra metadata storage" );
1874     }
1875
1876
1877     private static class ModelMapperHolder
1878     {
1879         private static ModelMapper MODEL_MAPPER = new ModelMapper();
1880     }
1881
1882     protected ModelMapper getModelMapper()
1883     {
1884         return ModelMapperHolder.MODEL_MAPPER;
1885     }
1886 }