source: OAIProvider/trunk/src/main/java/eu/clarin/oai/provider/impl/RepositoryAdapterImpl.java @ 1911

Last change on this file since 1911 was 1911, checked in by oschonef, 12 years ago
  • first round of major refactoring to simply the provider
    • result sets converted to iterator/cursor-mode (better streaming support)
    • delegate serializing of records to Result and ResultList?
    • prepare getting rid of over-engineered and complicated MetadataFormats? classes (not done, yet)

HEADS UP: breaks existing API

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1package eu.clarin.oai.provider.impl;
2
3import java.util.Collections;
4import java.util.Date;
5import java.util.HashSet;
6import java.util.Set;
7
8import eu.clarin.oai.provider.DublinCoreConverter;
9import eu.clarin.oai.provider.MetadataFormat;
10import eu.clarin.oai.provider.OAIException;
11import eu.clarin.oai.provider.Record;
12import eu.clarin.oai.provider.RecordList;
13import eu.clarin.oai.provider.Repository;
14import eu.clarin.oai.provider.SetSpecDesc;
15import eu.clarin.oai.provider.ext.RepositoryAdapter;
16import eu.clarin.oai.provider.ext.ResumptionToken;
17
18final class RepositoryAdapterImpl implements RepositoryAdapter {
19    private final OAIProvider provider;
20    private final Repository repository;
21    private final Set<String> adminEmailAddresses;
22    private final Set<MetadataFormat> metadataFormats =
23        new HashSet<MetadataFormat>();
24    private final Set<SetSpecDesc> setSpecs;
25
26    RepositoryAdapterImpl(OAIProvider provider, Repository repository)
27            throws OAIException {
28        this.provider = provider;
29        this.repository = repository;
30
31        this.adminEmailAddresses = repository.getAdminAddreses();
32//        if (this.adminEmailAddresses == null) {
33//            throw new OAIRepositoryImplementationError(
34//                    " retured null");
35//        }
36//        if (this.adminEmailAddresses.isEmpty()) {
37//            throw new OAIRepositoryImplementationError(
38//                    "getAdminAddreses() returned empty set");
39//        }
40
41        // handle Dublin Core and do some sanity checks
42        Set<DublinCoreConverter> converters =
43            repository.getDublinCoreConverters();
44//        if (converters == null) {
45//            throw new OAIRepositoryImplementationError(
46//                    "getDublinCoreConverters() retruned null");
47//        }
48//        if (converters.isEmpty()) {
49//            throw new OAIRepositoryImplementationError(
50//                    "getDublinCoreConverters() retruned empty set");
51//        }
52        this.metadataFormats.add(new DublinCoreMetadataFormat(converters));
53
54        // handle metadata custom formats and do some sanity checks
55        Set<MetadataFormat> formats = repository.getCustomMetadataFormats();
56        if (formats != null) {
57//            Set<String> prefixes = new HashSet<String>();
58            for (MetadataFormat format : formats) {
59//                String prefix = format.getPrefix();
60//                if (prefix == null) {
61//                    throw new OAIRepositoryImplementationError(
62//                            "metadata format needs prefix non-null " +
63//                            "prefix (MetadataFormat: " + format + ")");
64//                }
65//                if (prefixes.contains(prefix)) {
66//                    throw new OAIRepositoryImplementationError(
67//                            "metadata prefix '" + prefix + "' is not unique" +
68//                            "in repository (MetadataFormat: " + format + ")");
69//                }
70//                if ("oai_dc".equals(prefix)) {
71//                    throw new OAIRepositoryImplementationError(
72//                            "dublin core metadata format must " +
73//                            "not be in the set of supported custom metadata " +
74//                            "formats (MetadataFormat: " + format + ")");
75//                }
76                // add format
77                this.metadataFormats.add(format);
78            }
79        }
80
81        // cache set specs
82        Set<SetSpecDesc> tmp = repository.getSetDescs();
83        if ((tmp != null) && tmp.isEmpty()) {
84            tmp = null;
85        }
86        this.setSpecs = tmp;
87    }
88
89    @Override
90    public OAIProvider getProvider() {
91        return provider;
92    }
93
94    @Override
95    public String getId() {
96        return repository.getId();
97    }
98
99    @Override
100    public String getName() {
101        return repository.getName();
102    }
103
104    @Override
105    public Set<String> getAdminEmailAddresses() {
106        return adminEmailAddresses;
107    }
108
109    @Override
110    public Date getEarliestTimestamp() {
111        Date date = repository.getEarliestTimestamp();
112        if (date == null) {
113            date = new Date();
114        }
115        return date;
116    }
117
118    @Override
119    public Repository.DeletedNotion getDeletedNotion() {
120        return repository.getDeletedNotion();
121    }
122
123    @Override
124    public Repository.Granularity getGranularity() {
125        return repository.getGranularity();
126    }
127
128    @Override
129    public boolean supportsCompressionMethod(int method) {
130        int methods = repository.getCompressionMethods();
131        return (methods & method) > 0;
132    }
133
134    @Override
135    public String getDescription() {
136        String description = repository.getDescription();
137        if ((description != null) && !description.isEmpty()) {
138            return description;
139        }
140        return null;
141    }
142
143    @Override
144    public String getSampleRecordId() {
145        return createRecordId(repository.getSampleRecordLocalId());
146    }
147
148    @Override
149    public Set<MetadataFormat> getMetadataFormats() {
150        return metadataFormats;
151    }
152
153    @Override
154    public Set<MetadataFormat> getMetadataFormats(Record record) {
155        // FIXME: re-work
156//        final Class<?> clazz = record.getItem().getClass();
157        Set<MetadataFormat> result = null;
158//        for (MetadataFormat format : metadataFormats) {
159//            if (format.canWriteClass(clazz)) {
160//                if (result == null) {
161//                    result = new HashSet<MetadataFormat>();
162//                }
163//                result.add(format);
164//            }
165//        }
166        if ((result == null) || result.isEmpty()) {
167            result = Collections.emptySet();
168        }
169        return result;
170    }
171
172    @Override
173    public MetadataFormat getMetadataFormatByPrefix(String prefix) {
174        for (MetadataFormat format : metadataFormats) {
175            if (prefix.equals(format.getPrefix())) {
176                return format;
177            }
178        }
179        return null;
180    }
181
182    @Override
183    public Set<SetSpecDesc> getSetSpecs() {
184        return setSpecs;
185    }
186
187    @Override
188    public boolean isUsingSets() {
189        return setSpecs != null;
190    }
191
192    @Override
193    public String createRecordId(Object localId) {
194        StringBuilder sb = new StringBuilder("oai:");
195        sb.append(repository.getId());
196        sb.append(":");
197        sb.append(repository.unparseLocalId(localId));
198        return sb.toString();
199    }
200
201    @Override
202    public Object parseLocalId(String unparsedLocalId) {
203        return repository.parseLocalId(unparsedLocalId);
204    }
205
206    @Override
207    public Record getRecord(Object localId) throws OAIException {
208        try {
209            return repository.getRecord(localId);
210        } catch (OAIException e) {
211            throw e;
212        } catch (Exception e) {
213            throw new OAIException("error getting record", e);
214        }
215    }
216
217    @Override
218    public Record getRecordHeader(Object localId) throws OAIException {
219        try {
220            return repository.getRecordHeader(localId);
221        } catch (OAIException e) {
222            throw e;
223        } catch (Exception e) {
224            throw new OAIException("error getting record", e);
225        }
226    }
227
228    @Override
229    public RecordList getRecords(String prefix, Date from, Date until,
230            String set, int offset) throws OAIException {
231        /*
232         * XXX: maybe add a OAIProvider defined upper limit?
233         */
234        int limit = repository.getPreferredResultListSize();
235        if (limit < 1) {
236            limit = Integer.MAX_VALUE;
237        }
238        try {
239            return repository.getRecords(prefix, from, until, set, offset, limit);
240        } catch (OAIException e) {
241            throw e;
242        } catch (Exception e) {
243            throw new OAIException("error getting records", e);
244        }
245    }
246
247    @Override
248    public RecordList getRecordHeaders(String prefix, Date from, Date until,
249            String set, int offset) throws OAIException {
250        /*
251         * XXX: maybe add a OAIProvider defined upper limit?
252         */
253        int limit = repository.getPreferredResultListSize();
254        if (limit < 1) {
255            limit = Integer.MAX_VALUE;
256        }
257        try {
258            return repository.getRecordHeaders(prefix, from, until, set, offset, limit);
259        } catch (OAIException e) {
260            throw e;
261        } catch (Exception e) {
262            throw new OAIException("error getting records", e);
263        }
264    }
265
266    @Override
267    public ResumptionToken createResumptionToken() {
268        return provider.createResumptionToken(-1);
269    }
270
271    @Override
272    public ResumptionToken getResumptionToken(Long id) {
273        return provider.getResumptionToken(id, -1);
274    }
275
276} // class RepositoryAdapterImpl
Note: See TracBrowser for help on using the repository browser.