1 package org.apache.maven.archiva.reporting.database;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.reporting.model.ArtifactResults;
20 import org.apache.maven.archiva.reporting.model.MetadataResults;
21 import org.apache.maven.archiva.reporting.model.Reporting;
22 import org.apache.maven.archiva.reporting.model.Result;
23 import org.apache.maven.archiva.reporting.group.ReportGroup;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.LinkedHashSet;
36 * @todo i18n, including message formatting and parameterisation
38 public class ReportingDatabase
40 private final Reporting reporting;
42 private Map artifactMap;
44 private Map metadataMap;
46 private int numFailures;
48 private int numWarnings;
50 private ArtifactRepository repository;
52 private boolean inProgress;
54 private long startTime;
56 private final ReportGroup reportGroup;
58 private Set metadataWithProblems;
60 private Map filteredDatabases = new HashMap();
62 private int numNotices;
64 public ReportingDatabase( ReportGroup reportGroup )
66 this( reportGroup, new Reporting() );
69 public ReportingDatabase( ReportGroup reportGroup, Reporting reporting )
71 this( reportGroup, reporting, null );
74 public ReportingDatabase( ReportGroup reportGroup, ArtifactRepository repository )
76 this( reportGroup, new Reporting(), repository );
79 public ReportingDatabase( ReportGroup reportGroup, Reporting reporting, ArtifactRepository repository )
81 this.reportGroup = reportGroup;
83 this.reporting = reporting;
85 this.repository = repository;
92 public void addFailure( Artifact artifact, String processor, String problem, String reason )
94 ArtifactResults results = getArtifactResults( artifact );
95 Result result = createResult( processor, problem, reason );
96 if ( !results.getFailures().contains( result ) )
98 results.addFailure( result );
103 if ( filteredDatabases.containsKey( problem ) )
105 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
107 reportingDatabase.addFailure( artifact, processor, problem, reason );
111 public void addNotice( Artifact artifact, String processor, String problem, String reason )
113 ArtifactResults results = getArtifactResults( artifact );
114 Result result = createResult( processor, problem, reason );
115 if ( !results.getNotices().contains( result ) )
117 results.addNotice( result );
122 if ( filteredDatabases.containsKey( problem ) )
124 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
126 reportingDatabase.addNotice( artifact, processor, problem, reason );
130 public void addWarning( Artifact artifact, String processor, String problem, String reason )
132 ArtifactResults results = getArtifactResults( artifact );
133 Result result = createResult( processor, problem, reason );
134 if ( !results.getWarnings().contains( result ) )
136 results.addWarning( result );
141 if ( filteredDatabases.containsKey( problem ) )
143 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
145 reportingDatabase.addWarning( artifact, processor, problem, reason );
149 ArtifactResults getArtifactResults( Artifact artifact )
151 return getArtifactResults( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
152 artifact.getType(), artifact.getClassifier() );
155 private ArtifactResults getArtifactResults( String groupId, String artifactId, String version, String type,
158 Map artifactMap = this.artifactMap;
160 String key = getArtifactKey( groupId, artifactId, version, type, classifier );
161 ArtifactResults results = (ArtifactResults) artifactMap.get( key );
162 if ( results == null )
164 results = new ArtifactResults();
165 results.setArtifactId( artifactId );
166 results.setClassifier( classifier );
167 results.setGroupId( groupId );
168 results.setType( type );
169 results.setVersion( version );
171 artifactMap.put( key, results );
172 reporting.getArtifacts().add( results );
178 private void initArtifactMap()
180 Map map = new HashMap();
181 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
183 ArtifactResults result = (ArtifactResults) i.next();
185 String key = getArtifactKey( result.getGroupId(), result.getArtifactId(), result.getVersion(),
186 result.getType(), result.getClassifier() );
187 map.put( key, result );
189 numFailures += result.getFailures().size();
190 numWarnings += result.getWarnings().size();
191 numNotices += result.getNotices().size();
196 private static String getArtifactKey( String groupId, String artifactId, String version, String type,
199 return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
202 private static Result createResult( String processor, String problem, String reason )
204 Result result = new Result();
205 result.setProcessor( processor );
206 result.setProblem( problem );
207 result.setReason( reason );
211 public void addFailure( RepositoryMetadata metadata, String processor, String problem, String reason )
213 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
214 if ( !metadataWithProblems.contains( results ) )
216 metadataWithProblems.add( results );
218 Result result = createResult( processor, problem, reason );
219 if ( !results.getFailures().contains( result ) )
221 results.addFailure( result );
226 if ( filteredDatabases.containsKey( problem ) )
228 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
230 reportingDatabase.addFailure( metadata, processor, problem, reason );
234 public void addWarning( RepositoryMetadata metadata, String processor, String problem, String reason )
236 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
237 if ( !metadataWithProblems.contains( results ) )
239 metadataWithProblems.add( results );
241 Result result = createResult( processor, problem, reason );
242 if ( !results.getWarnings().contains( result ) )
244 results.addWarning( result );
249 if ( filteredDatabases.containsKey( problem ) )
251 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
253 reportingDatabase.addWarning( metadata, processor, problem, reason );
257 public void addNotice( RepositoryMetadata metadata, String processor, String problem, String reason )
259 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
260 if ( !metadataWithProblems.contains( results ) )
262 metadataWithProblems.add( results );
264 Result result = createResult( processor, problem, reason );
265 if ( !results.getNotices().contains( result ) )
267 results.addNotice( result );
272 if ( filteredDatabases.containsKey( problem ) )
274 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
276 reportingDatabase.addNotice( metadata, processor, problem, reason );
280 public Set getMetadataWithProblems()
282 return metadataWithProblems;
285 private void initMetadataMap()
287 Map map = new HashMap();
288 Set problems = new LinkedHashSet();
290 for ( Iterator i = reporting.getMetadata().iterator(); i.hasNext(); )
292 MetadataResults result = (MetadataResults) i.next();
294 String key = getMetadataKey( result.getGroupId(), result.getArtifactId(), result.getVersion() );
296 map.put( key, result );
298 numFailures += result.getFailures().size();
299 numWarnings += result.getWarnings().size();
300 numNotices += result.getNotices().size();
302 if ( !result.getFailures().isEmpty() || !result.getWarnings().isEmpty() || !result.getNotices().isEmpty() )
304 problems.add( result );
308 metadataWithProblems = problems;
311 private static String getMetadataKey( String groupId, String artifactId, String version )
313 return groupId + ":" + artifactId + ":" + version;
316 public int getNumFailures()
321 public int getNumWarnings()
326 public Reporting getReporting()
331 public Iterator getArtifactIterator()
333 return reporting.getArtifacts().iterator();
336 public Iterator getMetadataIterator()
338 return reporting.getMetadata().iterator();
341 public boolean isMetadataUpToDate( RepositoryMetadata metadata, long timestamp )
343 String key = getMetadataKey( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() );
344 Map map = metadataMap;
345 MetadataResults results = (MetadataResults) map.get( key );
346 return results != null && results.getLastModified() >= timestamp;
350 * Make sure the metadata record exists, but remove any previous reports in preparation for adding new ones.
352 * @param metadata the metadata
353 * @param lastModified the modification time of the file being tracked
355 public void cleanMetadata( RepositoryMetadata metadata, long lastModified )
357 MetadataResults results = getMetadataResults( metadata, lastModified );
359 results.setLastModified( lastModified );
361 numFailures -= results.getFailures().size();
362 results.getFailures().clear();
364 numWarnings -= results.getWarnings().size();
365 results.getWarnings().clear();
367 numNotices -= results.getWarnings().size();
368 results.getNotices().clear();
370 metadataWithProblems.remove( results );
373 MetadataResults getMetadataResults( RepositoryMetadata metadata, long lastModified )
375 return getMetadataResults( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion(),
379 private MetadataResults getMetadataResults( String groupId, String artifactId, String baseVersion,
382 String key = getMetadataKey( groupId, artifactId, baseVersion );
383 Map metadataMap = this.metadataMap;
384 MetadataResults results = (MetadataResults) metadataMap.get( key );
385 if ( results == null )
387 results = new MetadataResults();
388 results.setArtifactId( artifactId );
389 results.setGroupId( groupId );
390 results.setVersion( baseVersion );
391 results.setLastModified( lastModified );
393 metadataMap.put( key, results );
394 reporting.getMetadata().add( results );
399 public void removeArtifact( Artifact artifact )
401 Map map = artifactMap;
403 String key = getArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
404 artifact.getType(), artifact.getClassifier() );
405 ArtifactResults results = (ArtifactResults) map.get( key );
406 if ( results != null )
408 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
410 if ( results.equals( i.next() ) )
416 numFailures -= results.getFailures().size();
417 numWarnings -= results.getWarnings().size();
418 numNotices -= results.getNotices().size();
424 public ArtifactRepository getRepository()
429 public boolean isInProgress()
434 public void setInProgress( boolean inProgress )
436 this.inProgress = inProgress;
440 startTime = System.currentTimeMillis();
446 // clear the values rather than destroy the instance so that the "inProgress" indicator is in tact.
453 metadataWithProblems.clear();
454 filteredDatabases.clear();
456 reporting.getArtifacts().clear();
457 reporting.getMetadata().clear();
462 public void setStartTime( long startTime )
464 this.startTime = startTime;
467 public long getStartTime()
472 public void updateTimings()
474 long startTime = getStartTime();
475 Date endTime = new Date();
478 getReporting().setExecutionTime( endTime.getTime() - startTime );
480 getReporting().setLastModified( endTime.getTime() );
483 public ReportGroup getReportGroup()
488 public ReportingDatabase getFilteredDatabase( String filter )
490 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( filter );
492 if ( reportingDatabase == null )
494 reportingDatabase = new ReportingDatabase( reportGroup, repository );
496 Reporting reporting = reportingDatabase.getReporting();
497 reporting.setExecutionTime( this.reporting.getExecutionTime() );
498 reporting.setLastModified( this.reporting.getLastModified() );
500 for ( Iterator i = this.reporting.getArtifacts().iterator(); i.hasNext(); )
502 ArtifactResults results = (ArtifactResults) i.next();
503 ArtifactResults targetResults = null;
504 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
506 Result result = (Result) j.next();
508 if ( filter.equals( result.getProcessor() ) )
510 if ( targetResults == null )
512 // lazily create so it is not added unless it has to be
513 targetResults = createArtifactResults( reportingDatabase, results );
516 targetResults.addFailure( result );
517 reportingDatabase.numFailures++;
520 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
522 Result result = (Result) j.next();
524 if ( filter.equals( result.getProcessor() ) )
526 if ( targetResults == null )
528 // lazily create so it is not added unless it has to be
529 targetResults = createArtifactResults( reportingDatabase, results );
532 targetResults.addWarning( result );
533 reportingDatabase.numWarnings++;
536 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
538 Result result = (Result) j.next();
540 if ( filter.equals( result.getProcessor() ) )
542 if ( targetResults == null )
544 // lazily create so it is not added unless it has to be
545 targetResults = createArtifactResults( reportingDatabase, results );
548 targetResults.addNotice( result );
549 reportingDatabase.numNotices++;
553 for ( Iterator i = this.reporting.getMetadata().iterator(); i.hasNext(); )
555 MetadataResults results = (MetadataResults) i.next();
556 MetadataResults targetResults = null;
557 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
559 Result result = (Result) j.next();
561 if ( filter.equals( result.getProcessor() ) )
563 if ( targetResults == null )
565 // lazily create so it is not added unless it has to be
566 targetResults = createMetadataResults( reportingDatabase, results );
569 targetResults.addFailure( result );
570 reportingDatabase.numFailures++;
573 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
575 Result result = (Result) j.next();
577 if ( filter.equals( result.getProcessor() ) )
579 if ( targetResults == null )
581 // lazily create so it is not added unless it has to be
582 targetResults = createMetadataResults( reportingDatabase, results );
585 targetResults.addWarning( result );
586 reportingDatabase.numWarnings++;
589 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
591 Result result = (Result) j.next();
593 if ( filter.equals( result.getProcessor() ) )
595 if ( targetResults == null )
597 // lazily create so it is not added unless it has to be
598 targetResults = createMetadataResults( reportingDatabase, results );
601 targetResults.addNotice( result );
602 reportingDatabase.numNotices++;
607 filteredDatabases.put( filter, reportingDatabase );
610 return reportingDatabase;
613 private static MetadataResults createMetadataResults( ReportingDatabase reportingDatabase, MetadataResults results )
615 MetadataResults targetResults = reportingDatabase.getMetadataResults( results.getGroupId(),
616 results.getArtifactId(),
617 results.getVersion(),
618 results.getLastModified() );
619 reportingDatabase.metadataWithProblems.add( targetResults );
620 return targetResults;
623 private static ArtifactResults createArtifactResults( ReportingDatabase reportingDatabase, ArtifactResults results )
625 return reportingDatabase.getArtifactResults( results.getGroupId(), results.getArtifactId(),
626 results.getVersion(), results.getType(), results.getClassifier() );
629 public int getNumNotices()