001    package org.apache.archiva.rss.processor;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import com.sun.syndication.feed.synd.SyndFeed;
023    import com.sun.syndication.io.FeedException;
024    import org.apache.archiva.metadata.model.ArtifactMetadata;
025    import org.apache.archiva.metadata.repository.MetadataRepository;
026    import org.apache.archiva.metadata.repository.MetadataRepositoryException;
027    import org.apache.archiva.metadata.repository.MetadataResolutionException;
028    import org.apache.archiva.rss.RssFeedEntry;
029    import org.apache.archiva.rss.RssFeedGenerator;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    import org.springframework.stereotype.Service;
033    
034    import javax.inject.Inject;
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.Date;
038    import java.util.List;
039    import java.util.Map;
040    
041    /**
042     * Retrieve and process new versions of an artifact from the database and
043     * generate a rss feed. The versions will be grouped by the date when the artifact
044     * was gathered. Each group will appear as one entry in the feed.
045     *
046     */
047    @Service("rssFeedProcessor#new-versions")
048    public class NewVersionsOfArtifactRssFeedProcessor
049        extends AbstractArtifactsRssFeedProcessor
050    {
051        private Logger log = LoggerFactory.getLogger( NewVersionsOfArtifactRssFeedProcessor.class );
052    
053        private static final String title = "New Versions of Artifact ";
054    
055        private static final String desc = "These are the new versions of artifact ";
056    
057        /**
058         *
059         */
060        @Inject
061        private RssFeedGenerator generator;
062    
063        /**
064         * Process all versions of the artifact which had a rss feed request.
065         */
066        public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
067            throws FeedException
068        {
069            String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
070            String artifactId = reqParams.get( RssFeedProcessor.KEY_ARTIFACT_ID );
071    
072            if ( groupId != null && artifactId != null )
073            {
074                return processNewVersionsOfArtifact( groupId, artifactId, metadataRepository );
075            }
076    
077            return null;
078        }
079    
080        private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId,
081                                                       MetadataRepository metadataRepository )
082            throws FeedException
083        {
084            List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
085            try
086            {
087                for ( String repoId : metadataRepository.getRepositories() )
088                {
089                    Collection<String> versions = metadataRepository.getProjectVersions( repoId, groupId, artifactId );
090                    for ( String version : versions )
091                    {
092                        artifacts.addAll( metadataRepository.getArtifacts( repoId, groupId, artifactId, version ) );
093                    }
094                }
095            }
096            catch ( MetadataRepositoryException e )
097            {
098                throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
099                                         e );
100            }
101            catch ( MetadataResolutionException e )
102            {
103                throw new FeedException( "Unable to construct feed, metadata could not be retrieved: " + e.getMessage(),
104                                         e );
105            }
106    
107            long tmp = 0;
108            RssFeedEntry entry = null;
109            List<RssFeedEntry> entries = new ArrayList<RssFeedEntry>();
110            String description = "";
111            int idx = 0;
112            for ( ArtifactMetadata artifact : artifacts )
113            {
114                long whenGathered = artifact.getWhenGathered().getTime();
115    
116                if ( tmp != whenGathered )
117                {
118                    if ( entry != null )
119                    {
120                        entry.setDescription( description );
121                        entries.add( entry );
122                        entry = null;
123                    }
124    
125                    entry = new RssFeedEntry(
126                        this.getTitle() + "\'" + groupId + ":" + artifactId + "\'" + " as of " + new Date( whenGathered ) );
127                    entry.setPublishedDate( artifact.getWhenGathered() );
128                    description =
129                        this.getDescription() + "\'" + groupId + ":" + artifactId + "\'" + ": \n" + artifact.getId() +
130                            " | ";
131                }
132                else
133                {
134                    description = description + artifact.getId() + " | ";
135                }
136    
137                if ( idx == ( artifacts.size() - 1 ) )
138                {
139                    entry.setDescription( description );
140                    entries.add( entry );
141                }
142    
143                tmp = whenGathered;
144                idx++;
145            }
146    
147            String key = groupId + ":" + artifactId;
148    
149            return generator.generateFeed( getTitle() + "\'" + key + "\'",
150                                           "New versions of artifact " + "\'" + key + "\' found during repository scan.",
151                                           entries );
152        }
153    
154        public String getTitle()
155        {
156            return title;
157        }
158    
159        public String getDescription()
160        {
161            return desc;
162        }
163    
164        public RssFeedGenerator getGenerator()
165        {
166            return generator;
167        }
168    
169        public void setGenerator( RssFeedGenerator generator )
170        {
171            this.generator = generator;
172        }
173    }