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

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

Removed id padding from VCR handle
Refs #581,#596

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