1 package org.apache.maven.archiva.database.jdo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41 public class JdoArtifactDAO
42 implements ArtifactDAO
43 {
44
45
46
47 private JdoAccess jdo;
48
49
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 }