Package io.lumify.analystsNotebook.model

Source Code of io.lumify.analystsNotebook.model.CustomImage

package io.lumify.analystsNotebook.model;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import io.lumify.core.model.ontology.Concept;
import io.lumify.core.model.ontology.OntologyRepository;
import io.lumify.core.model.properties.LumifyProperties;
import org.apache.commons.codec.binary.Base64;
import org.securegraph.Vertex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomImage {
    @JacksonXmlProperty(isAttribute = true)
    private String id;

    @JacksonXmlProperty(isAttribute = true)
    private String data;

    @JacksonXmlProperty(isAttribute = true)
    private int dataLength;

    public CustomImage() {

    }

    public CustomImage(String id, byte[] data) {
        this.id = id;
        this.data = Base64.encodeBase64URLSafeString(data);
        dataLength = data.length;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public int getDataLength() {
        return dataLength;
    }

    public void setDataLength(int dataLength) {
        this.dataLength = dataLength;
    }

    public static List<CustomImage> createForVertices(Iterable<Vertex> vertices, OntologyRepository ontologyRepository) {
        Map<String, CustomImage> customImageMap = new HashMap<String, CustomImage>();
        for (Vertex vertex : vertices) {
            String conceptType = LumifyProperties.CONCEPT_TYPE.getPropertyValue(vertex);
            if (!customImageMap.containsKey(conceptType)) {
                CustomImage customImage = null;
                Concept concept = ontologyRepository.getConceptByIRI(conceptType);
                byte[] glyphIcon = getGlyphIcon(concept, ontologyRepository);
                if (glyphIcon != null) {
                    customImage = new CustomImage(conceptType, glyphIcon);
                }
                customImageMap.put(conceptType, customImage);
            }
        }

        List<CustomImage> customImages = new ArrayList<CustomImage>();
        for (CustomImage customImage : customImageMap.values()) {
            if (customImage != null) {
                customImages.add(customImage);
            }
        }
        return customImages;
    }

    private static byte[] getGlyphIcon(Concept concept, OntologyRepository ontologyRepository) {
        if (concept.hasGlyphIconResource()) {
            return concept.getGlyphIcon();
        } else {
            concept = ontologyRepository.getParentConcept(concept);
            if (concept != null) {
                return getGlyphIcon(concept, ontologyRepository);
            } else {
                return null;
            }
        }
    }
}
TOP

Related Classes of io.lumify.analystsNotebook.model.CustomImage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.