View Javadoc

1   package org.apache.maven.archiva.database.jdo;
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 org.apache.maven.archiva.database.ArchivaDatabaseException;
23  import org.apache.maven.archiva.database.ArtifactDAO;
24  import org.apache.maven.archiva.database.Constraint;
25  import org.apache.maven.archiva.database.DeclarativeConstraint;
26  import org.apache.maven.archiva.database.ObjectNotFoundException;
27  import org.apache.maven.archiva.model.ArchivaArtifact;
28  import org.apache.maven.archiva.model.ArchivaArtifactModel;
29  import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * JdoArtifactDAO 
36   *
37   * @version $Id: JdoArtifactDAO.java 1043850 2010-12-09 07:58:00Z brett $
38   * 
39   * @plexus.component role-hint="jdo"
40   */
41  public class JdoArtifactDAO
42      implements ArtifactDAO
43  {
44      /**
45       * @plexus.requirement role-hint="archiva"
46       */
47      private JdoAccess jdo;
48  
49      /* .\ Archiva Artifact \. _____________________________________________________________ */
50  
51      public ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String classifier,
52                                             String type, String repositoryId )
53      {
54          ArchivaArtifact artifact;
55  
56          try
57          {
58              artifact = getArtifact( groupId, artifactId, version, classifier, type, repositoryId );
59          }
60          catch ( ArchivaDatabaseException e )
61          {
62              artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type, repositoryId );
63          }
64  
65          return artifact;
66      }
67  
68      public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier,
69                                          String type, String repositoryId )
70          throws ObjectNotFoundException, ArchivaDatabaseException
71      {
72          ArchivaArtifactModelKey key = new ArchivaArtifactModelKey();
73          key.setGroupId( groupId );
74          key.setArtifactId( artifactId );
75          key.setVersion( version );
76          key.setClassifier( classifier );
77          key.setType( type );
78          key.setRepositoryId( repositoryId );
79  
80          ArchivaArtifactModel model = (ArchivaArtifactModel) jdo.getObjectById( ArchivaArtifactModel.class, key, null );
81  
82          return new ArchivaArtifact( model );
83      }
84  
85      @SuppressWarnings("unchecked")
86      public List<ArchivaArtifact> queryArtifacts( Constraint constraint )
87          throws ObjectNotFoundException, ArchivaDatabaseException
88      {
89          List<ArchivaArtifactModel> results = (List<ArchivaArtifactModel>) jdo.queryObjects( ArchivaArtifactModel.class, constraint );
90  
91          List<ArchivaArtifact> ret = null;
92          if ( results != null )
93          {
94              ret = new ArrayList<ArchivaArtifact>();
95              for ( ArchivaArtifactModel model : results )
96              {
97                  ret.add( new ArchivaArtifact( model ) );
98              }
99          }
100 
101         return ret;
102     }
103 
104     public ArchivaArtifact saveArtifact( ArchivaArtifact artifact )
105         throws ArchivaDatabaseException
106     {
107         ArchivaArtifactModel model = (ArchivaArtifactModel) jdo.saveObject( artifact.getModel() );
108         if ( model == null )
109         {
110             return null;
111         }
112 
113         return new ArchivaArtifact( model );
114     }
115 
116     public void deleteArtifact( ArchivaArtifact artifact )
117         throws ArchivaDatabaseException
118     {
119         jdo.removeObject( artifact.getModel() );
120     }
121 
122     public long countArtifacts( DeclarativeConstraint constraint )
123     {
124         return jdo.countObjects( ArchivaArtifactModel.class, constraint );
125     }
126 }