KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > repository > CompoundRepository


1 /*
2  * Created on Dec 14, 2004
3  */

4 package org.openedit.repository;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import com.openedit.util.PathUtilities;
13
14 /**
15  * @author Matthew Avery, mavery@einnovation.com
16  *
17  * This Repository implementation delegates to other repositories based
18  * on the requested path. Be sure to set a default Repository.
19  */

20 public class CompoundRepository implements Repository
21 {
22     protected Map JavaDoc fieldRepositoryMap;
23     protected Repository fieldDefaultRepository;
24     
25     public void addRepository( String JavaDoc inBasePath, Repository inRepository )
26     {
27         getRepositoryMap().put( inBasePath, inRepository );
28     }
29     
30     public void removeRepository( String JavaDoc inBasePath )
31     {
32         getRepositoryMap().remove( inBasePath );
33     }
34     public Map JavaDoc getRepositoryMap()
35     {
36         if (fieldRepositoryMap == null)
37         {
38             fieldRepositoryMap = new HashMap JavaDoc();
39         }
40         return fieldRepositoryMap;
41     }
42     
43     /**
44      * <p>
45      * Return the Repository that should be used for the specified path.
46      * </p>
47      *
48      * @param inPath
49      * @return
50      */

51     public Repository resolveRepository(String JavaDoc inPath)
52     {
53         Set JavaDoc keys = getRepositoryMap().keySet(); //replace with a list to keep ordering straight
54
for ( Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); )
55         {
56             String JavaDoc key = (String JavaDoc) iter.next();
57             if ( PathUtilities.match(inPath, key) )
58             {
59                 return (Repository) getRepositoryMap().get( key );
60             }
61         }
62         return getDefaultRepository();
63     }
64     
65     public ContentItem get( String JavaDoc inPath ) throws RepositoryException
66     {
67         return resolveRepository( inPath ).get( inPath );
68     }
69     public ContentItem getStub( String JavaDoc inPath ) throws RepositoryException
70     {
71         return resolveRepository( inPath ).getStub( inPath );
72     }
73
74     public void put( ContentItem inContent ) throws RepositoryException
75     {
76         resolveRepository( inContent.getPath() ).put( inContent );
77     }
78
79     public void copy( ContentItem inSource, ContentItem inDestination ) throws RepositoryException
80     {
81         Repository rep = resolveRepository( inDestination.getPath() );
82         rep.copy( inSource, inDestination );
83     }
84
85     public void move( ContentItem inSource, ContentItem inDestination ) throws RepositoryException
86     {
87         resolveRepository( inDestination.getPath() ).move( inSource, inDestination );
88         
89     }
90
91     public void remove( ContentItem inRevision ) throws RepositoryException
92     {
93         resolveRepository( inRevision.getPath() ).remove( inRevision );
94     }
95
96     public List JavaDoc getVersions( String JavaDoc inPath ) throws RepositoryException
97     {
98         return resolveRepository( inPath ).getVersions( inPath );
99     }
100     
101     public Repository getDefaultRepository()
102     {
103         return fieldDefaultRepository;
104     }
105     public void setDefaultRepository( Repository defaultRepository )
106     {
107         fieldDefaultRepository = defaultRepository;
108     }
109     public void setRepositoryMap( Map JavaDoc repositoryMap )
110     {
111         fieldRepositoryMap = repositoryMap;
112     }
113
114     public boolean doesExist(String JavaDoc inPath) throws RepositoryException
115     {
116         return resolveRepository( inPath ).doesExist(inPath);
117     }
118
119     public ContentItem getLastVersion(String JavaDoc inPath) throws RepositoryException
120     {
121         return resolveRepository( inPath ).getLastVersion(inPath );
122     }
123
124     public List JavaDoc getChildrenNames(String JavaDoc inParent) throws RepositoryException
125     {
126         return resolveRepository( inParent ).getChildrenNames(inParent );
127     }
128
129     public void deleteOldVersions(String JavaDoc inPath) throws RepositoryException
130     {
131         resolveRepository( inPath ).deleteOldVersions(inPath);
132     }
133 }
134
Popular Tags