001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.api.services.functions;
007
008import org.springframework.stereotype.Component;
009
010import static java.util.UUID.randomUUID;
011import javax.inject.Inject;
012import java.util.StringJoiner;
013import java.util.stream.IntStream;
014import org.fcrepo.config.FedoraPropsConfig;
015
016/**
017 * Unique value minter that creates hierarchical IDs from a UUID
018 *
019 * @author rdfloyd
020 * @author whikloj
021 */
022@Component
023public class ConfigurableHierarchicalSupplier implements UniqueValueSupplier {
024
025    @Inject
026    protected FedoraPropsConfig fedoraPropsConfig;
027
028    /**
029     * Mint a unique identifier by default using defaults or
030     * if set, use the length and count from fedora properties
031     */
032    public ConfigurableHierarchicalSupplier() {
033    }
034
035    /**
036     * Mint a unique identifier as a UUID
037     *
038     * @return uuid
039     */
040    @Override
041    public String get() {
042        final String s = randomUUID().toString();
043        final String id;
044
045        final int length = fedoraPropsConfig.getFcrepoPidMinterLength();
046        final int count = fedoraPropsConfig.getFcrepoPidMinterCount();
047
048        if (count > 0 && length > 0) {
049            final StringJoiner joiner = new StringJoiner("/", "", "/" + s);
050
051            IntStream.rangeClosed(0, count - 1)
052            .forEach(x -> joiner.add(s.substring(x * length, (x + 1) * length)));
053            id = joiner.toString();
054        } else {
055            id = s;
056        }
057        return id;
058    }
059}