All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.domain.gtaa.GTAAConcepts Maven / Gradle / Ivy

There is a newer version: 8.3.0
Show newest version
package nl.vpro.domain.gtaa;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import nl.vpro.openarchives.oai.Label;
import nl.vpro.w3.rdf.Description;

/**
 * @author Michiel Meeuwissen
 * @since 5.5
 */
@Slf4j
public class GTAAConcepts {
    private static final String SCHEME_URI = "http://data.beeldengeluid.nl/gtaa/";

    private static final ConcurrentMap CREATE_METHODS = new ConcurrentHashMap<>();

    private GTAAConcepts() {
    }

    public static Optional toConcept(Description d) {
        Optional optionalScheme = Scheme.ofUrl(d.getInScheme().getResource());

        return optionalScheme.map(scheme ->  {
            Method method = CREATE_METHODS.computeIfAbsent(scheme, s -> {
                try {
                    return s.getImplementation()
                        .getMethod("create", Description.class);
                } catch (NoSuchMethodException e) {
                    log.error(e.getMessage(), e);
                    return null;
                }
            });
            if (method != null) {
                try {
                    return (GTAAConcept) method.invoke(null, d);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    log.error(e.getMessage(), e);
                }
            }
            return null;
        });
    }


    public static Optional toConcept(GTAANewGenericConcept thesaurusObject) {
        Description description = Description.builder()
                .prefLabel(
                        Label.builder()
                                .value(thesaurusObject.getName())
                                .build()
                ).scopeNote(thesaurusObject.getScopeNotesAsLabel())
                .inScheme(SCHEME_URI + thesaurusObject.getObjectType())
                .build();
        return toConcept(description);
    }


    public static Scheme toScheme(GTAAConcept d) {
        return toScheme((Object) d).orElseThrow(() -> new IllegalArgumentException("" + d + " has no scheme"));
    }

     private static Optional toScheme(Object d) {
        GTAAScheme annotation = d.getClass().getAnnotation(GTAAScheme.class);
        if (annotation != null) {
            return Optional.of(annotation.value());
        } else {
            return Optional.empty();
        }
    }


    private static Description toDescription(GTAANewGenericConcept thesaurusObject) {
        return Description.builder()
                .prefLabel(
                        Label.builder()
                                .value(thesaurusObject.getName())
                                .build()
                ).scopeNote(thesaurusObject.getScopeNotesAsLabel())
                .inScheme(SCHEME_URI + thesaurusObject.getObjectType())
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy