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

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