001    package org.apache.archiva.model;
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 org.apache.commons.lang.StringUtils;
023    import org.apache.archiva.common.utils.VersionUtil;
024    
025    /**
026     * ArchivaArtifact - Mutable artifact object.
027     *
028     *
029     */
030    public class ArchivaArtifact
031    {
032        private ArchivaArtifactModel model;
033    
034        private ArchivaArtifactPlatformDetails platformDetails;
035    
036        private String baseVersion;
037    
038        private static final int PRIME = 31;
039    
040        public ArchivaArtifact( String groupId, String artifactId, String version,
041                                String classifier, String type, String repositoryId )
042        {
043            if ( empty( groupId ) )
044            {
045                throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty groupId ["
046                    + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
047            }
048    
049            if ( empty( artifactId ) )
050            {
051                throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty artifactId ["
052                    + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
053            }
054    
055            if ( empty( version ) )
056            {
057                throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty version ["
058                    + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
059            }
060    
061            if ( empty( type ) )
062            {
063                throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty type ["
064                    + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
065            }
066    
067            if ( empty( repositoryId ) )
068            {
069                throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty repositoryId ["
070                    + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
071            }
072    
073            model = new ArchivaArtifactModel();
074    
075            model.setGroupId( groupId );
076            model.setArtifactId( artifactId );
077            model.setVersion( version );
078            model.setClassifier( StringUtils.defaultString( classifier ) );
079            model.setType( type );
080            model.setSnapshot( VersionUtil.isSnapshot( version ) );
081            model.setRepositoryId(repositoryId);
082            
083            this.baseVersion = VersionUtil.getBaseVersion( version );
084        }
085    
086        public ArchivaArtifact( ArchivaArtifactModel artifactModel )
087        {
088            this.model = artifactModel;
089            model.setSnapshot( VersionUtil.isSnapshot( model.getVersion() ) );
090            this.baseVersion = VersionUtil.getBaseVersion( model.getVersion() );
091        }
092        
093        public ArchivaArtifact( ArtifactReference ref, String repositoryId )
094        {
095            this( ref.getGroupId(), ref.getArtifactId(), ref.getVersion(), ref.getClassifier(), ref.getType(), repositoryId );
096        }
097    
098        public ArchivaArtifactModel getModel()
099        {
100            return model;
101        }
102    
103        public String getGroupId()
104        {
105            return model.getGroupId();
106        }
107    
108        public String getArtifactId()
109        {
110            return model.getArtifactId();
111        }
112    
113        public String getVersion()
114        {
115            return model.getVersion();
116        }
117    
118        public String getBaseVersion()
119        {
120            return baseVersion;
121        }
122    
123        public boolean isSnapshot()
124        {
125            return model.isSnapshot();
126        }
127    
128        public String getClassifier()
129        {
130            return model.getClassifier();
131        }
132    
133        public String getType()
134        {
135            return model.getType();
136        }
137    
138        public boolean hasClassifier()
139        {
140            return StringUtils.isNotEmpty( model.getClassifier() );
141        }
142    
143        public String getRepositoryId()
144        {
145            return model.getRepositoryId();
146        }
147    
148        @Override
149        public int hashCode()
150        {
151    
152            int result = 1;
153            if ( model != null )
154            {
155                result = PRIME * result + ( ( model.getGroupId() == null ) ? 0 : model.getGroupId().hashCode() );
156                result = PRIME * result + ( ( model.getArtifactId() == null ) ? 0 : model.getArtifactId().hashCode() );
157                result = PRIME * result + ( ( model.getVersion() == null ) ? 0 : model.getVersion().hashCode() );
158                result = PRIME * result + ( ( model.getClassifier() == null ) ? 0 : model.getClassifier().hashCode() );
159                result = PRIME * result + ( ( model.getType() == null ) ? 0 : model.getType().hashCode() );
160            }
161            return result;
162        }
163    
164        @Override
165        public boolean equals( Object obj )
166        {
167            if ( this == obj )
168            {
169                return true;
170            }
171    
172            if ( obj == null )
173            {
174                return false;
175            }
176    
177            if ( getClass() != obj.getClass() )
178            {
179                return false;
180            }
181    
182            final ArchivaArtifact other = (ArchivaArtifact) obj;
183    
184            if ( model == null )
185            {
186                if ( other.model != null )
187                {
188                    return false;
189                }
190            }
191            if ( !model.equals( other.model ) )
192            {
193                return false;
194            }
195    
196            return true;
197        }
198    
199        @Override
200        public String toString()
201        {
202            StringBuilder sb = new StringBuilder();
203            if ( model.getGroupId() != null )
204            {
205                sb.append( model.getGroupId() );
206                sb.append( ":" );
207            }
208            appendArtifactTypeClassifierString( sb );
209            sb.append( ":" );
210            if ( model.getVersion() != null )
211            {
212                sb.append( model.getVersion() );
213            }
214    
215            return sb.toString();
216        }
217    
218        private void appendArtifactTypeClassifierString( StringBuilder sb )
219        {
220            sb.append( model.getArtifactId() );
221            sb.append( ":" );
222            sb.append( getType() );
223            if ( hasClassifier() )
224            {
225                sb.append( ":" );
226                sb.append( getClassifier() );
227            }
228        }
229    
230        private boolean empty( String value )
231        {
232            return ( value == null ) || ( value.trim().length() < 1 );
233        }
234    
235        public ArchivaArtifactPlatformDetails getPlatformDetails()
236        {
237            return platformDetails;
238        }
239    
240        public void setPlatformDetails( ArchivaArtifactPlatformDetails platformDetails )
241        {
242            this.platformDetails = platformDetails;
243        }
244    }