TAMULib / CAP

MIT License
3 stars 2 forks source link

Sprint tcdl b03909 include imported namespaces #110

Closed rladdusaw closed 5 years ago

ghost commented 5 years ago

https://github.com/TAMULib/CAP/blob/sprint-tcdl-staging/src/main/java/edu/tamu/cap/model/Property.java#L8

This embedded model should prevent persistence of null uri. Using @NotNull on property uri.

ghost commented 5 years ago

The list of namespaces should be lazy. Remove persisted property of namespaces, add synthetic getter to return the namespaces extracted from the properties upon serialization to the front end.

ghost commented 5 years ago
package edu.tamu.cap.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;

import edu.tamu.cap.model.validation.SchemaValidator;
import edu.tamu.weaver.validation.model.ValidatingBaseEntity;

@Entity
public class Schema extends ValidatingBaseEntity {

    private final static Pattern pattern = Pattern.compile("^(.*)[#|\\/]");

    @Column
    private String name;

    @Column
    private String abbreviation;

    @Column
    private String namespace;

    @ElementCollection(fetch = FetchType.EAGER)
    private List<Property> properties;

    public Schema() {
        setModelValidator(new SchemaValidator());
        setProperties(new ArrayList<Property>());
    }

    public Schema(String name, String namespace, String abbreviation) {
        this();
        setName(name);
        setNamespace(namespace);
        setAbbreviation(abbreviation);
    }

    public Schema(String name, String namespace, String abbreviation, List<Property> properties) {
        this(name, namespace, abbreviation);
        setProperties(properties);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String prefix) {
        this.namespace = prefix;
    }

    public List<Property> getProperties() {
        return properties;
    }

    private void setProperties(List<Property> properties) {
        this.properties = properties;
    }

    public void addProperty(Property property) {
        this.properties.add(property);
    }

    public void removeProperty(Property property) {
        this.properties.remove(property);
    }

    public List<String> getNamespaces() {
        List<String> namespaces = new CopyOnWriteArrayList<String>();
        properties.parallelStream().forEach(property -> {
            String prefix = parseNamespace(property);
            if (prefix != null && !namespaces.contains(prefix)) {
                namespaces.add(prefix);
            }
        });
        return namespaces;
    }

    private String parseNamespace(Property property) {
        String namespace = null;
        Matcher matcher = pattern.matcher(property.getUri());
        if (matcher.find()) {
            namespace = matcher.group(0);
        }
        return namespace;
    }

}