Search in sources :

Example 1 with DataAccessRequestGenerationException

use of org.obiba.mica.access.DataAccessRequestGenerationException in project mica2 by obiba.

the class DataAccessEntityService method generateId.

protected String generateId() {
    DataAccessConfig dataAccessConfig = dataAccessConfigService.getOrCreateConfig();
    Object exclusions = YamlClassPathResourceReader.read(EXCLUSION_IDS_YAML_RESOURCE_PATH, Map.class).get("exclusions");
    IdentifierGenerator.Builder builder = IdentifierGenerator.newBuilder().prefix(dataAccessConfig.getIdPrefix()).size(dataAccessConfig.getIdLength());
    if (dataAccessConfig.isAllowIdWithLeadingZeros()) {
        builder.zeros();
    }
    if (exclusions instanceof List) {
        log.info("Using exclusions {} to generate DAR id", exclusions.toString());
        builder.exclusions((List) exclusions);
    }
    IdentifierGenerator idGenerator = builder.build();
    int tries = 0;
    while (tries < 100) {
        tries++;
        String id = idGenerator.generateIdentifier();
        if (getRepository().findOne(id) == null)
            return id;
    }
    throw new DataAccessRequestGenerationException("Exceeded 100 id generation tries");
}
Also used : DataAccessConfig(org.obiba.mica.micaConfig.domain.DataAccessConfig) DataAccessRequestGenerationException(org.obiba.mica.access.DataAccessRequestGenerationException) List(java.util.List) Map(java.util.Map) IdentifierGenerator(org.obiba.mica.core.support.IdentifierGenerator)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 DataAccessRequestGenerationException (org.obiba.mica.access.DataAccessRequestGenerationException)1 IdentifierGenerator (org.obiba.mica.core.support.IdentifierGenerator)1 DataAccessConfig (org.obiba.mica.micaConfig.domain.DataAccessConfig)1