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

com.capitalone.dashboard.model.Component Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package com.capitalone.dashboard.model;

import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * A self-contained, independently deployable piece of the larger application. Each component of an application
 * has a different source repo, build job, deploy job, etc.
 *
 */
@Document(collection="components")
public class Component extends BaseModel {
    private String name; // must be unique to the application
    private String owner;
    private Map> collectorItems = new HashMap<>();

    public Component() {
    }

    public Component(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public Map> getCollectorItems() {
        return collectorItems;
    }

    public void setCollectorItems(Map> collectorItems) {
        this.collectorItems = collectorItems;
    }

    public List getCollectorItems(CollectorType type) {
        return collectorItems != null && collectorItems.get(type) != null ? collectorItems.get(type) : new ArrayList<>();
    }

    public void addCollectorItem(CollectorType collectorType, CollectorItem collectorItem) {
        // Currently only one collectorItem per collectorType is supported
        if (collectorItems.get(collectorType) == null) {
            collectorItems.put(collectorType, Collections.singletonList(collectorItem));
        } else {
            List existing = new ArrayList<> (collectorItems.get(collectorType));
            if (isNewCollectorItem(existing, collectorItem)) {
                existing.add(collectorItem);
                collectorItems.put(collectorType, existing);
            }
        }
    }

    public void updateCollectorItem(CollectorType collectorType, CollectorItem collectorItem) {
            List existing = new ArrayList<> (collectorItems.get(collectorType));
            if (!isNewCollectorItem(existing, collectorItem)) {
                findCollectorItem(existing,collectorItem).setLastUpdated(collectorItem.getLastUpdated());
                collectorItems.put(collectorType, existing);
            }
    }

    private boolean isNewCollectorItem (List existing, CollectorItem item) {
        return existing.stream().noneMatch(ci -> Objects.equals(ci.getId(), item.getId()));
    }

    private CollectorItem findCollectorItem (List existing, CollectorItem item) {
        return existing.stream().filter(ci-> Objects.equals(ci.getId(),item.getId())).findFirst().orElse(null);
    }

    public CollectorItem getFirstCollectorItemForType(CollectorType type){

        if(getCollectorItems().get(type) == null) {
            return null;
        }
        List collectorItems = new ArrayList<>(getCollectorItems().get(type));
        return collectorItems.get(0);
    }

    public CollectorItem getLastUpdatedCollectorItemForType(CollectorType type){

        if(getCollectorItems().get(type) == null || getCollectorItems().get(type).isEmpty()) {
            return null;
        }
        List collectorItems = new ArrayList<>(getCollectorItems().get(type));
        return getLastUpdateItem(collectorItems);
    }

    private CollectorItem getLastUpdateItem(List collectorItems){
        Comparator collectorItemComparator = Comparator.comparing(CollectorItem::getLastUpdated);
        collectorItems.sort(collectorItemComparator.reversed());
        return collectorItems.get(0);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy