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 results.addFailure( createResult( processor, problem, reason ) );
99 if ( filteredDatabases.containsKey( problem ) )
101 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
103 reportingDatabase.addFailure( artifact, processor, problem, reason );
107 public void addNotice( Artifact artifact, String processor, String problem, String reason )
109 ArtifactResults results = getArtifactResults( artifact );
110 results.addNotice( createResult( processor, problem, reason ) );
114 if ( filteredDatabases.containsKey( problem ) )
116 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
118 reportingDatabase.addNotice( artifact, processor, problem, reason );
122 public void addWarning( Artifact artifact, String processor, String problem, String reason )
124 ArtifactResults results = getArtifactResults( artifact );
125 results.addWarning( createResult( processor, problem, reason ) );
129 if ( filteredDatabases.containsKey( problem ) )
131 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
133 reportingDatabase.addWarning( artifact, processor, problem, reason );
137 private ArtifactResults getArtifactResults( Artifact artifact )
139 return getArtifactResults( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
140 artifact.getType(), artifact.getClassifier() );
143 private ArtifactResults getArtifactResults( String groupId, String artifactId, String version, String type,
146 Map artifactMap = this.artifactMap;
148 String key = getArtifactKey( groupId, artifactId, version, type, classifier );
149 ArtifactResults results = (ArtifactResults) artifactMap.get( key );
150 if ( results == null )
152 results = new ArtifactResults();
153 results.setArtifactId( artifactId );
154 results.setClassifier( classifier );
155 results.setGroupId( groupId );
156 results.setType( type );
157 results.setVersion( version );
159 artifactMap.put( key, results );
160 reporting.getArtifacts().add( results );
166 private void initArtifactMap()
168 Map map = new HashMap();
169 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
171 ArtifactResults result = (ArtifactResults) i.next();
173 String key = getArtifactKey( result.getGroupId(), result.getArtifactId(), result.getVersion(),
174 result.getType(), result.getClassifier() );
175 map.put( key, result );
177 numFailures += result.getFailures().size();
178 numWarnings += result.getWarnings().size();
179 numNotices += result.getNotices().size();
184 private static String getArtifactKey( String groupId, String artifactId, String version, String type,
187 return groupId + ":" + artifactId + ":" + version + ":" + type + ":" + classifier;
190 private static Result createResult( String processor, String problem, String reason )
192 Result result = new Result();
193 result.setProcessor( processor );
194 result.setProblem( problem );
195 result.setReason( reason );
199 public void addFailure( RepositoryMetadata metadata, String processor, String problem, String reason )
201 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
202 if ( !metadataWithProblems.contains( results ) )
204 metadataWithProblems.add( results );
206 results.addFailure( createResult( processor, problem, reason ) );
210 if ( filteredDatabases.containsKey( problem ) )
212 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
214 reportingDatabase.addFailure( metadata, processor, problem, reason );
218 public void addWarning( RepositoryMetadata metadata, String processor, String problem, String reason )
220 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
221 if ( !metadataWithProblems.contains( results ) )
223 metadataWithProblems.add( results );
225 results.addWarning( createResult( processor, problem, reason ) );
229 if ( filteredDatabases.containsKey( problem ) )
231 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
233 reportingDatabase.addWarning( metadata, processor, problem, reason );
237 public void addNotice( RepositoryMetadata metadata, String processor, String problem, String reason )
239 MetadataResults results = getMetadataResults( metadata, System.currentTimeMillis() );
240 if ( !metadataWithProblems.contains( results ) )
242 metadataWithProblems.add( results );
244 results.addNotice( createResult( processor, problem, reason ) );
248 if ( filteredDatabases.containsKey( problem ) )
250 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( problem );
252 reportingDatabase.addNotice( metadata, processor, problem, reason );
256 public Set getMetadataWithProblems()
258 return metadataWithProblems;
261 private void initMetadataMap()
263 Map map = new HashMap();
264 Set problems = new LinkedHashSet();
266 for ( Iterator i = reporting.getMetadata().iterator(); i.hasNext(); )
268 MetadataResults result = (MetadataResults) i.next();
270 String key = getMetadataKey( result.getGroupId(), result.getArtifactId(), result.getVersion() );
272 map.put( key, result );
274 numFailures += result.getFailures().size();
275 numWarnings += result.getWarnings().size();
276 numNotices += result.getNotices().size();
278 if ( !result.getFailures().isEmpty() || !result.getWarnings().isEmpty() || !result.getNotices().isEmpty() )
280 problems.add( result );
284 metadataWithProblems = problems;
287 private static String getMetadataKey( String groupId, String artifactId, String version )
289 return groupId + ":" + artifactId + ":" + version;
292 public int getNumFailures()
297 public int getNumWarnings()
302 public Reporting getReporting()
307 public Iterator getArtifactIterator()
309 return reporting.getArtifacts().iterator();
312 public Iterator getMetadataIterator()
314 return reporting.getMetadata().iterator();
317 public boolean isMetadataUpToDate( RepositoryMetadata metadata, long timestamp )
319 String key = getMetadataKey( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() );
320 Map map = metadataMap;
321 MetadataResults results = (MetadataResults) map.get( key );
322 return results != null && results.getLastModified() >= timestamp;
326 * Make sure the metadata record exists, but remove any previous reports in preparation for adding new ones.
328 * @param metadata the metadata
329 * @param lastModified the modification time of the file being tracked
331 public void cleanMetadata( RepositoryMetadata metadata, long lastModified )
333 MetadataResults results = getMetadataResults( metadata, lastModified );
335 results.setLastModified( lastModified );
337 numFailures -= results.getFailures().size();
338 results.getFailures().clear();
340 numWarnings -= results.getWarnings().size();
341 results.getWarnings().clear();
343 numNotices -= results.getWarnings().size();
344 results.getNotices().clear();
346 metadataWithProblems.remove( results );
349 private MetadataResults getMetadataResults( RepositoryMetadata metadata, long lastModified )
351 return getMetadataResults( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion(),
355 private MetadataResults getMetadataResults( String groupId, String artifactId, String baseVersion,
358 String key = getMetadataKey( groupId, artifactId, baseVersion );
359 Map metadataMap = this.metadataMap;
360 MetadataResults results = (MetadataResults) metadataMap.get( key );
361 if ( results == null )
363 results = new MetadataResults();
364 results.setArtifactId( artifactId );
365 results.setGroupId( groupId );
366 results.setVersion( baseVersion );
367 results.setLastModified( lastModified );
369 metadataMap.put( key, results );
370 reporting.getMetadata().add( results );
375 public void removeArtifact( Artifact artifact )
377 Map map = artifactMap;
379 String key = getArtifactKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
380 artifact.getType(), artifact.getClassifier() );
381 ArtifactResults results = (ArtifactResults) map.get( key );
382 if ( results != null )
384 for ( Iterator i = reporting.getArtifacts().iterator(); i.hasNext(); )
386 if ( results.equals( i.next() ) )
392 numFailures -= results.getFailures().size();
393 numWarnings -= results.getWarnings().size();
394 numNotices -= results.getNotices().size();
400 public ArtifactRepository getRepository()
405 public boolean isInProgress()
410 public void setInProgress( boolean inProgress )
412 this.inProgress = inProgress;
416 startTime = System.currentTimeMillis();
422 // clear the values rather than destroy the instance so that the "inProgress" indicator is in tact.
429 metadataWithProblems.clear();
430 filteredDatabases.clear();
432 reporting.getArtifacts().clear();
433 reporting.getMetadata().clear();
438 public void setStartTime( long startTime )
440 this.startTime = startTime;
443 public long getStartTime()
448 public void updateTimings()
450 long startTime = getStartTime();
451 Date endTime = new Date();
454 getReporting().setExecutionTime( endTime.getTime() - startTime );
456 getReporting().setLastModified( endTime.getTime() );
459 public ReportGroup getReportGroup()
464 public ReportingDatabase getFilteredDatabase( String filter )
466 ReportingDatabase reportingDatabase = (ReportingDatabase) filteredDatabases.get( filter );
468 if ( reportingDatabase == null )
470 reportingDatabase = new ReportingDatabase( reportGroup, repository );
472 Reporting reporting = reportingDatabase.getReporting();
473 reporting.setExecutionTime( this.reporting.getExecutionTime() );
474 reporting.setLastModified( this.reporting.getLastModified() );
476 for ( Iterator i = this.reporting.getArtifacts().iterator(); i.hasNext(); )
478 ArtifactResults results = (ArtifactResults) i.next();
479 ArtifactResults targetResults = null;
480 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
482 Result result = (Result) j.next();
484 if ( filter.equals( result.getProcessor() ) )
486 if ( targetResults == null )
488 // lazily create so it is not added unless it has to be
489 targetResults = createArtifactResults( reportingDatabase, results );
492 targetResults.addFailure( result );
493 reportingDatabase.numFailures++;
496 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
498 Result result = (Result) j.next();
500 if ( filter.equals( result.getProcessor() ) )
502 if ( targetResults == null )
504 // lazily create so it is not added unless it has to be
505 targetResults = createArtifactResults( reportingDatabase, results );
508 targetResults.addWarning( result );
509 reportingDatabase.numWarnings++;
512 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
514 Result result = (Result) j.next();
516 if ( filter.equals( result.getProcessor() ) )
518 if ( targetResults == null )
520 // lazily create so it is not added unless it has to be
521 targetResults = createArtifactResults( reportingDatabase, results );
524 targetResults.addNotice( result );
525 reportingDatabase.numNotices++;
529 for ( Iterator i = this.reporting.getMetadata().iterator(); i.hasNext(); )
531 MetadataResults results = (MetadataResults) i.next();
532 MetadataResults targetResults = null;
533 for ( Iterator j = results.getFailures().iterator(); j.hasNext(); )
535 Result result = (Result) j.next();
537 if ( filter.equals( result.getProcessor() ) )
539 if ( targetResults == null )
541 // lazily create so it is not added unless it has to be
542 targetResults = createMetadataResults( reportingDatabase, results );
545 targetResults.addFailure( result );
546 reportingDatabase.numFailures++;
549 for ( Iterator j = results.getWarnings().iterator(); j.hasNext(); )
551 Result result = (Result) j.next();
553 if ( filter.equals( result.getProcessor() ) )
555 if ( targetResults == null )
557 // lazily create so it is not added unless it has to be
558 targetResults = createMetadataResults( reportingDatabase, results );
561 targetResults.addWarning( result );
562 reportingDatabase.numWarnings++;
565 for ( Iterator j = results.getNotices().iterator(); j.hasNext(); )
567 Result result = (Result) j.next();
569 if ( filter.equals( result.getProcessor() ) )
571 if ( targetResults == null )
573 // lazily create so it is not added unless it has to be
574 targetResults = createMetadataResults( reportingDatabase, results );
577 targetResults.addNotice( result );
578 reportingDatabase.numNotices++;
583 filteredDatabases.put( filter, reportingDatabase );
586 return reportingDatabase;
589 private static MetadataResults createMetadataResults( ReportingDatabase reportingDatabase, MetadataResults results )
591 MetadataResults targetResults = reportingDatabase.getMetadataResults( results.getGroupId(),
592 results.getArtifactId(),
593 results.getVersion(),
594 results.getLastModified() );
595 reportingDatabase.metadataWithProblems.add( targetResults );
596 return targetResults;
599 private static ArtifactResults createArtifactResults( ReportingDatabase reportingDatabase, ArtifactResults results )
601 return reportingDatabase.getArtifactResults( results.getGroupId(), results.getArtifactId(),
602 results.getVersion(), results.getType(), results.getClassifier() );
605 public int getNumNotices()