source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/pid/EPICPersistentIdentifierProvider.java @ 5599

Last change on this file since 5599 was 5599, checked in by Twan Goosen, 10 years ago

assume no trailing slash in VCR base URI

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1package eu.clarin.cmdi.virtualcollectionregistry.pid;
2
3import de.uni_leipzig.asv.clarin.webservices.pidservices2.Configuration;
4import de.uni_leipzig.asv.clarin.webservices.pidservices2.HandleField;
5import de.uni_leipzig.asv.clarin.webservices.pidservices2.interfaces.PidWriter;
6import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException;
7import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection;
8import java.net.URI;
9import java.util.EnumMap;
10import java.util.Map;
11import org.apache.commons.httpclient.HttpException;
12import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
14import org.springframework.beans.factory.annotation.Autowired;
15import org.springframework.beans.factory.annotation.Value;
16import org.springframework.context.annotation.Profile;
17import org.springframework.stereotype.Service;
18
19/**
20 * PID provider that uses the EPIC API v2 to communicate with a handle provider.
21 * Wraps around the PID Resolver / PID Writer library by Thomas Eckart (Leipzig
22 * University)
23 *
24 * @author twagoo
25 * @see ​http://www.pidconsortium.eu/
26 * @see http://epic.gwdg.de/wiki/index.php/EPIC:API
27 * @see ​https://github.com/CatchPlus/EPIC-API-v2/
28 */
29@Service
30@Profile("vcr.pid.epic")
31public class EPICPersistentIdentifierProvider implements PersistentIdentifierProvider {
32
33    private static final Logger logger = LoggerFactory.getLogger(EPICPersistentIdentifierProvider.class);
34    private final PidWriter pidWriter;
35    private final Configuration configuration;
36
37    @Value("${eu.clarin.cmdi.virtualcollectionregistry.base_uri}")
38    private String baseUri;
39
40    /**
41     *
42     * @param pidWriter PID writer implementation to use
43     * @param configuration configuration to be passed to PID writer methods
44     */
45    @Autowired
46    public EPICPersistentIdentifierProvider(PidWriter pidWriter, Configuration configuration) {
47        this.pidWriter = pidWriter;
48        this.configuration = configuration;
49    }
50
51    @Override
52    public PersistentIdentifier createIdentifier(VirtualCollection vc) throws VirtualCollectionRegistryException {
53        logger.debug("creating handle for virtual collection \"{}\"", vc.getId());
54        final Map<HandleField, String> fieldMap = createPIDFieldMap(vc);
55        try {
56            final String requestedPid = String.format("VCR-%d", vc.getId());
57            final String pid = pidWriter.registerNewPID(configuration, fieldMap, requestedPid);
58            return new PersistentIdentifier(vc, PersistentIdentifier.Type.HANDLE, pid);
59        } catch (HttpException ex) {
60            throw new VirtualCollectionRegistryException("Could not create EPIC identifier", ex);
61        }
62    }
63
64    private Map<HandleField, String> createPIDFieldMap(VirtualCollection vc) {
65        final Map<HandleField, String> pidMap = new EnumMap<>(HandleField.class);
66        pidMap.put(HandleField.URL, makeCollectionURI(vc));
67        pidMap.put(HandleField.TITLE, vc.getName());
68        if (!vc.getCreators().isEmpty()) {
69            pidMap.put(HandleField.CREATOR, vc.getCreators().get(0).getPerson());
70        }
71        return pidMap;
72    }
73
74    @Override
75    public void updateIdentifier(String pid, URI target) throws VirtualCollectionRegistryException {
76        throw new UnsupportedOperationException("Not supported yet.");
77    }
78
79    @Override
80    public void deleteIdentifier(String pid) throws VirtualCollectionRegistryException {
81        throw new UnsupportedOperationException("Not supported yet.");
82    }
83
84    private String makeCollectionURI(VirtualCollection vc) {
85        return String.format("%s/service/virtualcollections/%d", baseUri, vc.getId());
86    }
87
88    protected void setBaseUri(String baseUri) {
89        this.baseUri = baseUri;
90    }
91
92}
Note: See TracBrowser for help on using the repository browser.